簡體   English   中英

html標記在itextsharp中不被接受,文本超出邊界

[英]html tag not accepted in itextsharp and text out of borders

我使用itextsharp創建了一個表,並用數據庫中的數據填充了它。 一切正常,但是一些數據包含html標簽 ,因此在我的表格中我得到的是標簽,而不是格式化的文本,還有一些文本超出了表格邊框

在此處輸入圖片說明

這是一些代碼:

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);

當您手動添加內容(無論是TableParagraphChunk還是其他內容)時,iTextSharp始終會完全按照其內容插入內容。 這意味着它不會解析HTML。

如果您只想剝離HTML標記, 請參閱這篇文章 ,然后使用RegEx(沒有依賴關系,但可能會破壞一些邊緣情況)或HtmlAgilityPack(我認為這是不必要的開銷)來移除標記。

如果要解釋標記(例如,遇到<strong>時加粗),則必須查看HTMLWorker對象。 這是一篇帖子 ,其中有一些細節。

編輯

下面是示例代碼,該代碼試圖溢出表的邊界,但不在我的測試機上。 它創建了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();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM