iText merge a stamped pdf with a pdf created at runtime - java

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();
}

Related

Can't remove whiteSpace in Java itext

Here i am combining 2 pdf documents using the Itext packages.
Merging was done successfully using the code below
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
document.open();
PdfContentByte cb = writer.getDirectContent();
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);
}
}
outputStream.flush();
document.close();
outputStream.close();
Here the list is an InputStream List.
And outputStream is an output stream
The problem i am having is i want to append the PDFdocuments in the list after the 1st PDF is added
(i.e 1st PDF has 4 lines...i want the 2nd PDF to continue in the same page after the 4th line).
What i am getting is the 2nd PDF is added in the second page.
Is there any alternate keyword for document.newPage();
Can anyone help me with it.
Thanks would like to hear any responses:)
It depends on the requirements you have. As long as
you only are interested in the page contents of the merged PDFs, not in the page annotations and
the pages have no content but the text lines you mention, in particular no background graphics, watermarks, or header/footer lines,
you can you use either the
PdfDenseMergeTool from this answer or the
PdfVeryDenseMergeTool from this answer.
If you are interested in annotations, it should be no problem to extend those classes accordingly. If your PDDFs have background graphics or watermarks, headers or footers, they should be removed beforehand.

function that can use iText to concatenate / merge pdfs together - causing some issues

I'm using the following code to merge PDFs together using iText:
public static void concatenatePdfs(List<File> listOfPdfFiles, File outputFile) throws DocumentException, IOException {
Document document = new Document();
FileOutputStream outputStream = new FileOutputStream(outputFile);
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
document.open();
PdfContentByte cb = writer.getDirectContent();
for (File inFile : listOfPdfFiles) {
PdfReader reader = new PdfReader(inFile.getAbsolutePath());
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
document.newPage();
PdfImportedPage page = writer.getImportedPage(reader, i);
cb.addTemplate(page, 0, 0);
}
}
outputStream.flush();
document.close();
outputStream.close();
}
This usually works great! But once and a while, it's rotating some of the pages by 90 degrees? Anyone ever have this happen?
I am looking into the PDFs themselves to see what is special about the ones that are being flipped.
There are errors once in a while because you are using the wrong method to concatenate documents. Please read chapter 6 of my book and you'll notice that using PdfWriter to concatenate (or merge) PDF documents is wrong:
You completely ignore the page size of the pages in the original document (you assume they are all of size A4),
You ignore page boundaries such as the crop box (if present),
You ignore the rotation value stored in the page dictionary,
You throw away all interactivity that is present in the original document, and so on.
Concatenating PDFs is done using PdfCopy, see for instance the FillFlattenMerge2 example:
Document document = new Document();
PdfCopy copy = new PdfSmartCopy(document, new FileOutputStream(dest));
document.open();
PdfReader reader;
String line = br.readLine();
// loop over readers
// add the PDF to PdfCopy
reader = new PdfReader(baos.toByteArray());
copy.addDocument(reader);
reader.close();
// end loop
document.close();
There are other examples in the book.
In case anyone is looking for it, using Bruno Lowagie's correct answer above, here is the version of the function that does not seem to have the page flipping issue i described above:
public static void concatenatePdfs(List<File> listOfPdfFiles, File outputFile) throws DocumentException, IOException {
Document document = new Document();
FileOutputStream outputStream = new FileOutputStream(outputFile);
PdfCopy copy = new PdfSmartCopy(document, outputStream);
document.open();
for (File inFile : listOfPdfFiles) {
PdfReader reader = new PdfReader(inFile.getAbsolutePath());
copy.addDocument(reader);
reader.close();
}
document.close();
}

Re-size page size in existing pdf using itext

I am using following code to create new document from existing pdf.
I want to shrink all pages(re-size) of existing pdf in new generated pdf.
Code i am using:
public void resize() throws IOException, DocumentException {
PdfReader reader = new PdfReader("D:/test/scaned4.pdf");
Document document = new Document(PageSize.LEGAL, 0, 0, 0, 0);
System.out.println(reader.isTampered());
PdfCopy copy = new PdfCopy(document, new FileOutputStream(
"D:/test/result.pdf"));
document.open();
PdfImportedPage page;
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
page = copy.getImportedPage(reader, i);
copy.addPage(page);
}
reader.close();
document.close();
}
There is any way to shrink imported page before add to new document.

Combining two PDF's in itext

I have two ByteArrayOutputStreams which contain PDF files.
I used java concatinate operation and it failed.
I want to add the second pdf after the first one.
Is there any Itext functions to merge two Pdfs using streams?
Found it atlast..This one worked for me
Document document = new Document();
//Rectangle pageSize = new Rectangle(792, 612);
Rectangle pageSize = PageSize._11X17;
document.setPageSize(pageSize);
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
document.open();
PdfContentByte cb = writer.getDirectContent();
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);
}
}
outputStream.flush();
document.close();
outputStream.close();

Append existing PDF file

I have some code that generates a PDF file programmatically and I need to append to it the existing file (to the end of generated one). Can somebody give an example or link?
Thank you
UPD#1: Actually I am looking for some piece of code of merging existing file and byte's array (of programmatically generated file)
Itext is the good solution.
To append , you need to read the already present data and write it to the next file.
PdfTextExtractor parser =new PdfTextExtractor(new PdfReader("D:/Text.pdf"));
String text1 = parser.getTextFromPage(3);
And before you write your code that generates a PDF file programmatically , you need to add text1.
Thanks for all repliers, I have found the solution:
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
writer.setPageEvent(new FooterGenerator());
document.open();
document.setMargins(MARGIN_LEFT, MARGIN_RIGHT, MARGIN_TOP, MARGIN_BOTTOM);
document.add(generateHeader());
document.add(generateContent());
appendTermsAndConditions(writer, document, context.getRealPath("/files/terms-and-conditions.pdf");
document.close();
protected void appendTermsAndConditions(PdfWriter writer, Document document, String fileName) throws IOException {
File f = new File(fileName);
if (f.exists()) {
PdfReader reader = new PdfReader(fileName);
PdfContentByte cb = writer.getDirectContent();
int pagesCount = reader.getNumberOfPages();
PdfImportedPage page;
for (int i = 0; i < pagesCount; i++) {
document.newPage();
page = writer.getImportedPage(reader, document.getPageNumber() + 1);
cb.addTemplate(page, 0, 0);
}
}
}

Categories

Resources