【问题标题】:How to apply Header Image in itextsharp Html to pdf convertor如何将 itextsharp Html 中的标题图像应用到 pdf 转换器
【发布时间】:2012-04-03 12:42:04
【问题描述】:

请给我任何解决方案;我正在使用此代码:

 HeaderFooter header = new HeaderFooter(new Phrase("This is a header"), false);
 document.Header = header;

但是发生了这个错误:

CS0246:
找不到类型或命名空间名称“HeaderFooter”(您是 缺少 using 指令或程序集引用?

【问题讨论】:

    标签: c# asp.net pdf itextsharp


    【解决方案1】:

    那个代码是deprecated and removed many years ago,但不幸的是仍然存在于源代码中的 cmets 中。

    您要做的是继承iTextSharp.text.pdf.PdfPageEventHelper 类并处理OnEndPage 方法,该方法将为文档中的每一页调用一次:

    public class MyPageEventHandler : iTextSharp.text.pdf.PdfPageEventHelper {
        public override void OnEndPage(PdfWriter writer, Document document) {
            //Create a simple ColumnText object
            var CT = new ColumnText(writer.DirectContent);
            //Bind it to the top of the document but take up the entire page width
            CT.SetSimpleColumn(0, document.PageSize.Height - 20, document.PageSize.Width, document.PageSize.Height);
            //Add some text
            CT.AddText(new Phrase("This is a test"));
            //Draw our ColumnText object
            CT.Go();
        }
    }
    

    要使用它,您只需将它的一个新实例绑定到您的 PdfWriterPageEvent 属性:

    writer.PageEvent = new MyPageEventHandler();
    

    以下是针对 iTextSharp 5.1.2.0 的完整工作 C# 2010 WinForms 应用程序,它显示了这一点:

    using System;
    using System.IO;
    using System.Windows.Forms;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    
    namespace WindowsFormsApplication1 {
        public partial class Form1 : Form {
            public Form1() {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e) {
                //Test file to create
                string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");
                //Standard PDF file stream creation
                using (FileStream output = new FileStream(outputFile, FileMode.Create,FileAccess.Write,FileShare.None)){
                    using (Document document = new Document(PageSize.LETTER)) {
                        using (PdfWriter writer = PdfWriter.GetInstance(document, output)) {
    
                            //Bind our custom event handler to the PdfWriter
                            writer.PageEvent = new MyPageEventHandler();
                            //Open our PDF for writing
                            document.Open();
    
                            //Add some text to page 1
                            document.Add(new Paragraph("This is page 1"));
                            //Add a new page
                            document.NewPage();
                            //Add some text to page 2
                            document.Add(new Paragraph("This is page 2"));
    
                            //Close the PDF
                            document.Close();
                        }
                    }
                }
    
                this.Close();
            }
        }
        public class MyPageEventHandler : iTextSharp.text.pdf.PdfPageEventHelper {
            public override void OnEndPage(PdfWriter writer, Document document) {
                //Create a simple ColumnText object
                var CT = new ColumnText(writer.DirectContent);
                //Bind it to the top of the document but take up the entire page width
                CT.SetSimpleColumn(0, document.PageSize.Height - 20, document.PageSize.Width, document.PageSize.Height);
                //Add some text
                CT.AddText(new Phrase("This is a test"));
                //Draw our ColumnText object
                CT.Go();
            }
        }
    }
    

    【讨论】:

    • 我们如何使用 PageEventHandler 将 HTML 添加到文件顶部。 CSS 限制太多,无法进行相对定位?
    • @DanielCasserly,如果将您的评论作为一个专门的问题发布,包括您尝试过的内容、有效的内容、无效的内容、您的期望等,您的评论会得到更好的回答。另外,请请记住,iText 不会以任何方式处理 HTML。 iText 有一个帮助程序库,如果/可能的话,它可以将 HTML 字符串转换为 PDF 对象,但这些仍然是 PDF 对象。
    • 谢谢你,事实上我在同一个类中找到了我正在寻找的 OnStartPage 覆盖方法(使用 XMLWorkerHelper 解析)。还是谢谢。
    猜你喜欢
    • 1970-01-01
    • 2017-09-30
    • 2014-09-29
    • 2015-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多