Java PdfBox 2.0.0在页面中的给定位置写入文本
我刚刚从PdfBox 1.8过渡到2.0.0,两者之间有很大的差异。在现有的pdf页面上编写文本之前,我使用了drawString。在2.0.0中,不推荐使用draw string,但showText在块文本中不起作用 我在1.8中的代码:Java PdfBox 2.0.0在页面中的给定位置写入文本,java,pdfbox,Java,Pdfbox,我刚刚从PdfBox 1.8过渡到2.0.0,两者之间有很大的差异。在现有的pdf页面上编写文本之前,我使用了drawString。在2.0.0中,不推荐使用draw string,但showText在块文本中不起作用 我在1.8中的代码: contentStream.beginText() contentStream.moveTextPositionByAmount(250, 665) contentStream.drawString("1 2 3 4 5 6 7 8 9
contentStream.beginText()
contentStream.moveTextPositionByAmount(250, 665)
contentStream.drawString("1 2 3 4 5 6 7 8 9 1 0")
contentStream.endText()
我在2.0中的代码
PDDocument newPdf=null
newPdf=PDDocument.load(sourcePdfFile)
PDPage firstPage=newPdf.getPage(0)
PDPageContentStream contentStream = new PDPageContentStream(newPdf, firstPage, PDPageContentStream.AppendMode.APPEND,true,true)
contentStream.setFont(pdfFont, fontSize)
contentStream.beginText()
contentStream.lineTo(200,685)
contentStream.showText("John")
contentStream.endText()
但它不起作用
任何人都知道我该如何写1.8中的文本,因为LineTo就是画一条线。您需要的是
newlineatofset
(moveTextPositionByAmount的弃用通知中这样说),因此您的代码如下所示:
PDDocument newPdf = PDDocument.load(sourcePdfFile);
PDPage firstPage=newPdf.getPage(0);
PDFont pdfFont= PDType1Font.HELVETICA_BOLD;
int fontSize = 14;
PDPageContentStream contentStream = new PDPageContentStream(newPdf, firstPage, PDPageContentStream.AppendMode.APPEND,true,true);
contentStream.setFont(pdfFont, fontSize);
contentStream.beginText();
contentStream.newLineAtOffset(200,685);
contentStream.showText("John");
contentStream.endText();
contentStream.close(); // don't forget that one!
如果您正在寻找在所需位置添加PDF中多行语句的代码
像这样
这是第一行
这是第二行
这是第三行
那么您需要使用
//to load PDF where you
PDDocument pdDocument = PDDocument.load(new File(resourceBundle.getString("F:\PDF\loki.pdf")));
//get the page where you want to write code,for first page you need to use 0
PDPage firstPage = pdDocument.getPage(0);
//you can load new font if required, by using `ttf` file for that font
PDFont pdfFont = PDType0Font.load(pdDocument,
new File(resourceBundle.getString("F:\PDF\data\verdana.ttf")));
int fontSize = 9;
//PDPageContentStream.AppendMode.APPEND this part is must if you want just add new data in exsitnig one
PDPageContentStream contentStream = new PDPageContentStream(pdDocument, pdDocument.getPage(0),
PDPageContentStream.AppendMode.APPEND, true, true);
contentStream.setFont(PDType0Font.load(pdDocument, new File(resourceBundle.getString("pdfFont"))), 9);
//for first Line
contentStream.beginText();
//For adjusting location of text on page you need to adjust this two values
contentStream.newLineAtOffset(200,685);
contentStream.showText("this is line first");
contentStream.endText();
//for second line
contentStream.beginText();
contentStream.newLineAtOffset(200,685);
contentStream.showText("this is line second");
contentStream.endText();
//for third line
contentStream.beginText();
contentStream.newLineAtOffset(200,685);
contentStream.showText("this is line third");
contentStream.endText();
//and so on.
//at last you need to close the document to save data
contentStream.close();
//this is for saving your PDF you can save with new name
//or you can replace existing one by giving same name
pdDocument.save(resourceBundle.getString("F:\PDF\lokiTheKing.pdf"));
如果我想加两行呢?e、 g.contentStream.Newlineatofset(200685);contentStream.showText(“约翰”);contentStream.newlineatofset(200785);contentStream.showText(“Doe”)?我有时间做beginText()和endText()吗?如果我这样做是不行的:contentStream.beginText()contentStream.newlineatofset(200685);contentStream.showText(“约翰”)contentStream.newlineatofset(250665);contentStream.showText(“1 2 3 4 5 6 7 8 9 1 0”)contentStream.endText()对不起,这些值是相对的。所以对于您来说,它应该是contentStream.newlineatofset(0,-20)。其他可能性:查看源代码下载中的EmbeddedFonts.java示例。顺便说一句,“它不工作”不是很具体。我只能猜测你的意思是“第二行没有出现”。