【问题标题】:add border to pdf page using itext使用itext为pdf页面添加边框
【发布时间】:2014-07-11 08:08:08
【问题描述】:

这是我的源代码。为什么即使启用了所有边的边框,我也无法为我的 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));

【问题讨论】:

    标签: java pdf pdf-generation itext


    【解决方案1】:
    1. 您没有定义边框宽度。
    2. 您只添加了一次边框。如果您希望边框出现在每一页上怎么办?

    您可以通过添加以下内容来修复 (1.):

    rect.setBorder(Rectangle.BOX);
    rect.setBorderWidth(2);
    

    请注意,我将删除 enableBorderSide() 调用。您会注意到您以错误的方式使用了setBorder() 方法。

    要修复 (2.),我会使用页面事件。请注意,您不能在页面事件中使用document.add(),因此您必须按照DrawRectangle 示例中所述执行某些操作,该示例是在回答问题iText: PdfContentByte.rectangle(Rectangle) does not behave as expected 时编写的

    您在创建Document 对象时没有定义页面大小,这意味着iText 将使用PageSize.A4。几行之后,您使用PageSize.LETTER。这些值是不可变的Rectangle 对象。您可以使用PageSize.A4 的尺寸/坐标创建一个新的Rectangle(或者在您的情况下:PageSize.LETTER)。您可以使用getWidth()和getHeight()方法获取尺寸,使用getLeft()、getBottom()、getRight()和getTop()获取坐标。

    【讨论】:

    • 如何添加边框作为页面大小。我是用字母作为页面大小,那么应该传递什么参数呢?
    • 我已经更新了我的答案。这将使您了解如何获取创建要用于绘制边框的Rectangle 所需的参数。
    • thanx 我明白了:) 如果我在每个新页面上添加bordr 那么会有什么问题吗?或者您能告诉我使用哪种方法为整个 pdf 添加边框吗?
    • 您的方法的问题是您不知道何时会添加新页面。当然,您知道何时手动使用document.newPage(),但在某些情况下,当内容不适合当前页面时,iText 会自动启动新页面。这就是为什么您应该在页面事件的onEndPage() 方法中添加边框。
    • table.addCell(String.format("第 %d 页", writer.getPageNumber()));这就是我们向每个页面添加表格的方式...添加矩形的相应方法是什么?
    【解决方案2】:
        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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-30
      • 1970-01-01
      • 2017-04-13
      • 2018-02-11
      • 2015-06-04
      • 2011-09-14
      • 1970-01-01
      • 2014-02-17
      相关资源
      最近更新 更多