在 Java 中将图像格式转换为 PDF

Aspose.PDF for Java 可以将多种光栅和矢量图像格式转换为 PDF 文档。

将 BMP 转换为 PDF

当需要将 BMP 图像放入 PDF 文档时,请使用此示例。

  1. 创建一个空的 Document 用于保存输出 PDF。
  2. 添加一个 Page 并将 BMP 放置在 page.addImage(...)。
  3. 使用以下方式定义目标图像矩形 Rectangle 因此光栅内容填满 PDF 页面区域。
  4. 保存输出 PDF 文件。
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);
    }

将 CGM 转换为 PDF

当需要将 CGM 图形文件转换为 PDF 时,请使用此示例。

  1. 通过传递文件路径打开 CGM 源并 CgmLoadOptions 进入 Document 构造函数。
  2. 让 Aspose.PDF 在文档加载期间解释 CGM 图形流。
  3. 将转换后的 PDF 保存到目标输出路径。
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);
}

将 DICOM 转换为 PDF

当需要将医学 DICOM 图像包装成 PDF 文档时,请使用此示例。

  1. 创建一个空的 Document 用于 PDF 输出。
  2. 创建一个 Image 对象,设置它的 ImageFileType 到 Dicom,并指定源文件路径。
  3. 添加一个 Page 并将 DICOM 图像追加到页面段落集合中。
  4. 将结果保存为 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);
}

将 EMF 转换为 PDF,直接加载文档

当需要通过主要的 EMF 加载路径将 EMF 文件转换为 PDF 时,请使用此示例。

  1. 创建一个空的 Document 并将 EMF 源以二进制流打开。
  2. 添加一个 Page 并清除其边距,使 EMF 艺术作品能够占据整个页面区域。
  3. 创建一个 Image, 将 EMF 流绑定到它,并将其添加到页面段落集合中。
  4. 保存输出 PDF 文件。
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);
}

使用替代工作流将 EMF 转换为 PDF

当需要使用替代设置或页面组合流程转换 EMF 内容时,请使用此示例。

  1. 使用 Aspose.Imaging 加载 EMF 源文件,并在 PDF 放置之前将其渲染为内存中的 PNG 流。
  2. 创建一个空的 Document 并添加一个 Page。
  3. 创建一个 Image 从中间字节流中提取并将其添加到页面。
  4. 保存已转换的 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);
}

将 GIF 转换为 PDF

在需要将 GIF 图像添加到 PDF 页面时使用此示例。

  1. 创建一个空的 Document 用于 PDF 输出。
  2. 添加一个 Page 并将 GIF 放置在 page.addImage(...)。
  3. 使用以下方式定义放置边界 Rectangle 所以图像填满页面区域。
  4. 保存输出的 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);
}

将 JPEG 转换为 PDF

当需要将 JPEG 图像转换为单页 PDF 时,请使用此示例。

  1. 创建一个空的 Document 用于输出 PDF。
  2. 添加一个 Page 并插入 JPEG 图像 page.addImage(...)。
  3. 使用 Rectangle 控制光栅图像如何映射到页面坐标。
  4. 保存生成的 PDF 文件。
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);
}

将 PNG 转换为 PDF

使用此示例将 PNG 图像包装成 PDF 文档。

  1. 创建一个空的 Document 用于转换输出。
  2. 添加一个 Page 并将 PNG 图像放置在其上 page.addImage(...)。
  3. 使用 Rectangle 根据页面画布对图像进行尺寸调整。
  4. 保存输出文件。
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);
}

将 SVG 转换为 PDF

当需要在 PDF 文档中渲染 SVG 艺术作品时,请使用此示例。

  1. 通过传递文件路径打开 SVG 源并 SvgLoadOptions 进入 Document 构造函数。
  2. 让 Aspose.PDF 在加载时解析 SVG 标记并创建相应的 PDF 图形模型。
  3. 将 PDF 输出保存到目标文件路径。
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);
}

将 TIFF 转换为 PDF

当需要将 TIFF 图像转换为 PDF 时,请使用此示例。

  1. 创建一个空的 Document 用于 PDF 输出。
  2. 添加一个 Page 并将 TIFF 图像放置在 page.addImage(...)。
  3. 定义放置区域的方式 Rectangle 因此,TIFF 内容映射到页面坐标。
  4. 将结果保存为 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);
}

将 CDR 转换为 PDF

当需要将 CorelDRAW CDR 文件转换为 PDF 时,请使用此示例。

  1. 通过传递文件路径打开 CDR 源并 CdrLoadOptions 进入 Document 构造函数。
  2. 让 Aspose.PDF 将 CorelDRAW 内容加载到 PDF 文档模型中。
  3. 将已转换的 PDF 文件保存到请求的输出路径。
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);
}