创新型Html到pdf转换器的页码在附加文档上不正确

本文关键字:附加文档 不正确 Html pdf 转换器 创新型 | 更新日期: 2023-09-27 18:04:09

我有一个问题,使用Winnovative Html到Pdf转换器v8.0.0.0,我的Pdf页码不正确。这是我所做的。我在转换器上设置了一个页脚

pdfConverter.PdfFooterOptions.AddTextArea(new TextArea(500, 0, 50, "&p; / &P;", new Font(new FontFamily("Arial"), 8)));

然后我从url

创建一个文档
pdfDocument = pdfConverter.GetPdfDocumentObjectFromUrl(FirstUrl);

然后从url中添加一个文档

pdfDocument.AppendDocument(pdfConverter.GetPdfDocumentObjectFromUrl(SecondUrl));

由第一个url生成的所有页面都包含正确的页码,但附加文档的页码似乎没有被重新计算

的例子:第一url的结果是3页,第二url的结果是2页。页码

1/5
2/5
3/5
1/2
2/2

我希望它们是这样的:

1/5
2/5
3/5
4/5
5/5

我也试着不把页脚添加到转换器中,而是在转换后的pdfDocument中,像这样:

PdfFont pdfFont = pdfDocument.Fonts.Add(new Font(new FontFamily("Arial"), 8));
  string strPageNum = "&p; / &P;";
  TextElement footerPageNumber = new TextElement(500, 0, 50, strPageNum, pdfFont);
  pdfDocument.FooterTemplate.AddElement(footerPageNumber);

但是在这种情况下。只有来自第一个Url的页面才能获得页码,并且在附加文档的页面上无处可见。

谢谢你的帮助。

创新型Html到pdf转换器的页码在附加文档上不正确

在另一个项目遇到同样的问题后。我终于找到了从许多Url创建pdf的正确方法。

我仍然像这样设置文档的页脚:

pdfConverter.PdfFooterOptions.AddTextArea(new TextArea(500, 0, 50, "&p; / &P;", new Font(new FontFamily("Arial"), 8)));

然后我从url创建一个文档,就像以前一样:

pdfDocument = pdfConverter.GetPdfDocumentObjectFromUrl(firstUrl);

但不是附加另一个pdf,而是在htmltopdelement中获得第二个url,如下所示:

 HtmlToPdfElement pdfBody = new HtmlToPdfElement(secoundUrl);

,最后添加到PDF文档使用一个新的页面。

PdfPage newPage = pdfDocument.Pages.AddNewPage();
newPage.AddElement(pdfBody);

在最新版本的Winovative HTML到PDF转换器中,您现在可以在外部PDF文档中添加页眉和页脚,附加或添加到转换结果中。你可以在外部PDF文档中添加页眉和页脚演示

中找到一个完整的c#源代码示例。

下面是从演示程序中复制的相关代码:

protected void convertToPdfButton_Click(object sender, EventArgs e)
{
    // Create a HTML to PDF converter object with default settings
    HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
    // Set the PDF file to be inserted before conversion result
    string pdfFileBefore = Server.MapPath("~/DemoAppFiles/Input/PDF_Files/Merge_Before_Conversion.pdf");
    htmlToPdfConverter.PdfDocumentOptions.AddStartDocument(pdfFileBefore, addHeaderFooterInInsertedPdfCheckBox.Checked,
                        showHeaderInFirstPageCheckBox.Checked, showFooterInFirstPageCheckBox.Checked);
    // Set the PDF file to be added after conversion result
    string pdfFileAfter = Server.MapPath("~/DemoAppFiles/Input/PDF_Files/Merge_After_Conversion.pdf");
    htmlToPdfConverter.PdfDocumentOptions.AddEndDocument(pdfFileAfter, addHeaderFooterInAppendedPdfCheckBox.Checked, true, true);
    // Enable header in the generated PDF document
    htmlToPdfConverter.PdfDocumentOptions.ShowHeader = true;
    // Draw header elements
    if (htmlToPdfConverter.PdfDocumentOptions.ShowHeader)
        DrawHeader(htmlToPdfConverter, true);
    // Enable footer in the generated PDF document
    htmlToPdfConverter.PdfDocumentOptions.ShowFooter = true;
    // Draw footer elements
    if (htmlToPdfConverter.PdfDocumentOptions.ShowFooter)
        DrawFooter(htmlToPdfConverter, true, true);
    string url = urlTextBox.Text;
    // Convert the HTML page to a PDF document and add the external PDF documents
    byte[] outPdfBuffer = htmlToPdfConverter.ConvertUrl(url);
    // Send the PDF as response to browser
    // Set response content type
    Response.AddHeader("Content-Type", "application/pdf");
    // Instruct the browser to open the PDF file as an attachment or inline
    Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Header_Footer_in_External_PDF.pdf; size={0}", outPdfBuffer.Length.ToString()));
    // Write the PDF document buffer to HTTP response
    Response.BinaryWrite(outPdfBuffer);
    // End the HTTP response and stop the current page processing
    Response.End();
}
/// <summary>
/// Draw the header elements
/// </summary>
/// <param name="htmlToPdfConverter">The HTML to PDF Converter object</param>
/// <param name="drawHeaderLine">A flag indicating if a line should be drawn at the bottom of the header</param>
private void DrawHeader(HtmlToPdfConverter htmlToPdfConverter, bool drawHeaderLine)
{
    string headerHtmlUrl = Server.MapPath("~/DemoAppFiles/Input/HTML_Files/Header_HTML.html");
    // Set the header height in points
    htmlToPdfConverter.PdfHeaderOptions.HeaderHeight = 60;
    // Set header background color
    htmlToPdfConverter.PdfHeaderOptions.HeaderBackColor = Color.White;
    // Create a HTML element to be added in header
    HtmlToPdfElement headerHtml = new HtmlToPdfElement(headerHtmlUrl);
    // Set the HTML element to fit the container height
    headerHtml.FitHeight = true;
    // Add HTML element to header
    htmlToPdfConverter.PdfHeaderOptions.AddElement(headerHtml);
    if (drawHeaderLine)
    {
        // Calculate the header width based on PDF page size and margins
        float headerWidth = htmlToPdfConverter.PdfDocumentOptions.PdfPageSize.Width -
                    htmlToPdfConverter.PdfDocumentOptions.LeftMargin - htmlToPdfConverter.PdfDocumentOptions.RightMargin;
        // Calculate header height
        float headerHeight = htmlToPdfConverter.PdfHeaderOptions.HeaderHeight;
        // Create a line element for the bottom of the header
        LineElement headerLine = new LineElement(0, headerHeight - 1, headerWidth, headerHeight - 1);
        // Set line color
        headerLine.ForeColor = Color.Gray;
        // Add line element to the bottom of the header
        htmlToPdfConverter.PdfHeaderOptions.AddElement(headerLine);
    }
}
/// <summary>
/// Draw the footer elements
/// </summary>
/// <param name="htmlToPdfConverter">The HTML to PDF Converter object</param>
/// <param name="addPageNumbers">A flag indicating if the page numbering is present in footer</param>
/// <param name="drawFooterLine">A flag indicating if a line should be drawn at the top of the footer</param>
private void DrawFooter(HtmlToPdfConverter htmlToPdfConverter, bool addPageNumbers, bool drawFooterLine)
{
    string footerHtmlUrl = Server.MapPath("~/DemoAppFiles/Input/HTML_Files/Footer_HTML.html");
    // Set the footer height in points
    htmlToPdfConverter.PdfFooterOptions.FooterHeight = 60;
    // Set footer background color
    htmlToPdfConverter.PdfFooterOptions.FooterBackColor = Color.WhiteSmoke;
    // Create a HTML element to be added in footer
    HtmlToPdfElement footerHtml = new HtmlToPdfElement(footerHtmlUrl);
    // Set the HTML element to fit the container height
    footerHtml.FitHeight = true;
    // Add HTML element to footer
    htmlToPdfConverter.PdfFooterOptions.AddElement(footerHtml);
    // Add page numbering
    if (addPageNumbers)
    {
        // Create a text element with page numbering place holders &p; and & P;
        TextElement footerText = new TextElement(0, 30, "Page &p; of &P;  ",
            new System.Drawing.Font(new System.Drawing.FontFamily("Times New Roman"), 10, System.Drawing.GraphicsUnit.Point));
        // Align the text at the right of the footer
        footerText.TextAlign = HorizontalTextAlign.Right;
        // Set page numbering text color
        footerText.ForeColor = Color.Navy;
        // Embed the text element font in PDF
        footerText.EmbedSysFont = true;
        // Add the text element to footer
        htmlToPdfConverter.PdfFooterOptions.AddElement(footerText);
    }
    if (drawFooterLine)
    {
        // Calculate the footer width based on PDF page size and margins
        float footerWidth = htmlToPdfConverter.PdfDocumentOptions.PdfPageSize.Width -
                    htmlToPdfConverter.PdfDocumentOptions.LeftMargin - htmlToPdfConverter.PdfDocumentOptions.RightMargin;
        // Create a line element for the top of the footer
        LineElement footerLine = new LineElement(0, 0, footerWidth, 0);
        // Set line color
        footerLine.ForeColor = Color.Gray;
        // Add line element to the bottom of the footer
        htmlToPdfConverter.PdfFooterOptions.AddElement(footerLine);
    }
}