將 XPS 文檔合併為 XPS 和 PDF

.NET API 解決方案,用於組合多個 XPS 文件

 

XPS 可以在一個文件中保存多個文檔,並且 XPS 文檔可能包含多個頁面。 Aspose.Page API 解決方案提供的合併功能已開發,因此您可以將幾個文件合併為一個。結果,您將獲得一個 XPS 或 PDF 文件,其中包含所有合併文件的內容。在這裡,您將找到有關如何將 XPS 與 C# .NET 合併的代碼示例。要了解如何將此類功能實施到 Web 解決方案或在線合併文件,請嘗試 XPS Merger

要合併 XPS 文件,我們需要:

  • Aspose.Page for .NET API 是一個功能豐富、功能強大且易於使用的 C# 平台文檔操作和轉換 API。

  • 打開 NuGet 包管理器,搜索 Aspose.Page 並安裝。您也可以從包管理器控制台使用以下命令。

Package Manager Console Command


    PM> Install-Package Aspose.Page

使用 C# .NET 將多個 XPS 文件合併為一個文件的步驟。

  1. 設置文檔目錄的路徑。
  2. 初始化 XPS 輸出流。
  3. 初始化 XPS 文件輸入流。
  4. 從流 XpsDocument Class 加載 XPS 文檔或直接從文件加載。在這種情況下,您不需要 xpsStream。
  5. 創建一組將與第一個文件合併的 XPS 文件。
  6. 合併 XPS 文件以輸出 XPS 文檔。

將 XPS 合併到 XPS 的 C# 代碼

    using Aspose.Page.XPS;
    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_WorkingWithDocumentMerging();
    // Initialize XPS output stream
    using (System.IO.Stream outStream = System.IO.File.Open(dataDir + "mergedXPSfiles.xps", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
    // Initialize XPS input stream
    using (System.IO.Stream inStream = System.IO.File.Open(dataDir + "input.xps", System.IO.FileMode.Open))
    {
        // Load XPS document from the stream
        XpsDocument document = new XpsDocument(inStream, new 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
        string[] filesToMerge = new string[] { dataDir + "Demo.xps", dataDir + "sample.xps" };

        // Merge XPS files to output PDF document
        document.Merge(filesToMerge, outStream);
    }

使用 C# .NET 將多個 XPS 合併為 PDF 的步驟。

  1. 設置文檔目錄的路徑。
  2. 初始化 PDF 輸出流。
  3. 初始化 XPS 文件輸入流。
  4. 從流 XpsDocument Class 加載 XPS 文檔或直接從文件加載。在這種情況下,您不需要 xpsStream。
  5. 通過 PdfSaveOptions 類使用輸出文件的必要參數初始化選項對象。
  6. 使用 PdfDevice 類,創建 PDF 格式的渲染設備。
  7. 創建一組將與第一個文件合併的 XPS 文件。
  8. 合併 XPS 文件以輸出 XPS 文檔。

C# 將 XPS 合併為 PDF

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_WorkingWithDocumentMerging();
    // Initialize the PDF output stream
    using (System.IO.Stream pdfStream = System.IO.File.Open(dataDir + "mergedXPSfiles.pdf", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
    // Initialize the XPS input stream
    using (System.IO.Stream xpsStream = System.IO.File.Open(dataDir + "input.xps", System.IO.FileMode.Open))
    {
        // Load the XPS document form the stream
        XpsDocument document = new XpsDocument(xpsStream, new XpsLoadOptions());
        // or load it directly from a file. No xpsStream is needed then.
        // XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());

        // Initialize options object with the necessary parameters.
        Aspose.Page.XPS.Presentation.Pdf.PdfSaveOptions options = new Aspose.Page.XPS.Presentation.Pdf.PdfSaveOptions()
        {
            JpegQualityLevel = 100,
            ImageCompression = Aspose.Page.XPS.Presentation.Pdf.PdfImageCompression.Jpeg,
            TextCompression = Aspose.Page.XPS.Presentation.Pdf.PdfTextCompression.Flate
        };

        // Create a rendering device for the PDF format
        Aspose.Page.XPS.Presentation.Pdf.PdfDevice device = new Aspose.Page.XPS.Presentation.Pdf.PdfDevice(pdfStream);

        // Create an array of XPS files that will be merged with the first one
        string[] filesToMerge = new string[] { dataDir + "Demo.xps", dataDir + "sample.xps" };

        // Merge XPS files to the output PDF document
        document.Merge(filesToMerge, device, options);
    }



常問問題

1. 如何合併 XPS 文件?

設置文檔目錄的路徑並初始化輸出和輸入流。從流或文件中加載 XPS 文件並合併它們。如果您想更好地在線合併 XPS 文件,請使用我們的免費跨平台 XPS Merger

2. 支持哪些輸出格式?

到目前為止,Aspose.Page Solution 允許您將 XPS 文件合併為單個 XPS 文件或 PDF 文件。

3. 該功能是免費的嗎?

跨平台合併是免費的,對於 API 解決方案,您可以獲得免費試用,然後在需要時購買產品。

XPS 什麼是 XPS 文件格式

XPS 格式類似於 PDF 格式。兩者都是頁面描述語言 (PDL) 格式。 EPS 基於 HTML 而不是 PostScript 語言。 .eps 文件能夠包含文檔結構的標記以及有關文檔外觀的信息。還添加了有關如何打印和呈現文檔的說明。該格式的特點是它修復了文檔的描述,這意味著無論誰以及從哪個操作系統打開它,它看起來都是一樣的。