在 PDFBox 2.0 中使用叠加 [英] Using Overlay in PDFBox 2.0

查看:66
本文介绍了在 PDFBox 2.0 中使用叠加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里要做的是创建文本并将其放置在空白页面上.然后将该页面叠加到另一个文档上,然后将其另存为一个文档.在 1.8 中,我能够在 PDF 中创建一个空白的 PDPage,根据需要向其中写入文本,然后将该 PDF 与另一个重叠,然后使用以下代码在屏幕上保存或查看 -

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 -

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");
}

创建流后,当我需要向覆盖文档的 contentStream 写入一些内容时,我会调用此方法,给它我的 x、y 坐标并告诉它要写入的文本(同样,这是在我的 1.8 版本中)):

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):

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 现在包含一个 PDDocument,它是我的原始 PDF,在需要的地方覆盖了文本.我可以保存它并在桌面上以 BufferedImage 的形式查看它.我迁移到 2.0 的原因是,首先我需要掌握最新的库,而且我在将图像放到页面上时遇到了问题(请参阅 此处).

finalDoc now contains a PDDocument which is my original PDF with text overlayed where needed. I could save it and view it as a BufferedImage on the desktop. 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 不再具有类似于 org.apache.pdfbox.Overlay 类的内容.更让我困惑的是 1.8 中有两个 Overlay 类(org.apache.pdfbox.Overlayorg.apache.pdfbox.util.Overlay) 而在 2.0 中只有一个.据我所知,我需要的类(org.apache.pdfbox.Overlay)或它至少提供的方法在 2.0 中不存在.我只能找到 org.apache.pdfbox.multipdf.Overlay.

The issue I am having in this question is that 2.0 no longer has something similar to the org.apache.pdfbox.Overlay class. 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. 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. I can only find 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:

  • 声明变量
  • 关闭内容流
  • 设置颜色
  • 设为前景
  • 设置文本位置(不是笔画路径位置)
  • 添加一个空地图

当然,我阅读了 OverlayPDF 源代码,它展示了您可以用该类做什么的更多可能性.

And of course, I read the OverlayPDF source code, it shows more possibilities what you can do with the class.

奖励内容:

在不使用 Overlay 类的情况下执行相同操作,该类允许在保存文档之前进一步操作.

Do the same without using the Overlay class, which allows further manipulation of the document before saving it.

    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();

这篇关于在 PDFBox 2.0 中使用叠加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆