将文档转换为PDF
能够轻松可靠地将文档从一种格式转换为另一种格式是Aspose.Words的一个关键特性。 最流行的转换格式之一是PDF–一种固定布局格式,它在各种平台上呈现时保留文档的原始外观。 在Aspose.Words中使用"呈现"术语来描述将文档转换为分页或具有页面概念的文件格式的过程。
将Word文档转换为PDF
从Word到PDF的转换是一个相当复杂的过程,需要几个阶段的计算。 Aspose.Words布局引擎模仿Microsoft Word的页面布局引擎的工作方式,使PDF输出文档看起来尽可能接近您在Microsoft Word中看到的内容。
使用Aspose.Words,您可以以编程方式将文档从DOC或DOCX格式转换为PDF,而无需使用Microsoft Office。 本文介绍如何执行此转换。
将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-C.git. | |
auto doc = MakeObject<Document>(MyDir + u"Document.docx"); | |
doc->Save(ArtifactsDir + u"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-C.git. | |
auto doc = MakeObject<Document>(MyDir + u"Rendering.docx"); | |
auto saveOptions = MakeObject<PdfSaveOptions>(); | |
saveOptions->set_Compliance(PdfCompliance::Pdf17); | |
doc->Save(ArtifactsDir + u"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-C.git. | |
ConvertImageToPdf(ImagesDir + u"Logo.jpg", ArtifactsDir + u"BaseConversions.JpgToPdf.pdf"); | |
ConvertImageToPdf(ImagesDir + u"Transparent background logo.png", ArtifactsDir + u"BaseConversions.PngToPdf.pdf"); | |
ConvertImageToPdf(ImagesDir + u"Windows MetaFile.wmf", ArtifactsDir + u"BaseConversions.WmfToPdf.pdf"); | |
ConvertImageToPdf(ImagesDir + u"Tagged Image File Format.tiff", ArtifactsDir + u"BaseConversions.TiffToPdf.pdf"); | |
ConvertImageToPdf(ImagesDir + u"Graphics Interchange Format.gif", ArtifactsDir + u"BaseConversions.GifToPdf.pdf"); |
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git. | |
/// <summary> | |
/// Converts an image to PDF using Aspose.Words for .NET. | |
/// </summary> | |
/// <param name="inputFileName">File name of input image file.</param> | |
/// <param name="outputFileName">Output PDF file name.</param> | |
void ConvertImageToPdf(String inputFileName, String outputFileName) | |
{ | |
std::cout << (String(u"Converting ") + inputFileName + u" to PDF ....") << std::endl; | |
auto doc = MakeObject<Document>(); | |
auto builder = MakeObject<DocumentBuilder>(doc); | |
// Read the image from file, ensure it is disposed. | |
{ | |
SharedPtr<System::Drawing::Image> image = System::Drawing::Image::FromFile(inputFileName); | |
// Insert a section break before each new page, in case of a multi-frame TIFF. | |
builder->InsertBreak(BreakType::SectionBreakNewPage); | |
// 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. | |
SharedPtr<PageSetup> ps = builder->get_PageSetup(); | |
ps->set_PageWidth(ConvertUtil::PixelToPoint(image->get_Width(), image->get_HorizontalResolution())); | |
ps->set_PageHeight(ConvertUtil::PixelToPoint(image->get_Height(), image->get_VerticalResolution())); | |
// 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->get_PageWidth(), ps->get_PageHeight(), WrapType::None); | |
} | |
doc->Save(outputFileName); | |
} |
要使此代码工作,您需要将对Aspose.Words和System.Drawing
的引用添加到您的项目中。
请参阅
- 文章 渲染图 有关固定页面和流布局格式的更多信息
- 文章 转换为固定页格式 有关页面布局的更多信息
- 文章 转换为PDF时指定渲染选项 有关使用
PdfSaveOptions
类的更多信息