如何在C++中将Word格式转换为PDF?Aspose.Words快速解决

在共享文档以前,一般使用Word到PDF的转换。可使用各类在线Word到PDF转换器,能够转换单个或有限数量的Word文档。可是,随着新兴的MS Word自动化和报告生成解决方案,Word到PDF的自动转换已成为系统的重要组成部分。同时,须要自动完成DOC / DOCX到PDF的批量转换,以减小时间和精力。编程

以前小编展现了如何使用Aspose.Words自动以Java/.NET编程方式将Word(DOC/DOCX)文档转换为PDF的过程。因为这是一个流行且普遍使用的功能,所以,在本文中,将展现如何在C ++应用程序中将Word DOC/DOCX转换为PDF。若是你尚未用过C ++版Aspose.Words能够点击这里下载最新版测试。测试

与此同时,Aspose.Words也提供.NET和Java等多个语言平台的支持,功能很是强大。spa

本文介绍了如下Word到PDF的转换:orm

  • 简单的Word DOC / DOCX转换为C ++中的PDF。
  • DOCX到具备特定标准的PDF,例如PDF 1.5,PDF / A-1a等。
  • 将DOCX的选定页面转换为PDF。
  • 将DOCX中的图像/文本压缩应用于PDF转换。

①在C ++中将Word DOC / DOCX转换为PDF

使用Aspose.Words for C ++,将Word文档转换为PDF就像饼同样简单。如下是将DOC / DOCX文件转换为PDF的步骤。对象

  • 建立Document类的对象,并使用Word文档的路径对其进行初始化。
  • 调用Document-> Save()方法将文档另存为PDF

下面的代码示例演示如何在C ++中将DOCX转换为PDF。blog

// Load the document from disk.
System::SharedPtrdoc = System::MakeObject( u"Word.docx");
// Set the output PDF path
System::String outputPath =  u"DOCX-to-PDF.pdf";
// Convert DOCX to PDF
doc->Save(outputPath);
std::cout << "Converted DOCX to PDF successfuly.";

输入文字文件索引

福利!使用Aspose.Words在C++中以编程方式将Word格式转换为PDF完整指南

转换后的PDF文档文档

福利!使用Aspose.Words在C++中以编程方式将Word格式转换为PDF完整指南

②将DOCX转换为C ++中的PDF / A或其余PDF标准

PDF格式支持各类PDF标准,包括PDF / A,PDF / E等。在某些状况下,须要将Word文件转换为特定的标准,例如PDF / A-1a。在这种状况下,Aspose.Words for C ++容许为转换后的PDF设置所需的PDF标准。如下是在Word中将PDF标准设置为PDF转换的步骤:get

  • 建立一个Document类的对象,并使用DOCX文件的路径对其进行初始化。
  • 建立PdfSaveOptions类的对象,并使用PdfSaveOptions-> set_Compliance()方法设置PDF听从性。
  • 调用Document-> Save()方法将文档另存为PDF。

下面的代码示例演示如何使用PDF / A-1a标准将Word DOCX转换为PDF。it

// Load the document from disk.
System::SharedPtrdoc = System::MakeObject( u"Word.docx");
// Set the output PDF path
System::String outputPath =  u"DOCX-to-PDFA.pdf";
// Set PDF options
System::SharedPtroptions = System::MakeObject();
options->set_Compliance(PdfCompliance::PdfA1a);
// Save the document in PDF format.
doc->Save(outputPath, options);
std::cout << "Converted DOCX to PDF/A successfuly.";

③在C ++中将单词的选定页面转换为PDF

能够只转换选定的页面,而不是将整个Word转换为PDF。如下是仅将所需的Word页面转换为PDF的步骤:

  • 使用Document类加载Word文档。
  • 使用PdfSaveOptions-> set_PageIndex()方法设置起始页的索引。
  • 使用PdfSaveOptions-> set_PageCount()方法设置要转换的页面数。
  • 使用Document-> Save()方法将文档另存为PDF 。

如下代码示例显示了如何在C ++中将DOCX的选定页面转换为PDF。

// Load the document from disk.
System::SharedPtrdoc = System::MakeObject( u"Word.docx");
// Set the output PDF path
System::String outputPath =  u"DOCX-to-PDF.pdf";
// Set PDF options
System::SharedPtroptions = System::MakeObject();
options->set_PageIndex(1);
options->set_PageCount(2);
// Save the document in PDF format.
doc->Save(outputPath, options);

④将DOCX中的图像/文本压缩应用于PDF转换

能够压缩生成的PDF文档以减少其尺寸。Aspose.Words for C ++容许您分别使用PdfSaveOptions-> set_TextCompression()和PdfSaveOptions-> set_ImageCompression()方法应用文本和图像压缩。如下代码示例显示了在C ++中将DOCX转换为PDF时如何应用压缩。

// Load the document from disk.
System::SharedPtrdoc = System::MakeObject( u"Word.docx");
// Set the output PDF path
System::String outputPath =  u"DOCX-to-PDF.pdf";
// Set PDF options
System::SharedPtroptions = System::MakeObject();
// Set JPEG quality
options->set_JpegQuality(100);
// Save the document in PDF format
doc->Save(outputPath, options);

若是您有任何疑问或需求,请随时加入Aspose技术交流群642018183,咱们很高兴为您提供查询和咨询

相关文章
相关标签/搜索