We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Issue with Footer when I provide MarginInfo

Hi,

I am using the following code to add the page footer.

TextFragment textFragment = new TextFragment(“Testing Footer Page Number 1”);
textFragment.getTextState().setFont(new com.aspose.pdf.FontRepository().findFont(“Helvetica”));
textFragment.getTextState().setFontSize(8f);

com.aspose.pdf.HeaderFooter footer= new com.aspose.pdf.HeaderFooter();

MarginInfo marginInfo = new MarginInfo();
marginInfo.setBottom(20.00);

footer.setMargin(marginInfo);

Paragraphs paragraphs = new Paragraphs();
paragraphs.add(textFragment);

footer.setParagraphs(paragraphs);

pdfDocument.getPages().get_Item(1).setFooter(header);

The above code does not place a footer on the page, but if I comment the Margin Info it works fine. Is there an issue with using Margin Info. I want the footer to be placed at a particular location from the bottom.

Regards,
Rajeev Mathur

Hi Rajeev,


Thanks for contacting support.

I have tested the scenario using Aspose.Pdf for Java 9.3.1 using following code snippet and as per my observations, the footer is properly being added to PDF file.

[Java]

Document document = new Document();<o:p></o:p>

Page page = document.getPages().add();

TextFragment textFragment = new TextFragment("Testing Footer Page Number 1");

textFragment.getTextState().setFont(new com.aspose.pdf.FontRepository().findFont("Helvetica"));

textFragment.getTextState().setFontSize(8f);

com.aspose.pdf.HeaderFooter footer = new com.aspose.pdf.HeaderFooter();

MarginInfo marginInfo = new MarginInfo();

marginInfo.setBottom(10.00);

footer.setMargin(marginInfo);

Paragraphs paragraphs = new Paragraphs();

paragraphs.add(textFragment);

footer.setParagraphs(paragraphs);

document.getPages().get_Item(1).setFooter(footer);

document.save("c:/pdftest/FooterWithMarginAdded.pdf");

Hi,

The issue is with an existing document, In my case I am creating a pdf document and after the document is created I run a for loop for all the pages and set the footer. In that scenario it does not show a footer in the final document. It only displays the header and if you see the header font width is different on every page. Below is the code snippet. One of the other issues I have noticed is, when I add a header on an existing page it creates a wide gap between the header and starting paragraph on the page. It could be possible that I might be doing some thing wrong, can you please guide me with this.

The workaround I am using is by adding textFragment at the Header and Footer positions and not using HeaderFooter object.

for(int i=1; i<= pdfDocument.getPages().size(); i++)
{
if(pdfDocument.getPages().get_Item(i).getFooter() == null){

TextFragment textFragment = new TextFragment(“testing the footer”);
textFragment.getTextState().setFont(new com.aspose.pdf.FontRepository().findFont(“Helvetica”));
textFragment.getTextState().setFontSize(8f);


com.aspose.pdf.HeaderFooter footer = new com.aspose.pdf.HeaderFooter();

MarginInfo marginInfo = new MarginInfo();
marginInfo.setBottom(20.00);

footer.setMargin(marginInfo);

Paragraphs paragraphs = new Paragraphs();
paragraphs.add(textFragment);

footer.setParagraphs(paragraphs);

pdfDocument.getPages().get_Item(i).setFooter(footer);

}


if(pdfDocument.getPages().get_Item(i).getHeader() == null){
pdfDocument.getPages().get_Item(i).setHeader(createPageHeader(“Test Header”));
}

}
pdfDocument.save(“c:\documents\testdata\footer.pdf”);

private com.aspose.pdf.HeaderFooter createPageHeader(String header) throws Exception {

TextFragment textFragment = new TextFragment(header);
textFragment.getTextState().setFont(new com.aspose.pdf.FontRepository().findFont(“Helvetica”));
textFragment.getTextState().setFontSize(8f);

com.aspose.pdf.HeaderFooter header = new com.aspose.pdf.HeaderFooter();

MarginInfo marginInfo = new MarginInfo();
marginInfo.setTop(10.00);

header.setMargin(marginInfo);

Paragraphs paragraphs = new Paragraphs();
paragraphs.add(textFragment);

header.setParagraphs(paragraphs);


return header;
}

Regards,
Rajeev Mathur

Hi Rajeev,


Thanks for sharing the details.

I have tested the scenario using Aspose.Pdf for Java 9.3.1 where I have used one of my sample PDF files and I am unable to notice any problem. The Header and Footer are properly appearing on resultant file. Can you please share the resource / input PDF causing this problem so that we can again test the scenario at our end. We are sorry for this inconvenience.

Hi Nayye,

Thanks for your response, I am using Aspose 9.3.0 but I have tried 9.3.1 and the issue still exist. Here is the exact scenario. We have a map with bytearrays, each of these bytearrays can have multiple pdf pages. We load the bytearray in the aspose PDF document object as shown below.

ByteArrayInputStream dataStream = new ByteArrayInputStream(bytes);
//This gives me the pdf pages in the bytearray which will be merged in one document.
com.aspose.pdf.Document dc = new com.aspose.pdf.Document(dataStream);

Now I try to add the header and footer here before merging them in the main document called finalDocument. I am using the following code.


for(int i=1; i< dc.getPages().size(); i++)
{
Page page = dc.getPages().get_Item(i);
ByteArrayOutputStream os = new ByteArrayOutputStream();
TextFragment textFragment = new TextFragment(“Testing Footer Page Number”);
textFragment.getTextState().setFont(new FontRepository().findFont(“Helvetica”));
textFragment.getTextState().setFontSize(8f);

com.aspose.pdf.HeaderFooter footer = new com.aspose.pdf.HeaderFooter();
MarginInfo marginInfo = new MarginInfo();
marginInfo.setBottom(20.00);
footer.setMargin(marginInfo);

Paragraphs paragraphs = new Paragraphs();
paragraphs.add(textFragment);

footer.setParagraphs(paragraphs);
page.setFooter(footer);
// dc.save(os);

}

After this I am adding all the pages in the dc document to the final document as shown below.

finalDocument.getPages().add(dc.getPages());

All the above code is called in a loop as I have mentioned it si a map of bytearrays. The final document does not have a footer. But what I have noticed is, as you can see in the above code I have commented saving dc object, if I uncomment it then in the final document I see the footer. I really do not get it why do I need to save since it is not my final document.

Please let me know if you see there is any issue in the above code.

Regards,
Rajeev Mathur



rajeevkrmathur:

All the above code is called in a loop as I have mentioned it si a map of bytearrays. The final document does not have a footer. But what I have noticed is, as you can see in the above code I have commented saving dc object, if I uncomment it then in the final document I see the footer. I really do not get it why do I need to save since it is not my final document.
Hi Rajeev,

Thanks for sharing the details.

I have tested the scenario and I am able to notice the same problem. For the sake of correction, I have logged this problem as PDFNEWJAVA-34379 in our issue tracking system. We will further look into the details of this problem and will keep you updated on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.