在 Java 中将图像格式转换为 PDF
Contents
[
Hide
]
Aspose.PDF for Java 可以将多种光栅和矢量图像格式转换为 PDF 文档。
将 BMP 转换为 PDF
当需要将 BMP 图像放入 PDF 文档时,请使用此示例。
- 创建一个空的
Document用于保存输出 PDF。 - 添加一个
Page并将 BMP 放置在page.addImage(...)。 - 使用以下方式定义目标图像矩形
Rectangle因此光栅内容填满 PDF 页面区域。 - 保存输出 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 时,请使用此示例。
- 通过传递文件路径打开 CGM 源并
CgmLoadOptions进入Document构造函数。 - 让 Aspose.PDF 在文档加载期间解释 CGM 图形流。
- 将转换后的 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 文档时,请使用此示例。
- 创建一个空的
Document用于 PDF 输出。 - 创建一个
Image对象,设置它的ImageFileType到Dicom,并指定源文件路径。 - 添加一个
Page并将 DICOM 图像追加到页面段落集合中。 - 将结果保存为 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 时,请使用此示例。
- 创建一个空的
Document并将 EMF 源以二进制流打开。 - 添加一个
Page并清除其边距,使 EMF 艺术作品能够占据整个页面区域。 - 创建一个
Image, 将 EMF 流绑定到它,并将其添加到页面段落集合中。 - 保存输出 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 内容时,请使用此示例。
- 使用 Aspose.Imaging 加载 EMF 源文件,并在 PDF 放置之前将其渲染为内存中的 PNG 流。
- 创建一个空的
Document并添加一个Page。 - 创建一个
Image从中间字节流中提取并将其添加到页面。 - 保存已转换的 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 页面时使用此示例。
- 创建一个空的
Document用于 PDF 输出。 - 添加一个
Page并将 GIF 放置在page.addImage(...)。 - 使用以下方式定义放置边界
Rectangle所以图像填满页面区域。 - 保存输出的 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 时,请使用此示例。
- 创建一个空的
Document用于输出 PDF。 - 添加一个
Page并插入 JPEG 图像page.addImage(...)。 - 使用
Rectangle控制光栅图像如何映射到页面坐标。 - 保存生成的 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 文档。
- 创建一个空的
Document用于转换输出。 - 添加一个
Page并将 PNG 图像放置在其上page.addImage(...)。 - 使用
Rectangle根据页面画布对图像进行尺寸调整。 - 保存输出文件。
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 艺术作品时,请使用此示例。
- 通过传递文件路径打开 SVG 源并
SvgLoadOptions进入Document构造函数。 - 让 Aspose.PDF 在加载时解析 SVG 标记并创建相应的 PDF 图形模型。
- 将 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 时,请使用此示例。
- 创建一个空的
Document用于 PDF 输出。 - 添加一个
Page并将 TIFF 图像放置在page.addImage(...)。 - 定义放置区域的方式
Rectangle因此,TIFF 内容映射到页面坐标。 - 将结果保存为 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 时,请使用此示例。
- 通过传递文件路径打开 CDR 源并
CdrLoadOptions进入Document构造函数。 - 让 Aspose.PDF 将 CorelDRAW 内容加载到 PDF 文档模型中。
- 将已转换的 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);
}