I can add extra margin on left side of the header text and footer text by setting bounds when creating it but cannot set a margin on right side, How can I achieve this.
My code for header
string headeralignment = headerProperties.Alignment.ToLower();
var headerMargins = headerMargin;
var pdfPageOrientations = pdfPageOrientation;
var pdfPageSizes = pdfPageSize;
var manifests = manifest;
var width = pdfPageSize.Width;
var height = pdfPageSize.Height;
var headerMarginLeft = headerMargin != null ? headerMargin.Left : 0;
var headerMarginRight = headerMargin != null ? headerMargin.Right : 0;
var headerMarginHeight = headerMargin != null ? headerMargin.Height : 0;
PdfStringFormat formatHeaderText = new PdfStringFormat();
// This code is used to set the text alignment to right.
RectangleF bounds = new RectangleF();
if (pdfPageOrientation.ToString() == "Portrait")
{
bounds = new RectangleF( headerMarginLeft , 0, width - (headerMarginRight + headerMarginLeft), headerMarginHeight);
}
else
{
bounds = new RectangleF( headerMarginLeft , 0, height - (headerMarginRight + headerMarginLeft), headerMarginHeight);
}
//Create a new page template and assigning the bounds
PdfPageTemplateElement header = new PdfPageTemplateElement(bounds);
formatHeaderText.Alignment = PdfTextAlignment.Right;
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 14);
PdfBrush brush = new PdfSolidBrush(Color.Black);
string headerText = "Syncfusion HTML to PDF Converter";
header.Graphics.DrawString(headerText, font, brush, bounds, formatHeaderText);
Hi Duminda,
Thank you for reaching out to Syncfusion support.
We have checked your issue on our end. Upon further analysis, we can overcome the reported issue by setting the margin left and right value through PageSettings in document. Additionally, We need to adjust the bounds width value based on the right and left margin value. This ensures the right margin works correctly and the header is properly rendered in the output pdf document. You can find the code snippet below
Code snippet:
PdfDocument document = new PdfDocument(); // Set the margins, page orientation, and page size. var pdfPageSize = PdfPageSize.A4; var width = pdfPageSize.Width; var height = pdfPageSize.Height; var marginLeft = 40; var marginRight = 150; document.PageSettings.Margins.Left = marginLeft; document.PageSettings.Margins.Right = marginRight; document.PageSettings.Orientation = PdfPageOrientation.Portrait; var pdfPageOrientation = "Portrait"; var headerMarginHeight = 50; // Set the text alignment and bounds for the header. PdfStringFormat formatHeaderText = new PdfStringFormat(); RectangleF bounds; // Determine page orientation and set header bounds. if (pdfPageOrientation == "Portrait") { bounds = new RectangleF(0, 0, width - ( marginLeft + marginRight), headerMarginHeight); //adjust the width based on the margin left and margin right. } else { bounds = new RectangleF(0, 0, width - (marginLeft + marginRight), headerMarginHeight); //adjust the width based on the margin left and margin right. } // Create a new page template element for the header using the bounds. PdfPageTemplateElement header = new PdfPageTemplateElement(bounds); // Set text alignment for the header text. formatHeaderText.Alignment = PdfTextAlignment.Right; // Create a font and brush for the header text. PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 14); PdfBrush brush = new PdfSolidBrush(Color.Black); // Define the header text. string headerText = "Syncfusion HTML to PDF Converter"; // Draw the header text on the header template. header.Graphics.DrawString(headerText, font, brush, bounds, formatHeaderText); // Assign the header template to the top of each page in the PDF document. document.Template.Top = header; // Create a new page in the document. PdfPage page = document.Pages.Add(); MemoryStream stream = new MemoryStream(); document.Save(stream); File.WriteAllBytes("../../../Output.pdf",stream.ToArray()); // Close the document. document.Close(true); |
However, we have attached the sample and output document below for your reference
Sample: HTML-to-PDF-Header-and-Footer1288148220
Output: Output-125415886
Please try the above solution and let us know the result. Kindly get back to us if you need any further assistance.
Regards,
Arumugam M
Hi Arumugam M,
I need to apply margins for header and footer only not for the whole document, because I need to attach the header and footer separately to my main document when converting the html to PDF with blink converter settings. Is this feasible.
Regards,
Duminda
Hi Duminda,
We have checked your issue on our end. Upon further analysis, we found that the bounds calculation is causing the headerMarginRight to not apply correctly when rendering the header string in the PDF document. To resolve this, the width in the bounds needs to be adjusted in accordance with the headerMarginRight value. You can find the code snippet below
Code snippet:
// Set the margins, page orientation, and page size. var pdfPageOrientation = "Portrait"; var pdfPageSize = PdfPageSize.A4; var width = pdfPageSize.Width; var height = pdfPageSize.Height; var headerMarginLeft = 0; var headerMarginRight = 50; var headerMarginHeight = 50; // Set the text alignment and bounds for the header. PdfStringFormat formatHeaderText = new PdfStringFormat(); RectangleF bounds; // Determine page orientation and set header bounds. if (pdfPageOrientation == "Portrait") { bounds = new RectangleF(headerMarginLeft, 0, width - (headerMarginRight), headerMarginHeight); else { bounds = new RectangleF(headerMarginLeft, 0, width - (headerMarginRight), headerMarginHeight); } // Create a new page template element for the header using the bounds. PdfPageTemplateElement header = new PdfPageTemplateElement(new RectangleF(0, 0, width, headerMarginHeight)); // Set text alignment for the header text. formatHeaderText.Alignment = PdfTextAlignment.Right; // Create a font and brush for the header text. PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 14); PdfBrush brush = new PdfSolidBrush(Color.Black); // Define the header text. string headerText = "Syncfusion HTML to PDF Converter"; // Draw the header text on the header template. header.Graphics.DrawString(headerText, font, brush, bounds, formatHeaderText); HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(); BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings(); blinkConverterSettings.PdfHeader = header; blinkConverterSettings.Orientation = PdfPageOrientation.Portrait; blinkConverterSettings.ViewPortSize = new Size(1024, 0); blinkConverterSettings.Margin.All = 0; htmlConverter.ConverterSettings = blinkConverterSettings; PdfDocument document = htmlConverter.Convert("https://www.google.com/"); MemoryStream stream = new MemoryStream(); document.Save(stream); File.WriteAllBytes("../../../Output.pdf", stream.ToArray()); // Close the document. document.Close(true); |
However, we have attached the Sample and output document below for your reference.
Sample: HTML-to-PDF-Header-and-Footer1288148220649960988
Output: Output205378556
Please try the above solution and let us know the result. Kindly get back to us if you need any further assistance.
Regards,
Arumugam M
Hi Arumugam M,
Still It's not the solution because aligning header text to right makes part of the text disappear. Converted document using blink converter settings is attached herewith.
Regards,
Duminda
Tested Code :
//setting the header of file converted using Blink Converter
finalDoc.Template.Top = !string.IsNullOrEmpty(header) ?
HeaderHTMLtoPDFSyncfusion(header, pageSetup.PageMargin.HeaderMargin, pdfPageOrientation, pdfpageSize, manifest) :
HeaderTexttoPDFSyncfusion(pageSetup.HeaderText, pageSetup.PageMargin.HeaderMargin, pdfPageOrientation, pdfpageSize, manifest);
// Function where the Issue exist
private static PdfPageTemplateElement HeaderTexttoPDFSyncfusion(HeaderFooterText headerProperties, HeaderFooterMargin headerMargin, PdfPageOrientation pdfPageOrientation, SizeF pdfPageSize, Manifest manifest)
#pragma warning restore RCS0056 // A line is too long
{
try
{
BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
blinkConverterSettings.CommandLineArguments.Add("--no-sandbox");
blinkConverterSettings.CommandLineArguments.Add("--disable-setuid-sandbox");
blinkConverterSettings.PdfPageSize = pdfPageSize;
blinkConverterSettings.Orientation = pdfPageOrientation;
blinkConverterSettings.ViewPortSize = new Syncfusion.Drawing.Size(1024, 0);
blinkConverterSettings.EnableJavaScript = manifest.EnableJavaScript;
float width = pdfPageSize.Width;
var headerMarginLeft = headerMargin != null ? headerMargin.Left : 0;
var headerMarginRight = headerMargin != null ? headerMargin.Right : 0;
var headerMarginHeight = headerMargin != null ? headerMargin.Height : 0;
RectangleF bounds = new RectangleF();
if (pdfPageOrientation.ToString() == "Portrait")
{
bounds = new RectangleF(headerMarginLeft, 0, pdfPageSize.Width - headerMarginRight , headerMarginHeight);
}
else
{
bounds = new RectangleF(headerMarginLeft, 0, pdfPageSize.Height - headerMarginRight , headerMarginHeight);
}
PdfPageTemplateElement header = new PdfPageTemplateElement(bounds);
PdfSolidBrush brush = new PdfSolidBrush(Syncfusion.Drawing.Color.Black);
string headerFont = headerProperties.Font.ToLower();
int headerFontSize;
if (!string.IsNullOrEmpty(headerProperties.FontSize.ToString()))
{
headerFontSize = headerProperties.FontSize;
}
else
{
headerFontSize = 20;
}
var headerFontFamily = Path.Combine(Environment.CurrentDirectory, @"Fonts//arial-unicode-ms.ttf");
#pragma warning disable CA2000
PdfTrueTypeFont font = new PdfTrueTypeFont(headerFontFamily, manifest.Input.PageSetup.FooterText.FontSize);
#pragma warning restore CA2000
//Create a new PDF string format instance.
PdfStringFormat format = new PdfStringFormat();
//Set the text alignment.
string headeralignment = headerProperties.Alignment.ToLower();
if (headeralignment == "center")
{
format.Alignment = PdfTextAlignment.Center;
}
else if (headeralignment == "left")
{
format.Alignment = PdfTextAlignment.Left;
}
else if (headeralignment == "right")
{
format.Alignment = PdfTextAlignment.Right;
}
else
{
format.Alignment = PdfTextAlignment.Center;
}
header.Graphics.DrawString(headerProperties.Text, font, brush, bounds, format);
return header;
}
catch (Exception ex)
{
throw new CustomException(Properties.Resources.ConvertingToPdfException + ex.Message);
}
}
Hi Duminda,
We have checked your issue on our end. Upon further analysis, the issue you're experiencing is related to the calculation of the bounds value, which significantly impacts how the header is rendered in the output PDF document. We have made adjustments to the calculation of this value. Specifically, when creating the header template, you should set the width to the default page width and the height to the desired headerHeight. This will ensure the header template is generated correctly. You can then use the calculated bounds value to draw the header text into the template, which will ensure the alignment is accurate based on the specified margins. Please refer the updated code snippet below, with the changes highlighted.
Code snippet:
static PdfPageTemplateElement HeaderTexttoPDFSyncfusion() #pragma warning restore RCS0056 // A line is too long { var headerProperties = "header text"; var pdfPageSize = PdfPageSize.A4; var pdfPageOrientation = PdfPageOrientation.Portrait; BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings(); blinkConverterSettings.CommandLineArguments.Add("--no-sandbox"); blinkConverterSettings.CommandLineArguments.Add("--disable-setuid-sandbox"); blinkConverterSettings.PdfPageSize = pdfPageSize; blinkConverterSettings.Orientation = pdfPageOrientation; blinkConverterSettings.ViewPortSize = new Syncfusion.Drawing.Size(1024, 0); blinkConverterSettings.EnableJavaScript = true; float width = pdfPageSize.Width; var headerMarginLeft = 10; var headerMarginRight = 50; var headerMarginHeight = 50; RectangleF bounds = new RectangleF(); if (pdfPageOrientation.ToString() == "Portrait") { bounds = new RectangleF(headerMarginLeft, 0, pdfPageSize.Width - (headerMarginRight + headerMarginLeft), headerMarginHeight); } else { bounds = new RectangleF(headerMarginLeft, 0, pdfPageSize.Width - (headerMarginRight + headerMarginLeft), headerMarginHeight); } PdfPageTemplateElement header = new PdfPageTemplateElement(new RectangleF(0, 0, width, headerMarginHeight)); PdfSolidBrush brush = new PdfSolidBrush(Syncfusion.Drawing.Color.Black); string headerFont = headerProperties.ToLower(); int headerFontSize; if (true) { headerFontSize = 20; } else { headerFontSize = 20; } var headerFontFamily = Path.Combine(Environment.CurrentDirectory, @"Fonts//arial.ttf"); #pragma warning disable CA2000 PdfTrueTypeFont font = new PdfTrueTypeFont(headerFontFamily, 10); #pragma warning restore CA2000 //Create a new PDF string format instance. PdfStringFormat format = new PdfStringFormat(); //Set the text alignment. string headeralignment = "right"; if (headeralignment == "center") { format.Alignment = PdfTextAlignment.Center; } else if (headeralignment == "left") { format.Alignment = PdfTextAlignment.Left; } else if (headeralignment == "right") { format.Alignment = PdfTextAlignment.Right; } else { format.Alignment = PdfTextAlignment.Center; } header.Graphics.DrawString(headerProperties, font, brush, bounds, format); return header; } |
However, we have attached the sample and output document below for your reference.
Sample: HTML-to-PDF-Header-1485700982
Output: Output-1803072841
Please try the above sample and let us know the result. Kindly get back to us if you need any further assistance.
Regards,
Arumugam M
Hi Arumugam M,
Above solution worked for me. Thanks for your immense support.
Regards,
Duminda
Hi Duminda,
Thanks for the update. We are glad to hear that the reported issue has been resolved on your end. Kindly get back to us if you need further assistance.
Regards,
Arumugam M