PdfBox 2.0.0 在页面给定位置写入文本
PdfBox 2.0.0 write text at given postion in a page
我刚刚从 PdfBox 1.8 升级到 2.0.0,两者之间存在相当大的差异。在现有的 pdf 页面上写文本之前,我使用了 drawString。在 2.0.0 中,绘图字符串已弃用,但 showText 在块文本中不起作用。
我在 1.8 中的代码:
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
就是划线。你想要的是newLineAtOffset
(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"));
我刚刚从 PdfBox 1.8 升级到 2.0.0,两者之间存在相当大的差异。在现有的 pdf 页面上写文本之前,我使用了 drawString。在 2.0.0 中,绘图字符串已弃用,但 showText 在块文本中不起作用。
我在 1.8 中的代码:
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
就是划线。你想要的是newLineAtOffset
(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"));