Java 使用 itext 为 pdf 页面添加边框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24692950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
add border to pdf page using itext
提问by user3819936
this is my source code. why am I not able to add border to my pdf page even after enabling borders for all sides? I've set border and its color too still I'm not able to add border.
这是我的源代码。为什么即使在为所有边启用边框后,我也无法为我的 pdf 页面添加边框?我也设置了边框及其颜色,但仍然无法添加边框。
void create() throws DocumentException,IOException{
// step 1
Document document = new Document();
// step 2
PdfWriter writer=PdfWriter.getInstance(document, new FileOutputStream(RESULT));
document.setPageSize(PageSize.LETTER);
document.setMargins(36, 72, 108, 180);
document.setMarginMirroring(false);
// step 3
document.open();
// step 4
Rectangle rect= new Rectangle(36,108);
rect.enableBorderSide(1);
rect.enableBorderSide(2);
rect.enableBorderSide(4);
rect.enableBorderSide(8);
rect.setBorder(2);
rect.setBorderColor(BaseColor.BLACK);
document.add(rect);
Font font = new Font(Font.FontFamily.TIMES_ROMAN, 26, Font.UNDERLINE, BaseColor.BLACK);
Paragraph title= new Paragraph("CURRICULUM VITAE\n\n",font);
title.setAlignment(Element.ALIGN_CENTER);
document.add(title);
Font f1= new Font (Font.FontFamily.UNDEFINED, 13, Font.NORMAL, BaseColor.BLACK);
Paragraph info= new Paragraph("Name\n\nEmail\n\nContact Number",f1);
Paragraph addr= new Paragraph("Street\n\nCity\n\nPin",f1);
PdfPTable table = new PdfPTable(2);
table.setWidthPercentage(100);
table.spacingAfter();
PdfPCell cell = new PdfPCell(info);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.disableBorderSide(Rectangle.BOX);
cell.setExtraParagraphSpace(1.5f);
table.addCell(cell);
cell = new PdfPCell(addr);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell.disableBorderSide(Rectangle.BOX);
cell.setExtraParagraphSpace(1.5f);
table.addCell(cell);
document.add(table);
document.add(new Chunk("\n"));
document.add(new LineSeparator(2f,100,BaseColor.DARK_GRAY,Element.ALIGN_CENTER,-1f));
采纳答案by Bruno Lowagie
- You didn't define a border width.
- You're adding the border only once. What if you want the border to appear on every page?
- 您没有定义边框宽度。
- 您只添加了一次边框。如果您希望边框出现在每一页上怎么办?
You can fix (1.) by adding:
您可以通过添加以下内容来修复(1.):
rect.setBorder(Rectangle.BOX);
rect.setBorderWidth(2);
Note that I would remove the enableBorderSide()
calls. You'll notice that you've used the setBorder()
method in the wrong way.
请注意,我会删除这些enableBorderSide()
电话。您会注意到您setBorder()
以错误的方式使用了该方法。
To fix (2.), I would use a page event. Note that you can't use document.add()
in a page event, so you'll have to do something as described in the DrawRectangle
example that was written in answer to the question iText: PdfContentByte.rectangle(Rectangle) does not behave as expected
要修复 (2.),我将使用页面事件。请注意,您不能document.add()
在页面事件中使用,因此您必须按照为DrawRectangle
回答iText: PdfContentByte.rectangle(Rectangle) 未按预期运行而编写的示例中所述执行某些操作
You didn't define a page size when creating the Document
object, which means that iText will be using PageSize.A4
. A couple of lines later, you use PageSize.LETTER
. These values are immutable Rectangle
objects. You can create a new Rectangle
using the dimensions/coordinates of PageSize.A4
(or in your case: PageSize.LETTER
). You can obtain the dimensions using the getWidth()
and getHeight()
method and the coordinates using getLeft()
, getBottom()
, getRight()
and getTop()
.
您在创建Document
对象时没有定义页面大小,这意味着 iText 将使用PageSize.A4
. 几行之后,您使用PageSize.LETTER
. 这些值是不可变的Rectangle
对象。您可以Rectangle
使用PageSize.A4
(或在您的情况下:)的尺寸/坐标创建一个新的PageSize.LETTER
。您可以获取使用的尺寸getWidth()
和getHeight()
使用方法和坐标getLeft()
,getBottom()
,getRight()
和getTop()
。
回答by Irfan Ali
Rectangle rect= new Rectangle(577,825,18,15); // you can resize rectangle
rect.enableBorderSide(1);
rect.enableBorderSide(2);
rect.enableBorderSide(4);
rect.enableBorderSide(8);
rect.setBorderColor(BaseColor.BLACK);
rect.setBorderWidth(1);
document.add(rect);