0

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.

1 Answers1

1

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.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thanks for your quick answer. I also had a library conflict between itext 5 and itext 7, this doesn't work on v7 and v5, but I downloaded the 5.5 and works perfectly. – Jesús Suárez Borrell Oct 11 '16 at 17:28
  • 1
    I answered with iText 5 code, because you presented iText 5 code: `PdfStamper` no longer exists in iText 7. If you want the iText 7 answer, please post it as a new question – Bruno Lowagie Oct 11 '16 at 23:22
  • @JesúsSuárezBorrell, the conflict is because 5 and 7 are not backwards compatible. itext 7 is a from the ground up rewrite of the functionality in iText5. – Samuel Huylebroeck Oct 12 '16 at 07:19