这是我的源代码。为什么即使启用了所有边界的边框后,我仍无法在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));
您可以通过添加以下内容来修复(1.):
rect.setBorder(Rectangle.BOX);
rect.setBorderWidth(2);
请注意,我将删除enableBorderSide()
呼叫。您会注意到您使用setBorder()
了错误的方法。
要解决(2.),我将使用page事件。请注意,您不能document.add()
在页面事件中使用它,因此您必须DrawRectangle
按照回答问题iText的示例中所述进行操作:PdfContentByte.rectangle(Rectangle)的行为不符合预期
创建Document
对象时,您没有定义页面大小,这意味着iText将使用PageSize.A4
。几行之后,您使用PageSize.LETTER
。这些值是不可变的Rectangle
对象。您可以Rectangle
使用的尺寸/坐标创建新的PageSize.A4
(或在您的情况下:)PageSize.LETTER
。您可以获取使用的尺寸getWidth()
和getHeight()
使用方法和坐标getLeft()
,getBottom()
,getRight()
和getTop()
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句