Convert Image Formats to PDF in Java
Aspose.PDF for Java can convert many raster and vector image formats into PDF documents.
Convert BMP to PDF
Use this example when a BMP image should be placed into a PDF document.
- Create an empty
Documentto hold the output PDF. - Add a
Pageand place the BMP withpage.addImage(...). - Define the target image rectangle with
Rectangleso the raster content fills the PDF page area. - Save the output PDF file.
public static void convertBmpToPdf(Path inputFile, Path outputFile) {
try (Document document = new Document()) {
try (Page page = document.getPages().add()) {
page.addImage(inputFile.toString(), new Rectangle(0, 0, 595, 842, true));
}
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert CGM to PDF
Use this example when a CGM graphics file should be converted into PDF.
- Open the CGM source by passing the file path and
CgmLoadOptionsinto theDocumentconstructor. - Let Aspose.PDF interpret the CGM graphics stream during document loading.
- Save the converted PDF to the target output path.
public static void convertCgmToPdf(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString(), new CgmLoadOptions())) {
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert DICOM to PDF
Use this example when a medical DICOM image should be wrapped into a PDF document.
- Create an empty
Documentfor the PDF output. - Create an
Imageobject, set itsImageFileTypetoDicom, and assign the source file path. - Add a
Pageand append the DICOM image to the page paragraphs collection. - Save the result as PDF.
public static void convertDicomToPdf(Path inputFile, Path outputFile) {
try (Document document = new Document()) {
Image image = new Image();
image.setFileType(ImageFileType.Dicom);
image.setFile(inputFile.toString());
try (Page page = document.getPages().add()) {
page.getParagraphs().add(image);
}
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert EMF to PDF with direct document loading
Use this example when an EMF file should be converted to PDF through the primary EMF load path.
- Create an empty
Documentand open the EMF source as a binary stream. - Add a
Pageand clear its margins so the EMF artwork can occupy the full page area. - Create an
Image, bind the EMF stream to it, and add it to the page paragraphs collection. - Save the output PDF file.
public static void convertEmfToPdf01(Path inputFile, Path outputFile) throws IOException {
try (Document document = new Document();
FileInputStream imageStream = new FileInputStream(inputFile.toFile())) {
try (Page page = document.getPages().add()) {
page.getPageInfo().getMargin().setBottom(0);
page.getPageInfo().getMargin().setTop(0);
page.getPageInfo().getMargin().setLeft(0);
page.getPageInfo().getMargin().setRight(0);
Image image = new Image();
image.setFileType(ImageFileType.Unknown);
image.setImageStream(imageStream);
page.getParagraphs().add(image);
}
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert EMF to PDF with an alternative workflow
Use this example when the EMF content should be converted using an alternative setup or page composition flow.
- Load the EMF source with Aspose.Imaging and render it to an in-memory PNG stream before PDF placement.
- Create an empty
Documentand add aPage. - Create an
Imagefrom the intermediate byte stream and add it to the page. - Save the converted PDF.
public static void convertEmfToPdf02(Path inputFile, Path outputFile) throws IOException {
try (Document document = new Document();
com.aspose.imaging.Image emfImage = com.aspose.imaging.Image.load(inputFile.toString());
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
emfImage.save(byteArrayOutputStream, new PngOptions());
try (Page page = document.getPages().add()) {
Image image = new Image();
image.setImageStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
page.getParagraphs().add(image);
}
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert GIF to PDF
Use this example when a GIF image should be added to a PDF page.
- Create an empty
Documentfor the PDF output. - Add a
Pageand place the GIF withpage.addImage(...). - Define the placement bounds with
Rectangleso the image fills the page area. - Save the output PDF.
public static void convertGifToPdf(Path inputFile, Path outputFile) {
try (Document document = new Document()) {
try (Page page = document.getPages().add()) {
page.addImage(inputFile.toString(), new Rectangle(0, 0, 595, 842, true));
}
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert JPEG to PDF
Use this example when a JPEG image should be converted into a one-page PDF.
- Create an empty
Documentfor the output PDF. - Add a
Pageand insert the JPEG image withpage.addImage(...). - Use
Rectangleto control how the raster image is mapped to page coordinates. - Save the generated PDF file.
public static void convertJpegToPdf(Path inputFile, Path outputFile) {
try (Document document = new Document()) {
try (Page page = document.getPages().add()) {
page.addImage(inputFile.toString(), new Rectangle(0, 0, 595, 842, true));
}
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert PNG to PDF
Use this example when a PNG image should be wrapped into a PDF document.
- Create an empty
Documentfor the conversion output. - Add a
Pageand place the PNG image on it withpage.addImage(...). - Use
Rectangleto size the image against the page canvas. - Save the output file.
public static void convertPngToPdf(Path inputFile, Path outputFile) {
try (Document document = new Document()) {
try (Page page = document.getPages().add()) {
page.addImage(inputFile.toString(), new Rectangle(0, 0, 595, 842, true));
}
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert SVG to PDF
Use this example when SVG artwork should be rendered inside a PDF document.
- Open the SVG source by passing the file path and
SvgLoadOptionsinto theDocumentconstructor. - Let Aspose.PDF parse the SVG markup and create the corresponding PDF graphics model during loading.
- Save the PDF output to the target file path.
public static void convertSvgToPdf(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString(), new SvgLoadOptions())) {
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert TIFF to PDF
Use this example when a TIFF image should be converted into PDF.
- Create an empty
Documentfor the PDF output. - Add a
Pageand place the TIFF image withpage.addImage(...). - Define the placement area with
Rectangleso the TIFF content is mapped to page coordinates. - Save the result as PDF.
public static void convertTiffToPdf(Path inputFile, Path outputFile) {
try (Document document = new Document()) {
try (Page page = document.getPages().add()) {
page.addImage(inputFile.toString(), new Rectangle(0, 0, 595, 842, true));
}
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert CDR to PDF
Use this example when a CorelDRAW CDR file should be converted into PDF.
«««< HEAD
- Open the CDR source by passing the file path and
CdrLoadOptionsinto theDocumentconstructor. ======= - Open the CDR source by passing the file path and
CdrLoadOptionsinto theDocumentconstructor.
fix-java-docs
- Let Aspose.PDF load the CorelDRAW content into the PDF document model.
- Save the converted PDF file to the requested output path.
public static void convertCdrToPdf(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString(), new CdrLoadOptions())) {
document.save(outputFile.toString());
}
System.out.println(inputFile + " converted into " + outputFile);
}