ในหัวข้อสั้นๆ นี้ เราจะได้เรียนรู้ วิธีแปลง Word เป็น PDF โดยใช้ C+* คุณสามารถปรับแต่งเอาต์พุต PDF ได้โดยเปลี่ยนรูปลักษณ์และคุณสมบัติอื่นๆ โดยใช้การเรียก API อย่างง่าย และ **สร้าง PDF จาก DOCX ใน C++++ ที่สามารถดูได้ในเบราว์เซอร์หรือโปรแกรมดูอื่นๆ
ขั้นตอนในการแปลง Word เป็น PDF โดยใช้ C++
- ติดตั้ง Aspose.Words for C++ จากตัวจัดการแพ็คเกจ NuGet
- รวมเนมสเปซ Aspose.Words และไฟล์ส่วนหัวที่จำเป็นทั้งหมด
- ใช้ Document class โหลดไฟล์ Word ต้นฉบับเพื่อส่งออกเป็น PDF ในภาษา C++
- สร้างอินสแตนซ์ของ PdfSaveOptions เพื่อตั้งค่าหน้า โหมดหน้า และโหมดการปฏิบัติตามข้อกำหนดของ PDF
- บันทึกไฟล์ DOCX เป็น PDF โดยใช้วิธีบันทึก
ในขั้นตอนข้างต้น คุณสามารถส่งออก Word เป็น PDF ใน C++ โดยใช้การเรียก API อย่างง่าย หลังจากโหลดเอกสารจากดิสก์แล้ว คุณสามารถปรับแต่งเอาต์พุต PDF โดยใช้คลาส PdfSaveOptions และตั้งค่าช่วงหน้า โหมดหน้า และความสอดคล้องกับ PDF สำหรับ PDF ที่ส่งออก สุดท้าย เราจะบันทึก PDF บนดิสก์หรือใน MemoryStream
รหัสเพื่อสร้าง PDF จาก DOCX ใน C ++
#pragma once | |
#include <cstdint> | |
#include <iostream> | |
#include <Aspose.Words.Cpp/License.h> | |
#include <Aspose.Words.Cpp/Document.h> | |
#include <Aspose.Words.Cpp/Range.h> | |
#include <Aspose.Words.Cpp/Saving/PageSet.h> | |
#include <Aspose.Words.Cpp/Saving/PdfSaveOptions.h> | |
#include <Aspose.Words.Cpp/Saving/SaveOutputParameters.h> | |
#include <Aspose.Words.Cpp/Saving/PageSet.h> | |
#include <Aspose.Words.Cpp/Saving/PdfPageMode.h> | |
#include <Aspose.Words.Cpp/Saving/PdfCompliance.h> | |
#include <system/enumerator_adapter.h> | |
#include <system/smart_ptr.h> | |
#include <system/shared_ptr.h> | |
#include <system/io/path.h> | |
using namespace Aspose::Words::Saving; | |
using namespace Aspose::Words; | |
using namespace Aspose::Words::Replacing; | |
using System::MakeObject; | |
using System::SharedPtr; | |
using System::String; | |
class WordToPDFEx | |
{ | |
public: | |
static void WordToPDF() | |
{ | |
// Load and Set API License | |
System::String LicFilePath = u"Aspose.Total.Net.lic"; | |
SharedPtr<Aspose::Words::License> WordsCPPLicenseForTable = System::MakeObject<Aspose::Words::License>(); | |
// Setting product license | |
WordsCPPLicenseForTable->SetLicense(LicFilePath); | |
// Open the DOCX file using Document class for saving as PDF | |
SharedPtr<Document> sampleDocx = MakeObject<Document>(u"TestAspose.docx"); | |
// Instantiate the PdfSaveOptions class object before converting the Docx to PDF | |
SharedPtr <PdfSaveOptions> options = MakeObject <PdfSaveOptions>(); | |
System::ArrayPtr <int32_t> pages = System::MakeObject<System::Array<int32_t>>(2); | |
pages[0] = 1; | |
pages[1] = 2; | |
SharedPtr <PageSet> pageSet = MakeObject<PageSet>(pages); | |
// Set the page numbers of the document to be rendered to output PDF | |
options->set_PageSet(pageSet); | |
// Configuring the full screen page mode while opening it in a viewer | |
options->set_PageMode(PdfPageMode::FullScreen); | |
// Set the output PDF document compliance mode | |
options->set_Compliance(PdfCompliance::Pdf17); | |
// Save the DOCX as PDF file using the above mentioned options | |
sampleDocx->Save(u"Output.pdf", options); | |
} | |
}; |
ตัวอย่างข้างต้น แปลง Word เป็น PDF ใน C++ การแสดงผลเป็น PDF เริ่มต้นด้วยการโหลดไฟล์ Word จากดิสก์ จากนั้น ในขั้นตอนต่อมา เราได้ใช้คลาส PdfSaveOptions เพื่อตั้งค่าตัวเลือก PDF ต่างๆ คุณยังสามารถปรับแต่ง PDF ได้ด้วยการตั้งค่าเสริมอื่นๆ เช่น การบีบอัดเนื้อหาที่เป็นข้อความ ลักษณะการซูม ปัจจัยการซูม ตัวเลือกเค้าร่าง ตัวเลือกในการใช้การลบรอยหยัก และอื่นๆ อีกมากมาย ในที่สุด เราได้บันทึก PDF ที่สร้างขึ้นบนดิสก์
เราได้เรียนรู้วิธี แปลง Word เป็น PDF โดยใช้ C++ ที่นี่ หากคุณสนใจที่จะเรียนรู้เกี่ยวกับการแปลงไฟล์ Word เป็นรูปแบบไฟล์ MD โปรดดูบทความใน วิธีแปลง Word เป็น Markdown โดยใช้ C ++