How to create multiple copies of documents using iText

I use iText to create a PDF document that consists of several copies of almost the same information.

For example: invoice. One copy is provided to the client, another is served, and a third is given to the accountant for accounting.

All copies should be exactly the same, except for a small piece of text that indicates who the copy is (Customer, Accounting, File, ...).

There are two possible scenarios (I don't know if the solution is the same for both of them):

a) Each copy is on the page .

b) All copies are on the page of the same page (the paper will have cutting holes for copying).

A wrapper class or helper class will be created that uses iText to create the PDF file so that it can do something like var pdf = HelperClass.CreateDocument(DocuemntInfo info); . The problem with multiple copies will be solved inside this shell / helper.

What does iText do to accomplish this? Do I need to write each element in a document several times in different positions / pages? Or does iText provide a way to write one copy to a document and then copy it to another position / page?


Note. This is a .Net project, but I marked the question with both java and C #, because this question is about how to use iText correctly, the answer will help both developers and developers.

+4
source share
3 answers

This is how I see it working.

 PdfReader reader = new PdfReader( templatePDFPath ); Document doc = new Document(); PdfWriter writer = PdfWriter.createInstance( doc, new FileOutputStream("blah.pdf" ) ); PdfImportedPage inputPage = writer.getImportedPage( reader, 1 ); PdfDirectContent curPageContent = writer.getDirectContent(); String extraStuff[] = getExtraStuff(); for (String stuff : extraStuff) { curPageContent.saveState(); curPageContent.addTemplate( inputPage /*, x, y*/ ); curPageContent.restoreState(); curPageContent.beginText(); curPageContent.setTextMatrix(x, y); curPageContent.setFontAndSize( someFont, someSize ); // the actual work: curPageContent.showText( stuff ); curPageContent.EndText(); // save the contents of curPageContent out to the file and reset it for the next page. doc.newPage(); } 

This is minimal computer work. Quite effective, and this will reduce the size of the PDF. Instead of having N copies of this page, with settings, you have one copy of this page, which is reused on N pages, with small settings on top.

You can do the same and use the "x, y" addTemplate in addTemplate to draw them on the same page. To you.

PS: you need to predefine the coordinates for setTextMatrix .

+2
source

If each copy is on a different page , you can create a new document and copy it to the page several times. Using iText in Java, you can do it like this:

 // Create output PDF Document document = new Document(PageSize.A4); PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); PdfContentByte cb = writer.getDirectContent(); // Load existing PDF PdfReader reader = new PdfReader(templateInputStream); PdfImportedPage page = writer.getImportedPage(reader, 1); // Copy first page of existing PDF into output PDF document.newPage(); cb.addTemplate(page, 0, 0); // Add your first piece of text here document.add(new Paragraph("Customer")); // Copy second page of existing PDF into output PDF document.newPage(); cb.addTemplate(page, 0, 0); // Add your second piece of text here document.add(new Paragraph("Accounting")); // etc... document.close(); 

If you want to put all copies on the same page , the code is similar, but instead of using zeros in addTemplate(page, 0, 0) you will need to set the values ​​for the correct position; The numbers used depend on the size and shape of your account.

See also iText - add content to existing PDF file - the above code is based on the code I wrote in this answer.

+4
source

You can also use PDfCopy or PDfSmartCopy for this.

 PdfReader reader = new PdfReader("Path\To\File"); Document doc = new Document(); PdfCopy copier = new PdfCopy(doc, ms1); //PdfSmartCopy copier = new PdfSmartCopy(doc, ms1); doc.Open(); copier.CloseStream = false; PdfImportedPage inputPage = writer.GetImportedPage(reader, 1); PdfContentByte curPageContent = writer.DirectContent; for (int i = 0; i < count; i++) { copier.AddPage(inputPage); } doc.Close(); ms1.Flush(); ms1.Position = 0; 

The difference between PdfCopy and PdfSmartCopy is that PdfCopy copies the entire PDF for each page, while PdfSmartCopy outputs a PDF file that contains only one copy, and all pages link to it, resulting in a smaller file and less bandwidth in network, however, it uses more memory on the server and takes longer to process.

+1
source

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


All Articles