When Footer text is center aligned or left aligned the footer text is ok with page numbering but when the text is right aligned the text stretches in resulted PDF
here is the code for applying footer text to PDF using blink converter:
try
{
var footerMarginLeft = footerMargin != null ? footerMargin.Left : 0;
var footerMarginRight = footerMargin != null ? footerMargin.Right : 0;
var footerMarginHeight = footerMargin != null ? footerMargin.Height : 0;
BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
blinkConverterSettings.CommandLineArguments.Add("--no-sandbox");
blinkConverterSettings.CommandLineArguments.Add("--disable-setuid-sandbox");
blinkConverterSettings.PdfPageSize = new SizeF(pdfPageSize.Width, footerMarginHeight);
blinkConverterSettings.Orientation = pdfPageOrientation;
blinkConverterSettings.ViewPortSize = new Syncfusion.Drawing.Size(1024, 0);
blinkConverterSettings.EnableJavaScript = manifest.EnableJavaScript;
float width = pdfPageSize.Width;
float height = pdfPageSize.Height;
RectangleF bounds = new RectangleF();
if (pdfPageOrientation.ToString() == "Portrait")
{
bounds = new RectangleF(footerMarginLeft, 0, width - (footerMarginRight + footerMarginLeft), footerMarginHeight);
}
else
{
bounds = new RectangleF(footerMarginLeft, 0, height - (footerMarginRight + footerMarginLeft), footerMarginHeight);
}
//RectangleF bounds = new RectangleF(footerMarginLeft, 0, width - (footerMarginRight + footerMarginLeft), footerMarginHeight);
PdfPageTemplateElement footer = new PdfPageTemplateElement(bounds);
PdfSolidBrush brush = new PdfSolidBrush(Syncfusion.Drawing.Color.Black);
string footerFont = footerProperties.Font.ToLower();
//PdfFontFamily footerFontFamily = new PdfFontFamily();
int footerFontSize;
//switch (footerFont)
//{
// case "courier":
// footerFontFamily = PdfFontFamily.Courier;
// break;
// case "helvetica":
// footerFontFamily = PdfFontFamily.Helvetica;
// break;
// case "timesroman":
// footerFontFamily = PdfFontFamily.TimesRoman;
// break;
// default:
// footerFontFamily = PdfFontFamily.Helvetica;
// break;
//}
if (!string.IsNullOrEmpty(footerProperties.FontSize.ToString()))
{
footerFontSize = footerProperties.FontSize;
}
else
{
footerFontSize = 15;
}
//Syncfusion.Pdf.Graphics.PdfFont font = new PdfStandardFont(footerFontFamily, footerFontSize);
var footerFontFamily = Path.Combine(Environment.CurrentDirectory, @"Fonts//arial-unicode-ms.ttf");
PdfFont font = new PdfTrueTypeFont(footerFontFamily, manifest.Input.PageSetup.FooterText.FontSize);
IFooterComponent footerComponent = new Footer();
string footeralignment = footerProperties.Alignment.ToLower();
//Create a new PDF string format instance.
PdfStringFormat format = new PdfStringFormat();
//Set the text alignment.
if (footeralignment == "center")
{
if (includePageNumber)
{
footerComponent = new CenterAlignmentDecorator(footerComponent);
footerComponent.Draw(footer, pdfPageSize, manifest.Input.PageSetup.Orientation, font);
}
format.Alignment = PdfTextAlignment.Center;
}
else if (footeralignment == "left")
{
if (includePageNumber)
{
footerComponent = new LeftAlignmentDecorator(footerComponent);
footerComponent.Draw(footer, pdfPageSize, manifest.Input.PageSetup.Orientation, font);
}
format.Alignment = PdfTextAlignment.Left;
}
else if (footeralignment == "right")
{
if (includePageNumber)
{
if (manifest.Input.PageSetup.Orientation.Equals("Portrait"))
{
bounds = new RectangleF(-25, 0, width - (footerMarginRight + footerMarginLeft), footerMarginHeight);
}
else
{
bounds = new RectangleF(-100, 0, width - (footerMarginRight + footerMarginLeft), footerMarginHeight);
}
footer = new PdfPageTemplateElement(bounds);
footerComponent = new RightAlignmentDecorator(footerComponent);
footerComponent.Draw(footer, pdfPageSize, manifest.Input.PageSetup.Orientation, font);
}
format.Alignment = PdfTextAlignment.Right;
}
else
{
format.Alignment = PdfTextAlignment.Center;
}
footer.Graphics.DrawString(footerProperties.Text, font, brush, bounds, format);
blinkConverterSettings.PdfFooter = footer;
return footer;
}
catch (Exception ex)
{
throw new Exception(ex?.Message);
}
Hi Duminda,
Thank you for reaching out to Syncfusion support. We are tried to recreate the reported behavior on our end. We suspect the reported issue occurs for particular input property values, input HTML/URL. So we kindly request you to share the simplified sample with input documents and environment details (such as OS platform, bit version and RAM size) with us to reproduce the reported issue on our end. This information will be more helpful for us to analyze and provide you with a prompt solution.
Karmegam
Hi karmegam,
Requested information as per your request are as follows
Platform / Version : Windows 11 22H2
bit Version : 64 bit
Ram Size : 32 GB
Regards,
Duminda.
Hi Duminda,
Thank you for providing additional information.
On our further validation, The footer text stretching issue occurs in landscape orientation. So, instead of width,
we need to set the height of the pdf page size within the bounds of the alignment handling condition to resolve this. We need to replace the width using the height in the Alignment bounds of the footer component. We have attached the code snippet for your reference,
Code snippet:
else { bounds = new RectangleF(-100, 0, height - (footerMarginRight + footerMarginLeft), footerMarginHeight); } |
Kindly try the solution and let us know the result.
Regards,
Arumugam M
Hi Arumugam M ,
I tested the code segment given above in the project, but couldn't find any difference in the resulting file. I also used another template to test this. plz find it attached here with.
Regards,
Duminda.
Hi Duminda,
Sorry for any inconvenience caused.
We have checked the reported issue with the provided input html document with your previously shared code snippet and it's working properly. We have attached the console sample for your reference. Please refer below.
Output: https://www.syncfusion.com/downloads/support/directtrac/general/ze/Output1796739129
Kindly try the provided sample on your end and let us know the result. If you are still facing the issue, we kindly request you to share the modified sample to reproduce the reported issue on our end. This information will be more helpful for us to analyze and provide you with a prompt solution.
Regards,
Arumugam M
Hi Arumugam M,
It seems to be ok in portrait orientation can you check this in landscape orientation, problem exists in landscape orientation for me.
Thank you
Regards,
Duminda
Hi Duminda,
Upon further
analysis, we have successfully replicated the reported issue on our end. As
mentioned previously, we can address this issue by calculating the height value
in landscape mode instead of width. We have also included a sample for your
reference.
Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/ConsoleApp1-2006457917
|
// This code is used to set the text alignment to right. if (pdfPageOrientation.ToString() == "Portrait") { bounds = new RectangleF(-25, 0, width - (footerMarginRight + footerMarginLeft), footerMarginHeight); } else { bounds = new RectangleF(-100, 0, height - (footerMarginRight + footerMarginLeft), footerMarginHeight); } footer = new PdfPageTemplateElement(bounds); format.Alignment = PdfTextAlignment.Right; footer.Graphics.DrawString("HTML to PDF Conversion", font, brush, bounds, format); |
Kindly try the sample and let us know the result. If you are still facing issue we kindly request you to share the modified sample to replicate same issue on our end.
Note: We have confirmed that the conversion can be achieved in both portrait and landscape modes by including the orientation value in the AddFooter method in the provided example. Additionally, we have included the code snippet specifically for aligning text to the right.
Regards,
Arumugam M
Hi Arumugam M,
Tried your console app the footer looks fine in the resulting PDF but still page numbers are missing, I used the above specified changes in my project but still makes no difference from my side.
Thank you,
Regards,
Duminda Jayasuriya.
Hi Duminda,
We have checked the reported issue on our end. We can add the Page number in footer template using the PdfPageNumberField and PdfPageCountField class. Please refer the below code snippet and attached sample for your reference.
Code Snippet:
//Add Page Number PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush); PdfPageCountField pageCount = new PdfPageCountField(font, brush); PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, pageCount) { Bounds = new RectangleF(0, 0, bounds.Width, bounds.Height), StringFormat = format }; footer.Graphics.DrawString("HTML to PDF Conversion", font, brush, bounds, format); compositeField.Draw(footer.Graphics, new PointF(0, bounds.Height / 2)); blinkConverterSettings.PdfFooter = footer; |
Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/ConsoleApp1-376560632
Kindly try the above solution and let us know the result.
Regards,
Arumugam
Hi Arumugam M,
The solution works fine on my end, next I want to set text alignment to left, right and center while the page number sits on the right corner of the page or for an instance where the page number sits on the middle of the page and text comes on right and left side of the footer.
Regards,
Duminda
Hi Duminda,
We have checked your issue on our end. We can customize the alignment of the page number and footer text template in the PDF document by using two separate instances of PdfStringFormat for each template. Please refer the below code snippet and attached sample for your reference.
Code Snippet:
//Create a new PDF string format instance. PdfStringFormat formatFooterText = new PdfStringFormat();
// This code is used to set the text alignment to right. if (pdfPageOrientation.ToString() == "Portrait") { bounds = new RectangleF(-25, 0, width - (footerMarginRight + footerMarginLeft), footerMarginHeight); } else { bounds = new RectangleF(-100, 0, height - (footerMarginRight + footerMarginLeft), footerMarginHeight); } footer = new PdfPageTemplateElement(bounds);
format.Alignment = PdfTextAlignment.Center; //set page number alignment formatFooterText.Alignment = PdfTextAlignment.Right; //set footer text alignment
//Add Page Number PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush); PdfPageCountField pageCount = new PdfPageCountField(font, brush);
PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, pageCount) { Bounds = new RectangleF(0, 0, bounds.Width, bounds.Height), StringFormat = format };
footer.Graphics.DrawString("HTML to PDF Conversion", font, brush, bounds, formatFooterText); compositeField.Draw(footer.Graphics, new PointF(0, bounds.Height / 2)); |
Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/ConsoleApp190597546
kindly try the above solution and let us know the result.
Regards,
Arumugam M