B4J Question Convert PDF to JPG using java and b4j

Lucas Siqueira

Active Member
Licensed User
Longtime User
Using b4j we can convert pdf documents to jpg

download jar https://www.apache.org/dyn/closer.lua/pdfbox/2.0.26/pdfbox-app-2.0.26.jar

b4j:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

#AdditionalJar: pdfbox-app-2.0.26.jar
' download https://www.apache.org/dyn/closer.lua/pdfbox/2.0.26/pdfbox-app-2.0.26.jar

Sub Process_Globals
    Private jo As JavaObject  
End Sub

Sub AppStart (Args() As String)
    Dim n As Long = DateTime.Now
    Log("start: " & DateTime.Time(DateTime.Now))
  
    jo = Me
  
    Dim pathPDF As String = "C:\Users\usuario\Downloads\B4J - PDF to Image\Files\Roadmap+Frontend.pdf"
    Dim exportFolderPDF As String = "C:\Users\usuario\Downloads\B4J - PDF to Image\Files"
  
    jo.RunMethod("convertPDFToImages", Array As Object(pathPDF, exportFolderPDF))
  
    Log("end: " & DateTime.Time(DateTime.Now))
    Log("Loading: " & (DateTime.Now - n) & "ms")
End Sub

#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);
        }
    }
}
#End If
 

Attachments

  • convert_pdf_to_jpg.zip
    1.4 KB · Views: 86
Last edited:
Solution
Thank you for your help!

I redid your examples.

download jar https://www.apache.org/dyn/closer.lua/pdfbox/2.0.26/pdfbox-app-2.0.26.jar


b4j:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

#AdditionalJar: pdfbox-app-2.0.26.jar
' download https://www.apache.org/dyn/closer.lua/pdfbox/2.0.26/pdfbox-app-2.0.26.jar

Sub Process_Globals
    Private jo As JavaObject   
End Sub

Sub AppStart (Args() As String)
    Dim n As Long = DateTime.Now
    Log("start: " & DateTime.Time(DateTime.Now))
    
    jo = Me
    
    Dim pathPDF As String = "C:\Users\usuario\Downloads\B4J - PDF to Image\Files\Roadmap+Frontend.pdf"
    Dim exportFolderPDF...

drgottjr

Expert
Licensed User
Longtime User
this is only part you need:

B4X:
    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);
            }
        }
    }



delete the rest. (don't forget the trailing "}" at the end of the class. it's easy to miss).

you can access convertPDFToImages(String pdfPath, String outputFolder) with a javaobject.
you can pass the input path and the output folder that you want.
 
Upvote 0

Lucas Siqueira

Active Member
Licensed User
Longtime User
Thank you for your help!

I redid your examples.

download jar https://www.apache.org/dyn/closer.lua/pdfbox/2.0.26/pdfbox-app-2.0.26.jar


b4j:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

#AdditionalJar: pdfbox-app-2.0.26.jar
' download https://www.apache.org/dyn/closer.lua/pdfbox/2.0.26/pdfbox-app-2.0.26.jar

Sub Process_Globals
    Private jo As JavaObject   
End Sub

Sub AppStart (Args() As String)
    Dim n As Long = DateTime.Now
    Log("start: " & DateTime.Time(DateTime.Now))
    
    jo = Me
    
    Dim pathPDF As String = "C:\Users\usuario\Downloads\B4J - PDF to Image\Files\Roadmap+Frontend.pdf"
    Dim exportFolderPDF As String = "C:\Users\usuario\Downloads\B4J - PDF to Image\Files"
    
    jo.RunMethod("convertPDFToImages", Array As Object(pathPDF, exportFolderPDF))
    
    Log("end: " & DateTime.Time(DateTime.Now))
    Log("Loading: " & (DateTime.Now - n) & "ms")
End Sub

#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);
        }
    }
}
#End If
 

Attachments

  • convert_pdf_to_jpg.zip
    1.4 KB · Views: 81
Upvote 1
Solution
Top