简体   繁体   English

使用 PDFBox 2.X 在所有页面上特定位置的所有页面上叠加 PDF

[英]Overlaying PDF on all Pages at specific location on all Pages using PDFBox 2.X

I'm trying to overlay a PDF on-top of all pages in a PDF, at the top left hand side of each page.我试图在每个页面的左上角将 PDF 叠加在 PDF 中所有页面的顶部。 The PDFs that will be of different sizes.不同大小的 PDF。 The PDF overlay is a constant size, which is smaller than all the pages of the PDF. PDF 叠加层的大小不变,小于 PDF 的所有页面。

I can only seem to get PDFBox to put the overlay in the middle of the PDFs.我似乎只能让 PDFBox 将叠加层放在 PDF 的中间。

I would prefer not to convert the PDF overlay to a bitmap (PDImageXObject) and insert it onto the pages.我不想将 PDF 叠加层转换为位图 (PDImageXObject) 并将其插入到页面上。 Here is some rough code which I'm playing about with:-这是我正在玩的一些粗略代码:-

public static void main(String[] args) throws Exception {
    String overlayPath = "C:\\OverlayPdf.pdf";
    String overlayOnMePath = "C:\\ToBeOverlayedOn.pdf";       
    PDDocument overlayOnMe = PDDocument.load(new File(overlayOnMePath)); //Document to write to.
    overlayPath = overlayPath + "Anno.pdf";

    HashMap<Integer, String> overlayGuide = new HashMap<>();
    for (int i = 0; i < overlayOnMe.getNumberOfPages(); i++) {
        overlayGuide.put(i + 1, overlayPath);
    }
    Overlay overlay = new Overlay();
    overlay.setInputPDF(overlayOnMe);
    overlay.setOverlayPosition(Overlay.Position.FOREGROUND);
    overlay.overlay(overlayGuide);

    overlayOnMe.save(new File(overlayOnMePath + "_OVERLAYED.pdf"));
    overlay.close();
}

My gut feeling is its an affine transformation but I couldn't get that working either.我的直觉是它是仿射变换,但我也无法让它发挥作用。

I have created a new issue and it allows to pass a transform, this will be in version 2.0.10 or higher.我创建了一个新问题,它允许通过转换,这将在 2.0.10 或更高版本中。 This will be done in calculateAffineTransform by extending the overlay class.这将通过扩展覆盖类在calculateAffineTransform中完成。 To put the stamp on the top left, the new method would look like this:要将图章放在左上角,新方法将如下所示:

protected AffineTransform calculateAffineTransform(PDPage page, PDRectangle overlayMediaBox)
{
    AffineTransform at = new AffineTransform();
    PDRectangle pageMediaBox = page.getMediaBox();
    at.translate(0, pageMediaBox.getHeight() - overlayMediaBox.getHeight());
    return at;
}

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

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