我想要一条边框线作为创建 PDF 时的边距。
我尝试过以下代码。但它不会在所有方面都留下余量。
PdfContentByte canvas = writer.getDirectContent();
Rectangle rect = document.getPageSize();
rect.setBorder(Rectangle.BOX); // left, right, top, bottom border
rect.setBorderWidth(1); // a width of 5 user units
rect.setBorderColor(BaseColor.LIGHT_GRAY); // a red border
rect.setUseVariableBorders(false); // the full width will be visible
canvas.rectangle(rect);
我也尝试过这个,但它不起作用。
PdfContentByte canvas = writer.getDirectContent();
canvas.rectangle(5, 5, document.getPageSize().getWidth()-50, document.getPageSize().getHeight());
假设,我想在所有边上留下 40 的边距,并且比我想要的边框还要多。这可能吗?
最佳答案
第一个代码片段应该可以工作,除非定义了 CropBox。如果有裁剪框,矩形的一部分将位于页面的可见区域之外。
第一个代码片段的第二个问题是:
rect.setUseVariableBorders(false);
您的边框宽度为 1,但由于您不使用可变边框,因此只能看到 0.5pt,另外 0.5pt 将位于页面的可见区域之外。
第二个片段将不起作用,因为它不完整。您忘记添加以下行:
canvas.stroke();
关于java - IText PDF 页边距边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26038910/