I need to add header and footer for an existing pdf file. In the header and footer i want to add a table with multiple rows and columns with text and images inside the cell.I am able to add in aspose.words.
Please provide code samples.
The Header and Footer members of the Page class allows to add content in the header and footer. Please try the following code example:
[C#]
string dataDir = @"C:\Pdf\test516\";
//Instantiate PDF instance by calling empty constructor
Document document = new Document();
//Create a section in the pdf document
Aspose.Pdf.Page page1 = document.Pages.Add();
// Create a Header Section of the PDF file
Aspose.Pdf.HeaderFooter header = new Aspose.Pdf.HeaderFooter();
//Instantiate a table object
Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table();
tab1.HorizontalAlignment = HorizontalAlignment.Center;
header.Paragraphs.Add(tab1);
tab1.ColumnWidths = "100";
//Create rows in the table and then cells in the rows
Aspose.Pdf.Row row1 = tab1.Rows.Add();
row1.Cells.Add("row 1 column 1").Alignment = HorizontalAlignment.Center;
row1.Cells.Add("row 1 column 2").Alignment = HorizontalAlignment.Center;
row1.Cells.Add("row 1 column 3").Alignment = HorizontalAlignment.Center;
Aspose.Pdf.Row row2 = tab1.Rows.Add();
row2.Cells.Add("row 1 column 1").Alignment = HorizontalAlignment.Center;
row2.Cells.Add("row 2 column 2").Alignment = HorizontalAlignment.Center;
row2.Cells.Add("row 3 column 3").Alignment = HorizontalAlignment.Center;
page1.Header = header;
document.Save(@"C:\Pdf\test517\Output.pdf", SaveFormat.Pdf);
This is the output PDF: Output.pdf (2.0 KB)
Thanks it worked. Is it possible to merge cells in rows?
Also how can i remove headers from an existing pdf. Thanks in advance
You can arrange table cells as per your need with ColSpan, RowSpan and Remove members of the Cell instance. as follows:
[C#]
row1.Cells[1].ColSpan = 2;
row1.Cells[1].RowSpan = 2;
row1.Cells.Remove(row1.Cells[3]);
You can remove the header and footer by defining the region of the page. The Rect property of the page instance returns the rectangular region of the page and we can modify it as per our need. Please refer to this help topic: Redact certain page region with RedactionAnnotation
Thanks
I have an issue that header entering into the page.
Kindly send us the complete details of your scenario, including source PDF and code. We will investigate and share our findings with you.
We are trying to add header and footer for a pdf file.We want to add a table into the pdf header, the header may be text or an image.It should be possible to merge cells.
We were able to add same features in word file.
We have shared the code example in an earlier post of this thread to add table in the header of the PDF document, ColSpan and RowSpan members of the table cell allow to merge cells. You can add an image inn the table’s cell as follows:
[C#]
private static void AddImageToCell(Aspose.Pdf.Cell cell, System.Drawing.Image image, int imageResolution = -1)
{
if (image != null)
{
MemoryStream ImageStream = GetImageStream(image);
Aspose.Pdf.Image AsposeImage = new Aspose.Pdf.Image();
AsposeImage.ImageStream = ImageStream;
AsposeImage.FileType = Aspose.Pdf.ImageFileType.Unknown;
if (imageResolution > 0)
{
AsposeImage.FixHeight = imageResolution;
AsposeImage.FixWidth = imageResolution;
}
else
{
AsposeImage.FixHeight = image.Height;
AsposeImage.FixWidth = image.Width;
}
cell.Paragraphs.Add(AsposeImage);
}
}
If this does not help, then kindly send us your source PDF document along with an expected output PDF. We will investigate and share our findings with you.