![]() ![]() Stream.Read doesn't guarantee that it will read everything it's asked for. I should perhaps explain why my answer is longer than the others. ![]() NET 4 and above, I'd use Stream.CopyTo, which is basically equivalent to the loop in my code - create the MemoryStream, call stream.CopyTo(ms) and then return ms.ToArray(). While ((read = input.Read(buffer, 0, buffer.Length)) > 0) Using (MemoryStream ms = new MemoryStream()) NET 4 - I'd use code like this: public static byte ReadFully(Stream input) For many streams, you just don't know how much data there will be. It really depends on whether or not you can trust s.Length. PdfContentByte cb = writer.DirectContent įor (int i = 1 i < pdfr.NumberOfPages + 1 i++)Ĭb.AddTemplate(page, / pdfr.GetPageSize(i).Width, 0, 0, / pdfr.GetPageSize(i).Height, 0, 0) PdfWriter writer = PdfWriter.GetInstance(doc, outPDF) Using (Document doc = new Document(PageSize.LETTER)) Using (PdfReader pdfr = new PdfReader(inPDF)) MemoryStream outPDF = new MemoryStream() Throw new ArgumentException("Function only supports from size A4 to size letter") If (fromSize != LetterSize.A4 || toSize != LetterSize.Letter) My code: public static byte ConvertPdfSize(byte inPDF, LetterSize fromSize, LetterSize toSize) How to get a byte array from PdfWriter, writing to a memorystream in iTextSharp In place B and C, the stream is closed, and cannot be accessed. My guess is the doc.Close() has to run first. In place A, the length of the memorystream is only 255, which doesn't work. I tried putting this code in three places, see place A, B and C in the code. After, I want to say outBytes = outPDF.ToArray(). I create the PdfWriter using a memorystream in the constructor ( outPDF), and then does my conversion. ![]() I want to return a byte array, and want to keep the whole thing in memory. The idea is it transforms a PDF file with a specific size, to another size. PdfContentByte cb = writer.DirectContent įor (int i = 1 i < pdfr.NumberOfPages + 1 i++)Ĭb.AddTemplate(page, / pdfr.GetPageSize(i).Width, 0, 0, / pdfr.GetPageSize(i).I have a method, which takes in the following: ![]() I have a method, which takes in the following: ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |