Quantcast
Channel: Aspose.Pdf Product Family
Viewing all articles
Browse latest Browse all 3131

Creating header and Footer does not work while using HeaderFooter object

$
0
0
Hi,

I am using the below code to create the footer but it does not work. Is there a work around to add a Page header and Footer in aspose.

Document document = new Document();

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

TextFragment textFragment = new TextFragment("Page Number 4");
textFragment.getTextState().setFont(font);
textFragment.getTextState().setFontSize(fontSize);

com.aspose.pdf.HeaderFooter footer = new com.aspose.pdf.HeaderFooter();
                        footer.getParagraphs().add(textFragment);

page.setFooter(footer);

document.getPages().add(page);

document.save("c:\\test.pdf");

I have also tried using PdfFileStamp but if the footer is dynamic and writing for every page in an existing document it starts overwritting the previous one.  I am loading a bytearray in bytearrayinputstream which is used by Document object.

com.aspose.pdf.Document doc= new com.aspose.pdf.Document();

The below code is run in a for loop.

ByteArrayInputStream dataStream = new ByteArrayInputStream(bytes);
com.aspose.pdf.Document dc = new com.aspose.pdf.Document(dataStream);

The above datastream can have multiple pages and I need to add footer for every page

//Appending the datastream loaded in dc to the main document
doc.getPages().add(dc.getPages());

I am passing this doc variable to PdfFileStamp and then calling the save method on the PdfFileStamp.

        //Footer text is the page title
        String FOOTER = "Footer text " + this.getSpaces(80) + "Page ";
        
         PdfFileStamp footerStamp = new PdfFileStamp(doc);
        
         FormattedText formattedText = new FormattedText(FOOTER);
         footerStamp.addFooter(formattedText, 24f);

        footerStamp.save("c:\\testdata.pdf");

}

private String getSpaces(int size) {
        StringBuffer buff = new StringBuffer();
        for(int i = 0; i < size; i++) {
            buff.append(' ');
        }
        return buff.toString();
    }


Regards,
Rajeev Mathur

Viewing all articles
Browse latest Browse all 3131

Trending Articles