我正在使用 FreeTextBox.dll 获取用户输入,并将该信息以 HTML 格式存储在数据库中。用户输入的示例如下:
133 Peachtree St NE
Atlanta, GA 30303
404-652-7777Cindy Cooley
www.somecompany.com
Product Stewardship Mgr9/9/2011
Deidre's Company
123 Test St
Atlanta, GA 30303
Test test.
I want the HTMLWorker to perserve the white spaces the users enters, but it strips it out. Is there a way to perserve the user's white space? Below is an example of how I am creating my PDF document.
Public Shared Sub CreatePreviewPDF(ByVal vsHTML As String, ByVal vsFileName As String)
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
最佳答案
iText 和 iTextSharp 中有一个小故障,但如果您不介意下载源代码并重新编译它,您可以很容易地修复它。您需要更改两个文件。我所做的任何更改都在代码中内嵌注释。行号基于 5.1.2.0 代码修订版 240
第一个在 iTextSharp.text.html.HtmlUtilities.cs
中。在第 249 行查找函数 EliminateWhiteSpace
并将其更改为:
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
中。在第 185 行的 Go
函数中,将第 248 行更改为:
if (html /*&& nowhite*/) {//removed the nowhite check from here because that should be handled by the HTML parser later, not the XML parser
关于html - iTextSharp HTML 到 PDF 保留空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7615496/