From 21102337f00703a7baf9a0406a909ed0607e79cb Mon Sep 17 00:00:00 2001 From: Sahara Yousuf Date: Thu, 15 Feb 2024 17:02:43 -0600 Subject: [PATCH] Add MergePDF Kotlin sample --- .github/workflows/test-kotlin-samples.yml | 2 +- MergePDF/.idea/runConfigurations/MergePDF.xml | 14 ++ MergePDF/pom.xml | 231 ++++++++++++++++++ .../com/datalogics/pdfl/samples/MergePDF.kt | 59 +++++ 4 files changed, 305 insertions(+), 1 deletion(-) create mode 100644 MergePDF/.idea/runConfigurations/MergePDF.xml create mode 100644 MergePDF/pom.xml create mode 100644 MergePDF/src/main/kotlin/com/datalogics/pdfl/samples/MergePDF.kt diff --git a/.github/workflows/test-kotlin-samples.yml b/.github/workflows/test-kotlin-samples.yml index 99d2962..7d4ade7 100644 --- a/.github/workflows/test-kotlin-samples.yml +++ b/.github/workflows/test-kotlin-samples.yml @@ -17,7 +17,7 @@ jobs: strategy: matrix: os: [windows-latest, ubuntu-latest] - dir: ['TextExtract', 'FlattenTransparency', 'SplitPDF', 'ConvertToOffice'] + dir: ['TextExtract', 'FlattenTransparency', 'SplitPDF', 'ConvertToOffice', 'MergePDF'] steps: - name: Checkout uses: actions/checkout@v4 diff --git a/MergePDF/.idea/runConfigurations/MergePDF.xml b/MergePDF/.idea/runConfigurations/MergePDF.xml new file mode 100644 index 0000000..054131c --- /dev/null +++ b/MergePDF/.idea/runConfigurations/MergePDF.xml @@ -0,0 +1,14 @@ + + + + + + + \ No newline at end of file diff --git a/MergePDF/pom.xml b/MergePDF/pom.xml new file mode 100644 index 0000000..730c47a --- /dev/null +++ b/MergePDF/pom.xml @@ -0,0 +1,231 @@ + + + 4.0.0 + com.datalogics.pdfl.samples + MergePDF + 1.0-SNAPSHOT + + + + mavenCentral + https://repo1.maven.org/maven2/ + + + + + UTF-8 + official + 1.8 + 1.8 + 1.8 + + + + + Windows64 + + + windows + amd64 + + + + win-x86-64-jni + + + + MacArm + + + mac + aarch64 + + + + mac-arm-64-jni + + + + Linux64 + + + + Linux + amd64 + + + + linux-x86-64-jni + + + + + + + org.jetbrains.kotlin + kotlin-stdlib-jdk8 + 1.9.21 + + + com.datalogics.pdfl + pdfl + 18.31.0 + pom + + + com.datalogics.pdfl + pdfl + 18.31.0 + + + com.datalogics.pdfl + pdfl + 18.31.0 + zip + ${jni.classifier} + + + com.datalogics.pdfl + pdfl + 18.31.0 + zip + resources + + + com.datalogics.pdfl + pdfl + 18.31.0 + javadoc + + + + + src/main/kotlin + + + org.jetbrains.kotlin + kotlin-maven-plugin + 1.9.21 + + + compile + compile + + compile + + + + + + maven-surefire-plugin + 2.22.2 + + + maven-failsafe-plugin + 2.22.2 + + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + MergePDF + + + + org.apache.maven.plugins + maven-dependency-plugin + + + unpack-resources + generate-resources + + unpack + + + + + com.datalogics.pdfl + pdfl + resources + zip + ${project.build.directory}/lib/Resources + + + + + + unpack-jni + generate-resources + + unpack + + + + + com.datalogics.pdfl + pdfl + ${jni.classifier} + zip + ${project.build.directory}/lib + + + + + + unpack-license + generate-resources + + unpack + + + + + com.datalogics.pdfl + pdfl + license + zip + ${project.build.directory}/lib + + + + + + + + maven-assembly-plugin + + + package + + single + + + + + + + true + com.datalogics.pdfl.samples.MergePDFKt + + + + jar-with-dependencies + + + + + + + + org.apache.maven.plugins + maven-dependency-plugin + 3.0.2 + + + + + + diff --git a/MergePDF/src/main/kotlin/com/datalogics/pdfl/samples/MergePDF.kt b/MergePDF/src/main/kotlin/com/datalogics/pdfl/samples/MergePDF.kt new file mode 100644 index 0000000..4d6a0f7 --- /dev/null +++ b/MergePDF/src/main/kotlin/com/datalogics/pdfl/samples/MergePDF.kt @@ -0,0 +1,59 @@ +package com.datalogics.pdfl.samples + +import com.datalogics.PDFL.* +import java.util.EnumSet + +/* + * + * This sample demonstrates merging one PDF document into another. The program + * offers two optional input documents and defines a default output document. The sample + * takes the content from the second PDF input document and inserts it in the first + * input document, and saves the result to the output PDF document. + * + * Copyright (c) 2024, Datalogics, Inc. All rights reserved. + * + */ +fun main(args: Array) { + println("MergePDF sample:") + + val lib = Library() + + var sInput1 = Library.getResourceDirectory() + "Sample_Input/merge_pdf1.pdf" + var sInput2 = Library.getResourceDirectory() + "Sample_Input/merge_pdf2.pdf" + var sOutput = "MergePDF-out.pdf" + + if (args.isNotEmpty()) + sInput1 = args[0] + if (args.size > 1) + sInput2 = args[1] + if (args.size > 2) + sOutput = args[2] + + println("Adding $sInput1 and $sInput2 and writing to $sOutput") + + val doc1 = Document(sInput1) + val doc2 = Document(sInput2) + + try { + doc1.insertPages( + Document.LAST_PAGE, doc2, 0, Document.ALL_PAGES, EnumSet.of( + PageInsertFlags.BOOKMARKS, PageInsertFlags.THREADS, + // For best performance processing large documents, set the following flags. + PageInsertFlags.DO_NOT_MERGE_FONTS, PageInsertFlags.DO_NOT_RESOLVE_INVALID_STRUCTURE_PARENT_REFERENCES, PageInsertFlags.DO_NOT_REMOVE_PAGE_INHERITANCE + ) + ) + } catch (ex: LibraryException) { + if (!ex.message!!.contains("An incorrect structure tree was found in the PDF file but operation continued")) { + throw ex + } + println(ex.message) + } + + // For best performance processing large documents, set the following flags. + doc1.save(EnumSet.of(SaveFlags.FULL, SaveFlags.SAVE_LINEARIZED_NO_OPTIMIZE_FONTS, SaveFlags.COMPRESSED), sOutput) + + doc1.close() + doc2.close() + + lib.delete() +}