Re-size page size in existing pdf using itext - java

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.

Related

iText7 : how to use the same reader in multiple page of the same pdf

As the title said,I have an existing pdf that contains an empty table with a custom design, I want to create a pdf file that write data in that table, but I need to have that same table on every page of my new pdf.
public byte[] pdfRdv(Rdv rdv, List<RdvClient> clients) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//this file contains a empty table in which I want to insert each iteration of my
//table clients
String src = "C://Users//ASUS//Downloads//rdv.pdf";
PdfReader reader = new PdfReader(src);
PdfWriter writer = new PdfWriter(baos);
PdfDocument pdfDoc = new PdfDocument(reader, writer);
Document document = new Document(pdfDoc);
for(int i = 0; i < clients.size(); i++){
if(i > 0){
// I tried to add for each itaration a new page but i don't know how to insert in
//it the table in the existing pdf (reader)
pdfDoc.addNewPage(i+2);
}
Paragraph p1 = new Paragraph(clients.get(i).getId().toString()).setFontSize(10);
p1.setFixedPosition(140, 687, 400);
document.add(p1);
//..
}
document.close();
pdfDoc.close();
byte[] pdf = baos.toByteArray();
return pdf;
}
Thank you in advance

Merging PDF Files in-between another PDF File in java using IText or PDFBox

I have two PDF files A and B, I have a requirement where i need to Merge both these PDF files based on a condition.Like,If i find a string like "attach PDF" in my A, i have to merge the B file into A from that particular page in A. For Example,If i spot the word in Page No 3 in my A file I need to merge the B file from Page No:3. I'm using I-Text 5.5.10. Is it possible to achieve this in I-Text or PDFBox. Here is what i have tried as of now.
public static void mergePdf() throws IOException, DocumentException
{
PdfReader reader1 = new PdfReader("C:\\Users\\user1\\Downloads\\generatedSample.pdf");
PdfReader reader2 = new PdfReader("C:\\Users\\user1\\Desktop\\sample1.pdf");
Document document = new Document();
document.addHeader("Header Text", "");
FileOutputStream fos = new FileOutputStream("C:\\Users\\user1\\Downloads\\MergeFile.pdf");
PdfCopy copy = new PdfCopy(document, fos);
document.open();
PdfImportedPage page;
PdfCopy.PageStamp stamp;
Phrase phrase;
BaseFont bf = BaseFont.createFont();
Font font = new Font(bf, 9);
int n = reader1.getNumberOfPages();
for (int i = 1; i <= reader1.getNumberOfPages(); i++)
{
page = copy.getImportedPage(reader1, i);
stamp = copy.createPageStamp(page);
ColumnText.showTextAligned(stamp.getOverContent(), Element.ALIGN_CENTER, null, 520, 5, 0);
stamp.alterContents();
copy.addPage(page);
}
for (int i = 1; i <= reader2.getNumberOfPages(); i++)
{
page = copy.getImportedPage(reader2, i);
stamp = copy.createPageStamp(page);
ColumnText.showTextAligned(stamp.getOverContent(), Element.ALIGN_CENTER, null, 520, 5, 0);
stamp.alterContents();
copy.addPage(page);
}
document.close();
reader1.close();
reader2.close();
}
Thanks for the solution in advance !!

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

iText merge a stamped pdf with a pdf created at runtime

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

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