iText generated dynamic pdf printing blank page - java

I have a dynamic pdf form , which is filled programatically from xml input data by using iText APIs. But when we print the filled dynamic pdf form, blank page is printed. If we open and save the output pdf and then print, its working fine.
Can anybody help why would be the reason for blank page and how this issue can be resolved?
My form filling code:
PdfReader reader=new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader,new FileOutputStream(dest),'\0', true);
PdfWriter writer=stamper.getWriter();
File file = new File(src);
String fileName=file.getName();
PdfAction action = null;
action = PdfAction.javaScript(readJsFileTostring(jsFileName), writer);
writer.setOpenAction(action);
stamper.setPageAction(PdfWriter.PAGE_OPEN, action, 1);
AcroFields form = stamper.getAcroFields();
XfaForm xfa = form.getXfa();
xfa.fillXfaForm(new FileInputStream(xmlData));
stamper.close();
I use Adobe Acrobat Reader DC 2018.009.20050 to print the form.

Related

Trying to attach a file to a pdf using PdfStamper

I have a String that i want to attach either to the pdf raw code somehow or, what i'm trying here is adding the string as a text file to the pdf, this is what i've come up with but it does not work.
Document document = new Document();
String directoryPath = "/storage/emulated/0/Download";
PdfWriter.getInstance(document, new FileOutputStream(directoryPath+"/Aa.pdf"));
String data = PaintView.full_string_return();
System.out.println(data);
PdfReader reader = new PdfReader(directoryPath+"/Aa.pdf");
PdfStamper stamper = new PdfStamper(reader,new FileOutputStream(directoryPath+"/Aa.pdf") );
PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(stamper.getWriter(), null, String.format("data.txt",data),String.format("data",data).getBytes(StandardCharsets.UTF_8));
stamper.addFileAttachment(String.format("data",data),fs);
stamper.close();
I know i'm not using the pdfWriter here but i use it elsewhere, just added it in case it is messing up something.

Form fields created using iText5 PDF Java library, duplicates the fields when uploaded to Adobe sign tool

Downloaded pdf with form field from code
Adobe sign tool duplicating the form fields
I have tried creating date fields and signature fields for PDF using iText5 Library and generated the PDF. In the downloaded pdf when opened in Adobe Acrobat Reader, I see only one field created, but when the pdf is uploaded to Adobe Sign, the form fields are duplicated.
Below is my code:
Document document = new Document(PageSize.A4,40,40,60,50);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(document,baos);
PdfPTable signatureTable = createPdfTable(2);
signatureTable.setWidths(new int[] {1,1});
PdfPCell signatureCell = new PdfPCell();
TextField signatureText = new TextField(writer,signatureCell,"TCSig1_es_:signer1:signature");
signatureText.setBackgroundColor(BaseColor.WHITE);
PdfFormField signField = signatureText.getTextField();
signField.setPlaceInPage(4);
FieldPositioningEvents signatureEvent = new FieldPositioningEvents(writer, signField);
signatureCell.setCellEvent(signatureEvent);
signatureTable.addCell(signatureCell);
document.add(signatureTable);

How to copy/move AcroForm fields from one document to new blank one using IText5 or IText7?

I need to copy whole AcroForm including field positions and values from template PDF to a new blank PDF file. How can I do that?
In short words - I need to get rid of "background" from the template and leave only filed forms.
The whole point of this is to create a PDF with content that would be printed on pre-printed templates.
I am using IText 5 but I can switch to 7 if usefull examples would be provided
After a lot of trial and error I have found the solution to "How to copy AcfroForm fields into another PDF". It is a iText v7 version. I hope it will help somebody someday.
private byte[] copyFormElements(byte[] sourceTemplate) throws IOException {
PdfReader completeReader = new PdfReader(new ByteArrayInputStream(sourceTemplate));
PdfDocument completeDoc = new PdfDocument(completeReader);
ByteArrayOutputStream out = new ByteArrayOutputStream();
PdfWriter offsetWriter = new PdfWriter(out);
PdfDocument offsetDoc = new PdfDocument(offsetWriter);
offsetDoc.initializeOutlines();
PdfPage blank = offsetDoc.addNewPage();
PdfAcroForm originalForm = PdfAcroForm.getAcroForm(completeDoc, false);
// originalForm.getPdfObject().copyTo(offsetDoc,false);
PdfAcroForm offsetForm = PdfAcroForm.getAcroForm(offsetDoc, true);
for (String name : originalForm.getFormFields().keySet()) {
PdfFormField field = originalForm.getField(name);
PdfDictionary copied = field.getPdfObject().copyTo(offsetDoc, false);
PdfFormField copiedField = PdfFormField.makeFormField(copied, offsetDoc);
offsetForm.addField(copiedField, blank);
}
offsetDoc.close();
completeDoc.close();
return out.toByteArray();
}
Did you check the PdfCopyForms object:
Allows you to add one (or more) existing PDF document(s) to create a new PDF and add the form of another PDF document to this new PDF.
I didn't find an example, but you could try something like this:
PdfReader reader1 = new PdfReader(src1); // a document with a form
PdfReader reader2 = new PdfReader(src2); // a document without a form
PdfCopyForms copy = new PdfCopyForms(new FileOutputStream(dest));
copy.AddDocument(reader1); // add the document without the form
copy.CopyDocumentFields(reader2); // add the fields of the document with the form
copy.close();
reader1.close();
reader2.close();
I see that the class is deprecated. I'm not sure of that's because iText 7 makes it much easier to do this, or if it's because there were technical problems with the class.

How to show a limited number of pages of PDF file in JSP using iText?

I have to display a PDF document in a JSP page. The PDF document has 25 pages, but I want to display only 10 pages of the PDF file. How can I achieve this with help of iText?
Assuming you have the PDF file already.
You can use PdfStamper and PdfCopy to slice the PDF up:
PdfReader reader = new PdfReader("THE PDF SOURCE");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Document document = new Document();
PdfCopy copy = new PdfCopy(document, outputStream);
document.open();
PdfStamper stamper = new PdfStamper(reader, outputStream);
for (int i = 1; i < reader.getNumberOfPages(); i++) {
// Select what pages you need here
PdfImportedPage importedPage = stamper.getImportedPage(reader, i);
copy.addPage(importedPage);
}
copy.freeReader(reader);
outputStream.flush();
document.close();
// Now you can send the byte array to your user
// set content type to application/pdf
As for sending the pdf to display, it depends on the way you display it. The outputstream will at the end of the supplied code contain the pages you copy in the loop, in the example it is all of the pages.
This essentially is a new PDF file, but in memory. If it is the same 10 pages of the same file every time, you may consider saving it as a file.

Remove page from PDF

I'm currently using iText and I'm wondering if there is a way to delete a page from a PDF file?
I have opened it up with a reader etc., and I want to remove a page before it is then saved back to a new file; how can I do that?
The 'better' way to 'delete' pages is doing
reader.selectPages("1-5,10-12");
Which means we only select pages 1-5, 10-12 effectively 'deleting' pages 6-9.
Get the reader of existing pdf file by
PdfReader pdfReader = new PdfReader("source pdf file path");
Now update the reader by
pdfReader.selectPages("1-5,15-20");
then get the pdf stamper object to write the changes into a file by
PdfStamper pdfStamper = new PdfStamper(pdfReader,
new FileOutputStream("destination pdf file path"));
close the PdfStamper by
pdfStamper.close();
It will close the PdfReader too.
Cheers.....
For iText 7 I found this example:
PdfReader pdfReader = new PdfReader(PATH + name + ".pdf");
PdfDocument srcDoc = new PdfDocument(pdfReader);
PdfDocument resultDoc = new PdfDocument(new PdfWriter(PATH + name + "_cut.pdf"));
resultDoc.initializeOutlines();
srcDoc.copyPagesTo(1, 2, resultDoc);
resultDoc.close();
srcDoc.close();
See also here: clone-reordering-pages
and here: clone-splitting-pdf-file
You can use a PdfStamper in combination with PdfCopy.
In this answer it is explained how to copy a whole document. If you change the criteria for the loop in the sample code you can remove the pages you don't need.
Here is a removing function ready for real life usage. Proven to work ok with itext 2.1.7. It does not use "strigly typing" also.
/**
* Removes given pages from a document.
* #param reader document
* #param pagesToRemove pages to remove; 1-based
*/
public static void removePages(PdfReader reader, int... pagesToRemove) {
int pagesTotal = reader.getNumberOfPages();
List<Integer> allPages = new ArrayList<>(pagesTotal);
for (int i = 1; i <= pagesTotal; i++) {
allPages.add(i);
}
for (int page : pagesToRemove) {
allPages.remove(new Integer(page));
}
reader.selectPages(allPages);
}

Categories

Resources