IText Stamper 取消划线特定文本
IText Stamper undeline specific text
我正在尝试从 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);
}
}
我需要在 2 行下划线。
我该怎么做?
还有其他方法可以让我更轻松地做到这一点吗?
谢谢。
您正在使用非常困难的方式来添加内容。官方文档中解释了一种更简单的方法: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
就像这个 FAQ 条目中所做的那样:
所以让我们改编之前的片段:
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 text and the search results for underline。
我正在尝试从 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);
}
}
我需要在 2 行下划线。
我该怎么做?
还有其他方法可以让我更轻松地做到这一点吗?
谢谢。
您正在使用非常困难的方式来添加内容。官方文档中解释了一种更简单的方法: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
就像这个 FAQ 条目中所做的那样:
所以让我们改编之前的片段:
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 text and the search results for underline。