我在ckeditor粗体,斜体等中使用了一些基本样式,以允许我的用户为报告撰写设置文本样式。
当此字符串传递给iTextSharp时,我将删除html,否则html将打印在pdf上。我正在删除
Regex.Replace(item.DevelopmentPractice.ToString(), @"<[^>]*>| ", String.Empty)
有没有办法格式化pdf上的文字以保留粗体但不显示
<strong></strong>
更新
我已根据要求提供了以下完整代码。
public FileStreamResult pdf(int id)
{
// Set up the document and the Memory Stream to write it to and create the PDF writer instance
MemoryStream workStream = new MemoryStream();
Document document = new Document(PageSize.A4, 30, 30, 30, 30);
PdfWriter.GetInstance(document, workStream).CloseStream = false;
// Open the pdf Document
document.Open();
// Set up fonts used in the document
Font font_body = FontFactory.GetFont(FontFactory.HELVETICA, 10);
Font font_body_bold = FontFactory.GetFont(FontFactory.HELVETICA, 10, Font.BOLD);
Chunk cAreasDevelopmentHeading = new Chunk("Areas identified for development of practice", font_body_bold);
Chunk cAreasDevelopmentComment = new Chunk(item.DevelopmentPractice != null ? Regex.Replace(item.DevelopmentPractice.ToString(), @"<[^>]*>| ", String.Empty) : "", font_body);
Paragraph paraAreasDevelopmentHeading = new Paragraph();
paraAreasDevelopmentHeading.SpacingBefore = 5f;
paraAreasDevelopmentHeading.SpacingAfter = 5f;
paraAreasDevelopmentHeading.Add(cAreasDevelopmentHeading);
document.Add(paraAreasDevelopmentHeading);
Paragraph paraAreasDevelopmentComment = new Paragraph();
paraAreasDevelopmentComment.SpacingBefore = 5f;
paraAreasDevelopmentComment.SpacingAfter = 15f;
paraAreasDevelopmentComment.Add(cAreasDevelopmentComment);
document.Add(paraAreasDevelopmentComment);
document.Close();
byte[] byteInfo = workStream.ToArray();
workStream.Write(byteInfo, 0, byteInfo.Length);
workStream.Position = 0;
// Setup to Download
HttpContext.Response.AddHeader("content-disposition", "attachment; filename=supportform.pdf");
return File(workStream, "application/pdf");
答案 0 :(得分:0)
这实际上不是HTML到PDF的最佳方式 - iText或没有iText。尝试寻找一种不同的方法,您实际上并没有将HTML转换为PDF,而是使用Chunks将拼写文本插入PDF。
最常见的iText HTML2PDF方法似乎是使用HTMLWorker(我认为它可能是新版本中的XMLWorker),但人们也抱怨这一点;见this。看起来你正在使用没有HTML的非转换iText元素构建PDF,并希望在这些元素中使用HTML,我猜这将是非常非常困难的。
在链接的HTML worker示例中,查看程序的结构。他们进行HTML2PDF转换 - 但如果失败,他们会使用其他iText方法创建PDF,例如Paragraph和Chunk。他们在那里设置Chunk也有一些造型。
我猜你必须解析传入的HTML,自己将它分成块,然后将s转换为带有样式的块,然后将它们呕吐到PDF上。现在想象一下使用像CKE这样的数据源 - 即使使用非常严格的ACF,这也是一场噩梦。如果有人知道除此之外的任何其他方式,我也想知道(我基本上以CKE为PDF来谋生)!
您是否有任何选项,例如创建自己的编辑器或使用其他PDF技术?我使用wkhtmltopdf,但我的情况非常不同。我会使用PrinceXML,但它太贵了。