Java 使用itext向pdf页面添加边框

Java 使用itext向pdf页面添加边框,java,pdf,pdf-generation,itext,Java,Pdf,Pdf Generation,Itext,这是我的源代码。为什么即使在启用了所有方面的边界后,我也无法将边界添加到我的pdf页面?我已经设置了边框和它的颜色,但仍然无法添加边框 void create() throws DocumentException,IOException{ // step 1 Document document = new Document(); // step 2 PdfWriter writer=PdfWriter.getInstance(doc

这是我的源代码。为什么即使在启用了所有方面的边界后,我也无法将边界添加到我的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()
    调用。您会注意到,您以错误的方式使用了
    setboorder()
    方法

    为了修复(2),我将使用页面事件。请注意,您不能在页面事件中使用
    document.add()
    ,因此您必须按照为回答问题而编写的示例中所述进行操作


    创建
    文档
    对象时未定义页面大小,这意味着iText将使用
    PageSize.A4
    。几行之后,使用
    PageSize.LETTER
    。这些值是不可变的
    矩形
    对象。您可以使用
    PageSize.A4
    的尺寸/坐标创建一个新的
    矩形
    (或者在您的情况下:
    PageSize.LETTER
    )。您可以使用
    getWidth()
    getHeight()
    方法获取尺寸,以及使用
    getLeft()
    getBottom()
    getRight()
    getTop()
    如何将边框添加为页面大小来获取坐标。我使用字母作为页面大小,那么应该传递什么参数呢?我已经更新了答案。这将使您了解如何获得创建要用于绘制边框的
    矩形所需的参数。thanx我得到了:)如果我在每个新页面添加bordr,那么会有任何问题吗?或者你能告诉我用哪种方法给整个pdf添加边框吗?你的方法的问题是你不知道什么时候会添加新页面。当然,您知道何时手动使用
    document.newPage()
    ,但在某些情况下,当内容不适合当前页面时,iText会自动启动新页面。这就是为什么您应该在page event.table.addCell(String.format(“第%d页,共页”,writer.getPageNumber())的
    onedPage()方法中添加边框的原因;这就是我们如何将表格添加到每个页面。。。添加矩形的相应方法是什么?
    
        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);