千家信息网

怎么使用Aspose.words在Java中将Word和图像转换为PDF

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,本篇内容主要讲解"怎么使用Aspose.words在Java中将Word和图像转换为PDF",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"怎么使用Aspos
千家信息网最后更新 2026年11月01日怎么使用Aspose.words在Java中将Word和图像转换为PDF

本篇内容主要讲解"怎么使用Aspose.words在Java中将Word和图像转换为PDF",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"怎么使用Aspose.words在Java中将Word和图像转换为PDF"吧!

将Word文档转换为PDF

从Word到PDF的转换是一个相当复杂的过程,需要几个计算阶段。Aspose.Words布局引擎模仿了Microsoft Word的页面布局引擎的工作方式,使PDF输出文档看起来与Microsoft Word中所看到的尽可能接近。使用Aspose.Words,您可以通过编程方式将文档从DOC或DOCX格式转换为PDF,而无需使用Microsoft Office。本文介绍了如何执行此转换。

请注意,文档中的页数会影响转换时间。

将DOC或DOCX转换为PDF

在Aspose.Words中从DOC或DOCX文档格式转换为PDF格式非常容易,只需两行代码即可完成:

  • 通过使用扩展名指定文档名称,使用其构造函数之一将文档加载到 Document对象中。

  • 调用Document对象上的Document.Save方法 之一,并通过输入扩展名为" .PDF"的文件名将所需的输出格式指定为PDF。

下面的代码示例演示如何使用Save方法将文档从DOCX转换为PDF:

// Load the document from disk.Document doc = new Document(dataDir + "Template.doc");// Save the document in PDF format.dataDir = dataDir + "output.pdf";doc.save(dataDir);

请注意,使用相同的技术,可以将任何流程布局格式的文档转换为PDF格式。

转换为各种PDF标准

Aspose.Words提供 PdfCompliace 枚举以支持将DOC或DOCX转换为各种PDF格式标准(例如PDF 1.7,PDF 1.5等)。下面的代码示例演示如何将文档转换为PDF使用1.7 PdfSaveOptions 与符合PDF17:

// The path to the documents directory.Document originalDoc = new Document(dataDir + "Document.docx");// Provide PDFSaveOption compliance to PDF17// or just convert without SaveOptionsPdfSaveOptions pso = new PdfSaveOptions();pso.setCompliance(PdfCompliance.PDF_17);originalDoc.save(dataDir + "Output.pdf", pso);
将图像转换为PDF

转换为PDF不受Microsoft Word文档格式的限制。Aspose.Words支持的任何格式,包括以编程方式创建的格式,都可以转换为PDF。例如,我们可以将单页图像(例如JPEG,PNG,BMP,EMF或WMF)以及多页图像(例如TIFF和GIF)转换为PDF。

下面的代码示例演示如何将JPEG和TIFF图像转换为PDF:

//将指定格式的图像转换为PDF。ConvertImageToPDF(dataDir +  " Test.jpg ",dataDir +  " TestJpg_out.pdf ");ConvertImageToPDF(dataDir +  " Test.tiff ",dataDir +  " TestTif_out.pdf ");
/**
* Converts an image to PDF using Aspose.Words for Java.
*
* @param inputFileName File name of input image file.
* @param outputFileName Output PDF file name.
* @throws Exception
*/
public static void ConvertImageToPDF(String inputFileName, String outputFileName) throws Exception {
// Create Aspose.Words.Document and DocumentBuilder.
// The builder makes it simple to add content to the document.
Document doc = new 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.
ImageInputStream iis = ImageIO.createImageInputStream(new File(inputFileName));
ImageReader reader = ImageIO.getImageReaders(iis).next();
reader.setInput(iis, false);
// Get the number of frames in the image.
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.
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.
PageSetup ps = builder.getPageSetup();
ps.setPageWidth(ConvertUtil.pixelToPoint(image.getWidth()));
ps.setPageHeight(ConvertUtil.pixelToPoint(image.getHeight()));
// Insert the image into the document and position it at the top left corner of the page.
builder.insertImage(
image,
RelativeHorizontalPosition.PAGE,
0,
RelativeVerticalPosition.PAGE,
0,
ps.getPageWidth(),
ps.getPageHeight(),
WrapType.NONE);
}
if (iis != null) {
iis.close();
reader.dispose();
}
doc.save(outputFileName);
}

到此,相信大家对"怎么使用Aspose.words在Java中将Word和图像转换为PDF"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

文档 格式 图像 代码 中将 布局 方式 方法 示例 面的 演示 内容 对象 引擎 标准 学习 支持 编程 输出 复杂 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 科技金融是不是互联网金融 金融科技还是互联网 互联网科技社 极限挑战互联网科技 兼蜜互联网科技 互联网金融科技委员会 哈尔滨新华互联网科技 互联网科技公司的市场部门 互联网金融中的现代科技 成都互联网科技公司工作好不好 恒生互联网科技指数跳水 工业互联网展湾科技股份有限公司 南通创西澳互联网科技有限公司 工业互联网科技成果转化 九思连强互联网电子科技有限公司 宁波跨境商互联网科技有限公司 广西科技产业互联网是什么 华人运通互联网科技公司 广州全民伙伴互联网科技有限公司 互联网 科技节活动 微视频大赛 深圳市人人互联网科技有限公司 重庆互联网络科技 互联网科技股票 互联网科技公司名称 阿大互联网科技有限公司 苏州铭阳互联网科技有限公司 互联网科技创新的路径 互联网金融科技公司的优势 陕西云淼互联网科技公司 西安左小互联网科技有限公司
0