PdfBox 2.0.0 在页面中的给定位置写入文本

Amr*_*a D 4 java pdfbox

我刚刚从 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()
Run Code Online (Sandbox Code Playgroud)

我在 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()
Run Code Online (Sandbox Code Playgroud)

但它不起作用......

任何人都知道我如何像 1.8 一样编写文本

Til*_*err 12

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!
Run Code Online (Sandbox Code Playgroud)

  • 抱歉,这些值是相对的。所以对你来说,它将是 contentStream.newLineAtOffset(0, -20); 。其他可能性:查看源代码下载中的 EmbeddedFonts.java 示例。顺便说一句,“它不起作用”不是很具体。我只能猜测你的意思是“第二行没有出现”。 (2认同)

v8-*_*8-E 5

如果您正在寻找在 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"));
Run Code Online (Sandbox Code Playgroud)