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.
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));
asked Jul 11, 2014 at 8:08
user3819936 user3819936
35 1 1 gold badge 2 2 silver badges 11 11 bronze badges
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);
answered Oct 6, 2017 at 11:30
199 1 1 silver badge 9 9 bronze badges
You can fix (1.) by adding:
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.
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
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() .