Using pdfbox in java to overlay text onto previous

2019-05-24 21:14发布

I already have several PDF documents that have been created. What I am attempting to do is by using PDFBox. I need to put text into several places on these created documents but I do NOT want to modify the text that is within those areas. For instance, there may be a a section as follows -

NAME: ______________________________

I will put text into that area, but I need the underline to remain the same length. I believe the best solution would be to just create a textbox or similar that goes above the area so the line remains the same length.

In other words, I do not want to edit the text inline so it will remain the same length. I have no code for this as I am just attempting to understand the pdfbox package. I have been looking for examples online, but most of them just show how to create a document and not how to update a previously document. How do I do this?

标签: java pdfbox
3条回答
啃猪蹄的小仙女
2楼-- · 2019-05-24 21:24

I found the answer and wanted to share.

In the pdfbox package there is a class called Overlay.

    PDDocument pdfDocument = new Overlay();
    PDDocument final = pdfDocument.overlay(PDDocument firstDoc, PDDocument otherDoc);

firstDoc will be overlaid onto otherDoc. Easy peasy. I just didn't know where to look.

查看更多
一纸荒年 Trace。
3楼-- · 2019-05-24 21:25

In case you need concrete example of use, you can refer to OverlayPDF.java in the PDFBox reposity:

Overlay overlayer = new Overlay();
overlayer.setInputFile(inputFile);  //the file to be overlayed

PDDocument result = overlayer.overlay(overlayFile); //This will add overlays to a documents.
result.save(outputFilename);
result.close();
overlayer.close();  //close the input files AFTER saving the resulting file
查看更多
萌系小妹纸
4楼-- · 2019-05-24 21:44

If I understand you correctly, you want to underline text in an existing pdf document. You can try to use Java Itext, check this example and see if it helps.

http://tutorials.jenkov.com/java-itext/underline-strikethrough.html

查看更多
登录 后发表回答