I'm trying to do a report from a java swing application, I tried jasper reports and other tools, but I don't understand how to use them properly.
I gave a try to itext, and I actually have something like this.
public void createPDF(){
try {
PdfReader reader = new PdfReader("pdf/watermark.pdf"); // input PDF
PdfStamper stamper = new PdfStamper(reader,new FileOutputStream("C:\\Users\\FIREFENIX\\Documents\\NetBeansProjects\\Java\\PDFCreator\\src\\pdf\\watermarkFinal.pdf")); // output PDF
BaseFont bfArialPlain = BaseFont.createFont("/font/arial.ttf",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
BaseFont bfArialBold = BaseFont.createFont("/font/arialbd.ttf",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
BaseFont bfArialBlack = BaseFont.createFont("/font/ariblk.ttf",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
for (int i=1; i<=reader.getNumberOfPages(); i++){
PdfContentByte over = stamper.getOverContent(i);
//TOP LEFT
over.beginText();
over.setFontAndSize(bfArialBold, 9);
over.setTextMatrix(040, 670);
over.showText("Registry Date: "); //<-----That field/line/text
over.endText();
over.beginText();
over.setFontAndSize(bfArialPlain, 9);
over.setTextMatrix(115, 670);
over.showText("21/07/2016");
over.endText();
over.beginText();
over.setFontAndSize(bfArialBold, 9);
over.setTextMatrix(040, 660);
over.showText("Validation Date: "); //<-----And that one
over.endText();
over.beginText();
over.setFontAndSize(bfArialPlain, 9);
over.setTextMatrix(115, 660);
over.showText("21/07/2016");
over.endText();
//TOP RIGHT...
//...
//...
}
File myFile = new File("C:\\Users\\FIREFENIX\\Documents\\NetBeansProjects\\Java\\PDFCreator\\src\\pdf\\watermarkFinal.pdf");
Desktop.getDesktop().open(myFile);
//////////PRINT DISABLED////////Desktop.getDesktop().print(myFile);
stamper.close();
reader.close();
} catch (IOException | DocumentException ex) {
Logger.getLogger(PDFCreator.class.getName()).log(Level.SEVERE, null, ex);
}
}
I need to have thoose 2 lines underlined.
How can I do it?
There is any other way I can do this easyer?
Thanks.
You are using the very difficult way to add content. There's a much easier way as explained in the official documentation: How to add text at an absolute position on the top of the first page?
Instead of:
cb.BeginText();
cb.MoveText(700, 30);
cb.SetFontAndSize(bf, 12);
cb.ShowText("My status");
cb.EndText();
Use:
ColumnText ct = new ColumnText(stamper.getOverContent(i));
ct.setSimpleColumn(new Rectangle(30, 600, 523, 720));
ct.addElement(new Paragraph("My status"));
ct.go();
If you want to underline text, you need to use a Chunk
as is done in this FAQ entry: Strikethrough in cell using itext in Android/Java
So let's adapt the previous snippet:
ColumnText ct = new ColumnText(stamper.getOverContent(i));
ct.setSimpleColumn(new Rectangle(30, 600, 523, 720));
Chunk c = new Chunk("Underlined text");
c.setUnderline(1.5f, -1);
ct.addElement(new Paragraph(c));
ct.go();
For more info, see Absolute positioning of text and the search results for underline.