Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Fri Jan 19, 2024 3:27 pm

I am creating a pdf file from html string. Is there any way i can set the margin and page number in one shot instead of having to create different pdf files for each changes to the PDF?

NOC
 
Posts: 9
Joined: Thu Aug 03, 2017 9:04 pm

Mon Jan 22, 2024 2:11 am

Hello,

Thank you for your inquiry.
I would like to clarify that our Spire.PDF tool does support setting page margins directly when converting HTML to PDF. However, adding page numbers requires the file to be reloaded before applying the page numbering feature. Below is an example code snippet that demonstrates how to convert HTML to PDF with custom page margins and add page numbers:
Code: Select all
 static void Main(string[] args)
 {
     // Define the HTML content to convert to PDF
     string htmls = "";

     // Create a MemoryStream to hold the generated PDF data
     MemoryStream stream = new MemoryStream();

     // Set the margins for the PDF document
     PdfMargins margins = new PdfMargins(40, 40, 40, 40);

     // Set the path to the HTML converter plugins
     string pluginPath = "F:\\Libraries\\plugins-windows-x64\\plugins";
     HtmlConverter.PluginPath = pluginPath;

     // Convert the specified HTML content to PDF and write it to the MemoryStream
     HtmlConverter.Convert(htmls, stream, true, 100000, new Size(1080, 1000), margins, LoadHtmlType.SourceCode);

     // Create a new PdfDocument object and load the converted PDF from the MemoryStream
     PdfDocument doc = new PdfDocument();
     doc.LoadFromStream(stream);

     // Get the page margins from the PDF document
     PdfMargins margin = doc.PageSettings.Margins;

     // Draw page numbers on each page of the PDF document
     DrawPageNumber(doc, margin, 1, doc.Pages.Count);

     // Save the PDF document to a file named "result.pdf"
     doc.SaveToFile("result.pdf");

 }
 // Method for drawing page numbers on each page of the PDF document
 private static void DrawPageNumber(PdfDocument doc, PdfMargins margin, int startNumber, int pageCount)
 {
     // Iterate through each page in the document
     foreach (PdfPageBase page in doc.Pages)
     {
         // Set transparency for the canvas
         page.Canvas.SetTransparency(0.5f);

         // Define brush and pen styles for drawing
         PdfBrush brush = PdfBrushes.Black;
         PdfPen pen = new PdfPen(brush, 0.75f);

         // Define font style for the page number label
         PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Italic), true);

         // Define string format for aligning the page number label
         PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
         format.MeasureTrailingSpaces = true;

         // Calculate spacing and positioning for the page number label
         float space = font.Height * 0.75f;
         float x = margin.Left;
         float width = page.Canvas.ClientSize.Width - margin.Left - margin.Right;
         float y = page.Canvas.ClientSize.Height - margin.Bottom + space;

         // Draw a horizontal line below the page number label
         page.Canvas.DrawLine(pen, x, y, x + width, y);

         // Increment the y position and format the page number label
         y = y + 1;
         String numberLabel = String.Format("{0} of {1}", startNumber++, pageCount);

         // Draw the page number label on the canvas
         page.Canvas.DrawString(numberLabel, font, brush, x + width, y, format);

         // Reset the transparency for the canvas
         page.Canvas.SetTransparency(1);
     }
 }

If you have any further questions or require additional assistance, please feel free to reach out to us.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Thu Jan 25, 2024 5:00 pm

Where can i get the HtmlConverter plug-in? I already have SPire.PDF 9.10. Will this installation copy already have the HtmlConverter?
Do i need to buy separate license for this plug-in?

string pluginPath = "F:\\Libraries\\plugins-windows-x64\\plugins";
HtmlConverter.PluginPath = pluginPath;

NOC
 
Posts: 9
Joined: Thu Aug 03, 2017 9:04 pm

Fri Jan 26, 2024 1:26 am

Hello,

Thank you for your feedback.
Please note that the QT plugin needs to be downloaded separately and is not included in the Spire.PDF component. You can download the QT plugin package by following the instructions provided in this tutorial, which also provides detailed information on how to use the QT plugin.
We apologize for any inconvenience caused by the separate download requirement. However, the tutorial will guide you through the process of obtaining the QT plugin and provide comprehensive instructions on its usage.
We understand that you may have concerns about additional licensing requirements. We would like to clarify that the QT plugin does not require the purchase of a new license. It can be used in conjunction with the Spire.PDF component under the existing licensing terms.
If you have any further questions or require additional assistance, please feel free to let us know.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.PDF