B4J Question Merge PDF (SOLVED)

LGS

Member
Licensed User
Longtime User
Hello everyone
I'm looking to merge 2 pdf files and found this on the forum
https://www.b4x.com/android/forum/threads/looking-to-merge-pdf.124916/post-780020

I have tried to do what they indicate here:
https://www.tutorialspoint.com/pdfbox/pdfbox_merging_multiple_pdf_documents.htm

But some PDFBox functions are deprecated
So I am using PDFBox 3.0.1 (requires Java 8)
https://pdfbox.apache.org/download.html

But I couldn't even compile the example
I am attaching an example of what I am trying to compile, it is probably something simple, but I can't see it.

Thanks in advance
 

Attachments

  • MergePdf.zip
    1.1 KB · Views: 274
Solution
Hello everyone
I'm looking to merge 2 pdf files and found this on the forum
https://www.b4x.com/android/forum/threads/looking-to-merge-pdf.124916/post-780020

I have tried to do what they indicate here:
https://www.tutorialspoint.com/pdfbox/pdfbox_merging_multiple_pdf_documents.htm

But some PDFBox functions are deprecated
So I am using PDFBox 3.0.1 (requires Java 8)
https://pdfbox.apache.org/download.html

But I couldn't even compile the example
I am attaching an example of what I am trying to compile, it is probably something simple, but I can't see it.

Thanks in advance
Try this code. it works in PDFBox 3.0.1
B4X:
Sub AppStart (Args() As String)
    Log("PDFBox")
    Me.as(JavaObject).RunMethod("mulFile2One"...

teddybear

Well-Known Member
Licensed User
Hello everyone
I'm looking to merge 2 pdf files and found this on the forum
https://www.b4x.com/android/forum/threads/looking-to-merge-pdf.124916/post-780020

I have tried to do what they indicate here:
https://www.tutorialspoint.com/pdfbox/pdfbox_merging_multiple_pdf_documents.htm

But some PDFBox functions are deprecated
So I am using PDFBox 3.0.1 (requires Java 8)
https://pdfbox.apache.org/download.html

But I couldn't even compile the example
I am attaching an example of what I am trying to compile, it is probably something simple, but I can't see it.

Thanks in advance
Try this code. it works in PDFBox 3.0.1
B4X:
Sub AppStart (Args() As String)
    Log("PDFBox")
    Me.as(JavaObject).RunMethod("mulFile2One", Null)
End Sub
#if java
import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.apache.pdfbox.io.RandomAccessStreamCache;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public static void mulFile2One() throws IOException{
        PDFMergerUtility mergePdf = new PDFMergerUtility();
        mergePdf.addSource(new File("D:\\test\\f1.pdf"));
        mergePdf.addSource(new File("D:\\test\\f2.pdf"));
        mergePdf.setDestinationFileName("D:\\test\\mul2one.pdf");
        RandomAccessStreamCache.StreamCacheCreateFunction streamCache
        = IOUtils.createMemoryOnlyStreamCache();
        mergePdf.mergeDocuments(streamCache);
    }
#End If
 
Upvote 0
Solution

LGS

Member
Licensed User
Longtime User
Dear teddybear
Once again, thank you for your time and for sharing your knowledge with this community.

Blessings ?
 
Upvote 0

AHilton

Active Member
Licensed User
Longtime User
And how would the following be translated into the new PDFBox-app 3 requirements?

B4X:
Public Sub MergePDFs(lstSourceFiles As List, CombinedFile As String) As Boolean
    Dim ret As Boolean
    Try
    Dim joMemUsage As JavaObject
        Dim jo As JavaObject
        jo.InitializeNewInstance("org.apache.pdfbox.multipdf.PDFMergerUtility", Null)
        For Each s As String In lstSourceFiles
            jo.RunMethod("addSource", Array(s))
        Next
        jo.RunMethod("setDestinationFileName", Array(CombinedFile))
        jo.RunMethod("mergeDocuments", Null)
        ret = True
    Catch
        Log(LastException)
        ret = False
    End Try
    Return ret
End Sub

Nevermind. I think the following fixes it...

B4X:
Public Sub MergePDFs(lstSourceFiles As List, CombinedFile As String) As Boolean
    Dim ret As Boolean
    Try
    Dim joMemUsage As JavaObject

        Dim joIOUtils As JavaObject
        joIOUtils.InitializeStatic("org.apache.pdfbox.io.IOUtils")
        Dim joRASC As JavaObject
        joRASC.InitializeStatic("org.apache.pdfbox.io.RandomAccessStreamCache")
        Dim jo As JavaObject
        jo.InitializeNewInstance("org.apache.pdfbox.multipdf.PDFMergerUtility", Null)
        For Each s As String In lstSourceFiles
            jo.RunMethod("addSource", Array(s))
        Next
        jo.RunMethod("setDestinationFileName", Array(CombinedFile))
        jo.RunMethod("mergeDocuments", Array(joIOUtils.RunMethod("createMemoryOnlyStreamCache", Null)))
        ret = True
    Catch
        Log(LastException)
        ret = False
    End Try
    Return ret
End Sub
 
Last edited:
Upvote 0
Top