0

Winnovative PDF Converter を使用して HTML から PDF ドキュメントを生成しようとしています。

PDF を正確に 842 x 595 ピクセルに変換したい

私はやってみました:

pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A5

しかし、これは正しく適合しません。だから私は試しました:

    pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.Custom
    pdfConverter.PdfDocumentOptions.CustomPdfPageSize = New SizeF(842, 595)

ただし、サイズがピクセルとは異なる形式で測定されていると思うので、これも正しく機能しませんか?

HTML コンテンツと一致するように正確に 842 x 595 ピクセルの PDF を生成するにはどうすればよいですか?

4

2 に答える 2

2

HTML コンテンツを PDF にレンダリングする際には、2 つのことが関係しています。PDF ページ サイズとコンバーターの内部 HTML ビューアーの幅。FitWidth を有効にすると、HTML コンテンツを縮小して PDF ページの幅に合わせることができます。

PDF ページのサイズはポイント (1 ポイントは 1/72 インチ) で表され、内部 HTML ビューア ウィンドウのサイズはピクセル (i ピクセルは 1/96 インチ) で表されます。

PDF ページの A4 ポートレート サイズは 595 x 842 ポイントで、595 ポイントに対応する HTML ビューアーの幅は 595 x 96 / 72 = 793 ピクセルです。

したがって、コンバーターの設定は次のとおりです。

pdfConverter.HtmlViewerWidth = 793 pdfConverter.PdfDocumentOptions.PdfPageSize = 新しい PdfPageSize(595,842)

すべての HTML スケーリングおよびフィッティング オプションとサンプル コードの説明は、PDF ページでの HTML スケーリングの制御デモ にあります。以下は、そのデモからの関連コードのコピーです。

protected void convertToPdfButton_Click(object sender, EventArgs e)
{
    // Create a HTML to PDF converter object with default settings
    HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();

    // Set license key received after purchase to use the converter in licensed mode
    // Leave it not set to use the converter in demo mode
    htmlToPdfConverter.LicenseKey = "fvDh8eDx4fHg4P/h8eLg/+Dj/+jo6Og=";

    // Html Viewer Options

    // Set HTML Viewer width in pixels which is the equivalent in converter of the browser window width
    // This is a preferred width of the browser but the actual HTML content width can be larger in case the HTML page 
    // cannot be entirely displayed in the given viewer width
    // This property gives the size of the HTML content which can be further scaled to fit the PDF page based on selected options
    // The HTML content size is in pixels and the PDF page size is in points (1 point = 1/72 inches)
    // The converter is using a 96 DPI resolution to transform pixels to points with the following formula: Points = Pixels/96 * 72            
    htmlToPdfConverter.HtmlViewerWidth = int.Parse(htmlViewerWidthTextBox.Text);

    // Set HTML viewer height in pixels to convert the top part of a HTML page 
    // Leave it not set to convert the entire HTML
    if (htmlViewerHeightTextBox.Text.Length > 0)
        htmlToPdfConverter.HtmlViewerHeight = int.Parse(htmlViewerHeightTextBox.Text);

    // Set the HTML content clipping option to force the HTML content width to be exactly HtmlViewerWidth pixels
    // If this option is false then the actual HTML content width can be larger than HtmlViewerWidth pixels in case the HTML page 
    // cannot be entirely displayed in the given viewer width
    // By default this option is false and the HTML content is not clipped
    htmlToPdfConverter.ClipHtmlView = clipContentCheckBox.Checked;

    // PDF Page Options

    // Set PDF page size which can be a predefined size like A4 or a custom size in points 
    // Leave it not set to have a default A4 PDF page
    htmlToPdfConverter.PdfDocumentOptions.PdfPageSize = SelectedPdfPageSize();

    // Set PDF page orientation to Portrait or Landscape
    // Leave it not set to have a default Portrait orientation for PDF page
    htmlToPdfConverter.PdfDocumentOptions.PdfPageOrientation = SelectedPdfPageOrientation();

    // Set PDF page margins in points or leave them not set to have a PDF page without margins
    htmlToPdfConverter.PdfDocumentOptions.LeftMargin = float.Parse(leftMarginTextBox.Text);
    htmlToPdfConverter.PdfDocumentOptions.RightMargin = float.Parse(rightMarginTextBox.Text);
    htmlToPdfConverter.PdfDocumentOptions.TopMargin = float.Parse(topMarginTextBox.Text);
    htmlToPdfConverter.PdfDocumentOptions.BottomMargin = float.Parse(bottomMarginTextBox.Text);

    // 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);

    // Scaling Options

    // Use this option to fit the HTML content width in PDF page width
    // By default this property is true and the HTML content can be resized to fit the PDF page width
    htmlToPdfConverter.PdfDocumentOptions.FitWidth = fitWidthCheckBox.Checked;

    // Use this option to enable the HTML content stretching when its width is smaller than PDF page width
    // This property has effect only when FitWidth option is true
    // By default this property is false and the HTML content is not stretched
    htmlToPdfConverter.PdfDocumentOptions.StretchToFit = stretchCheckBox.Checked;

    // Use this option to automatically dimension the PDF page to display the HTML content unscaled
    // This property has effect only when the FitWidth property is false
    // By default this property is true and the PDF page is automatically dimensioned when FitWidth is false
    htmlToPdfConverter.PdfDocumentOptions.AutoSizePdfPage = autoSizeCheckBox.Checked;

    // Use this option to fit the HTML content height in PDF page height
    // If both FitWidth and FitHeight are true then the HTML content will resized if necessary to fit both width and height 
    // preserving the aspect ratio at the same time
    // By default this property is false and the HTML content is not resized to fit the PDF page height
    htmlToPdfConverter.PdfDocumentOptions.FitHeight = fitHeightCheckBox.Checked;

    // Use this option to render the whole HTML content into a single PDF page
    // The PDF page size is limited to 14400 points
    // By default this property is false
    htmlToPdfConverter.PdfDocumentOptions.SinglePage = singlePageCheckBox.Checked;

    string url = urlTextBox.Text;

    // Convert the HTML page to a PDF document using the scaling options
    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=HTML_Content_Scaling.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();
}
于 2014-08-26T08:41:44.573 に答える
1

PdfConverter.PageWidth および PdfConverter.PageHeight 属性を 842 および 595 に設定すると、Web コンテンツが PDF ページ領域全体を占めるようになります。

使用可能なページ領域に合わせて Web コンテンツを縮小しない場合は、False に設定できる FitWidth プロパティもあります。ただし、コンテンツが大きすぎると、コンテンツがページからはみ出すことになります。

Winnovative PDF について指摘すべきもう 1 つの点は、高解像度の画像を扱うのは簡単ではないということです。そのため、PDF のニーズ (および入力) によっては、高品質の画像を取得するのに苦労する可能性があります。

画像といえば、Winnovative PDF は入力 html を受け取り、そのすべての画像を 1 つ作成し、それを画面解像度 (72 dpi) で PDF に追加するようです。これにより、単純なテキストが低品質に見えることさえあります。

HTML レイアウトを自分で処理したい場合は、ITextSharpなどの別の製品を使用してコンテンツを直接 PDF に変換することができます。これにより、より高い解像度の画像に対してより柔軟に対応できます。私は本当にあなたの全体的なニーズに依存します

于 2011-10-12T14:32:55.970 に答える