B4J Question PDF with pdfbox-app-2.0.26 vs pdfbox-app-3.0.4

T201016

Active Member
Licensed User
Longtime User
Hello everyone,
I am looking for the appropriate version "PDFBOX-APP-X.X.X"

I use for compilation B4J Version: 10.00, Java version: 8.

In my B4J project I use two Java functions: (DHQIPARSEPDF and Convertpdftoimages)
I am looking for the right version "PDFBOX-APP-X.X.X" so that I can do the correct compilation of the project.


If I add the 'PDFBOX-APP-2.0.26' version to the project,
I get a complication error for the "DHQIPARSEPDF" function (see Example 1):

Example 1:
#AdditionalJar: pdfbox-app-2.0.26

#if java
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.io.RandomAccessReadBufferedFile;
import org.apache.pdfbox.Loader;

import java.io.IOException;

public static String DHQIParsePDF(String inputPdfPath){

try {

PDDocument documents = Loader.loadPDF(new RandomAccessReadBufferedFile(inputPdfPath));

PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(documents);

documents.close(); // Important: Close the document
System.out.println("PDF converted to text successfully");

return text;

} catch (IOException e) {
System.err.println("Error during PDF processing: " + e.getMessage());
e.printStackTrace(); // Print the full stack trace For debugging
return "--";
}
}

#end if
Compilation of the generated Java code. Error
javac 1.8.0_441
src\b4j\html_editorwrapper\pdfmanager.java:11: error: cannot find symbol
import org.apache.pdfbox.io.RandomAccessReadBufferedFile;
^
symbol: class RandomAccessReadBufferedFile
location: package org.apache.pdfbox.io
Note: src\b4j\html_editorwrapper\dialogsfx.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error


If I exchange it for the version 'PDFBOX-APP-3.0.4' in the same project,
I get a complication error for "Convertpdftoimages" (see Example 2):

Compilation of the generated Java code. Error
B4J line: 59
End Sub
javac 1.8.0_441
src\b4j\html_editorwrapper\pdfmanager.java:1163: error: cannot find symbol
PDDocument document = PDDocument.load(new File(pdfPath));
^
symbol: method load(File)
location: class PDDocument

Example 2:
#AdditionalJar: pdfbox-app-3.0.4

#if java
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public static void convertPDFToImages(String pdfPath, String outputFolder) throws IOException {
try {
PDDocument document = PDDocument.load(new File(pdfPath));
PDFRenderer renderer = new PDFRenderer(document);

for (int pageNumber = 0; pageNumber < document.getNumberOfPages(); pageNumber++) {
BufferedImage image = renderer.renderImageWithDPI(pageNumber, 300); // DPI (dots per inch)

// Salva a imagem em formato JPG
File outputImage = new File(outputFolder + File.separator + "pagina_" + (pageNumber + 1) + ".jpg");
ImageIO.write(image, "jpg", outputImage);
}
} catch (Exception e) {
e.printStackTrace();

}
}
#End If

I will gladly accept every clue, thank you.
 

Attachments

  • Sample.zip
    1.9 KB · Views: 20
Last edited:

T201016

Active Member
Licensed User
Longtime User
For now, I used the alternative library "JPDFBOX" from the great product B4J 👌
Therefore, for everyone's news - none of the versions tested by PDFBOX -APP from 2.0.26 to 3.0.4 does not work for my both cases (#1). 👎

Example::
Public Sub ConvertPDFtoTEXT As Boolean
    Dim fc As FileChooser
    fc.Initialize
    Dim jTXT As String
    Dim jPDF As jPDFBoxDocument
    Dim FileName As String = fc.ShowOpen(MainFormBox)
  
    jPDF.Initialize(FileName)
  
    If FileName <> "" Then
        Try
            'Extracts all pages ( Optional Extract only a range of pages )
            jTXT = jPDF.ExtractText2(1, jPDF.NumberOfPages)
            FileName = File.GetName(FileName).ToLowerCase.Replace(".pdf",".txt")
            File.WriteString(fc.InitialDirectory, FileName, jTXT)
            jPDF.Close
        Catch
            Return False
        End Try
    Else
        Return False
    End If
    Return True
End Sub
 
Upvote 0
Top