Hợp nhất các tài liệu XPS thành PDF

Giải pháp API C++ để kết hợp một số tệp XPS

 

Định dạng tệp XPS cho phép lưu trữ nhiều tài liệu trong một tệp và cũng có thể chứa nhiều trang. Giải pháp API Aspose.Page cho C++ cung cấp khả năng hợp nhất nhiều tệp thành một, dẫn đến tệp XPS hoặc PDF chứa nội dung kết hợp của tất cả các tệp gốc.

Tại đây, bạn sẽ tìm thấy một mã ví dụ về cách sử dụng API để hợp nhất các tệp XPS. Để khám phá cách chức năng này có thể được tích hợp vào một giải pháp web hoặc được sử dụng để hợp nhất tệp trực tuyến, bạn có thể dùng thử XPS Merger đa nền tảng dụng cụ.

Để hợp nhất các tệp PS và EPS, bạn cần:

  • Aspose.Page cho API C++ là API chuyển đổi và thao tác tài liệu giàu tính năng, mạnh mẽ và dễ sử dụng cho nền tảng C++.

  • Bạn có thể trực tiếp tải xuống phiên bản mới nhất của nó, chỉ cần mở trình quản lý gói NuGet, tìm kiếm Aspose.Page.Cpp và cài đặt. Bạn cũng có thể sử dụng lệnh sau từ Bảng điều khiển quản lý gói.

Package Manager Console Command


    PM> Install-Package Aspose.Page

Các bước để hợp nhất một số tệp XPS thành PDF bằng C++.

  1. Đặt đường dẫn đến thư mục tài liệu.
  2. Khởi tạo các luồng đầu vào tệp đầu ra PDF và tệp XPS.
  3. Tạo phiên bản của XpsDocument từ luồng đầu vào đã tạo trước đó.
  4. Khởi tạo đối tượng tùy chọn với các tham số TextCompression, ImageCompression, JpegQualityLevel cho tệp đầu ra bằng PdfSaveOptions Lớp.
  5. Sử dụng Lớp PdfDevice , tạo thiết bị hiển thị cho định dạng PDF.
  6. Hợp nhất các tệp XPS để xuất tài liệu XPS.

Mã C++ để hợp nhất XPS thành PDF

    using Aspose::Page::XPS;
    using Aspose::Page::IO;
    using Aspose::Page::Presentation::Pdf;
    // Input file
    System::SharedPtr<System::IO::Stream> pdfStream = System::IO::File::Open(RunExamples::outDir() + u"XPStoPDF.pdf", System::IO::FileMode::Create, System::IO::FileAccess::Write);
    // Clearing resources under 'using' statement
    System::Details::DisposeGuard<1> __dispose_guard_1({ pdfStream });
    // ------------------------------------------
    try {
	    System::SharedPtr<System::IO::Stream> xpsStream = System::IO::File::Open(RunExamples::dataDir() + u"input xps", System::IO::FileMode::Open, System::IO::FileAccess::Read);
	    // Clearing resources under 'using' statement
	    System::Details::DisposeGuard<1> __dispose_guard_0({ xpsStream });
	    // ------------------------------------------

	    try
	    {
		    // Load XPS document form the stream
		    System::SharedPtr<XpsDocument> document = System::MakeObject<XpsDocument>(xpsStream, System::MakeObject<XpsLoadOptions>());
		    // or load XPS document directly from file. No xpsStream is needed then.
		    // XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());

		    // Initialize options object with necessary parameters.
		    System::SharedPtr<Aspose::Page::Xps::Presentation::Pdf::PdfSaveOptions> options = [&] { auto tmp_0 = System::MakeObject<Aspose::Page::Xps::Presentation::Pdf::PdfSaveOptions>(); tmp_0->set_JpegQualityLevel(100); tmp_0->set_ImageCompression(Aspose::Page::Xps::Presentation::Pdf::PdfImageCompression::Jpeg); tmp_0->set_TextCompression(Aspose::Page::Xps::Presentation::Pdf::PdfTextCompression::Flate); tmp_0->set_PageNumbers(System::MakeArray<int32_t>({ 1, 2, 6 })); return tmp_0; }();

		    // Create rendering device for PDF format
		    System::SharedPtr<Aspose::Page::Xps::Presentation::Pdf::PdfDevice> device = System::MakeObject<Aspose::Page::Xps::Presentation::Pdf::PdfDevice>(pdfStream);

		    // Create an array of XPS files that will be merged with the first one
		    System::ArrayPtr<System::String> filesForMerge = System::MakeArray<System::String>({dataDir() + u"input2.xps", dataDir() + u"input3.xps"});

		    document->Merge(filesForMerge, device, options);
	    }
	    catch (...)
	    {
		    __dispose_guard_0.SetCurrentException(std::current_exception());
	    }
    }
    catch (...)
    {
	    __dispose_guard_1.SetCurrentException(std::current_exception());
    }

Các bước để hợp nhất một số tệp XPS thành một tệp duy nhất bằng C++.

  1. Khởi tạo luồng đầu ra và đầu vào XPS.
  2. Tạo một mảng các tệp XPS để hợp nhất với tệp đầu tiên.
  3. Tạo một phiên bản của XpsDocument từ luồng đầu vào đã tạo trước đó.
  4. Hợp nhất các tệp XPS để xuất tài liệu XPS.

Mã C++ để hợp nhất XPS với XPS

    // Output stream
    System::SharedPtr<System::IO::Stream> outStream = System::IO::File::Open(RunExamples::outDir() + u"mergedXPSfiles.xps", System::IO::FileMode::Create, System::IO::FileAccess::Write);
    // Clearing resources under 'using' statement
    System::Details::DisposeGuard<1> __dispose_guard_1({ outStream });
    // ------------------------------------------
    try {
	    System::SharedPtr<System::IO::Stream> inStream = System::IO::File::Open(RunExamples::dataDir() + u"input.xps", System::IO::FileMode::Open, System::IO::FileAccess::Read);
	    // Clearing resources under 'using' statement
	    System::Details::DisposeGuard<1> __dispose_guard_0({ inStream });
	    // ------------------------------------------

	    try
	    {
		    // Load XPS document form the stream
		    System::SharedPtr<XpsDocument> document = System::MakeObject<XpsDocument>(xpsStream, System::MakeObject<XpsLoadOptions>());
		    // or load XPS document directly from file. No xpsStream is needed then.
		    // XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());

		    // Create an array of XPS files that will be merged with the first one
		    System::ArrayPtr<System::String> filesForMerge = System::MakeArray<System::String>({dataDir() + u"input2.xps", dataDir() + u"input3.xps"});

		    document->Merge(filesForMerge, outStream);
	    }
	    catch (...)
	    {
		    __dispose_guard_0.SetCurrentException(std::current_exception());
	    }
    }
    catch (...)
    {
	    __dispose_guard_1.SetCurrentException(std::current_exception());
    }

XPS Những gì là XPS Tập Tin Định Dạng

Định dạng XPS tương tự như định dạng PDF. Cả hai đều là định dạng ngôn ngữ mô tả trang (PDL). EPS dựa trên HTML và không dựa trên ngôn ngữ PostScript. Tệp .eps có thể chứa phần đánh dấu cấu trúc của tài liệu cùng với thông tin về cách tài liệu sẽ trông như thế nào. Ngoài ra còn có các hướng dẫn bổ sung về cách in và kết xuất tài liệu. Đặc điểm của định dạng là nó sửa chữa mô tả của tài liệu, có nghĩa là nó sẽ trông giống nhau cho dù ai và từ hệ thống hoạt động nào mở nó ra.