如何在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等多个语言平台的支持,功能非常强大。

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

  • 简单的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。

// 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.";

输入文字文件

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

转换后的PDF文档

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

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

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

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

下面的代码示例演示如何使用PDF / A-1a标准将Word 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-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);