Using pdfbox in java to overlay text onto previously created pdf document

流过昼夜 提交于 2019-12-05 22:51:37
Rabbit Guy

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.

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!