Bu kısa konuda, C++ kullanarak Word’ü PDF’ye dönüştürmeyi öğreneceğiz. Basit API çağrılarını kullanarak görünümünü ve diğer özelliklerini değiştirerek PDF çıktısını özelleştirebilir ve tarayıcıda veya başka herhangi bir görüntüleyicide görüntülenebilen C++‘da DOCX PDF oluşturabilirsiniz.
C++ kullanarak Word’ü PDF’ye Dönüştürme Adımları
- NuGet paket yöneticisinden Aspose.Words for C++ yükleyin
- Aspose.Words ad alanını ve gerekli tüm başlık dosyalarını dahil edin
- Document class kullanarak, C++‘da PDF’ye dışa aktarmak için kaynak Word dosyasını yükleyin
- PDF’nin sayfaları, sayfa modunu ve uyumluluk modunu ayarlamak için PdfSaveOptions örneğini oluşturun
- DOCX dosyasını Kaydet yöntemini kullanarak PDF olarak kaydedin
Yukarıdaki adımlarda, basit API çağrılarını kullanarak Word’ü C++‘da PDF’ye aktarabilirsiniz. Belgeyi diskten yükledikten sonra, PdfSaveOptions sınıfını kullanarak çıktı PDF’sini özelleştirebilir ve dışa aktarılan PDF için sayfa aralığını, sayfa modunu ve PDF uyumluluğunu ayarlayabilirsiniz. Son olarak, PDF’yi diske veya MemoryStream’e kaydedeceğiz.
C++‘da DOCX’ten PDF Oluşturma Kodu
#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); | |
} | |
}; |
Yukarıdaki örnek Word’ü C++‘da PDF’ye dönüştürür. PDF’ye oluşturma, Word dosyasını diskten yükleyerek başlar. Daha sonraki adımlarda farklı PDF seçeneklerini ayarlamak için PdfSaveOptions sınıfını kullandık. Ayrıca, metin içeriği sıkıştırma, yakınlaştırma davranışı, yakınlaştırma faktörü, anahat seçenekleri, kenar yumuşatma kullanma seçeneği ve çok daha fazlası gibi diğer isteğe bağlı ayarları ayarlayarak da PDF’yi özelleştirebilirsiniz. Son olarak, oluşturulan PDF’yi diske kaydettik.
Burada C++* kullanarak Word’ü PDF’ye dönüştürmeyi öğrendik. Word dosyasını MD dosya biçimine dönüştürmekle ilgileniyorsanız, C++ kullanarak Word’ü Markdown’a dönüştürme makalesine bakın.