在itextsharp中不接受html标记,并且边界外的文本不被接受

时间:2011-07-08 11:48:12

标签: html itext

我用itextsharp创建了一个表,并用我数据库中的数据填充它。 一切都还可以,但有些数据包含 html标签,所以在我的表格中我得到的是标签而不是格式化的文本,还有一些文字在表格边框之外

enter image description here

以下是一些代码:

PdfPTable table4 = new PdfPTable(3);
                PdfPCell cell8 = new PdfPCell(new Phrase("Protocol", new Font(FontFactory.GetFont("Helvetica", 12f, Font.BOLD, new BaseColor(0, 0, 0)))));
                cell8.BackgroundColor = new BaseColor(242, 242, 242);
                table4.AddCell(cell8);
                PdfPCell cell9 = new PdfPCell(new Phrase("Port", new Font(FontFactory.GetFont("Helvetica", 12f, Font.BOLD, new BaseColor(0, 0, 0)))));
                cell9.BackgroundColor = new BaseColor(242, 242, 242);
                table4.AddCell(cell9);
                PdfPCell cell10 = new PdfPCell(new Phrase("Service", new Font(FontFactory.GetFont("Helvetica", 12f, Font.BOLD, new BaseColor(0, 0, 0)))));
                cell10.BackgroundColor = new BaseColor(242, 242, 242);
                table4.AddCell(cell10);

                foreach (int t in myprotocol)
                {
                    table4.AddCell(t.Protocol);
                    table4.AddCell(t.Port.ToString());
                    table4.AddCell(t.Service);
                }
                document.Add(table4);

1 个答案:

答案 0 :(得分:1)

当您手动添加内容时,无论是TableParagraphChunk还是其他内容,iTextSharp将始终完全插入内容。 这意味着它不会解析HTML。

如果要做的就是删除HTML标记然后see this post并使用RegEx(没有依赖关系,但有一些边缘情况可能会中断)或者HtmlAgilityPack(在我看来很多不必要的开销)删除标签。

如果要解释标记(例如遇到<strong>时的粗体),那么您将不得不查看HTMLWorker对象。 Here's a post详细介绍了它。

修改

下面是尝试溢出表边界但不在我的测试计算机上的示例代码。它创建了4个表行,第3行和第4行有一些错综复杂的尝试来破坏表的边界但不是。 (你会看到复杂的部分,我注入了一些返回,制表符和特殊的Unicode空格。)

(此代码必须完全运行,并且不能选择它才能正常工作,它的目标是iTextSharp 5.1.1.0。)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //Sample object that mimic's the OPs structure
        public class SampleObject
        {
            public string Protocol { get; set; }
            public int Port { get; set; }
            public string Service { get; set; }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            //Create some sample data the mimics the OP's data structure and include some edge cases that could (but don't) cause things to blow up
            List<SampleObject> myprotocol = new List<SampleObject>();
            //General text
            myprotocol.Add(new SampleObject { Protocol = "Short text", Port = 80, Service = "This is a test" });
            //Long text w/ HTML
            myprotocol.Add(new SampleObject { Protocol = "Long HTML text", Port = 81, Service = string.Format("<p>{0}{0}<p>{1}Configure the database server to only allow acces to trusted systems.{0}{1}For Example, the PCI DSS standard requires you the place the database in an{0}{1}internal network zone, segregated from the DMZ.{0}</p>", "\r\n", "\t") });
            //Long text w/ spaces replaced by Unicode FEFF which is a zero-width non-breaking space
            myprotocol.Add(new SampleObject { Protocol = "Long HTML text with zero width no-break space", Port = 82, Service = string.Format("<p>{0}{0}<p>{1}Configure the database server to only allow acces to trusted systems.{0}{1}For Example, the PCI DSS standard requires you the place the database in an{0}{1}internal network zone, segregated from the DMZ.{0}</p>", "\r\n", "\t").Replace(" ", "\uFEFF") });
            //Long text w/ sapces reaplces by Unicode 0020 which is a regular non-breaking space
            myprotocol.Add(new SampleObject { Protocol = "Long HTML text with non-breaking space", Port = 83, Service = string.Format("<p>{0}{0}<p>{1}Configure the database server to only allow acces to trusted systems.{0}{1}For Example, the PCI DSS standard requires you the place the database in an{0}{1}internal network zone, segregated from the DMZ.{0}</p>", "\r\n", "\t").Replace(" ", "\u0020") });

            using (iTextSharp.text.Document Doc = new iTextSharp.text.Document(PageSize.LETTER))
            {
                using (FileStream FS = new FileStream(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "TableTest.pdf"), FileMode.Create, FileAccess.Write, FileShare.Read))
                {
                    using (PdfWriter writer = PdfWriter.GetInstance(Doc, FS))
                    {
                        Doc.Open();

                        Doc.NewPage();

                        PdfPTable table4 = new PdfPTable(3);
                        table4.SetWidths(new float[] { 0.9f, 1f, 1.2f });

                        PdfPCell cell8 = new PdfPCell(new Phrase("Protocol", new iTextSharp.text.Font(FontFactory.GetFont("Helvetica", 12.0f, iTextSharp.text.Font.BOLD, new BaseColor(0, 0, 0)))));
                        cell8.BackgroundColor = new BaseColor(242, 242, 242);
                        table4.AddCell(cell8);

                        PdfPCell cell9 = new PdfPCell(new Phrase("Port", new iTextSharp.text.Font(FontFactory.GetFont("Helvetica", 12f, iTextSharp.text.Font.BOLD, new BaseColor(0, 0, 0)))));
                        cell9.BackgroundColor = new BaseColor(242, 242, 242);
                        table4.AddCell(cell9);

                        PdfPCell cell10 = new PdfPCell(new Phrase("Service", new iTextSharp.text.Font(FontFactory.GetFont("Helvetica", 12f, iTextSharp.text.Font.BOLD, new BaseColor(0, 0, 0)))));
                        cell10.BackgroundColor = new BaseColor(242, 242, 242);
                        table4.AddCell(cell10);

                        foreach (SampleObject t in myprotocol)
                        {
                            table4.AddCell(t.Protocol);
                            table4.AddCell(t.Port.ToString());
                            table4.AddCell(t.Service);
                        }

                        Doc.Add(table4);

                        Doc.Close();
                    }
                }
            }

            this.Close();
        }
    }
}