iText 7 table ignoring my table border settings - java

I want a table with no borders. I've tried to set the border property, individual border properties, set the border manually, set the cells border to no border, etc. None remove the border. What is the proper way to have an iText 7 table with no border?
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outputStream));
Document doc = new Document(pdfDoc);
PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA);
Table table = new Table(new float[] { 1, 1 });
table.setProperty(Property.BORDER_BOTTOM, Border.NO_BORDER);
table.setProperty(Property.BORDER_LEFT, Border.NO_BORDER);
table.setProperty(Property.BORDER_RIGHT, Border.NO_BORDER);
table.setProperty(Property.BORDER_TOP, Border.NO_BORDER);
table.setProperty(Property.BORDER, Border.NO_BORDER);
table.setBorder(Border.NO_BORDER);
table.setWidthPercent(100);
// Header
File file = new ClassPathResource("logo.png").getFile();
Image logo = new Image(ImageDataFactory.create(file.getPath()));
Paragraph headerParagraph = new Paragraph();
Text headerTitle = new Text("Title of PDF")
.setFont(font)
.setFontSize(20)
.setFontColor(new DeviceRgb(0, 128, 128));
Text headerDescription = new Text("Description")
.setFont(font)
.setFontSize(11);
headerParagraph.add(headerTitle);
headerParagraph.add(NEW_LINE);
headerParagraph.add(headerDescription);
table.addCell(logo);
table.addCell(headerParagraph).setTextAlignment(TextAlignment.RIGHT);
None of these settings seem to work.Using iText 7.0.2

First of all, run the next snippet and see that iText7 can create tables without borders.
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
Document doc = new Document(pdfDoc);
Table table = new Table(new float[] {50, 50 });
Paragraph headerParagraph = new Paragraph();
Text headerTitle = new Text("Title of PDF")
.setFontSize(20)
.setFontColor(new DeviceRgb(0, 128, 128));
Text headerDescription = new Text("Description")
.setFontSize(11);
headerParagraph.add(headerTitle);
headerParagraph.add(headerDescription);
table.addCell(new Cell().add("logo").setBorder(Border.NO_BORDER));
table.addCell(new Cell().add(headerParagraph).setBorder(Border.NO_BORDER).setTextAlignment(TextAlignment.RIGHT));
doc.add(table);
This is the line responsible for such "magic" :
table.addCell(new Cell().add("logo").setBorder(Border.NO_BORDER));
However there is no magic at all.
By default, cells have borders in iText7 (0.5px solid black). So if you want to add a cell without border you should specify it by setting NO_BORDER as a cell border.
On the other hand, tables don't have borders by default (I mean bounding borders). So there is no need in these lines:
table.setProperty(Property.BORDER_BOTTOM, Border.NO_BORDER);
table.setProperty(Property.BORDER_LEFT, Border.NO_BORDER);
table.setProperty(Property.BORDER_RIGHT, Border.NO_BORDER);
table.setProperty(Property.BORDER_TOP, Border.NO_BORDER);
table.setProperty(Property.BORDER, Border.NO_BORDER);
table.setBorder(Border.NO_BORDER);
Also you should understand that table.setBorder(border) stands for table.setProperty(Property.BORDER, border). The same for table.setBorderLeft(border), etc.

Related

The content of cell does not display in iText7

If the content of cell is bigger than height of cell in the height-fixed table, the content does not display.
This is probable a specification of iText library, but is there any way to make the content visible in iText7 in this case?
I'm migrating from iText5 to iText7.
Even though I set the same value(table and cell height, padding value, font size) as iText5, the content of cell does not display in iText7.
If I set the TopPadding and BottomPadding of Cell to 0, the content of cell does display.
I don't think that iText5 and iText have diffent coordinates and units.
How do I get the same result as iText5?
iText 5's code
PdfPTable ptable = new PdfPTable(1);
ptable.setTotalWidth(143f);
ptable.setWidthPercentage(100f);
PdfPCell pcell = new PdfPCell();
pcell.setPadding(2f);
pcell.setUseAscender(true);
pcell.setUseDescender(true);
pcell.setFixedHeight(16.05603f);
Paragraph p = new Paragraph();
p.setLeading(4.032f, 1.0f);
p.setSpacingBefore(0f);
p.setSpacingAfter(0f);
Chunk str = new Chunk("Sample");
Font font = FontFactory.getFont("batang", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
font.setSize(12f);
str.setFont(font);
p.add(str);
pcell.addElement(p);
ptable.addCell(pcell);
ptable.writeSelectedRows(0, -1, 300f, 150f, contentByte);
iText 7's code
Table table = new Table(new float[] {143f});
table.setWidthPercent(100f);
table.setHeight(16.05603f);
Cell cell = new Cell();
cell.setPadding(2f);
cell.setHeight(16.05603f);
Paragraph p = new Paragraph();
p.setFixedLeading(4.032f);
p.setMultipliedLeading(1.0f);
p.setMarginTop(0f);
p.setMarginBottom(0f);
PdfFont font = PdfFontFactory.createRegisteredFont("batang", PdfEncodings.IDENTITY_H, true);
Text str = new Text("Sample").setFontSize(12f).setFont(font);
p.add(str);
cell.add(p);
table.addCell(cell);
table.setFixedPosition(300f, 150f, 143f);
document.add(table);

align cell in itextpdf java

I'm using itextpdf to create my pdf with tables. While creating table i need to align some column to right, but its now working properly , can you guys help me.
I tried googling too, but didt work out for me. im using itextpdf 5.4 version.
public void generateMonthlySubReport(String[][] StrArray,String dueMonth,int Amt){
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(MON_SUB_FILE));
PdfPTable pt = new PdfPTable(StrArray[0].length);
pt.setTotalWidth(new float[]{ 55,120,360,140});
pt.setLockedWidth(true);
PdfPCell pcell = new PdfPCell();
document.open();
addKvLogo(document);
Chunk glue = new Chunk(new VerticalPositionMark());
Paragraph p1 = new Paragraph("Monthly Subscription Report",catFont);
p1.setAlignment(Element.ALIGN_CENTER);
addEmptyLine(p1,2);
document.add(p1);
Paragraph p2 = new Paragraph("Month : "+dueMonth);
p2.add(new Chunk(glue));
p2.add("Per Member : Rs."+Amt);
addEmptyLine(p2,2);
document.add(p2);
for(int i=0;i<StrArray.length;i++){
for(int j=0;j<StrArray[i].length;j++){
pcell = new PdfPCell();
if(i==0){
pcell.setBackgroundColor(BaseColor.LIGHT_GRAY);
}else{
pcell.setBackgroundColor(BaseColor.WHITE);
}
pcell.setUseAscender(true);
pcell.setMinimumHeight(22);
pcell.setPaddingLeft(10);
pcell.setHorizontalAlignment(Element.ALIGN_RIGHT);
pcell.setVerticalAlignment(Element.ALIGN_MIDDLE);
pcell.addElement(new Phrase(StrArray[i][j]));
pt.addCell(pcell);
}
}
pt.setTotalWidth(PageSize.A4.getWidth()-(document.leftMargin()*2));
pt.setLockedWidth(true);
document.add(pt);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
} `
You are mixing text mode with composite mode.
This is text mode:
pcell = new PdfPCell(new Phrase(StrArray[i][j]));
pcell.setHorizontalAlignment(Element.ALIGN_RIGHT);
In this case, the alignment of the cell will be used for the alignment of the text.
This is composite mode:
pcell = new PdfPCell();
Paragraph p = new Parapgraph(StrArray[i][j])
p.setAlignment(Element.ALIGN_RIGHT);
pcell.addElement(p);
In this case, the alignment of the cell is ignored, in favor of the alignment of the element.
How to know the difference between text mode and composite mode?
iText automatically switches from text mode to composite mode in a PdfPCell the moment you use the addElement() method. As soon as you do this, some properties defined at the cell level are ignored. This explains why the content you are adding isn't right-aligned.

how to place text and images in small sized page using iText

I need to make a PDF page that looks something like this:
I'm having problems to make two columns that fit on a small sized page.
This is my code:
public void createSizedPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.setMargins(5,5,5,5);
Rectangle one = new Rectangle(290,100);
one.setBackgroundColor(Color.YELLOW);
document.setPageSize(one);
document.open();
Paragraph consigneeName = new Paragraph("Ahmed");
Paragraph address = new Paragraph("Casa ST 121");
String codeBL = "14785236987541";
PdfContentByte cb = writer.getDirectContent();
Barcode128 code128 = new Barcode128();
code128.setBaseline(9);
code128.setSize(9);
code128.setCode(codeBL);
code128.setCodeType(Barcode128.CODE128);
Image code128Image = code128.createImageWithBarcode(cb, null, null);
Paragraph right = new Paragraph();
right.add(consigneeName);
right.add(address);
right.add(code128Image);
Chunk glue = new Chunk(new VerticalPositionMark());
Paragraph p = new Paragraph();
p.add(right);
p.add(new Chunk(glue));
p.add(code128Image);
document.add(p);
document.close();
}
One way to solve your problem, would be to create a template PDF with AcroForm fields. You could create a nice design manually, and then fill out the form programmatically by putting data (text, bar codes) at the appropriate places defined by the fields that act as placeholders.
Another way, is to create the PDF from scratch, which is the approach you seem to have taken, looking at your code.
Your question isn't entirely clear, in the sense that you share your code, but you don't explain the problem you are experiencing. As I already commented:
are you unable to scale images? are you unable to define a smaller
font size? are you unable to create a table with specific dimension?
You say I'm having problems to make two columns that fits in a small
sized page but you forgot to describe the problems.
You didn't give an answer to those questions, and that makes it very hard for someone to answer your question. The only thing a Stack Overflow reader could do, is to do your work in your place. That's not what Stack Overflow is for.
Moreover, the answer to your question is so trivial that it is hard for a Stack Overflow reader to understand why you posted a question.
You say you need to add data (text and bar codes) in two columns, you are actually saying that you want to create a table. This is an example of such a table:
If you look at the SmallTable example, you can see how it's built.
You want a PDF that measures 290 by 100 user units, with a margin of 5 user units on each side. This means that you have space for a table measuring 280 by 90 user units. Looking at your screen shot, I'd say that you have a column of 160 user units with and a column of 120 user units. I'd also say that you have three rows of 30 user units high each.
OK, then why don't you create a table based on those dimensions?
public void createPdf(String dest) throws IOException, DocumentException {
Rectangle small = new Rectangle(290,100);
Font smallfont = new Font(FontFamily.HELVETICA, 10);
Document document = new Document(small, 5, 5, 5, 5);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable table = new PdfPTable(2);
table.setTotalWidth(new float[]{ 160, 120 });
table.setLockedWidth(true);
PdfContentByte cb = writer.getDirectContent();
// first row
PdfPCell cell = new PdfPCell(new Phrase("Some text here"));
cell.setFixedHeight(30);
cell.setBorder(Rectangle.NO_BORDER);
cell.setColspan(2);
table.addCell(cell);
// second row
cell = new PdfPCell(new Phrase("Some more text", smallfont));
cell.setFixedHeight(30);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
Barcode128 code128 = new Barcode128();
code128.setCode("14785236987541");
code128.setCodeType(Barcode128.CODE128);
Image code128Image = code128.createImageWithBarcode(cb, null, null);
cell = new PdfPCell(code128Image, true);
cell.setBorder(Rectangle.NO_BORDER);
cell.setFixedHeight(30);
table.addCell(cell);
// third row
table.addCell(cell);
cell = new PdfPCell(new Phrase("and something else here", smallfont));
cell.setBorder(Rectangle.NO_BORDER);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
document.add(table);
document.close();
}
In this example,
you learn how to change the font of the content in a cell,
you learn how to change horizontal and vertical alignment,
you learn how to scale a bar code so that it fits into a cell,
...
All of this functionality is explained in the official documentation. As I said before: you didn't explain the nature of your problem. What wasn't clear in the documentation for you? What is your question?

Changing Font on PDF Rotated Text

I'm using iText to create barcodes on a PDF with the same format as this one:
The problem is the the left number, the first zero digits must be smaller, while the rest of the numbers must also be bold. "T.T.C." also has to be even smaller (it doesn't have to be on another line).
I was able to rotate the number with the following code:
String price = "23000 T.T.C.";
PdfContentByte cb = docWriter.getDirectContent();
PdfTemplate textTemplate = cb.createTemplate(50, 50);
ColumnText columnText = new ColumnText(textTemplate);
columnText.setSimpleColumn(0, 0, 50, 50);
columnText.addElement(new Paragraph(price));
columnText.go();
Image image;
image = Image.getInstance(textTemplate);
image.setAlignment(Image.MIDDLE);
image.setRotationDegrees(90);
doc.add(image);
The problem is that I cannot find a way online to change the font of certain characters of the String price when it is printed on the PDF.
I have created a small Proof of Concept that results in a PDF that looks like this:
As you can see, it has text in different sizes and styles. It also has a bar code that is rotated.
Take a look at the RotatedText example:
public void createPdf(String dest) throws IOException, DocumentException {
// step 1
Document document = new Document(new Rectangle(60, 120), 5, 5, 5, 5);
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
// step 3
document.open();
// step 4
PdfContentByte canvas = writer.getDirectContent();
Font big_bold = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
Font small_bold = new Font(FontFamily.HELVETICA, 6, Font.BOLD);
Font regular = new Font(FontFamily.HELVETICA, 6);
Paragraph p1 = new Paragraph();
p1.add(new Chunk("23", big_bold));
p1.add(new Chunk("000", small_bold));
document.add(p1);
Paragraph p2 = new Paragraph("T.T.C.", regular);
p2.setAlignment(Element.ALIGN_RIGHT);
document.add(p2);
BarcodeEAN barcode = new BarcodeEAN();
barcode.setCodeType(Barcode.EAN8);
barcode.setCode("12345678");
Rectangle rect = barcode.getBarcodeSize();
PdfTemplate template = canvas.createTemplate(rect.getWidth(), rect.getHeight() + 10);
ColumnText.showTextAligned(template, Element.ALIGN_LEFT,
new Phrase("DARK GRAY", regular), 0, rect.getHeight() + 2, 0);
barcode.placeBarcode(template, BaseColor.BLACK, BaseColor.BLACK);
Image image = Image.getInstance(template);
image.setRotationDegrees(90);
document.add(image);
Paragraph p3 = new Paragraph("SMALL", regular);
p3.setAlignment(Element.ALIGN_CENTER);
document.add(p3);
// step 5
document.close();
}
This example solves all of your issues:
You want a Paragraph to use different fonts: compose a Paragraph using different Chunk objects.
You want to add extra text on top of a bar code: add the bar code to a PdfTemplate and add the extra text using ColumnText.showTextAligned() (not that you can also compose a Phrase using different Chunk objects if you need more than one font in that extra text).
You want to rotate the bar code: wrap the PdfTemplate inside an Image object and rotate the image.
You can check the result: rotated_text.pdf
I hope this helps.

Applying color to Strings in Paragraph using Itext

I am combining 2 strings to Paragraph this way,
String str2="";
String str1="";
ColumnText ct = new ColumnText(cb);
ct.setSimpleColumn(36, 600, 600, 800);
ct.addElement(new Paragraph(str1 + str2));
int status1 = ct.go();
The problem is I am getting same font color for both str1 & str2.
I want to have different font color and size for str1 & str2..
How Can i do that on ColumnText/Paragraph?
Can someone help me in this...
When you combine text into a Paragraph like this:
Paragraph p = new Paragraph("abc" + "def");
You implicitly tell iText that "abc" and "def" should be rendered using the same (default) font. As you probably know, a Paragraph is a collection of Chunk objects. In iText, a Chunk is like an atomic part of text in the sense that all the text in a Chunk has the same font, font size, font color, etc...
If you want to create a Paragraph with different font colors, you need to compose your Paragraph using different Chunk objects. This is shown in the ColoredText example:
Font red = new Font(FontFamily.HELVETICA, 12, Font.NORMAL, BaseColor.RED);
Chunk redText = new Chunk("This text is red. ", red);
Font blue = new Font(FontFamily.HELVETICA, 12, Font.BOLD, BaseColor.BLUE);
Chunk blueText = new Chunk("This text is blue and bold. ", blue);
Font green = new Font(FontFamily.HELVETICA, 12, Font.ITALIC, BaseColor.GREEN);
Chunk greenText = new Chunk("This text is green and italic. ", green);
Paragraph p1 = new Paragraph(redText);
document.add(p1);
Paragraph p2 = new Paragraph();
p2.add(blueText);
p2.add(greenText);
document.add(p2);
In this example, we create two paragraphs. One with a single Chunk in red. Another one that contains two Chunks with a different color.
In your question, you refer to ColumnText. The next code snippet uses p1 and p2 in a ColumnText context:
ColumnText ct = new ColumnText(writer.getDirectContent());
ct.setSimpleColumn(new Rectangle(36, 600, 144, 760));
ct.addElement(p1);
ct.addElement(p2);
ct.go();
As a result, the paragraphs are added twice: once positioned by iText, once positioned by ourselves by defining coordinates using a Rectangle:

Categories

Resources