将文档转换为PDF
能够轻松可靠地将文档从一种格式转换为另一种格式是Aspose.Words的一个关键特性。 最流行的转换格式之一是PDF–一种固定布局格式,它在各种平台上呈现时保留文档的原始外观。 在Aspose.Words中使用"呈现"术语来描述将文档转换为分页或具有页面概念的文件格式的过程。
将Word文档转换为PDF
从Word到PDF的转换是一个相当复杂的过程,需要几个阶段的计算。 Aspose.Words布局引擎模仿Microsoft Word的页面布局引擎的工作方式,使PDF输出文档看起来尽可能接近您在Microsoft Word中看到的内容。
使用Aspose.Words,您可以以编程方式将文档从DOC或DOCX格式转换为PDF,而无需使用MicrosoftOffice。 本文介绍如何执行此转换。
将DOCX或DOC转换为PDF
从DOC或DOCX文档格式转换为Aspose.Words中的PDF格式非常容易,只需两行代码即可完成:
- 通过指定文档名称及其格式扩展名,使用其构造函数之一将文档加载到Document对象中。
- 调用Document对象上的Document.Save方法之一,并通过输入带有"的文件名将所需的输出格式指定为PDF。PDF"扩展。
下面的代码示例演示如何使用Save
方法将文档从DOCX转换为PDF:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "Document.docx"); | |
doc.save(getArtifactsDir() + "BaseConversions.DocxToPdf.pdf"); |
您可以从以下位置下载此示例的模板文件 Aspose.Words GitHub.
有时需要指定其他选项,这可能会影响将文档保存为PDF的结果。 这些选项可以通过使用PdfSaveOptions类来指定,其中包含确定如何显示PDF输出的属性。
请注意,使用相同的技术,您可以将任何流布局格式文档转换为PDF格式。
转换为不同的PDF标准
Aspose.Words提供PdfCompliace枚举以支持将DOC或DOCX转换为各种PDF格式标准(例如PDF 1.7, PDF 1.5, 等。).
下面的代码示例演示如何使用符合PDF17的PdfSaveOptions将文档转换为PDF1.7:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "Rendering.docx"); | |
PdfSaveOptions saveOptions = new PdfSaveOptions(); { saveOptions.setCompliance(PdfCompliance.PDF_17); } | |
doc.save(getArtifactsDir() + "WorkingWithPdfSaveOptions.ConversionToPdf17.pdf", saveOptions); |
将图像转换为PDF
转换为PDF不受Microsoft Word文档格式的限制。 Aspose.Words支持的任何格式,包括以编程方式创建的格式,也可以转换为PDF。 例如,我们可以转换单页图像,例如JPEG, PNG, BMP, EMF, 或WMF,以及多页图像,例如TIFF和GIF,到PDF。
下面的代码示例演示如何将JPEG和TIFF图像转换为PDF:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
convertImageToPDF(getImagesDir() + "Logo.jpg", getArtifactsDir() + "BaseConversions.JpgToPdf.pdf"); | |
convertImageToPDF(getImagesDir() + "Transparent background logo.png", getArtifactsDir() + "BaseConversions.PngToPdf.pdf"); | |
convertImageToPDF(getImagesDir() + "Windows MetaFile.wmf", getArtifactsDir() + "BaseConversions.WmfToPdf.pdf"); | |
convertImageToPDF(getImagesDir() + "Tagged Image File Format.tiff", getArtifactsDir() + "BaseConversions.TiffToPdf.pdf"); | |
convertImageToPDF(getImagesDir() + "Graphics Interchange Format.gif", getArtifactsDir() + "BaseConversions.GifToPdf.pdf"); |
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
/** | |
* 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 | |
*/ | |
private 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的引用。啊图像,和javax。imageio类包到您的项目。
减小PDF输出大小
保存到PDF时,可以指定是否要优化输出。 为此,您需要将OptimizeOutput标志设置为true,然后将删除冗余嵌套画布和空画布,并连接具有相同格式的相邻字形。
下面的代码示例演示如何优化输出:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git. | |
Document doc = new Document(getMyDir() + "Rendering.docx"); | |
PdfSaveOptions saveOptions = new PdfSaveOptions(); { saveOptions.setOptimizeOutput(true); } | |
doc.save(getArtifactsDir() + "WorkingWithPdfSaveOptions.OptimizeOutput.pdf", saveOptions); |
请参阅
- 文章 渲染图 有关固定页面和流布局格式的更多信息
- 文章 转换为固定页格式 有关页面布局的更多信息
- 文章 转换为PDF时指定渲染选项 有关使用
PdfSaveOptions
类的更多信息