PDF Libraries

From Chorke Wiki
Revision as of 10:03, 5 December 2023 by Shahed (talk | contribs)
Jump to navigation Jump to search

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);
}

References