0

HiQPdf を使用して、html ページのリストを 1 つの pdf ドキュメントに変換して結合しています。これが私がこれをやっている方法です:

public class HtmlToPdfEditor
{
    private string _firstPage;
    private string _secondPage;
    //private const string _HiQPdfSerialNumber = "";
    private PdfDocument _document;
    public HtmlToPdfEditor(string firstPage, string secondPage)
    {
        _firstPage = firstPage;
        _secondPage=secondPage;
    }

    public void ConvertAll(string outputPath) 
    {
         HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
        _document = new PdfDocument();
        //_document.SerialNumber = _HiQPdfSerialNumber;
        string firstPageDoc = GetDocument(_firstPage, "firstPage.pdf");
        string secondPageDoc = GetDocument(_secondPage, "secondtPage.pdf");

        this.JoinDocument(PdfFromFile(firstPageDoc));
        this.JoinDocument(PdfFromFile(secondPageDoc));
        _document.WriteToFile(outputPath);
        _document.Close();
        _document = null;
    }

    private PdfDocument PdfFromFile(string path)
    {
        return PdfDocument.FromFile(path);
    }


    private int JoinDocument(PdfDocument document)
    {
        var nbPages = _document.Pages.Count;
        _document.AddDocument(document);
        document.Close();
        return nbPages;
    }

    private string GetDocument(string content, string outputFile)
    {
       var baseUrl = "";
       var htmlToPdfConverter = GetPdfExporter();
       htmlToPdfConverter.ConvertHtmlToFile(content, baseUrl, outputFile);
       return outputFile;
    }

    public HtmlToPdf GetPdfExporter()
    {
        HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
        //htmlToPdfConverter.SerialNumber = _HiQPdfSerialNumber;
        htmlToPdfConverter.Document.PageSize = PdfPageSize.A4;
        htmlToPdfConverter.Document.PageOrientation = PdfPageOrientation.Portrait;
        htmlToPdfConverter.Document.Margins = new PdfMargins(2);
        htmlToPdfConverter.HtmlLoadedTimeout = 60;
        htmlToPdfConverter.TriggerMode = ConversionTriggerMode.WaitTime; //Time to load the html
        htmlToPdfConverter.WaitBeforeConvert = 1;
        return htmlToPdfConverter;

    }
}

ここでの問題は、結果のドキュメントでは、html から変換されたページが空のページとして表示され、Google Chrome のみがそれらを正しく表示することです。Firefox では、これらのページは読み込み状態で無期限に継続します。

ファイルに保存してから結合するのではなく、Html を PdfDocument に変換すると注意してください。結果のドキュメントは完全に読めますが、残念ながらこの方法は使用できません。

どんな助けでも大歓迎です!! どうも!!

4

2 に答える 2

1

はい、そのとおりです。メイン ドキュメントに追加した PDF ドキュメントは、メイン ドキュメントを閉じるまで開いたままにしておく必要があります。

マージする PDF ドキュメントが HTML から作成されている場合、多数の HTML を PDF に変換する例のアプローチに従って、HTML ドキュメントを PDF にマージする簡単な方法が実際にあります。

// create an empty PDF document
PdfDocument document = new PdfDocument();

// add a page to document
PdfPage page1 = document.AddPage(PdfPageSize.A4, new PdfDocumentMargins(5), 
        PdfPageOrientation.Portrait);

try
{
    // set the document header and footer before 
    // adding any objects to document
    SetHeader(document);
    SetFooter(document);

    // layout the HTML from URL 1
    PdfHtml html1 = new PdfHtml(textBoxUrl1.Text);
    PdfLayoutInfo html1LayoutInfo = page1.Layout(html1);

    // determine the PDF page where to add URL 2
    PdfPage page2 = null;
    System.Drawing.PointF location2 = System.Drawing.PointF.Empty;
    if (checkBoxNewPage.Checked)
    {
        // URL 2 is laid out on a new page with the selected orientation
        page2 = document.AddPage(PdfPageSize.A4, new PdfDocumentMargins(5), 
            GetSelectedPageOrientation());
        location2 = System.Drawing.PointF.Empty;
    }
    else
    {
        // URL 2 is laid out immediately after URL 1 and html1LayoutInfo
        // gives the location where the URL 1 layout finished
        page2 = document.Pages[html1LayoutInfo.LastPageIndex];
        location2 = new System.Drawing.PointF(html1LayoutInfo.LastPageRectangle.X, 
            html1LayoutInfo.LastPageRectangle.Bottom);
    }

    // layout the HTML from URL 2
    PdfHtml html2 = new PdfHtml(location2.X, location2.Y, textBoxUrl2.Text);
    page2.Layout(html2);

    // write the PDF document to a memory buffer
    byte[] pdfBuffer = document.WriteToMemory();

    // inform the browser about the binary data format
    HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");

    // let the browser know how to open the PDF document
    HttpContext.Current.Response.AddHeader("Content-Disposition", 
                String.Format("attachment; filename=LayoutMultipleHtml.pdf;
                        size={0}",
                pdfBuffer.Length.ToString()));

    // write the PDF buffer to HTTP response
    HttpContext.Current.Response.BinaryWrite(pdfBuffer);

    // call End() method of HTTP response 
    // to stop ASP.NET page processing
    HttpContext.Current.Response.End();
}
finally
{
    document.Close();
}
于 2016-02-16T09:23:43.833 に答える