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.

  1. Create an empty Document to hold the output PDF.
  2. Add a Page and place the BMP with page.addImage(...).
  3. Define the target image rectangle with Rectangle so the raster content fills the PDF page area.
  4. 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.

  1. Open the CGM source by passing the file path and CgmLoadOptions into the Document constructor.
  2. Let Aspose.PDF interpret the CGM graphics stream during document loading.
  3. 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.

  1. Create an empty Document for the PDF output.
  2. Create an Image object, set its ImageFileType to Dicom, and assign the source file path.
  3. Add a Page and append the DICOM image to the page paragraphs collection.
  4. 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.

  1. Create an empty Document and open the EMF source as a binary stream.
  2. Add a Page and clear its margins so the EMF artwork can occupy the full page area.
  3. Create an Image, bind the EMF stream to it, and add it to the page paragraphs collection.
  4. 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.

  1. Load the EMF source with Aspose.Imaging and render it to an in-memory PNG stream before PDF placement.
  2. Create an empty Document and add a Page.
  3. Create an Image from the intermediate byte stream and add it to the page.
  4. 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.

  1. Create an empty Document for the PDF output.
  2. Add a Page and place the GIF with page.addImage(...).
  3. Define the placement bounds with Rectangle so the image fills the page area.
  4. 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.

  1. Create an empty Document for the output PDF.
  2. Add a Page and insert the JPEG image with page.addImage(...).
  3. Use Rectangle to control how the raster image is mapped to page coordinates.
  4. 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.

  1. Create an empty Document for the conversion output.
  2. Add a Page and place the PNG image on it with page.addImage(...).
  3. Use Rectangle to size the image against the page canvas.
  4. 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.

  1. Open the SVG source by passing the file path and SvgLoadOptions into the Document constructor.
  2. Let Aspose.PDF parse the SVG markup and create the corresponding PDF graphics model during loading.
  3. 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.

  1. Create an empty Document for the PDF output.
  2. Add a Page and place the TIFF image with page.addImage(...).
  3. Define the placement area with Rectangle so the TIFF content is mapped to page coordinates.
  4. 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

  1. Open the CDR source by passing the file path and CdrLoadOptions into the Document constructor. =======
  2. Open the CDR source by passing the file path and CdrLoadOptions into the Document constructor.

fix-java-docs

  1. Let Aspose.PDF load the CorelDRAW content into the PDF document model.
  2. 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);
}