繁体   English   中英

使用itext将边框添加到pdf页面

[英]add border to pdf page using itext

这是我的源代码。 为什么即使启用了所有边界的边框后,我仍无法在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. 您没有定义边框宽度。
  2. 您只需添加一次边框。 如果希望边框出现在每一页上怎么办?

您可以通过添加以下内容来修复(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对象。 您可以使用PageSize.A4的尺寸/坐标(或者您的情况: PageSize.LETTER )创建一个新的Rectangle 您可以使用getWidth()getHeight()方法获取尺寸,并使用getLeft()getBottom()getRight()getTop()获得坐标。

    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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM