itext7生成pdf页脚页码

itext7生成pdf页脚页码
在document.close()之前调用

int numberOfPages = pdf.getNumberOfPages();
for (int i = 1; i <= numberOfPages; i++) {
    if (i != 1) {
        String text = String.format("第 %s 页 / 共 %s 页", i, numberOfPages);
        document.showTextAligned(new Paragraph(text),
                595 / 2, document.getBottomMargin() / 2, i, TextAlignment.CENTER, VerticalAlignment.MIDDLE, 0);

    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要在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库添加到你的项目中,并根据需要提供正确的图片路径。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值