PDF Libraries: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
Line 29: | Line 29: | ||
merger.setDestinationFileName(destinationFilePath); | merger.setDestinationFileName(destinationFilePath); | ||
merger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly()); | merger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly()); | ||
Duration duration = Duration.between(startTime, LocalDateTime.now()); | |||
LOG.info("Duration: {} Millis", duration.toMillis()); | |||
Assertions.assertTrue(true); | |||
} | |||
@Test | |||
public void splitMergeTest() throws IOException { | |||
LocalDateTime startTime = LocalDateTime.now(); | |||
try (PDDocument target = new PDDocument()) { | |||
resourcePaths.forEach(path -> { | |||
try { | |||
PDDocument source = PDDocument.load(new ClassPathResource(path).getInputStream()); | |||
target.addPage(source.getPage(0)); | |||
} catch (IOException e) { | |||
throw new RuntimeException(e); | |||
} | |||
}); | |||
target.save(splitMergeTarget); | |||
} | |||
Duration duration = Duration.between(startTime, LocalDateTime.now()); | Duration duration = Duration.between(startTime, LocalDateTime.now()); | ||
LOG.info("Duration: {} Millis", duration.toMillis()); | LOG.info("Duration: {} Millis", duration.toMillis()); |
Revision as of 23:47, 5 December 2023
Apache PDFBox
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.24</version>
</dependency>
@BeforeEach
public void setUp() {
destinationFilePath = String.format(DESTINATION_FORMAT, System.getProperty(USER_HOME));
String[] names = new String[]{"pdf_source_1.pdf", "pdf_source_2.pdf", "pdf_source_3.pdf"};
resourcePaths = Arrays.stream(names).map(name -> String.format(CLASSPATH_FORMAT, name)).collect(Collectors.toList());
}
@Test
public void mergeTest() throws IOException {
LocalDateTime startTime = LocalDateTime.now();
PDFMergerUtility merger = new PDFMergerUtility();
resourcePaths.forEach(path -> {
try {
merger.addSource(new ClassPathResource(path).getInputStream());
} catch (IOException e) {
throw new RuntimeException(e);
}
});
merger.setDestinationFileName(destinationFilePath);
merger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
Duration duration = Duration.between(startTime, LocalDateTime.now());
LOG.info("Duration: {} Millis", duration.toMillis());
Assertions.assertTrue(true);
}
@Test
public void splitMergeTest() throws IOException {
LocalDateTime startTime = LocalDateTime.now();
try (PDDocument target = new PDDocument()) {
resourcePaths.forEach(path -> {
try {
PDDocument source = PDDocument.load(new ClassPathResource(path).getInputStream());
target.addPage(source.getPage(0));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
target.save(splitMergeTarget);
}
Duration duration = Duration.between(startTime, LocalDateTime.now());
LOG.info("Duration: {} Millis", duration.toMillis());
Assertions.assertTrue(true);
}
References
| ||