Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@conholdate-gists
Created August 4, 2022 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conholdate-gists/f2ae4ede1eddadf46700ef4e1c1c9c42 to your computer and use it in GitHub Desktop.
Save conholdate-gists/f2ae4ede1eddadf46700ef4e1c1c9c42 to your computer and use it in GitHub Desktop.
Convert PNG to PDF using Java
public static void ConvertImageToPDF(String inputFileName, String outputFileName) throws Exception {
// Instantiate an instance of Document class
Document doc = new Document();
// Create an object of DocumentBuilder class to make it simple to add content to the document.
DocumentBuilder builder = new DocumentBuilder(doc);
// Load images from the disk using the appropriate reader.
// The file formats that can be loaded depends on the image readers available on the machine.
// Load the input image file by calling the createImageInputStream method and assign it to the object of ImageInputStream.
ImageInputStream iis = ImageIO.createImageInputStream(new File(inputFileName));
// Invoke the getImageReaders method that returns an Iterator containing all currently registered ImageReaders and assigns it to the object of ImageReader class.
ImageReader reader = ImageIO.getImageReaders(iis).next();
// Call the setInput method that sets the input source to use to the given ImageInputStream or other Object.
reader.setInput(iis, false);
// Get the number of frames in the image by calling the getNumImages method
int framesCount = reader.getNumImages(true);
// Loop through all frames.
for (int frameIdx = 0; frameIdx < framesCount; frameIdx++) {
// Insert a section break before each new page, in case of a multi-frame image.
if (frameIdx != 0)
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
// Select active frame and assign it to the object of BufferedImage class
BufferedImage image = reader.read(frameIdx);
// We want the size of the page to be the same as the size of the image.
// Convert pixels to points to size the page to the actual image size.
// Invoke the getPageSetup method to access current page setup and assign it to the object of the PageSetup class
PageSetup ps = builder.getPageSetup();
// Set the page height by calling the setPageWidth method
ps.setPageWidth(ConvertUtil.pixelToPoint(image.getWidth()));
// Invoke the setPageHeight method to set the width of the page
ps.setPageHeight(ConvertUtil.pixelToPoint(image.getHeight()));
// Insert the image into the document and position it at the top left corner of the page by calling the insertImage method.
builder.insertImage(
image,
RelativeHorizontalPosition.PAGE,
0,
RelativeVerticalPosition.PAGE,
0,
ps.getPageWidth(),
ps.getPageHeight(),
WrapType.NONE);
}
if (iis != null) {
iis.close();
reader.dispose();
}
// Save the file as a PDF file format by calling the save method
doc.save(outputFileName);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment