福利!使用Aspose.Words在C++中以編程方式將Word格式轉換為PDF完整指南
在共享文檔之前,通常使用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可以點擊這里下載最新版測試。
本文介紹了以下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.";
輸入文字文件
轉換后的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轉換的步驟:
- 創建一個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);還想要更多嗎?您可以點擊閱讀【2019 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問或需求,請隨時加入Aspose技術交流群(642018183),我們很高興為您提供查詢和咨詢。