3

我在使用 Winnovative Html to Pdf Converter 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 产生的所有页面都包含正确的页码,但附加文档上的页码似乎没有重新计算

示例:FirstUrl 结果为 3 页,第二个为 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 中的页面在它们上获得页码,并且在附加文档的页面上无处可见。

谢谢你的帮助。

4

2 回答 2

0

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

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

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

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

pdfDocument = pdfConverter.GetPdfDocumentObjectFromUrl(firstUrl);

但我没有附加另一个 pdf,而是在 HtmlToPdfElement 中获得了第二个 url,如下所示:

 HtmlToPdfElement pdfBody = new HtmlToPdfElement(secoundUrl);

最后使用新页面将其添加到 pdf 文档中。

PdfPage newPage = pdfDocument.Pages.AddNewPage();
newPage.AddElement(pdfBody);
于 2013-10-17T16:53:51.793 回答
-1

在最新版本的 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);
    }
}
于 2014-08-10T07:51:57.593 回答