Hello,
I am trying to add some space between the footer and my data but have not successful. Also, it is
not appearing on all the pages. I even set the IsFirstPageOnly = false;
Could you please show me how to fix it?
Attached is a screen shot showing the footer is too close to my PDF content and I need to add a line break there. Below is my code:
//add footer
HeaderFooter footer = new HeaderFooter();
footer.IsFirstPageOnly = false;
secLogo.OddFooter = secLogo.EvenFooter = footer;
string strFooter = "company ABC";
Text txtFooter = BillAnalysis.PdfFunctions.AddStringToPDFTextObject(strFooter, "center", false, Page);
txtFooter.IsHtmlTagSupported = false;
txtFooter.TextInfo.Alignment = AlignmentType.Center;
txtFooter.TextInfo.FontSize = 8;
footer.Paragraphs.Add(txtFooter);
Hello Susan,
Thanks for using our products.
In order to set some space between page contents and Header/Footer section, you need to try using DistanceFromEdge property of HeaderFooter class.
However, during my testing with following code snippet with Aspose.Pdf for .NET 5.0.0, I am unable to notice the problem that Footer is not appearing on all pages. Can you please take a look over the attached PDF document.
[C#]
//Instantiate a Pdf instance
Pdf pdf1 = new Pdf();
//Create a section in the Pdf instance
Section sec1 = pdf1.Sections.Add();
//Instantiate HeaderFooter object and pass the Section reference in which
for(int i=0;i<=200; i++)
{
sec1.Paragraphs.Add(new Text("sample text line inside section object. Serial number " +i.ToString()));
}
//the header or footer is to be added
HeaderFooter hf1 = new HeaderFooter(sec1);
// specify the Distance of Header or Footer's distance from the edge of the page
hf1.DistanceFromEdge = 20;
//Set the header of odd pages of the PDF document
sec1.OddFooter = hf1;
//Set the header of even pages of the PDF document
sec1.EvenFooter = hf1;
//Instantiate a Text paragraph that will store the content to show as header
Text text = new Text(hf1, "header");
text.TextInfo.Alignment = AlignmentType.Center;
text.TextInfo.FontSize = 8;
text.IsHtmlTagSupported = false;
//Add the text object to the Paragraphs collection of HeaderFooter object to
//display header on the pages of PDF document
hf1.Paragraphs.Add(text);
pdf1.Save(@"D:\pdftest\Footer-Distance-Issue.pdf");
Nayyer,
Thanks for the quick reply. DistanceFromEdge fixed my first problem. In regard to my 2nd problem (footer does not appear on all pages), I noticed that you added series of Paragraph/Text objects into the Section. In my case, I am importing a datatable into only one Paragraph/Table and add that table to the Section and then another Section after that. Could that be the problem? Below is the code:
//Create a section in the Pdf instance
Section sec1 = pdf1.Sections.Add();
sec1.IsNewPage = false;
//create a table to import data
Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table();
tab1.DefaultCellTextInfo.FontSize = 8;
tab1.DefaultCellTextInfo.FontName = "Arial Unicode MS";
tab1.DefaultCellPadding.Left = 2;
tab1.DefaultCellPadding.Right = 2;
tab1.DefaultCellPadding.Bottom = 2;
tab1.DefaultCellPadding.Top = 2;
sec1.Paragraphs.Add(tab1);
tab1.ColumnWidths = "70 300 150 50 50 50 50 50 50 50 50 50";
tab1.ColumnAdjustment = ColumnAdjustmentType.AutoFitToContent;
tab1.DefaultCellBorder = new BorderInfo((int)BorderSide.All, 0.1F);
//Import data into the Table object from the DataTable created above
tab1.ImportDataTable(dtData, false, 2, 0);
//=============section Note =========================================================================
Section sectionNote = pdf1.Sections.Add();
sectionNote.IsNewPage = false;
sectionNote.PageInfo.Margin.Left = 100;
sectionNote.PageInfo.Margin.Top = 15;
string strNote = Resources.ResourceSummary.pdf_note;
Text txtNote = BillAnalysis.PdfFunctions.AddStringToPDFTextObject(strNote, "center", true, Page);
txtNote.TextInfo.FontSize = 10;
txtNote.TextInfo.Alignment = AlignmentType.Center;
txtNote.TextInfo.FontName = "Arial Unicode MS";
txtNote.Margin.Top = 10;
sectionNote.Paragraphs.Add(txtNote);
//add footer
HeaderFooter footer = new HeaderFooter(sec1);
footer.DistanceFromEdge = 10;
sec1.OddFooter = footer;
sec1.EvenFooter = footer;
string strFooter = Resources.ResourceSummary.pdf_footer;
Text txtFooter = new Text(footer, strFooter);
txtFooter.IsHtmlTagSupported = false;
txtFooter.TextInfo.Alignment = AlignmentType.Center;
txtFooter.TextInfo.FontSize = 8;
txtFooter.TextInfo.FontName = "Arial Unicode MS";
footer.Paragraphs.Add(txtFooter);
pdf1.IsPageNumberForDocument = true;
pdf1.Title = Resources.ResourceSummary._pageHeader_Text;
//Saves file to memory
MemoryStream stream = new MemoryStream();
pdf1.SetUnicode();
pdf1.Save(stream);
Hello Susan.
Thanks for sharing the information.
As far as I can understand from your code snippet, you have imported DataTable contents inside sec1. Also you have specified footer as OddFooter and EvenFooter for Sec1. However, txtNote is being added to paragraphs collection of sectionNote and Footer information is not being specified for this section.
The sections inside PDF document are independent and the pages inside them are also independent. In case you need to display footer inside sectionNote, you need to explicitly specify it.
In case you still face any problem, please feel free to contact.
Thanks Nayyer.
I do not need the footer to go with sectionNote. I need the footer to go in sec1 because this is where all my data are displayed. The DataTable contains multiple pages of data and the footer is not displaying on those pages. sectionNote is working correctly in that it only appears on the last page of data.
Hello Susan.
I have tested the scenario using the code snippet that you have shared earlier and I am unable to notice the problem. The footer is properly being displayed on all pages of Sec1. I have used the following code to create a DataTable and the resultant PDF that I have generated is in attachment, please take a look. I have used Aspose.Pdf for .NET 5.0.0 to test the scenario.
[C#]
DataTable dt = new DataTable("Employee");
dt.Columns.Add("Employee_ID", typeof(Int32));
dt.Columns.Add("Employee_Name", typeof(string));
dt.Columns.Add("Gender", typeof(string));
//Add 2 rows into the DataTable object programmatically
DataRow dr = dt.NewRow();
for (int i = 0; i <= 200; i++)
{
dr[0] = i;
dr[1] = "John Smith";
dr[2] = "Male";
dt.Rows.Add(dr);
dr = dt.NewRow();
}