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
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
Last edited: