Asp.net 生成特定宽度/高度像素大小的Winnovative PDF

Asp.net 生成特定宽度/高度像素大小的Winnovative PDF,asp.net,pdf,pdf-generation,winnovative,Asp.net,Pdf,Pdf Generation,Winnovative,我希望使用Winnovative PDF转换器从HTML生成PDF文档 我希望将PDF转换为842 x 595像素 我试过做: pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A5 但这不合适。所以我试着: pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.Custom pdfConverter.PdfDocumentOptions.Custom

我希望使用Winnovative PDF转换器从HTML生成PDF文档

我希望将PDF转换为842 x 595像素

我试过做:

pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A5
但这不合适。所以我试着:

    pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.Custom
    pdfConverter.PdfDocumentOptions.CustomPdfPageSize = New SizeF(842, 595)
然而,这也不正确,因为我认为尺寸是以不同于像素的格式测量的


如何生成一个842 x 595像素的PDF,使其与我的HTML内容匹配?

如果将PdfConverter.PageWidth和PdfConverter.PageHeight属性设置为842和595,则会导致web内容填满整个PDF页面区域

还有一个FitWidth属性,如果您不想缩小web内容以适应可用页面区域,则可以将该属性设置为False。这将意味着,如果页面太大,您的内容将溢出页面

关于Winnovative PDF的另一个问题是,处理高分辨率图像并不容易,因此根据您的PDF需求(和输入),您可能很难获得高质量的图像-也许这只是我的经验

说到图像,Winnovative PDF似乎将输入的html创建为一个图像,然后以屏幕分辨率(72 dpi)添加到PDF中,这甚至会使简单的文本看起来质量很低


如果你想自己动手处理html布局,你可以随时使用其他产品将内容直接转换为PDF格式,这样可以在更高分辨率的图像上提供更大的灵活性。我真的取决于您的总体需求,但是如果您将PdfConverter.PageWidth和PdfConverter.PageHeight属性设置为842和595,那么您的web内容将填充整个PDF页面区域

还有一个FitWidth属性,如果您不想缩小web内容以适应可用页面区域,则可以将该属性设置为False。这将意味着,如果页面太大,您的内容将溢出页面

关于Winnovative PDF的另一个问题是,处理高分辨率图像并不容易,因此根据您的PDF需求(和输入),您可能很难获得高质量的图像-也许这只是我的经验

说到图像,Winnovative PDF似乎将输入的html创建为一个图像,然后以屏幕分辨率(72 dpi)添加到PDF中,这甚至会使简单的文本看起来质量很低


如果你想自己动手处理html布局,你可以随时使用其他产品将内容直接转换为PDF格式,这样可以在更高分辨率的图像上提供更大的灵活性。我真的取决于您的总体需求,尽管在将HTML内容呈现为PDF时涉及到两件事。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(595842)

您可以在中找到所有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();
}

将HTML内容呈现为PDF时涉及两件事。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(595842)

您可以在中找到所有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();
}