【问题标题】:PdfBox 2.0.0 write text at given postion in a pagePdfBox 2.0.0 在页面中的给定位置写入文本
【发布时间】:2019-01-16 12:42:22
【问题描述】:

我刚刚从 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 中编写文本

【问题讨论】:

    标签: java pdfbox


    【解决方案1】:

    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!
    

    【讨论】:

    • 如果我想添加两行怎么办?例如contentStream.newLineAtOffset(200,685); contentStream.showText("约翰"); contentStream.newLineAtOffset(200,785); contentStream.showText("Doe") ?我有时间做 beginText() 和 endText() 吗?
    • 如果我这样做是不行的:contentStream.beginText() contentStream.newLineAtOffset(200,685); contentStream.showText("John") contentStream.newLineAtOffset(250, 665); contentStream.showText("1 2 3 4 5 6 7 8 9 1 0") contentStream.endText()
    • 对不起,这些值是相对的。所以对你来说,它将是 contentStream.newLineAtOffset(0, -20); .其他可能性:查看源代码下载中的 EmbeddedFonts.java 示例。顺便说一句,“它不起作用”不是很具体。我只能猜你的意思是“第二行没有出现”。
    【解决方案2】:

    如果您正在寻找在 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"));
    

    【讨论】:

    • 你不需要这样做。您可以使用 newLineAtOffset,它只是相对于最后一个偏移量。请参阅@Tilman Hausherr 并在上面发表评论。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-30
    • 1970-01-01
    • 2011-07-08
    • 2019-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多