iTextSharp保留pdf上的html格式(iTextSharp preserve html formatting on pdf)

编程入门 行业动态 更新时间:2024-10-27 05:22:13
iTextSharp保留pdf上的html格式(iTextSharp preserve html formatting on pdf)

我在ckeditor粗体,斜体等中使用了一些基本样式,以允许我的用户为报表编写设置样式。

当此字符串传递给iTextSharp时,我将删除html,否则将在pdf上打印html。 我正在删除它

Regex.Replace(item.DevelopmentPractice.ToString(), @"<[^>]*>|&nbsp;", String.Empty)

有没有办法格式化pdf上的文本以保留粗体但不显示

<strong></strong>

UPDATE

我已根据要求提供了以下完整代码。

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(), @"<[^>]*>|&nbsp;", 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");

I am using some basic styles in ckeditor bold, italic, etc. to allow my users to style their text for report writing.

When this string is passed to iTextSharp I am removing the html otherwise the html is printed on the pdf. I am removing this with

Regex.Replace(item.DevelopmentPractice.ToString(), @"<[^>]*>|&nbsp;", String.Empty)

Is there a way to format the text on the pdf to preserve the bold but not display

<strong></strong>

UPDATE

I have provided full code below as requested.

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(), @"<[^>]*>|&nbsp;", 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");

最满意答案

这实际上不是HTML到PDF的最佳方式 - iText或没有iText。 尝试寻找一种不同的方法,您实际上并没有将HTML转换为PDF,而是使用Chunks将拼写文本插入PDF。

最常见的做iText HTML2PDF的方法似乎是使用HTMLWorker (我认为它可能是新版本中的XMLWorker),但人们也抱怨这一点; 看到这个 。 看起来你正在使用没有HTML的非转换iText元素构建PDF,并希望在这些元素中使用HTML,我猜它会非常非常难。

在链接的HTML worker示例中,查看程序的结构。 他们进行HTML2PDF转换 - 但如果失败,他们会使用其他iText方法创建PDF,例如Paragraph和Chunk。 他们在那里设置Chunk也有一些造型。

我想你必须解析传入的HTML,自己将它分成块,然后将s转换为带有样式的块,然后将它们呕吐到PDF上。 现在想象一下使用像CKE这样的数据源 - 即使使用非常严格的ACF,这也是一场噩梦。 如果有人知道除此之外的任何其他方式,我也想知道(我基本上以CKE为生,以PDF为生)!

您是否有任何选项,例如创建自己的编辑器或使用其他PDF技术? 我使用wkhtmltopdf,但我的情况非常不同。 我会使用PrinceXML,但它太贵了。

This really is not the best way to do HTML to PDF - iText or no iText. Try to look for a different method, you are not actually converting HTML to PDF, you are inserting scraped text to PDF using Chunks.

The most common way to do iText HTML2PDF seems to be to use HTMLWorker (I think it might be XMLWorker in newer versions), but people complain about that too; see this. It looks like you are building the PDF using non-converted iText elements without HTML and want to use HTML within those elements and I'm guessing that it will be very, very hard.

In the linked HTML worker example, have a look at the structure of the program. They do a HTML2PDF conversion - but if that fails, they create the PDF using the other iText methods, like Paragraph and Chunk. They there set the Chunk to have some styling as well.

I guess that you would have to parse the incoming HTML, divide it to chunks yourself, convert the s to Chunks with styling and only then vomit them onto the PDF. Now imagine doing that with a data source like CKE - even with a very strict ACF it would be a nightmare. If anyone knows of any other way than this, I want to know too (I do basically CKE to PDF for a living)!

Do you have any options, such as creating your own editor or using some other PDF technique? I use wkhtmltopdf but my situation is very different. I would use PrinceXML but it's too expensive.

更多推荐

本文发布于:2023-07-17 04:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1139231.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:格式   pdf   iTextSharp   html   formatting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!