Convert PDF to JPEG2000
JPedal provides several methods to allow the conversion of a PDF file or directory of PDF files into JPX. Java examples use the ConvertPagesToImages and Jpeg2000EncoderOptions classes.
Convert PDF to JPG2000 from Command Line or another language
Converting all pages to images
java -jar jpedal.jar --convert "inputFileOrFolder" "outputFolder" jpx
You can also provide different settings to adjust the conversion.
java -jar jpedal.jar --convert "inputFileOrFolder" "outputFolder" jpx ScalingAsFloat
java -jar jpedal.jar --convert "inputFileOrFolder" "outputFolder" jpx ScalingAsFloat PageRange
The use of the named variables is detailed below.
- ScalingAsFloat: A float value specifying the scaling applied to the output. This is the view scaling divided by 100, for instance 150% becomes 1.5
- PageRange: The range of pages of convert. This follows the same rules as SetOfIntegerSyntax.
This can be a series of comma separated values which are either single page number or range between to values using a dash. For example “1,3,4-7” would give pages 1,3,4,5,6,7
Convert PDF to JPG2000 in Java with convenience static method
ConvertPagesToImages.writeAllPagesAsImagesToDir(
"inputFileOrFolder",
"outputFolder",
"jpx",
1.33f);
Convert PDF to JPG2000 in Java with control over the image output
ConvertPagesToImages convert = new ConvertPagesToImages("inputFile.pdf");
try {
if (convert.openPDFFile()) {
for(int page = 1; page <= convert.getPageCount(); page++) {
final BufferedImage bi = convert.getPageAsImage(page);
final File out = new File("outputFolder" + page + ".jpx");
// Setters to control output (example with compression)
final Jpeg2000EncoderOptions options = new Jpeg2000EncoderOptions();
options.setQuality(90); //Default is 75. No compression is 100
options.setOutputSubtype(Jpeg2000OutputSubtype.JPX); //Default is JPX. JP2 also
JDeli.write(bi, options, out);
}
}
} catch (PdfException | IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
convert.closePDFfile();
Convert PDF to JPG2000 in Java with control over the page range of the output
ConvertPagesToImages convert = new ConvertPagesToImages("inputFile.pdf");
// setPageRange gives you the ability to chose the pages you'd like using '-' or ':' for range
// and ',' to move to the next range or you can simply put null for all the pages
convert.setPageRange(new PageRanges("1-5,8:10,15"));
// Above will give us pages 1 to 5(inclusive),8 to 10(inclusive) and 15
try {
if (convert.openPDFFile()) {
convert.getPageRange().forEachRemaining(page -> {
try {
final BufferedImage bi = convert.getPageAsImage(page);
final File out = new File("outputFolder" + page + ".jpx");
JDeli.write(bi, options, out);
} catch (Exception e) {
e.printStackTrace();
}
});
}
} catch (PdfException e) {
e.printStackTrace();
}
convert.closePDFfile();
Convert PDF to JPG2000 thumbnails in Java with control over output image dimensions
ConvertPagesToImages.writeAllPagesAsImagesToDir(
"inputFileOrFolder",
"outputFolder",
"jpx",
new int[] {width, height});
or
ConvertPagesToImages convert = new ConvertPagesToImages("inputFile.pdf");
//fit with aspect ratio preserved (width will be 300 or height will be 400)
convert.setFitToSize(new int[] {300,400});
try {
if (convert.openPDFFile()) {
for (int page = 1; page <= convert.getPageCount(); page++) {
final BufferedImage bi = convert.getPageAsImage(page);
final File out = new File("outputFolder" + page + ".jpx");
JDeli.write(bi, OutputFormat.JPEG2000, out);
}
}
} catch (PdfException | IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
convert.closePDFfile();
Convert PDF to JPG2000 thumbnails in Java with control over scaling
ConvertPagesToImages convert = new ConvertPagesToImages("inputFile.pdf");
convert.setPageScaling(1.33f); //which gives same size as Acrobat at 100%
try {
if (convert.openPDFFile()) {
for (int page = 1; page <= convert.getPageCount(); page++) {
final BufferedImage bi = convert.getPageAsImage(page);
final File out = new File("outputFolder" + page + ".jpx");
JDeli.write(bi, OutputFormat.JPEG2000, out);
}
}
} catch (PdfException | IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
convert.closePDFfile();
Convert PDF to JPG2000 in Java with a password-protected PDF file
ConvertPagesToImages convert = new ConvertPagesToImages("inputFile.pdf");
convert.setPassword("password");
try {
if (convert.openPDFFile()) {
for (int page = 1; page <= convert.getPageCount(); page++) {
final BufferedImage bi = convert.getPageAsImage(page);
final File out = new File("outputFolder" + page + ".jpx");
JDeli.write(bi, OutputFormat.JPEG2000, out);
}
}
} catch (PdfException | IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
convert.closePDFfile();
If you are looking for upscaling or more complex control over the PDF to JPX conversion, ConvertPagesToHiResImages class has lots of additional options