2011-09-26 83 views
0

我期待使用Winnovative PDF轉換生成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內容相匹配?

回答

1

如果您將PdfConverter.PageWidth和PdfConverter.PageHeight屬性設置爲842和595,那麼應該會導致您的Web內容填充整個PDF頁面區域。

還有一個FitWidth屬性,如果您不希望您的網頁內容縮小到適合可用頁面區域,可將其設置爲False。這意味着如果內容太大,您的內容會溢出。

關於Winnovative PDF的另一個觀點是,要使用高分辨率圖像並不容易,所以根據您的PDF需求(和輸入),您可能很難獲得高質量的圖像 - 也許這僅僅是我的經驗雖然。

說到圖像,看起來Winnovative PDF需要輸入html並創建一個單一的圖像,然後以屏幕​​分辨率(72 dpi)將其添加到PDF中,這甚至可以使簡單的文本看起來質量較差。

如果您想自己處理html佈局,您可以使用其他產品(如ITextSharp)直接將您的內容轉換爲PDF,這樣可以在更高分辨率的圖像上提供更多的靈活性。雖然

2

將HTML內容呈現爲PDF時涉及2件事情。 PDF頁面大小和轉換器內部HTML查看器寬度。當啓用FitWidth時,可以縮小HTML內容以適應PDF頁面寬度。

PDF頁面大小以點(1點爲1/72英寸)表示,內部HTML查看器窗口大小以像素(i像素爲1/96英寸)表示。

PDF頁面A4 potrait尺寸爲595 x 842點,對應於595點的HTML查看器寬度爲595 x 96/72 = 793像素。

所以轉換器的設置是:

pdfConverter.HtmlViewerWidth = 793 pdfConverter.PdfDocumentOptions.PdfPageSize =新PdfPageSize(595842)

你可以找到所有的HTML縮放和配件選項的說明以及Control HTML Scaling in PDF Page demo中的示例代碼。以下是該演示的相關代碼的副本:

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(); 
} 
相關問題