Add Header/Footer in existing PDF document using Aspose.PDF

Hi Support,


I have a requirement to upload images to my application and convert them into PDF documents. I am able to convert Images to PDF documents successfully using aspose Words. We have license for entire suite of products.

Right now i am trying to add footer to these PDF documents and there is no good example on how i can achieve this, all i got was output stream of the file and need to add footer example “Page X of Y” - can you please let me know which product i can use to add footer.

regards
Srini
CC to : srinivasa.kanakam@judiciary.state.nj.us

Hi Srini,

Thanks for using our products.

Please note that we have a product named Aspose.Pdf.Kit for Java which provides the capability to manipulate existing PDF documents in Java environment. And as per your requirement of adding Footer to existing PDF document, please try using addFooter(…) method of PdfFileStamp Class to accomplish your requirement.

Besides this, if you are creating PDF files by converting Image files into PDF format, you may consider using Aspose.Pdf for Java which provides the capability to convert Image files into PDF format and it also supports the feature to add Header/Footer while creating the PDF file. Please visit the following links for further details on


In the event of any further query, please feel free to contact.

Second link - "Set Page Header and Footer is failing, can you please give me the correct link.


Thanks
Srini

Hi Srini,

We are sorry for the inconvenience.

Please try the below link and do let us know if any further assistance is required.

Set Page Header and Footer

Thank You & Best Regards,

I have an output stream and creating PDF object from it, after adding header or footer i need a stream instead of saving it to file system. Do you have any method to save it to output stream after adding footer.


public static void addFooter(ByteArrayOutputStream out){
try {
BinaryFileStream bs = new BinaryFileStream(((ByteArrayOutputStream)out).toByteArray());
Pdf pdf1 = new Pdf(bs);
aspose.pdf.Section sec1 = pdf1.getSections().add();
HeaderFooter footer = new HeaderFooter(sec1);
sec1.setOddFooter(footer);
sec1.setEvenFooter(footer);
Text t1 = new Text(footer, "Page: ($p of $P) ");
footer.getParagraphs().add(t1);
pdf1.save(bs);
} catch (Exception ex) {
ex.printStackTrace();
}
}


Srini

Hi Srini,

Once you have created the PDF file, you may consider saving it in OutputStream or BasicStream, instead of generating a PDF document over the system. Please take a look over the following screenshot which shows the overloaded methods of Pdf.Save(…).

Also please note that in your above code snippet, you have passed BinaryFileStream object as an argument to Pdf object. Please note that this approach is used in Direct to File mode and output is saved directly in BinaryFileStream object and once the PDF file is saved in BinaryFileStream object, you may consider writing its contents to Byte[] array or write the contents to other Stream objects. Also please note that when using this approach, you need to call Pdf.Close(…) method, instead of Pdf.Save(…).

In the event of any further query, please free to contact.

PS, You may also consider visiting the following link for details on how to add Image & Page Number in Header/Footer section

I got the process of creating PDF from scratch and adding footer. But i am looking for more process to take input stream and modify input stream and get output stream or byte array as output after adding footer to PDF document. I have tried following and its not working.

BinaryFileStream bs = null;
try {
bs = new BinaryFileStream(IOUtils.toByteArray(inStream));
Pdf pdf1 = new Pdf(bs);
aspose.pdf.Section sec1 = pdf1.getSections().add();
HeaderFooter footer = new HeaderFooter(sec1);
sec1.setOddFooter(footer);
sec1.setEvenFooter(footer);
//Text t1 = new Text(footer, "Page: ($p of $P) ");
Text t1 = new Text(footer, "Page: ($p of $P) ");
footer.getParagraphs().add(t1);
//pdf1.save(out);
pdf1.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return bs.toArray();

Can you please lookinto above code and let me know if i have to make any corrections or need to use different API to get footer into existing PDF document?
Regards
Srini

Hi Srini,

Thanks for contacting support.

I am glad to hear that you have managed to create a PDF document with Footer section. However concerning to your requirement on modifying an existing PDF document, please note that Aspose.Pdf for Java only supports the capability to create PDF documents from scratch whereas in order to modify the existing PDF document, you need to try using Aspose.Pdf.Kit for Java.

Aspose.Pdf.Kit for Java is a separate API which provides the capability to Edit/Manipulate existing PDF documents. In order to add Footer to existing PDF document, please try using addFooter(…) method of PdfFileStamp Class. Also please note that PdfFileStamp supports the capability to load source PDF file as InputStreaminputStream object and save the updated document in OutputStreamoutputStream object. Please take a look over the following overloaded constructor of PdfFileStamp class.

PdfFileStamp(java.io.InputStreaminputStream, java.io.OutputStreamoutputStream)