-2

我正在开发一个将 html 数据导出到 pdf 文件(在 c# 中)的应用程序。我按照这些步骤来转换数据

StyleSheet styles = new StyleSheet();
tempText = tempText.Replace("\"", """);
ArrayList objects = HTMLWorker.ParseToList(new StringReader(tempText), styles);
       for (int k = 0; k < objects.Count; k++)
       {
                document.Add((iTextSharp.text.IElement)objects[k]);
       }

让我们假设我的文字是否类似于

<h3 style="color:blue;">
       Write a Java 
       <span style="font-size:16px;">
           <span style="background-color:yellow;">
                program that prints two separate text
           </span> 
       </span>
       strings on the same line.

![ exported data in pdf is shown in the image below ][1]</h3>

问题是内部跨度标记的转换失败。它不会background-color从样式解析。我怎样才能做到这一点?我不想使用任何第三方工具。

4

1 回答 1

1

在 c#.Net Web 应用程序中将 Html 字符串导出为 pdf

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=LetterListing.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter(PrintLetter());
HtmlTextWriter hw = new HtmlTextWriter(sw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
于 2013-03-22T10:36:29.797 回答