iText 7-为页面添加边框 [英] IText 7 - Adding border for the page

查看:648
本文介绍了iText 7-为页面添加边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在IText 7中添加页面边框且不对宽度和高度进行硬编码的方法.

I am looking for adding page border without hardcoding width and height in IText 7.

尝试使用矩形,但在Itext 7中没有setBorder方法.同样,这适用于整个页面.

Tried with Rectangle, but no setBorder method in Itext 7. Also, this is for the entire page.

        Rectangle rect= new Rectangle(100, 100);
        rect.setBorder(2);
        rect.setBorderColor(BaseColor.BLACK);
        document.add(rect);

Pdf创建

final PdfDocument pdfDoc = new PdfDocument(new PdfWriter(pdfFileName)); 
 final Document doc = new Document(pdfDoc); 
 PdfPage page = pdfDoc.getPage(1); 
 Rectangle pageRect = new Rectangle(page.getTrimBox()); 

推荐答案

以下代码使您可以在文档的指定页面周围绘制红色边框.除页码外,无其他硬编码.

The following piece of code allows you to draw red border around the specified page of your document. No hard-coding except page number.

PdfPage page = pdfDocument.getPage(1);
Rectangle pageRect = new Rectangle(page.getTrimBox());
new PdfCanvas(page).setStrokeColor(ColorConstants.RED).setLineWidth(5).rectangle(pageRect).stroke();

这篇关于iText 7-为页面添加边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆