add border to pdf page using itext
Solution 1
- You didn't define a border width.
- You're adding the border only once. What if you want the border to appear on every page?
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()
.
Solution 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);
user3819936
Updated on June 04, 2022Comments
-
user3819936 almost 2 years
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));
-
user3819936 almost 10 yearshow to add border as the size of page. I'm using letter as page size, so what parameters should be passed?
-
Bruno Lowagie almost 10 yearsI've updated my answer. This will give you an idea on how to get the parameters you need to create the
Rectangle
you want to use to draw a border. -
user3819936 almost 10 yearsthanx I got it :) if I add bordr at every new page then will there be any problem? or can you please tell me which method to use to add border to entire pdf?
-
Bruno Lowagie almost 10 yearsThe problem with your approach is that you don't know when a new page will be added. Sure, you know when you manually use
document.newPage()
, but in some cases iText starts a new page automatically when content doesn't fit on the current page. That's why you should add the border in theonEndPage()
method of a page event. -
user3819936 almost 10 yearstable.addCell(String.format("Page %d of", writer.getPageNumber())); this is how we add table to each page... what is the corresponding method for adding rectangle?
-
user3819936 almost 10 yearsthis method is for adding table, I want corresponding method for rectangle in page event so that I won't need 2 add page border again and again....... or is there any method for adding solid border??
-
user3819936 almost 10 yearsbtw I read your book 3-4 days back... amazing author you are... Reading is not my cup of tea but I loved reading your book.. the way you explain things is very simple and interesting too! :)
-
user3819936 almost 10 yearsI want corresponding method for rectangle in page event so that I won't need 2 add page border again and again....... or is there any method for adding solid border??
-
Bruno Lowagie almost 10 yearsAdding a rectangle can be done with the
rectangle()
method, but I don't think I understand the question. You're already adding a table: I seetable.addCell()
in your question. Why not define a border for the cell? Then you don't have to worry about adding it yourself. -
Bruno Lowagie almost 10 yearsAdding a border in the
onEndPage()
event is just a matter of copying some code from the example I mentioned ( itextpdf.com/sandbox/objects/DrawRectangle ) into the body of theonEndPage()
method. -
user3819936 almost 10 yearsis there any method in itext than render pdf in a jframe?
-
Bruno Lowagie almost 10 yearsiText is about creating PDF, not about rendering PDF, not about Swing components, so the answer is: no, that's out of scope for iText.
-
Bruno Lowagie almost 10 yearsThe answer is no, that's out of scope for iText.