【问题标题】:IText Stamper undeline specific textIText Stamper 取消标记特定文本
【发布时间】:2017-02-20 05:24:12
【问题描述】:

我正在尝试从 java swing 应用程序做报告,我尝试了 jasper 报告和其他工具,但我不明白如何正确使用它们。

我试了一下itext,居然有这样的东西。

   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);
           }

  }

我需要在这两行下划线。

我该怎么做?

还有其他方法可以让我更轻松地做到这一点吗?

谢谢。

【问题讨论】:

    标签: java itext underline pdfstamper


    【解决方案1】:

    您正在使用非常困难的方式添加内容。官方文档中解释了一种更简单的方法:How to add text at an absolute position on the top of the first page?

    代替:

    cb.BeginText();
    cb.MoveText(700, 30);
    cb.SetFontAndSize(bf, 12);
    cb.ShowText("My status");
    cb.EndText();
    

    用途:

    ColumnText ct = new ColumnText(stamper.getOverContent(i));
    ct.setSimpleColumn(new Rectangle(30, 600, 523, 720));
    ct.addElement(new Paragraph("My status"));
    ct.go();
    

    如果你想给文本加下划线,你需要使用Chunk,正如这个常见问题条目中所做的那样:Strikethrough in cell using itext in Android/Java

    那么让我们改编一下之前的sn-p:

    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();
    

    有关详细信息,请参阅Absolute positioning of textunderline 的搜索结果。

    【讨论】:

    • 感谢您的快速回答。我在 itext 5 和 itext 7 之间也有库冲突,这在 v7 和 v5 上不起作用,但我下载了 5.5 并且运行良好。
    • 我用 iText 5 代码回答,因为您提供了 iText 5 代码:PdfStamper 在 iText 7 中不再存在。如果您想要 iText 7 答案,请将其作为新问题发布
    • @JesúsSuárezBorrell,冲突是因为 5 和 7 不向后兼容。 itext 7 是对 iText5 中功能的完全重写。
    猜你喜欢
    • 2018-04-14
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 2013-06-12
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    相关资源
    最近更新 更多