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

Free Support Forum - aspose.com

Set different headers for first and subsequent pages

Hi,


I am using Aspose.pdf 10.9 version. I want to add different headers for first and subsequent pages in the document. I was able to do it with the legacy API.
How can I do this in the new API? I did not see any options in com.aspose.pdf.HeaderFooter.

Thanks
Mamatha

Hi Mamatha,


Thanks for contacting support.

In order to accomplish your requirement, please try using following code snippet. For your reference, I have also attached the output generated over my end.

In case I have not properly understood your requirements, or you have any further query, please feel free to contact.

[Java]

//open source document<o:p></o:p>

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

doc.getPages().add();

doc.getPages().add();

doc.getPages().add();

//create three stamps

com.aspose.pdf.TextStamp stamp1 = new com.aspose.pdf.TextStamp("Header 1");

com.aspose.pdf.TextStamp stamp2 = new com.aspose.pdf.TextStamp("Header 2");

com.aspose.pdf.TextStamp stamp3 = new com.aspose.pdf.TextStamp("Header 3");

//set stamp alignment (place stamp on page top, centered horiznotally)

stamp1.setVerticalAlignment(com.aspose.pdf.VerticalAlignment.Top);

stamp1.setHorizontalAlignment(com.aspose.pdf.HorizontalAlignment.Center);

// specify the font style as Bold

stamp1.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Bold);

// set the text fore ground color information as red

stamp1.getTextState().setForegroundColor(com.aspose.pdf.Color.getRed());

// specify the font size as 14

stamp1.getTextState().setFontSize(14);

// now we need to set the vertical alignment of 2nd stamp object as Top

stamp2.setVerticalAlignment(com.aspose.pdf.VerticalAlignment.Top);

// set Horizontal alignment information for stamp as Center aligned

stamp2.setHorizontalAlignment(com.aspose.pdf.HorizontalAlignment.Center);

// set the zooming factor for stamp object

stamp2.setZoom(10);

// set the formatting of 3rd stamp object

// specify the Vertical alignment information for stamp object as TOP

stamp3.setVerticalAlignment(com.aspose.pdf.VerticalAlignment.Top);

// Set the Horizontal alignment inforamtion for stamp object as Center aligned

stamp3.setHorizontalAlignment(com.aspose.pdf.HorizontalAlignment.Center);

// set the rotation angle for stamp object

stamp3.setRotateAngle(35);

// set pink as background color for stamp

stamp3.getTextState().setBackgroundColor(com.aspose.pdf.Color.getPink());

// change the font face information for stamp to Verdana

stamp3.getTextState().setFont(com.aspose.pdf.FontRepository.findFont("Verdana"));

//first stamp is added on first page;

doc.getPages().get_Item(1).addStamp(stamp1);

//second stamp is added on second page;

doc.getPages().get_Item(2).addStamp(stamp2);

//third stamp is added on third page.

doc.getPages().get_Item(3).addStamp(stamp3);

// save the updated document

doc.save(“c://pdftest//multiheader.pdf”);

Nayyer,

In legacy api, header object had option like isFirstPagesOnly , isSubsequentPagesOnly. I am looking for same functionality in the new API.

Thanks
Mamatha

Hi Mamatha,


Thanks for your inquiry. In new DOM similar properties are not available that were in old generator. However you can achieve your requirements as following, hopefully it will help you to accomplish the task.

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

Aspose.Pdf.Page page = pdf.Pages.Add();

page.OnBeforePageGenerate += OnPageGenerate;

var text = new TextFragment("Aspose.Pdf for .NET");

for (int i = 0; i < 400; i++)

{

page.Paragraphs.Add(text);

}

pdf.Save("E:/data/Headerfooter_DOM.pdf");

---------------

public static void OnPageGenerate(Aspose.Pdf.Page page)

{

if (page.Number == 1)

{

page.Footer = new Aspose.Pdf.HeaderFooter();

page.Footer.Margin = new Aspose.Pdf.MarginInfo();

page.Footer.Margin.Left = 50;

page.Footer.Margin.Right = 50;

page.Footer.Margin.Top = 30;

page.Footer.Margin.Bottom = 10;

TextFragment footerText = new TextFragment();

TextSegment footerSegment = new TextSegment("First page");

footerSegment.TextState.FontSize = 12;

footerSegment.TextState.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;

footerText.Segments.Add(footerSegment);

footerText.TextState.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;

page.Footer.Paragraphs.Add(footerText);

}

else

{

page.Footer = new Aspose.Pdf.HeaderFooter();

page.Footer.Margin = new Aspose.Pdf.MarginInfo();

page.Footer.Margin.Left = 50;

page.Footer.Margin.Right = 50;

page.Footer.Margin.Top = 30;

page.Footer.Margin.Bottom = 10;

TextFragment footerText = new TextFragment();

TextSegment footerSegment = new TextSegment("Subsequent page $p / $P");

footerSegment.TextState.FontSize = 12;

footerSegment.TextState.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;

footerText.Segments.Add(footerSegment);

footerText.TextState.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;

page.Footer.Paragraphs.Add(footerText);

}

}


Please feel free to contact us for any further assistance.

Best Regards,

Hi,



Legacy API for headers were very convenient. You guys should have retained them.

Anyhow can you please provide sample code for Java.

In the meanwhile I tried to add header by iterating over the pages but it didn’t work.



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

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

//Added mutiple text fragments to the page which spaned across 4 pages



TextFragment ptxt = new TextFragment("$p of $P"); //Text fragment used for the footer

Here $p and $P are giving right page numbers.

But doc.getPages().size() is giving 1 instead of 4.



Thanks

Mamatha

Hi Mamatha,


Thanks for your inquiry. My apologies for sharing .NET sample code, we will share the Java version soon.

Furthermore, Please note you need to save document and reload it to get page numbers. As PDF document is created dynamically by API and in order to get real PDF document it needs to render its programming/dynamic model. So we can render PDF document model by calling save() method or processParagraph(). you can iterate through the pages as following to add footers/headers.

String outFile = myDir + “headerjava.pdf”;<o:p></o:p>

com.aspose.pdf.Document doc = new
com.aspose.pdf.Document();<o:p></o:p>

<o:p></o:p>

//creating the first page with
footer
<o:p></o:p>

Page page1 = doc.getPages().add();<o:p></o:p>

page1.getParagraphs().add(new TextFragment(“Sample
text”
));<o:p></o:p>

doc.getPages().add(page1);<o:p></o:p>

doc.getPages().add(page1);<o:p></o:p>

doc.getPages().add(page1);<o:p></o:p>

ByteArrayOutputStream outStream = new
ByteArrayOutputStream();<o:p></o:p>

doc.save(outStream);<o:p></o:p>

doc=new Document(new
ByteArrayInputStream(outStream.toByteArray()));<o:p></o:p>


for (int pageCount = 1;
pageCount <= doc.getPages().size(); pageCount++)<o:p></o:p>


{<o:p></o:p>

if (pageCount==1)<o:p></o:p>

{<o:p></o:p>


com.aspose.pdf.HeaderFooter footer = new
com.aspose.pdf.HeaderFooter();<o:p></o:p>


TextFragment ftext=new TextFragment(“First
Page”
);<o:p></o:p>


footer.getParagraphs().add(ftext);<o:p></o:p>


doc.getPages().get_Item(pageCount).setFooter(footer);<o:p></o:p>


}<o:p></o:p>

else<o:p></o:p>

{<o:p></o:p>


com.aspose.pdf.HeaderFooter footer = new
com.aspose.pdf.HeaderFooter();<o:p></o:p>


TextFragment ftext=new
TextFragment(pageCount+" of "+doc.getPages().size());<o:p></o:p>


footer.getParagraphs().add(ftext);<o:p></o:p>


doc.getPages().get_Item(pageCount).setFooter(footer);<o:p></o:p>

}<o:p></o:p>


}<o:p></o:p>

<o:p></o:p>

doc.save(outFile);<o:p></o:p>


Best Regards,

Hi Mamatha,


Thanks for your patience. Please check following code snippet to add Header/Footer using BeforePageGenerate delegate, while creating a new PDF document.

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

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

page.OnBeforePageGenerate.add(new BeforePageGenerate() {

public void invoke(Page page) {

onPageGenerate(page);

}});

TextFragment text = new TextFragment("Aspose.Pdf for Java");

for (int i = 0; i < 400; i++)

{

page.getParagraphs().add(text);

}

pdf.save(myDir+"Headerfooter_DOM.pdf");

-------

public static void onPageGenerate(Page page)

{

if (page.getNumber() == 1)

{

page.setFooter(new com.aspose.pdf.HeaderFooter());

page.getFooter().setMargin (new MarginInfo());

page.getFooter().getMargin().setLeft (50);

page.getFooter().getMargin().setRight (50);

page.getFooter().getMargin().setTop (30);

page.getFooter().getMargin().setBottom (10);

TextFragment footerText = new TextFragment();

TextSegment footerSegment = new TextSegment("First page");

footerSegment.getTextState().setFontSize (12);

footerSegment.getTextState().setHorizontalAlignment (HorizontalAlignment.Center);

footerText.getSegments().add(footerSegment);

footerText.getTextState().setHorizontalAlignment (HorizontalAlignment.Center);

page.getFooter().getParagraphs().add(footerText);

}

else

{

page.setFooter(new com.aspose.pdf.HeaderFooter());

page.getFooter().setMargin (new MarginInfo());

page.getFooter().getMargin().setLeft (50);

page.getFooter().getMargin().setRight (50);

page.getFooter().getMargin().setTop (30);

page.getFooter().getMargin().setBottom (10);

TextFragment footerText = new TextFragment();

TextSegment footerSegment = new TextSegment("Subsequent page $p / $P");

footerSegment.getTextState().setFontSize (12);

footerSegment.getTextState().setHorizontalAlignment (HorizontalAlignment.Center);

footerText.getSegments().add(footerSegment);

footerText.getTextState().setHorizontalAlignment (HorizontalAlignment.Center);

page.getFooter().getParagraphs().add(footerText);

}

}


Please feel free to contact us for any further assistance.

Best Regards,

That worked. Thank you.

Hi Mamatha,



Thanks for your feedback. It is good to know that you have managed to accomplish your requirement.

Please keep using our API and feel free to contact us for any further assistance, we will be more than happy to extend our support.

Best Regards,