Search code examples
c#asp.netwebformshtml-to-pdfevopdf

EvoPDF page margins set into the HTML


I'm using the EvoPDF with which I save HTML to PDF file. The HTML contains long text (can contains lists, tables, etc.). I want to add pages margins, but I don't want to use the pdfConverter.PdfDocumentOptions.{Bottom/Top}Margin - properties, I want to set pages margins into the HTML (something like in Microsoft Word, where I set the page size and margins and text auto move to next page, which has previously set margins). I previewed their Help page, but I couldn't find info about that.

My Convert code is:

        EvoPdf.HtmlToPdf.PdfConverter pdfConverter = new EvoPdf.HtmlToPdf.PdfConverter();
        pdfConverter.LicenseKey = System.Configuration.ConfigurationManager.AppSettings["EvoHtmlToPdfLicence"];
        pdfConverter.PdfDocumentOptions.PdfPageSize = EvoPdf.HtmlToPdf.PdfPageSize.Letter;
        pdfConverter.PdfDocumentOptions.PdfPageOrientation = EvoPdf.HtmlToPdf.PdfPageOrientation.Portrait;
        byte[] pdf = pdfConverter.GetPdfBytesFromHtmlString(htmlText);

Thank you!


Solution

  • The Margin properties are global thus header, footer, body are affected by this. If you want to effect the rendered HTML solely, you can use the following settings:

    Documentation

    • HTML Content Destination in PDF. The HTML content destination is given by the X and Y coordinates where to start rendering in first PDF page and by the destination rectangle width and height. All the values are expressed in points. 1 point is 1/72 inches. If you don't set any destination rectangle then by default the converter will start rendering in the top left corner of the first page, will try to use the entire PDF page width for rendering and will auto determine the destination rectangle height such that the entire HTML content is visible. The properties you can set in your code to control the HTML content destination in PDF are X, Y, Width and Height. [...]

    • HTML Content Top and Bottom Spacing. Using these options you can set a top and a bottom padding for the HTML content. This can be useful for example when you want to introduce a spacing between the PDF page header or footer and the main content. The properties you can set in your code to control the top and bottom spacing are TopSpacing and BottomSpacing. [...]

    Code example:

    // Category: HTML Content Destination and Spacing Options
    // Set HTML content destination in PDF page
    if (xLocationTextBox.Text.Length > 0)
        htmlToPdfConverter.PdfDocumentOptions.X = float.Parse(xLocationTextBox.Text);
    if (yLocationTextBox.Text.Length > 0)
        htmlToPdfConverter.PdfDocumentOptions.Y = float.Parse(yLocationTextBox.Text);
    if (contentWidthTextBox.Text.Length > 0)
        htmlToPdfConverter.PdfDocumentOptions.Width = float.Parse(contentWidthTextBox.Text);
    if (contentHeightTextBox.Text.Length > 0)
        htmlToPdfConverter.PdfDocumentOptions.Height = float.Parse(contentHeightTextBox.Text);
    
    // Set HTML content top and bottom spacing or leave them not set to have no spacing for the HTML content
    htmlToPdfConverter.PdfDocumentOptions.TopSpacing = float.Parse(topSpacingTextBox.Text);
    htmlToPdfConverter.PdfDocumentOptions.BottomSpacing = float.Parse(bottomSpacingTextBox.Text);