PDF Libraries: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
No edit summary
 
(One intermediate revision by the same user not shown)
Line 10: Line 10:
@BeforeEach
@BeforeEach
public void setUp() {
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"};
     String[] names = new String[]{"pdf_source_1.pdf", "pdf_source_2.pdf", "pdf_source_3.pdf"};
    mergeTargetPath = String.format(DESTINATION_FORMAT, System.getProperty(USER_HOME), "Merge");
    splitTargetPath = String.format(DESTINATION_FORMAT, System.getProperty(USER_HOME), "Split Merge");
     resourcePaths = Arrays.stream(names).map(name -> String.format(CLASSPATH_FORMAT, name)).collect(Collectors.toList());
     resourcePaths = Arrays.stream(names).map(name -> String.format(CLASSPATH_FORMAT, name)).collect(Collectors.toList());
}
}
Line 27: Line 28:
     });
     });


     merger.setDestinationFileName(destinationFilePath);
     merger.setDestinationFileName(mergeTargetPath);
     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(splitTargetPath);
    }
     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());

Latest revision as of 23:51, 5 December 2023

Apache PDFBox

<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.24</version>
</dependency>
@BeforeEach
public void setUp() {
    String[] names = new String[]{"pdf_source_1.pdf", "pdf_source_2.pdf", "pdf_source_3.pdf"};
    mergeTargetPath = String.format(DESTINATION_FORMAT, System.getProperty(USER_HOME), "Merge");
    splitTargetPath = String.format(DESTINATION_FORMAT, System.getProperty(USER_HOME), "Split Merge");
    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(mergeTargetPath);
    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(splitTargetPath);
    }
    Duration duration = Duration.between(startTime, LocalDateTime.now());
    LOG.info("Duration: {} Millis", duration.toMillis());
    Assertions.assertTrue(true);
}

References