이 간단한 주제에서는 C++를 사용하여 Word를 PDF로 변환하는 방법을 배웁니다. 간단한 API 호출을 사용하여 모양 및 기타 속성을 변경하여 출력 PDF을 사용자 정의하고 브라우저나 다른 뷰어에서 볼 수 있는 **C++의 **DOCX에서 PDF를 생성할 수 있습니다.
C++를 사용하여 Word를 PDF로 변환하는 단계
- NuGet 패키지 관리자에서 Aspose.Words for C++ 설치
- Aspose.Words 네임스페이스 및 필요한 모든 헤더 파일 포함
- Document class를 사용하여 C++에서 PDF로 내보내기 위한 소스 Word 파일 로드
- PdfSaveOptions를 인스턴스화하여 PDF의 페이지, 페이지 모드 및 규정 준수 모드를 설정합니다.
- Save 메소드를 사용하여 DOCX 파일을 PDF로 저장
위의 단계에서 간단한 API 호출을 사용하여 *Word를 C++*의 PDF로 내보낼 수 있습니다. 디스크에서 문서를 로드한 후 PdfSaveOptions 클래스를 사용하여 출력 PDF를 사용자 정의하고 내보낸 PDF에 대한 페이지 범위, 페이지 모드 및 PDF 준수를 설정할 수 있습니다. 마지막으로 PDF를 디스크나 MemoryStream에 저장합니다.
C++의 DOCX에서 PDF를 생성하는 코드
#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); | |
} | |
}; |
위의 예는 *C++*에서 Word를 PDF로 변환합니다. PDF로의 렌더링은 디스크에서 Word 파일을 로드하여 시작됩니다. 그런 다음 후속 단계에서 PdfSaveOptions 클래스를 사용하여 다른 PDF 옵션을 설정했습니다. 텍스트 내용 압축, 확대/축소 동작, 확대/축소 비율, 윤곽선 옵션, 앤티앨리어싱 사용 옵션 등과 같은 기타 옵션 설정을 지정하여 PDF를 사용자 지정할 수도 있습니다. 마지막으로 생성된 PDF를 디스크에 저장했습니다.
여기에서 C++*를 사용하여 *Word를 PDF로 변환하는 방법을 배웠습니다. Word 파일을 MD 파일 형식으로 변환하는 방법에 대해 알아보려면 C++를 사용하여 Word를 Markdown으로 변환하는 방법에 대한 문서를 참조하세요.