Edit DirectContent class iTextSharp PdfSmartCopy

In my work, sometimes I have to combine from several to several hundred pdf files. All the time I used the Writer and ImportedPages classes. But when I combined all the files into one, the file size becomes huge, the sum of all the combined file sizes, because the fonts are attached to each page and are not reused (fonts are embedded in every page, not the entire document).

Not so long ago, I learned about the PdfSmartCopy class, which repeats embedded fonts and images. And here the problem works. Very often, before combining files, I have to add additional content (images, text) to them. For this, I usually use the PdfContentByte from the Writer object.

 Document doc = new Document(); PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("C:\test.pdf", FileMode.Create)); PdfContentByte cb = writer.DirectContent; cb.Rectangle(100, 100, 100, 100); cb.SetColorStroke(BaseColor.RED); cb.SetColorFill(BaseColor.RED); cb.FillStroke(); 

When I do a similar thing with the PdfSmartCopy object, the pages are merged, but additional content is added. Full code of my test with PdfSmartCopy :

 using (Document doc = new Document()) { using (PdfSmartCopy copy = new PdfSmartCopy(doc, new FileStream(Path.GetDirectoryName(pdfPath[0]) + "\\testas.pdf", FileMode.Create))) { doc.Open(); PdfContentByte cb = copy.DirectContent; for (int i = 0; i < pdfPath.Length; i++) { PdfReader reader = new PdfReader(pdfPath[i]); for (int ii = 0; ii < reader.NumberOfPages; ii++) { PdfImportedPage import = copy.GetImportedPage(reader, ii + 1); copy.AddPage(import); cb.Rectangle(100, 100, 100, 100); cb.SetColorStroke(BaseColor.RED); cb.SetColorFill(BaseColor.RED); cb.FillStroke(); doc.NewPage();// net nesessary line //ColumnText col = new ColumnText(cb); //col.SetSimpleColumn(100,100,500,500); //col.AddText(new Chunk("wdasdasd", PdfFontManager.GetFont(@"C:\Windows\Fonts\arial.ttf", 20))); //col.Go(); } } } } } 

Now I have a few questions:

  • Is it possible to edit the PdfSmartCopy DirectContent object?
  • If not, is there another way to combine multiple PDF files into one without increasing its size, and still be able to add additional content to the pages when merging?
+4
source share
3 answers

First up is: using PdfWriter / PdfImportedPage not a good idea. You throw away all the interactive features! As the author of iText, it is unfortunate that so many people make the same mistake, despite the fact that I wrote two books about it, and despite the fact that I convinced my publisher to offer one of the most important chapters for free: http: //www.manning.com/lowagie2/samplechapter6.pdf

Did I really write poorly? Or is there another reason people continue to merge documents using PdfWriter / PdfImportedPage ?

As for your specific questions, here are the answers:

  • Yes. Download a sample chapter and find the PDF for PageStamp .
  • Only if you create a PDF in two passes. For example: first create a huge PDF file, then reduce the size by transferring it through PdfCopy ; or create a merged PDF first with PdfCopy, then add additional content to the second pass using PdfStamper .
+9
source

Code after using Bruno Loughay's answer

 for (int i = 0; i < pdfPath.Length; i++) { PdfReader reader = new PdfReader(pdfPath[i]); PdfImportedPage page; PdfSmartCopy.PageStamp stamp; for (int ii = 0; ii < reader.NumberOfPages; ii++) { page = copy.GetImportedPage(reader, ii + 1); stamp = copy.CreatePageStamp(page); PdfContentByte cb = stamp.GetOverContent(); cb.Rectangle(100, 100, 100, 100); cb.SetColorStroke(BaseColor.RED); cb.SetColorFill(BaseColor.RED); cb.FillStroke(); stamp.AlterContents(); // don't forget to add this line copy.AddPage(page); } } 
+4
source

2. Only if you create a PDF in two passes. For example: first create a huge PDF file, then reduce the size by transferring it through PdfCopy; or create a merged PDF first with PdfCopy, then add additional content in the second pass using PdfStamper.

It is much harder to use PdfStamper with a second pass. When you work with a lot of data, it is much easier to create 1 pdf stamp and then add.

PdfCopyFields did a good job of this. Now this does not work with release 5.4.4.0, so I am here.

0
source

Source: https://habr.com/ru/post/1437933/


All Articles