2011-09-30 188 views
2

我正在使用FreeTextBox.dll來獲取用戶輸入,並將該信息以HTML格式存儲在數據庫中。用戶輸入的samle是如下:iTextSharp HTML to PDF保留空格

                                                                     133 Peachtree St NE
                                                                     Atlanta,  GA 30303
                                                                     404-652-7777

                                                                     Cindy Cooley
                                                                     www.somecompany.com
                                                                     Product Stewardship Mgr

                                                                    9/9/2011
Deidre's Company
123 Test St
Atlanta, GA 30303

Test test.

 

我想HTMLWorker持之以恆的白色空間的用戶進入,但它剝離出來。有沒有辦法維持用戶的空白空間?以下是我如何創建我的PDF文檔的示例。

公共共享子CreatePreviewPDF(BYVAL vsHTML作爲字符串,BYVAL vsFileName作爲字符串)

 Dim output As New MemoryStream() 
     Dim oDocument As New Document(PageSize.LETTER) 
     Dim writer As PdfWriter = PdfWriter.GetInstance(oDocument, output) 
     Dim oFont As New Font(Font.FontFamily.TIMES_ROMAN, 8, Font.NORMAL, BaseColor.BLACK) 

     Using output 
      Using writer 
       Using oDocument 
        oDocument.Open() 
        Using sr As New StringReader(vsHTML) 
         Using worker As New html.simpleparser.HTMLWorker(oDocument) 

          worker.StartDocument() 
          worker.SetInsidePRE(True) 
          worker.Parse(sr) 
          worker.EndDocument() 
          worker.Close() 
          oDocument.Close() 

         End Using 
        End Using 

        HttpContext.Current.Response.ContentType = "application/pdf" 
        HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment;filename={0}.pdf", vsFileName)) 
        HttpContext.Current.Response.BinaryWrite(output.ToArray()) 
        HttpContext.Current.Response.End() 

       End Using 
      End Using 
      output.Close() 
     End Using 


    End Sub 
+0

只要給你一些幫助 - 這可能是錯誤的,如果你將它重新標記爲Visual Basic,可能會獲得更多幫助。 – element119

回答

0

感謝大家的幫助。我能夠做的找到周圍的小工作如下:

vsHTML.Replace(" ", "&nbsp;&nbsp;").Replace(Chr(9), "&nbsp;&nbsp;&nbsp;&nbsp;").Replace(Chr(160), "&nbsp;").Replace(vbCrLf, "<br />") 

實際的代碼不能正常顯示,但是,第一個取而代之的是與&nbsp;代替空格,,並Chr(160)&nbsp;

0

我建議使用wkhtmltopdf代替iText的。 wkhtmltopdf將輸出完全由webkit(Google Chrome,Safari)渲染的html代替iText的轉換。這只是一個可以調用的二進制文件。話雖如此,我可能會檢查html以確保用戶輸入中有段落和/或換行符。轉換之前可能會將其刪除。

+0

謝謝。我們決定採用http://www.html-to-pdf.net/ExpertPDF-HtmlToPdf-Converter.aspx。它效果很好。 – user973754

1

在iText和iTextSharp中有一個小故障,但如果您不介意下載源代碼並重新編譯它,您可以很容易地修復它。您需要對兩個文件進行更改。我所做的任何更改都是在代碼中內聯註釋的。行號基於5.1.2.0代碼rev 240

第一個代碼是iTextSharp.text.html.HtmlUtilities.cs。查找功能EliminateWhiteSpace在行249並將其更改爲:

public static String EliminateWhiteSpace(String content) { 
     // multiple spaces are reduced to one, 
     // newlines are treated as spaces, 
     // tabs, carriage returns are ignored. 
     StringBuilder buf = new StringBuilder(); 
     int len = content.Length; 
     char character; 
     bool newline = false; 
     bool space = false;//Detect whether we have written at least one space already 
     for (int i = 0; i < len; i++) { 
      switch (character = content[i]) { 
      case ' ': 
       if (!newline && !space) {//If we are not at a new line AND ALSO did not just append a space 
        buf.Append(character); 
        space = true; //flag that we just wrote a space 
       } 
       break; 
      case '\n': 
       if (i > 0) { 
        newline = true; 
        buf.Append(' '); 
       } 
       break; 
      case '\r': 
       break; 
      case '\t': 
       break; 
      default: 
       newline = false; 
       space = false; //reset flag 
       buf.Append(character); 
       break; 
      } 
     } 
     return buf.ToString(); 
    } 

第二個變化是在iTextSharp.text.xml.simpleparser.SimpleXMLParser.cs。在功能Go在185行將248行改爲:

if (html /*&& nowhite*/) {//removed the nowhite check from here because that should be handled by the HTML parser later, not the XML parser