简体   繁体   English

如何将itextsharp HTML中的标题图像应用于pdf转换器

[英]How to apply Header Image in itextsharp Html to pdf convertor

Please give me any solution; 请给我任何解决方案; I am using this code: 我正在使用此代码:

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

but this error occured: 但发生此错误:

CS0246: CS0246:
The type or namespace name 'HeaderFooter' could not be found (are you missing a using directive or an assembly reference? 找不到类型或名称空间名称“ HeaderFooter”(您是否缺少using指令或程序集引用?

That code was deprecated and removed many years ago but still lives on in comments in the source code unfortunately. 该代码在多年前已弃用并删除,但不幸的是它仍然存在于源代码的注释中。

What you want to do is subclass the iTextSharp.text.pdf.PdfPageEventHelper class and handle the OnEndPage method which will get called once for every page in your document: 您想要做的是将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();
    }
}

To use this you just bind a new instance of it to your PdfWriter 's PageEvent property: 要使用它,您只需将其新实例绑定到PdfWriterPageEvent属性:

writer.PageEvent = new MyPageEventHandler();

Below is a full working C# 2010 WinForms app targeting iTextSharp 5.1.2.0 that shows this: 以下是针对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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM