iText - add content to existing PDF file
iText has more than one way of doing this. The PdfStamper
class is one option. But I find the easiest method is to create a new PDF document then import individual pages from the existing document into the new PDF.
// 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 new data / text here
// for example...
document.add(new Paragraph("my timestamp"));
document.close();
This will read in a PDF from templateInputStream
and write it out to outputStream
. These might be file streams or memory streams or whatever suits your application.
How to append a PDF file to an existing one with iText?
I hope this code will help you.
public static void mergePdfs(){
try {
String[] files = { "D:\\1.pdf" ,"D:\\2.pdf" ,"D:\\3.pdf" ,"D:\\4.pdf"};
Document pDFCombineUsingJava = new Document();
PdfCopy copy = new PdfCopy(pDFCombineUsingJava , new FileOutputStream("D:\\CombinedFile.pdf"));
pDFCombineUsingJava.open();
PdfReader ReadInputPDF;
int number_of_pages;
for (int i = 0; i < files.length; i++) {
ReadInputPDF = new PdfReader(files[i]);
copy.addDocument(ReadInputPDF);
copy.freeReader(ReadInputPDF);
}
pDFCombineUsingJava.close();
}
catch (Exception i)
{
System.out.println(i);
}
}
Add existing PDF document to opened document
Read the answer to this question
Read BLOB (PDF Content) from database and edit and output PDF ,
for more detailed description
Use PdfContentByte
to hold the PDF content to be added
PdfContentByte cb = writer.getDirectContent();
Create a PdfImportedPage
page object for each page you want to import from another document using getImportedPage()
and add the page using to writer
using addTemplate()
:
trunk.newPage();
page = writer.getImportedPage(pdfReader, pagenumber);
cb.addTemplate(page, 0, 0);
Make sure to close the document
and the pdfReader
.
Note: do not use this code snippet if you merely want to merge a bunch of files. The reason why you shouldn't do this, is explained in the answer to the question How to merge documents correctly?
itext7 - adding content from existing PDF to a new one
To stamp your template page origPage
onto the current first page of pdf
instead of a new one, simply replace
PdfPage page = pdf.addNewPage(1, PageSize.A4);
by
PdfPage page = pdf.getPage(1);
Now page
references the already existing first page instead of a new one, and your further manipulations add the template page thereupon.
Create a link in Existing PDF Using iText 5.5.13.2
I am referring to this answer. Have a look. Modifying existing pdf file using iText 5.5.13.2 is complicated. But the referred solution is more easier.
iText 7 has handier way to modify existing pdf.
There are several other ways. Like PdfStamper etc.
From referred answer, add following code to make an anchor.
Phrase phrase = new Phrase("Open ");
Phrase phrase1 = new Phrase(" on Click On it.");
Font anchorFont = new Font(Font.FontFamily.UNDEFINED, 11);
anchorFont.setColor(BaseColor.BLUE);
anchorFont.setStyle(Font.FontStyle.UNDERLINE.getValue());
Anchor anchor = new Anchor("Google", anchorFont);
anchor.setReference("www.google.com");
phrase.add(anchor);
phrase.add(phrase1);
document.add(phrase);
Change the font and colors based on your needs.
Full code:
try {
PdfReader reader = new PdfReader("test.pdf"); //src pdf path (the pdf I need to modify)
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("test2.pdf")); // destination pdf path
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfImportedPage page = writer.getImportedPage(reader, 1);
document.newPage();
document.setPageSize(reader.getPageSize(1));
cb.addTemplate(page, 0, 0);
Phrase phrase = new Phrase("Open ");
Phrase phrase1 = new Phrase(" on Click On it.");
Font anchorFont = new Font(Font.FontFamily.UNDEFINED, 11);
anchorFont.setColor(BaseColor.BLUE);
anchorFont.setStyle(Font.FontStyle.UNDERLINE.getValue());
Anchor anchor = new Anchor("Google", anchorFont);
anchor.setReference("https://www.google.com");
phrase.add(anchor);
phrase.add(phrase1);
document.add(phrase);
document.close();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
Related Topics
Hibernate - Foreign Keys Instead of Entities
How to Use a Java8 Lambda to Sort a Stream in Reverse Order
Access Maven Properties Defined in the Pom
Does Stream.Foreach Respect the Encounter Order of Sequential Streams
Convert Java.Util.Hashmap to Scala.Collection.Immutable.Map in Java
JSON and Java - Circular Reference
On-The-Fly, In-Memory Java Code Compilation for Java 5 and Java 6
Regex That Will Match a Java Method Declaration
Hibernate Table Not Mapped Error in Hql Query
Passing Parameters to a Jdbc Preparedstatement
How to Read an Aws S3 File with Java
Using a Prepared Statement and Variable Bind Order by in Java with Jdbc Driver
Annotations from Javax.Validation.Constraints Not Working
How Does Java Garbage Collector Handle Self-Reference
Stop Scheduled Timer When Shutdown Tomcat
How Does the Jvm Ensure That System.Identityhashcode() Will Never Change