Adding images to Header and footer along with page number

Hi Aspose Team,

We are converting HTML to PDF (multi page pdf) using HtmlLoadOptions class and then passing HTML to Aspose.Pdf.Document class.
Now we have a requirement in which every page of generated pdf will have an image in header and image and page number in footer.
How can we accomplish this task using Aspose.Pdf....???
We cannot use the approach mentioned here : http://www.aspose.com/docs/display/pdfnet/Image++and++Page+Number+in+Header+Footer+section
because we no more using Aspose.Pdf.Generator.Pdf class for PDF generation.

Can your team help us with some code to accomplish our tasks.

Thanks in advance.

Hi Siddesh,


Thanks for your inquiry. Please check following code snippet for adding header/footer in PDF document. Hopefully it will help you to accomplish the requirements. If you want to add more than one element in a header/footer then you can use table for the purpose.

Document doc = new
Document(“Input.pdf”);<o:p></o:p>

Aspose.Pdf.HeaderFooter header = new Aspose.Pdf.HeaderFooter();

Aspose.Pdf.HeaderFooter footer = new Aspose.Pdf.HeaderFooter();

FileStream fs = new FileStream(myDir + "logo.png", FileMode.Open, FileAccess.Read);

Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();

image1.FixWidth = 50;

image1.FixHeight = 50;

//Add the image into paragraphs collection of the section

header.Paragraphs.Add(image1);

//Set the ImageStream to a MemoryStream object

image1.ImageStream = fs;

//Add footer text

Aspose.Pdf.Text.TextFragment fTxt = new Aspose.Pdf.Text.TextFragment("$p / $P ");

fTxt.TextState.Font = FontRepository.FindFont("Arial");

fTxt.TextState.FontSize = 16;

fTxt.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right;

footer.Paragraphs.Add(fTxt);

for (int cnt = 1; cnt <= doc.Pages.Count; cnt++)

{

doc.Pages[cnt].Header = header;

doc.Pages[cnt].Footer = footer;

}

doc.Save(myDir + "Output.pdf");

Please feel free to contact us for any further assistance.


Best Regards,

Hi Tilal,

Thanks for the above reference code.
While generating Pdf we have an image, text (Copy rights) which is to be fixed at a particular location and page number in footer section.
We are referring the below URL :
http://www.aspose.com/docs/display/pdfnet/Image++and++Page+Number+in+Header+Footer+section

Now we have concatenated PDF file, and we want to add image, text and page number to every page of generated PDF.
So how can we achieve it using Inline Paragraph or table in section.
We were stuck on how to add multiple elements on each pdf page....using Aspose.Pdf.Generator.Headerfooter........
It will be very helpful if you provide us with some code snippet.

Regards,
Siddesh



Hi Siddesh,


Thanks for your inquiry. Please note Aspose.Pdf.Generator(old generator) is only used to create PDF files from scratch. However Aspose.Pdf(new generator) is used for both manipulating existing PDF document and creating PDF file from scratch. As you want to add image, text in existing PDF document, so please used above suggested code to add image and text in Page header/footer. Hopefully it will help you to accomplish the task.

Please feel free to contact us for any further assistance.

Best Regards,

Hi Tilal,

How can we add image, some text and page number (3 items) in the footer section of an existing pdf using Aspose.Pdf.HeaderFooter...??
Please provide sample code snippet for the same.

Regards,

Siddesh

Hi Siddesh,


In order to add Footer with three elements (Image, Text and page numbering), following code snippet can be used. However during my testing, I have observed that Image and text are appearing only on first page and page numbering information is not being displayed over first page. Whereas on subsequent pages, Image and text are not being added but only Page numbering information is being displayed. For the sake of correction, I have logged this problem as PDFNEWNET-38279 in our issue tracking system. We will further look into the details of this matter and will keep you posted on the status of correction. Please be patient and spare us little time.

As a workaround, you may consider using following code snippet to accomplish your requirement.

[C#]

Document doc = new Document(“c:/pdftest/16±+scheduling+order.pdf”);<o:p></o:p>

// Aspose.Pdf.HeaderFooter header = new Aspose.Pdf.HeaderFooter();

Aspose.Pdf.HeaderFooter footer = new Aspose.Pdf.HeaderFooter();

//create image stamp

ImageStamp imageStamp = new ImageStamp("c:/pdftest/logoSpin.png");

imageStamp.Height = 50;

imageStamp.Width = 50;

imageStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Left;

//create footer

TextStamp textStamp = new TextStamp("Footer Text");

//set properties of the stamp

textStamp.BottomMargin = 10;

textStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;

textStamp.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Bottom;

//create page number stamp

PageNumberStamp pageNumberStamp = new PageNumberStamp();

//whether the stamp is background

pageNumberStamp.Background = false;

pageNumberStamp.Format = "Page # of " + doc.Pages.Count;

pageNumberStamp.BottomMargin = 10;

pageNumberStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right;

pageNumberStamp.StartingNumber = 1;

for (int cnt = 1; cnt <= doc.Pages.Count; cnt++)

{

doc.Pages[cnt].AddStamp(imageStamp);

doc.Pages[cnt].AddStamp(textStamp);

//add stamp to particular page

doc.Pages[cnt].AddStamp(pageNumberStamp);

}

doc.Save(“c:/pdftest/FooterAdded_Output.pdf”);

We use Aspose to save webpages as pdfs. I used this to add the url as page header and page numbers. But it works only on the 1st page, from second page on wards it shows some odd symbols.

Pls help

Hi there,


Thanks for your inquiry. Please share your sample code along with input/output PDF documents here, We will look into it and will guide you accordingly.

We are sorry for the inconvenience.

Best Regards,
This is the code

var pdfDocument = new Aspose.Pdf.Document(stream, options);

// Create footer
Aspose.Pdf.HeaderFooter footer = new Aspose.Pdf.HeaderFooter();
Aspose.Pdf.Text.TextFragment fTxt = new Aspose.Pdf.Text.TextFragment("$p / $P ");
fTxt.TextState.Font = FontRepository.FindFont("Arial");
fTxt.TextState.FontSize = 12;
fTxt.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right;
footer.Paragraphs.Add(fTxt);

foreach (Page page in pdfDocument.Pages)
{
page.Footer = footer;
}

Output is attached, only 1st page shoes page number

All the pages show page number footer when creating footer is moved inside the loop. Is that a correct solution?


foreach (Page page in pdfDocument.Pages)
{
// Create footer
HeaderFooter footer = new HeaderFooter();
TextFragment footerText = new TextFragment("$p / $P ");
footerText.TextState.Font = FontRepository.FindFont(“Arial”);
footerText.TextState.FontSize = 12;
footerText.TextState.ForegroundColor = Color.LightGray;
footerText.HorizontalAlignment = HorizontalAlignment.Right;
footer.Paragraphs.Add(footerText);

page.Footer = footer;
}
Hi there,

Helani:
This is the code

var pdfDocument = new Aspose.Pdf.Document(stream, options);

// Create footer
Aspose.Pdf.HeaderFooter footer = new Aspose.Pdf.HeaderFooter();
Aspose.Pdf.Text.TextFragment fTxt = new Aspose.Pdf.Text.TextFragment("$p / $P ");
fTxt.TextState.Font = FontRepository.FindFont("Arial");
fTxt.TextState.FontSize = 12;
fTxt.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right;
footer.Paragraphs.Add(fTxt);

foreach (Page page in pdfDocument.Pages)
{
page.Footer = footer;
}

Output is attached, only 1st page shoes page number

Thanks for your feedback. I have tested the scenario and noticed that when we initialize Footer object outside the loop page numbers are not reflecting on subsequent pages as expected, so logged a ticket PDFNET-41574 in our issue tracking system for further investigation and rectification.

We are sorry for the inconvenience.

Best Regards,

Hi there,


Helani:
All the pages show page number footer when creating footer is moved inside the loop. Is that a correct solution?

foreach (Page page in pdfDocument.Pages)
{
// Create footer
HeaderFooter footer = new HeaderFooter();
TextFragment footerText = new TextFragment("$p / $P ");
footerText.TextState.Font = FontRepository.FindFont(“Arial”);
footerText.TextState.FontSize = 12;
footerText.TextState.ForegroundColor = Color.LightGray;
footerText.HorizontalAlignment = HorizontalAlignment.Right;
footer.Paragraphs.Add(footerText);

page.Footer = footer;
}

Yes, you can initialize the HeaderFooter object inside loop as a workaround till we investigate and resolve above logged issue PDFNET-41574, please check following code snippet for details.

var pdfDocument = new
Aspose.Pdf.Document(myDir + “bookmark_grouping.pdf”);<o:p></o:p>

Aspose.Pdf.HeaderFooter footer = null;

Aspose.Pdf.Text.TextFragment fTxt = new Aspose.Pdf.Text.TextFragment("$p / $P ");

fTxt.TextState.Font = FontRepository.FindFont("Arial");

fTxt.TextState.FontSize = 12;

fTxt.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right;

foreach (Page page in pdfDocument.Pages)

{

footer = new Aspose.Pdf.HeaderFooter();

footer.Paragraphs.Add(fTxt);

page.Footer = footer;

}


Best Regards,