Note that there are some explanatory texts on larger screens.

plurals
  1. POManipulate PDF in memory and then write the content to FileStream
    text
    copied!<p>can anyone tell me where i'm wrong? I'm trying to merge different files .pdf into a unique file and also write the index of the page for each page but i alway have a corrupted file. I'm not practical of the use of different stream types</p> <pre><code> public static bool MergeFiles(string destinationFile, List&lt;string&gt; sourceFiles) { try { int f = 0; // we create a reader for a certain document PdfReader reader = new PdfReader(sourceFiles[f]); // we retrieve the total number of pages int n = reader.NumberOfPages; //Console.WriteLine("There are " + n + " pages in the original file."); // step 1: creation of a document-object Document document = new Document(reader.GetPageSizeWithRotation(1)); // step 2: we create a writer that listens to the document MemoryStream ms = new MemoryStream(); PdfWriter writer = PdfWriter.GetInstance(document, ms); // step 3: we open the document document.Open(); PdfContentByte cb = writer.DirectContent; PdfImportedPage page; int rotation; // step 4: we add content while (f &lt; sourceFiles.Count) { int i = 0; while (i &lt; n) { i++; document.SetPageSize(reader.GetPageSizeWithRotation(i)); document.NewPage(); page = writer.GetImportedPage(reader, i); rotation = reader.GetPageRotation(i); if (rotation == 90 || rotation == 270) { cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height); } else { cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); } } f++; if (f &lt; sourceFiles.Count) { reader = new PdfReader(sourceFiles[f]); // we retrieve the total number of pages n = reader.NumberOfPages; //Console.WriteLine("There are " + n + " pages in the original file."); } } byte[] content = ms.ToArray(); using (FileStream fs = File.Create(destinationFile)) { fs.Write(content, 0, (int)content.Length); } PdfReader reader2 = new PdfReader(destinationFile); MemoryStream ms2 = new MemoryStream(); PdfStamper stamper = new PdfStamper(reader, ms2); for (int i = 0; i &lt; reader2.NumberOfPages; i++) { PdfContentByte canvas = stamper.GetOverContent(i); ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("Page " + i + " of" + reader2.NumberOfPages), 10, 350, 90); } stamper.Close(); MessageBox.Show("I file PDF sono stati uniti correttamente"); return true; } catch (Exception e) { MessageBox.Show(e.Message); return false; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload