I'm manipulating a PDF available from https://www.census.gov/content/dam/Census/library/publications/2015/econ/g13-aspef.pdf. Part of the manipulation is to copy the pages from the original PDF to a new PDF, and also to copy the named destinations. The iText Java API method addNamedDestinations isn't inserting the destinations into the new PDF.
Below is my code segment which is based on the example in the book iText in Action, 2nd edition.
try {
PdfReader reader1 = new PdfReader("C:\\Temp\\g13-aspef.pdf");
Document doc = new Document();
PdfCopy copy2 = new PdfCopy(doc, fileout);
doc.open();
reader1.consolidateNamedDestinations();
int n = reader1.getNumberOfPages();
for (int i = 0; i < n;) {
copy2.addPage(copy2.getImportedPage(reader1, ++i));
}
/* myDests indeed includes all 23 destinations appearing in the original PDF. */
HashMap<String,String> myDests = SimpleNamedDestination.getNamedDestination(reader1, false);
/* Use addNamedDestinations to insert the original destinations into the new PDF. */
copy2.addNamedDestinations(myDests, 0);
doc.close();
} catch (IOException e) {
System.out.println("Could not copy");
}
However, when I open the created PDF, the pages are there, but not the destinations. Why don't I see the destinations in the new PDF?
Thank you in advance!
Related
This is my code:
try {
dozen = magazijn.getFfd().vraagDozenOp();
for (int i = 0; i < dozen.size(); i++) {
PdfWriter.getInstance(doc, new FileOutputStream("Order" + x + ".pdf"));
System.out.println("Writer instance created");
doc.open();
System.out.println("doc open");
Paragraph ordernummer = new Paragraph(order.getOrdernummer());
doc.add(ordernummer);
doc.add( Chunk.NEWLINE );
for (String t : text) {
Paragraph klant = new Paragraph(t);
doc.add(klant);
}
doc.add( Chunk.NEWLINE );
Paragraph datum = new Paragraph (order.getDatum());
doc.add(datum);
doc.add( Chunk.NEWLINE );
artikelen = magazijn.getFfd().vraagArtikelenOp(i);
for (Artikel a : artikelen){
artikelnr.add(a.getArtikelNaam());
}
for (String nr: artikelnr){
Paragraph Artikelnr = new Paragraph(nr);
doc.add(Artikelnr);
}
doc.close();
artikelnr.clear();
x++;
System.out.println("doc closed");
}
} catch (Exception e) {
System.out.println(e);
}
I get this exception: com.itextpdf.text.DocumentException: The document has been closed. You can't add any Elements.
can someone help me fix this so that the other pdf can be created and paragrphs added?
Alright, your intent is not very clear from your code and question so I'm going to operate under the following assumptions:
You are creating a report for each box you're processing
Each report needs to be a separate PDF file
You're getting a DocumentException on the second iteration of the loop, you're trying to add content to a Document that has been closed in the previous iteration via doc.close();. 'doc.close' will finalize the Document and write everything still pending to any linked PdfWriter.
If you wish to create separate pdfs for each box, you need to create a seperate Document in your loop statement as well, since creating a new PdfWriter via PdfWriter.getInstance(doc, new FileOutputStream("Order" + x + ".pdf")); will not create a new Document on its own.
If I'm wrong with assumption 2 and you wish to add everything to a single PDF, move doc.close(); outside of the loop and create only a single PdfWriter
You can try something like this using Apache PDFBox
File outputFile = new File(path);
outputFile.createNewFile();
PDDocument newDoc = new PDDocument();
then create a PDPage and write what you wanna write in that page. After your page is ready, add it to the newDoc and in the end save it and close it
newDoc.save(outputFile);
newDoc.close()
repeat this dozen.size() times and keep changing the file's name in path for every new document.
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.
Am using "itext-5.5.8", trying to insert (one) page of portrait pdf into a main pdf document, code works perfect but after inserting portrait pages automatically changes to landscape pages, don't know why?
CODE:
try {
PdfReader firstPdf = new PdfReader(mainFileWithPath); //main doc
PdfReader secondPdf =new PdfReader(addFileNameWithPath); // inserting pages
PdfStamper stamp = new PdfStamper(firstPdf, new FileOutputStream(outputPDFFile));
int totalNumOfPagesToInsert = secondPdf.getNumberOfPages();
int i =1;
while (i<=totalNumOfPagesToInsert) {
// Get a page(s) from secondPdf with the given pageNo
PdfImportedPage page = stamp.getImportedPage(secondPdf,i);
// insert new page in to the newly created pdf at specified page number.
stamp.insertPage(INSERT_AT_PAGE_NO + (i-1), secondPdf.getPageSize(i));
// copy the content of the page copied from secondPdf.
stamp.getUnderContent(INSERT_AT_PAGE_NO + (i-1)).addTemplate(page, 0, 0);
i++;
}
//close the new created pdf.
stamp.close();
Please give me directions to fix this! Thanks
As Author #Bruno Lowagie mentioned that "didn't take into account that rotation"
Have fixed the issue like ... Code Below ...
try {
PdfReader firstPdf = new PdfReader(mainFileWithPath);
PdfReader secondPdf =new PdfReader(addFileNameWithPath);
// create new pdf with the content from firstPdf
PdfStamper stamp = new PdfStamper(firstPdf, new FileOutputStream(outputPDFFile));
stamp.setRotateContents(false);
int totalNumOfPagesToInsert = secondPdf.getNumberOfPages();
int i =1;
while (i<=totalNumOfPagesToInsert) {
// Get a single page from secondPdf with the given pageNo
PdfImportedPage page = stamp.getImportedPage(secondPdf,i); //Actual working code
// insert new page in to the newly created pdf at specified page number.
// choose page size bas
stamp.insertPage(INSERT_AT_PAGE_NO + (i-1), secondPdf.getPageSizeWithRotation(i)); //Actual working code
// copy the content of the page copied from secondPdf.
stamp.getUnderContent(INSERT_AT_PAGE_NO + (i-1)).addTemplate(page, 0, 0); //Actual working code
i++;
}
//close the new created pdf.
stamp.close();
I'd like to have a program that removes all rectangles from a PDF file. One use case for this is to unblacken a given PDF file to see if there is any hidden information behind the rectangles. The rest of the PDF file should be kept as-is.
Which PDF library is suitable to this task? In Java, I would like the code to look like this:
PdfDocument doc = PdfDocument.load(new File("original.pdf"));
PdfDocument unblackened = doc.transform(new CopyingPdfVisitor() {
public void visitRectangle(PdfRect rect) {
if (rect.getFillColor().getBrightness() >= 0.1) {
super.visitRectangle(rect);
}
}
});
unblackened.save(new File("unblackened.pdf"));
The CopyingPdfVisitor would copy a PDF document exactly as-is, and my custom code would leave out all the dark rectangles.
Itext pdf library have ways to modify pdf content.
The *ITEXT CONTENTPARSER Example * may give you any idea. "qname" parameter (qualified name) may be used to detected rectangle element.
http://itextpdf.com/book/chapter.php?id=15
Other option, if you want obtain the text on the document use the PdfReaderContentParser to extract text content
public void parsePdf(String pdf, String txt) throws IOException {
PdfReader reader = new PdfReader(pdf);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
PrintWriter out = new PrintWriter(new FileOutputStream(txt));
TextExtractionStrategy strategy;
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
strategy = parser.processContent(i, new SimpleTextExtractionStrategy());
out.println(strategy.getResultantText());
}
out.flush();
out.close();
reader.close();
}
example at http://itextpdf.com/examples/iia.php?id=277
I want to merge 2 pdf documents using iText in java, one of the pdfs is created at runtime while the other is an existing pdf that I read in and using the PdfStamper function stamp an image onto it. I want to then merge these two pdfs and display them using a servlet.
I want to know if this is possible and how to do it.
I have no problem creating or stamping them separately but I just can't seem to figure out how to merge them.
Thanks
I suppose this code can help you. You would have to import IText.Jar for this
public static void doMerge(List<InputStream> list,
OutputStream outputStream) throws DocumentException,
IOException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
document.open();
PdfContentByte cb = writer.getDirectContent();
float k = 0;
for (InputStream in : list) {
PdfReader reader = new PdfReader(in);
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
// document.newPage();
//import the page from source pdf
PdfImportedPage page = writer.getImportedPage(reader, i);
//add the page to the destination pdf
cb.addTemplate(page, 0, 0);
System.out.println(page.getHeight());
}
}
outputStream.flush();
document.close();
outputStream.close();
}