如何使用itext将有序列表转换为pdf?

发布于 2024-11-27 05:19:04 字数 82 浏览 1 评论 0原文

我必须将有序列表转换为 pdf。存储的数据采用 html 格式。使用 itextsharp 导出为 pdf 时,应将 ol-li 标签替换为有序列表。

I have to get an ordered list into pdf.The data stored is in html format.When exporting to pdf using itextsharp,the ol-li tags should be replaced by an ordered list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉薄对峙 2024-12-04 05:19:04

您需要使用 iTextSharp 的 iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList() 方法。下面是一个针对 iTextSharp 5.1.1.0 的完整工作示例 WinForms 应用程序,它可以满足您的需求。请参阅内嵌评论以了解发生的情况。

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

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

        private void Form1_Load(object sender, EventArgs e)
        {
            //File to export to
            string exportFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "HTML.pdf");

            //Create our PDF document
            using (Document doc = new Document(PageSize.LETTER)){
                using (FileStream fs = new FileStream(exportFile, FileMode.Create, FileAccess.Write, FileShare.Read)){
                    using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)){

                        //Open the doc for writing
                        doc.Open();

                        //Insert a page
                        doc.NewPage();

                        //This is our sample HTML
                        String HTML = "<ol><li>Row 1</li><li>Row 2</li></ol>";

                        //Create a StringReader to parse our text
                        using (StringReader sr = new StringReader(HTML))
                        {
                            //Pass our StringReader into iTextSharp's HTML parser, get back a list of iTextSharp elements
                            List<IElement> ies = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, null);

                            //Loop through each element and add to the document
                            foreach (IElement ie in ies)
                            {
                                doc.Add(ie);
                            }
                        }
                        //Close our document
                        doc.Close();
                    }
                }
            }
        }
    }
}

You'll want to use iTextSharp's iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList() method. Below is a full working sample WinForms app targeting iTextSharp 5.1.1.0 that does what you're looking for. See the inline comments for what's going on.

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

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

        private void Form1_Load(object sender, EventArgs e)
        {
            //File to export to
            string exportFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "HTML.pdf");

            //Create our PDF document
            using (Document doc = new Document(PageSize.LETTER)){
                using (FileStream fs = new FileStream(exportFile, FileMode.Create, FileAccess.Write, FileShare.Read)){
                    using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)){

                        //Open the doc for writing
                        doc.Open();

                        //Insert a page
                        doc.NewPage();

                        //This is our sample HTML
                        String HTML = "<ol><li>Row 1</li><li>Row 2</li></ol>";

                        //Create a StringReader to parse our text
                        using (StringReader sr = new StringReader(HTML))
                        {
                            //Pass our StringReader into iTextSharp's HTML parser, get back a list of iTextSharp elements
                            List<IElement> ies = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, null);

                            //Loop through each element and add to the document
                            foreach (IElement ie in ies)
                            {
                                doc.Add(ie);
                            }
                        }
                        //Close our document
                        doc.Close();
                    }
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文