简体   繁体   English

在 PDFBox 2.0 中使用叠加

[英]Using Overlay in PDFBox 2.0

What I am trying to do here is to create text and place it onto a blank page.我在这里要做的是创建文本并将其放置在空白页面上。 That page would then be overlayed onto another document and that would then be saved as one document.然后将该页面叠加到另一个文档上,然后将其另存为一个文档。 In 1.8 I was able to create a blank PDPage in a PDF, write text to it as needed, then overlay that PDF with another and then save or view on screen using the code below -在 1.8 中,我能够在 PDF 中创建一个空白的 PDPage,根据需要向其中写入文本,然后将该 PDF 与另一个重叠,然后使用以下代码在屏幕上保存或查看 -

overlayDoc = new PDDocument();
page = new PDPage();
overlayDoc.addPage(page);
overlayObj = new Overlay();
font = PDType1Font.COURIER_OBLIQUE;
try {
    contentStream = new PDPageContentStream(overlayDoc, page);
    contentStream.setFont(font, 10);
}
catch (Exception e){
    System.out.println("content stream failed");
}

After I created the stream, when I needed to write something to the overlay document's contentStream, I would call this method, give it my x, y coords and tell it what text to write (again, this is in my 1.8 version):创建流后,当我需要向覆盖文档的 contentStream 写入一些内容时,我会调用此方法,为其提供我的 x、y 坐标并告诉它要写入的文本(同样,这是在我的 1.8 版本中):

protected void writeString(int x, int y, String text) {
    if (text == null) return;
    try {
        contentStream.moveTo(x, y);
        contentStream.beginText();
        contentStream.drawString(text);  // deprecated. Use showText(String text)
        contentStream.endText();
    }
    catch (Exception e){
        System.out.println(text + " failed. " + e.toString());
    }
}

I would call this method whenever I needed to add text and to wherever I needed to do so.每当我需要添加文本时,我都会调用此方法以及在我需要添加的任何位置。 After this, I would close my content stream and then merge the documents together as such:在此之后,我将关闭我的内容流,然后将文档合并在一起:

import org.apache.pdfbox.Overlay;
Overlay overlayObj = new Overlay();

....

PDDocument finalDoc = overlayObj.overlay(overlayDoc, originalDoc);

finalDoc now contains a PDDocument which is my original PDF with text overlayed where needed. finalDoc现在包含一个 PDDocument,它是我的原始 PDF,在需要的地方覆盖了文本。 I could save it and view it as a BufferedImage on the desktop.我可以保存它并在桌面上以 BufferedImage 的形式查看它。 The reason I moved to 2.0 was that first off I needed to stay on top of the most recent library and also that I was having issues putting an image onto the page (see here ).我迁移到 2.0 的原因是首先我需要保持在最新库的顶部,而且我在将图像放到页面上时遇到了问题(请参阅此处)。

The issue I am having in this question is that 2.0 no longer has something similar to the org.apache.pdfbox.Overlay class.我在这个问题中遇到的问题是 2.0 不再有类似于org.apache.pdfbox.Overlay类的东西。 To confuse me even more is that there are two Overlay classes in 1.8 ( org.apache.pdfbox.Overlay and org.apache.pdfbox.util.Overlay ) whereas in 2.0 there is only one.更让我困惑的是,1.8 中有两个 Overlay 类( org.apache.pdfbox.Overlayorg.apache.pdfbox.util.Overlay ),而在 2.0 中只有一个。 The class I need ( org.apache.pdfbox.Overlay ), or the methods it offers at least, are not present in 2.0 as far as I can tell.据我所知,我需要的类( org.apache.pdfbox.Overlay )或它至少提供的方法在 2.0 中不存在。 I can only find org.apache.pdfbox.multipdf.Overlay .我只能找到org.apache.pdfbox.multipdf.Overlay

Here's some quick code that works, it adds "deprecated" over a document and saves it elsewhere:这是一些有效的快速代码,它在文档上添加了“已弃用”并将其保存在其他地方:

    PDDocument overlayDoc = new PDDocument();
    PDPage page = new PDPage();
    overlayDoc.addPage(page);
    Overlay overlayObj = new Overlay();
    PDFont font = PDType1Font.COURIER_OBLIQUE;

    PDPageContentStream contentStream = new PDPageContentStream(overlayDoc, page);
    contentStream.setFont(font, 50);
    contentStream.setNonStrokingColor(0);
    contentStream.beginText();
    contentStream.moveTextPositionByAmount(200, 200);
    contentStream.drawString("deprecated");  // deprecated. Use showText(String text)
    contentStream.endText();
    contentStream.close();

    PDDocument originalDoc = PDDocument.load(new File("...inputfile.pdf"));
    overlayObj.setOverlayPosition(Overlay.Position.FOREGROUND);
    overlayObj.setInputPDF(originalDoc);
    overlayObj.setAllPagesOverlayPDF(overlayDoc);
    Map<Integer, String> ovmap = new HashMap<Integer, String>(); // empty map is a dummy
    overlayObj.setOutputFile("... result-with-overlay.pdf");
    overlayObj.overlay(ovmap);
    overlayDoc.close();
    originalDoc.close();

What I did additionally to your version:我对你的版本做了什么:

  • declare variables声明变量
  • close the content stream关闭内容流
  • set a color设置颜色
  • set to foreground设置为前台
  • set a text position (not a stroke path position)设置文本位置(不是笔画路径位置)
  • add an empty map添加空地图

And of course, I read the OverlayPDF source code , it shows more possibilities what you can do with the class.当然,我阅读了OverlayPDF 源代码,它展示了您可以用该类做什么的更多可能性。

Bonus content:奖金内容:

Do the same without using the Overlay class, which allows further manipulation of the document before saving it.在不使用 Overlay 类的情况下执行相同操作,它允许在保存文档之前进一步操作文档。

    PDFont font = PDType1Font.COURIER_OBLIQUE;
    PDDocument originalDoc = PDDocument.load(new File("...inputfile.pdf"));
    PDPage page1 = originalDoc.getPage(0);
    PDPageContentStream contentStream = new PDPageContentStream(originalDoc, page1, true, true, true);
    contentStream.setFont(font, 50);
    contentStream.setNonStrokingColor(0);
    contentStream.beginText();
    contentStream.moveTextPositionByAmount(200, 200);
    contentStream.drawString("deprecated");  // deprecated. Use showText(String text)
    contentStream.endText();
    contentStream.close();
    originalDoc.save("....result2.pdf");
    originalDoc.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM