itext高版本的pdf的页眉和页脚信息显示方法

本文介绍如何在iText高版本中创建PDF文档,并展示动态的页眉和页脚信息,例如日期和入库单据详情。通过自定义PdfPageEventHelper子类,设置中文文字和内容,实现在每一页上显示不同数据。
摘要由CSDN通过智能技术生成

本次对itext的研究主要针对的是pdf 的table,主要应用于打印单据。

框架为jfinal,表头信息中有动态数据,比如:年月日等。

下面直接贴代码:

/**
     * 前台调用打印方法
     * LMM
     *
     */
    public void printStorage(){
        try {
            Integer id=getParaToInt("pid",0);//获取前台传数据
            List<Stordetail> stolist=Stordetail.dao.find("select s.*,st.*,p.pub_name,e.edi_name,i.iss_name from stordetail s " +
                    " left join publish p on s.pub_id=p.pub_id left join edition e on s.edi_id=e.edi_id" +
                    " left join storsummary st on s.stsu_id=st.stsu_id " +
                    " left join issue i on s.iss_id=i.iss_id where s.stsu_id="+id+" order by det_id desc");//需要显示的数据
            Storsummary stsu=Storsummary.dao.findById(id);//需要显示的数据
            //start print
            Rectangle rec = new Rectangle(240*2.5f,140*2.5f); //自定义纸张大小
            Document document = new Document(rec,10,10,50,50);//纸张大小,marginleft,marginright,margintop,marginbottom
            //设置输出的位置并把对象装入输出对象中
            PdfWriter writer=PdfWriter.getInstance(document, new FileOutputStream(PathKit.getWebRootPath() + "/temp/printStorage.pdf"));
            //设置pdf每页的页眉和页脚
            writer.setPageEvent(new HeadFootInfoPdfPageEvent(stolist,stsu));
            //打开文档
            document.open();
            //设置中文字体
            BaseFont baseFont = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);//配置中文
            //字体和字号
            Font font = new Font(baseFont, 12, Font.NORMAL);
          
要在PDF页眉页脚中插入图片,并写入页码,你可以使用iText7库的以下示例代码: ```csharp using iText.Kernel.Pdf; using iText.Layout; using iText.Layout.Element; using iText.Layout.Properties; using iText.Kernel.Pdf.Canvas; using System.IO; public void AddHeaderAndFooterWithImage(string inputPdfPath, string outputPdfPath, string headerImagePath, string footerImagePath) { PdfDocument pdfDoc = new PdfDocument(new PdfReader(inputPdfPath), new PdfWriter(outputPdfPath)); Document doc = new Document(pdfDoc); // 添加页眉 Image headerImage = new Image(ImageDataFactory.Create(headerImagePath)); Table headerTable = new Table(1).UseAllAvailableWidth(); headerTable.AddCell(new Cell().Add(headerImage).SetBorder(Border.NO_BORDER)); doc.SetHeader(headerTable); // 添加页脚 Image footerImage = new Image(ImageDataFactory.Create(footerImagePath)); Table footerTable = new Table(1).UseAllAvailableWidth(); footerTable.AddCell(new Cell().Add(footerImage).SetBorder(Border.NO_BORDER)); doc.SetFooter(footerTable); // 写入页码 for (int pageNum = 1; pageNum <= pdfDoc.GetNumberOfPages(); pageNum++) { Canvas canvas = new Canvas(pdfDoc.GetPage(pageNum), pdfDoc.GetPage(pageNum).GetPageSize()); Paragraph pageNumber = new Paragraph("页码: " + pageNum); canvas.ShowTextAligned(pageNumber, 559, 20, pageNum, TextAlignment.RIGHT); } doc.Close(); } ``` 在这个示例代码中,你需要将`inputPdfPath`替换为你要添加页眉页脚PDF文件路径,将`outputPdfPath`替换为生成的带有页眉页脚PDF文件路径。`headerImagePath`和`footerImagePath`应该是你要插入到页眉页脚的图片的路径。 我们使用`Image`类创建了要插入的图片,并使用`Table`将图片添加到页眉页脚中。然后,我们使用`Canvas`类将页码写入每一页的指定位置。 确保你已经将iText7库添加到你的项目中,并根据需要提供正确的图片路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值