我试图将 PDF 覆盖在 PDF 中所有页面的顶部,即每个页面的左上角。PDF 的大小不同。PDF 叠加层的大小是恒定的,小于 PDF 的所有页面。
我似乎只能让 PDFBox 将覆盖层放在 PDF 的中间。
我不想将 PDF 覆盖转换为位图 (PDImageXObject) 并将其插入到页面上。这是我正在使用的一些粗略代码:-
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 …Run Code Online (Sandbox Code Playgroud) 我试图让 JODConverter 在带有 Jdk 1.8.0_144 的 Window 10 上工作。正如您从代码中看到的,我认为这可能是一个时间问题,因此延迟。如您所见,JODConverter 认为 OfficeManager 正在运行。我正在使用以下代码:
import java.io.File;
import org.jodconverter.JodConverter;
import org.jodconverter.office.LocalOfficeManager;
import org.jodconverter.office.OfficeException;
import org.jodconverter.office.OfficeManager;
import org.jodconverter.office.OfficeUtils;
import org.jodconverter.process.ProcessManager;
public class JodConverterTest {
public static void main(String[] args) throws OfficeException, InterruptedException {
OfficeManager officeManager
= LocalOfficeManager.builder()
.officeHome("C:\\Program Files\\LibreOffice")
.portNumbers(2372)
.build();
officeManager.start();
File inputFile = new File("c:\\test\\rtf.rtf");
File outputFile = new File("c:\\test\\rtf.pdf");
try {
System.out.println("officeManager.isRunning()="+officeManager.isRunning());
Thread.sleep(10000);
System.out.println("officeManager.isRunning()="+officeManager.isRunning());
JodConverter.convert(inputFile).to(outputFile).execute();
} finally {
// Stop the office process
OfficeUtils.stopQuietly(officeManager);
}
}
}
Run Code Online (Sandbox Code Playgroud)
运行时出现以下错误:-
officeManager.isRunning()=true
officeManager.isRunning()=true
Exception …Run Code Online (Sandbox Code Playgroud) 我希望获得 PDF 中每个页面的准确大小,作为我将创建的 PDF 单元测试的一部分。当我处理每个文档中有许多不同页面大小的 PDF 时,代码返回一个维度的 ArrayList。
AFAIK 每个页面也可以有自己的 DPI 设置。
我已经做了相当多的谷歌搜索,但我只想到了这个,它只给了我部分答案,因为我仍然需要计算出每个页面的 DPI 是多少。
public static ArrayList<float[]> getDimentions(PDDocument document) {
ArrayList<float[]> dimensions = new ArrayList<>();
float[] dim = new float[2];
//Loop Round Each Page
//Get Dimensions of each page and DPI
for (int i = 0; i < document.getNumberOfPages(); i++) {
PDPage currentPage = document.getPage(i);
PDRectangle mediaBox = currentPage.getMediaBox();
float height = mediaBox.getHeight();
float width = mediaBox.getWidth();
// How do I get the DPI now????
} …Run Code Online (Sandbox Code Playgroud)