2013-06-13 121 views
1

這是我在C#中的代碼,其中給出加載pdf的指令。使用SAME樣式從HTML生成PDF

public void SetPdfFormat() 
{ 
    Response.ContentType = "application/pdf"; 
    Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf"); 
    Response.AddHeader("content-disposition", "filename=Panel.pdf"); 
    Response.Cache.SetCacheability(HttpCacheability.NoCache); 
    StringWriter sw = new StringWriter(); 
    HtmlTextWriter hw = new HtmlTextWriter(sw); 
    pnlQuotation.RenderControl(hw); 
    StringReader sr = new StringReader(sw.ToString()); 
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f); 
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc); 
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream); 

    pdfDoc.Open(); 
    htmlparser.Parse(sr); 
    pdfDoc.Close(); 
    Response.Write(pdfDoc); 
    Response.End(); 
} 

但是,當我呼籲該函數pdf加載爲更多的項目,但數據表格加載與邊界單元格。我只需要左/右邊界的邊界,而不是頂部和底部。

我可以重寫和添加單獨的PDF格式的單元格?

請給我一份有例子代碼

回答

2

有一個.NET庫這裏的解決方案:http://www.winnovative-software.com/

我發現這個鏈接轉到:Convert HTML to PDF in .NET

我沒有親自使用這個庫。

這裏是winnovative軟件的示例代碼:

/// <summary> 
/// Convert the HTML code from the specified URL to a PDF document 
    and send the document to the browser 
/// </summary> 
private void ConvertURLToPDF() 
{ 
    string urlToConvert = textBoxWebPageURL.Text.Trim(); 

    // Create the PDF converter. Optionally the HTML viewer width can 
     be specified as parameter 
    // The default HTML viewer width is 1024 pixels. 
    PdfConverter pdfConverter = new PdfConverter(); 

    // set the license key - required 
    pdfConverter.LicenseKey = "R8nYyNnI2MjRxtjI29nG2drG0dHR0Q=="; 

    // set the converter options - optional 
    pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4; 
    pdfConverter.PdfDocumentOptions.PdfCompressionLevel = PdfCompressionLevel.Normal; 
    pdfConverter.PdfDocumentOptions.PdfPageOrientation = PdfPageOrientation.Portrait; 


    // set if header and footer are shown in the PDF - optional - default 
     is false 
    pdfConverter.PdfDocumentOptions.ShowHeader = cbAddHeader.Checked; 
    pdfConverter.PdfDocumentOptions.ShowFooter = cbAddFooter.Checked; 
    // set if the HTML content is resized if necessary to fit the PDF 
     page width - default is true 
    pdfConverter.PdfDocumentOptions.FitWidth = cbFitWidth.Checked; 

    // set the embedded fonts option - optional - default is false 
    pdfConverter.PdfDocumentOptions.EmbedFonts = cbEmbedFonts.Checked; 
    // set the live HTTP links option - optional - default is true 
    pdfConverter.PdfDocumentOptions.LiveUrlsEnabled = cbLiveLinks.Checked; 

    // set if the JavaScript is enabled during conversion to a PDF - default 
     is true 
    pdfConverter.JavaScriptEnabled = cbClientScripts.Checked; 

    // set if the images in PDF are compressed with JPEG to reduce the 
     PDF document size - default is true 
    pdfConverter.PdfDocumentOptions.JpegCompressionEnabled = cbJpegCompression.Checked; 

    // enable auto-generated bookmarks for a specified list of HTML selectors 
     (e.g. H1 and H2) 
    if (cbBookmarks.Checked) 
    { 
     pdfConverter.PdfBookmarkOptions.HtmlElementSelectors = new string[] { "H1", "H2" }; 
    } 

    // add HTML header 
    if (cbAddHeader.Checked) 
     AddHeader(pdfConverter); 
    // add HTML footer 
    if (cbAddFooter.Checked) 
     AddFooter(pdfConverter); 

    // Performs the conversion and get the pdf document bytes that can 

    // be saved to a file or sent as a browser response 
    byte[] pdfBytes = pdfConverter.GetPdfBytesFromUrl(urlToConvert); 

    // send the PDF document as a response to the browser for download 
    System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; 
    response.Clear(); 
    response.AddHeader("Content-Type", "application/pdf"); 
    if (radioAttachment.Checked) 
     response.AddHeader("Content-Disposition", 
       String.Format("attachment; filename=GettingStarted.pdf; size={0}", 
       pdfBytes.Length.ToString())); 
    else 
     response.AddHeader("Content-Disposition", 
       String.Format("inline; filename=GettingStarted.pdf; size={0}", 
       pdfBytes.Length.ToString())); 
    response.BinaryWrite(pdfBytes); 
    // Note: it is important to end the response, otherwise the ASP.NET 
    // web page will render its content to PDF document stream 
    response.End(); 
} 
+0

謝謝親愛的Kenny.I'll嘗試,並讓你知道 –

+1

這篇對你的工作? –

+0

耶謝謝親愛的肯尼思 –