PDF Libraries: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
(Created page with "==References== {| | valign="top" | * Flying Saucer iText PDF Renderer * Microsoft Document to PDF * TeXlipse Config for MacTex * Apache POI * FastReport |...")
 
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
==Apache PDFBox==
<source lang="xml">
<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.24</version>
</dependency>
</source>
<source lang="java">
@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);
}
</source>
==References==
==References==
{|
{|
Line 5: Line 61:
* [[Microsoft Document to PDF]]
* [[Microsoft Document to PDF]]
* [[TeXlipse Config for MacTex]]
* [[TeXlipse Config for MacTex]]
* [https://stackoverflow.com/questions/3585329/ Merge multiple PDF Files]
* [https://ironpdf.com/java/ IronPDF for Java]
* [https://pdfbox.apache.org/ Apache PDFBox]
* [[Apache POI]]
* [[Apache POI]]
* [[FastReport]]
* [[FastReport]]

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