
XPS 和 OXPS 文件因其分辨率獨立性而經常用於打印。但是,有時我們需要將 XPS 轉換為 PDF 或將 OXPS 轉換為 PDF。 Aspose.Page for .NET API 可讓您以高保真度和快速渲染執行這些轉換。讓我們探討以下用例:
使用 C# 以編程方式將 XPS 轉換為 PDF
使用 Aspose.Page for .NET API,XPS 到 PDF 的轉換很簡單。我們將學習以下執行 XPS 文件轉換的方法:
i) 在 C# 中將 XPS 的特定頁面轉換為 PDF
要將 XPS 文檔的選定頁面轉換為 PDF,請按照以下步驟操作:
- 初始化 XPS 輸入流
- 從流中加載 XPS 文檔
- 初始化 PdfSaveOptions 對象
- 指定要轉換的頁碼
- 將文檔另存為 PDF 文件
下面的代碼片段遵循這些步驟並展示瞭如何使用 C# 將 XPS 轉換為 PDF:
// 初始化 PDF 輸出流
using (System.IO.Stream pdfStream = System.IO.File.Open(dataDir + "XPStoPDF.pdf", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
// 初始化 XPS 輸入流
//使用 (System.IO.Stream xpsStream = System.IO.File.Open(dataDir + "input.xps", System.IO.FileMode.Open))
using (System.IO.Stream xpsStream = System.IO.File.Open(dataDir + "sample.xps", System.IO.FileMode.Open))
{
// 從流中加載 XPS 文檔
Aspose.Page.XPS.XpsDocument document = new Aspose.Page.XPS.XpsDocument(xpsStream, new Aspose.Page.XPS.XpsLoadOptions());
// 或直接從文件加載 XPS 文檔。那麼就不需要 xpsStream 了。
// XpsDocument 文檔 = new XpsDocument(inputFileName, new XpsLoadOptions());
// 使用必要的參數初始化選項對象。
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,
PageNumbers = new int[] {1, 3}
};
// 為PDF格式創建渲染器
Aspose.Page.XPS.Presentation.Pdf.PdfDevice device = new Aspose.Page.XPS.Presentation.Pdf.PdfDevice(pdfStream);
document.Save(device, options);
}
此代碼片段使用包含多個頁面的 XPS 文檔作為輸入文件。然而,只有第 1 頁和第 3 頁被轉換為代碼片段中指定的 PDF。以下屏幕截圖顯示呈現為 PDF 文檔的 2 個頁面:

ii) 在 C# 中將 XPS 的所有頁面轉換為 PDF
您可以將整個 XPS 文件轉換為 PDF。按照以下步驟操作,XPS 文件的所有頁面都將轉換為 PDF 文件:
- 加載輸入 XPS 文件
- 使用必要的參數初始化選項對象
- 創建 PdfDevice 的實例進行渲染
- 將 XPS 導出為 PDF 文檔
下面的代碼片段基於所有這些步驟,展示瞭如何使用 C# 語言將 XPS 文件轉換為 PDF:
// 初始化 PDF 輸出流
using (System.IO.Stream pdfStream = System.IO.File.Open(dataDir + "XPStoPDF.pdf", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
// 初始化 XPS 輸入流
//使用 (System.IO.Stream xpsStream = System.IO.File.Open(dataDir + "input.xps", System.IO.FileMode.Open))
using (System.IO.Stream xpsStream = System.IO.File.Open(dataDir + "sample.xps", System.IO.FileMode.Open))
{
// 從流中加載 XPS 文檔
Aspose.Page.XPS.XpsDocument document = new Aspose.Page.XPS.XpsDocument(xpsStream, new Aspose.Page.XPS.XpsLoadOptions());
// 或直接從文件加載 XPS 文檔。那麼就不需要 xpsStream 了。
// XpsDocument 文檔 = new XpsDocument(inputFileName, new XpsLoadOptions());
// 使用必要的參數初始化選項對象。
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,
};
// 為PDF格式創建渲染器
Aspose.Page.XPS.Presentation.Pdf.PdfDevice device = new Aspose.Page.XPS.Presentation.Pdf.PdfDevice(pdfStream);
document.Save(device, options);
}
使用 C# 將 OXPS 轉換為 PDF
OXPS 格式是 XPS 文件格式的更新和高級形式。但是,某些舊操作系統不支持此類文件。 Aspose.Page for .NET API 也能夠轉換 OXPS 文件。讓我們繼續學習以下使用場景:
i) 在 C# 中將 OXPS 的一些頁面轉換為 PDF
一個 OXPS 文件可能包含許多頁面,可以通過以下步驟將任意數量的頁面轉換為 PDF:
- 加載 OXPS 文件
- 聲明 PdfSaveOptions 對象
- 設置要轉換的頁碼
- 將 OXPS 渲染為 PDF
以下代碼片段顯示瞭如何使用 C# 將 OXPS 轉換為 PDF。如代碼片段中所述,它將 OXPS 文件的第一頁轉換為 PDF。
// 初始化 PDF 輸出流
using (System.IO.Stream pdfStream = System.IO.File.Open(dataDir + "OXPStoPDF.pdf", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
// 初始化 OXPS 輸入流
//使用 (System.IO.Stream xpsStream = System.IO.File.Open(dataDir + "input.oxps", System.IO.FileMode.Open))
using (System.IO.Stream xpsStream = System.IO.File.Open(dataDir + "sample.oxps", System.IO.FileMode.Open))
{
// 從流中加載 OXPS 文檔
Aspose.Page.XPS.XpsDocument document = new Aspose.Page.XPS.XpsDocument(xpsStream, new Aspose.Page.XPS.XpsLoadOptions());
// 或直接從文件加載 XPS 文檔。那麼就不需要 xpsStream 了。
// XpsDocument 文檔 = new XpsDocument(inputFileName, new XpsLoadOptions());
// 使用必要的參數初始化選項對象。
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,
PageNumbers = new int[] {1}
};
// 為PDF格式創建渲染器
Aspose.Page.XPS.Presentation.Pdf.PdfDevice device = new Aspose.Page.XPS.Presentation.Pdf.PdfDevice(pdfStream);
document.Save(device, options);
}
ii) 在 C# 中將 OXPS 的所有頁面轉換為 PDF
轉換 OXPS 的所有頁面很簡單,並且與我們上面考慮的示例相關。讓我們按照以下步驟將 OXPS 文件的所有頁面轉換為單個 PDF 文檔:
- 初始化 OXPS 輸入流
- 從流中加載 OXPS 文件
- 實例化 PdfSaveOptions 類的對象
- 將 OXPS 導出為 PDF 文件
下面的代碼片段一一遵循這些步驟,並使用 C# 將 OXPS 轉換為 PDF:
// 初始化 PDF 輸出流
using (System.IO.Stream pdfStream = System.IO.File.Open(dataDir + "OXPStoPDF.pdf", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
// 初始化 OXPS 輸入流
//使用 (System.IO.Stream xpsStream = System.IO.File.Open(dataDir + "input.oxps", System.IO.FileMode.Open))
using (System.IO.Stream xpsStream = System.IO.File.Open(dataDir + "sample.oxps", System.IO.FileMode.Open))
{
// 從流中加載 OXPS 文檔
Aspose.Page.XPS.XpsDocument document = new Aspose.Page.XPS.XpsDocument(xpsStream, new Aspose.Page.XPS.XpsLoadOptions());
// 或者直接從文件加載 OXPS 文檔。那麼就不需要 xpsStream 了。
// XpsDocument 文檔 = new XpsDocument(inputFileName, new XpsLoadOptions());
// 使用必要的參數初始化選項對象。
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,
};
// 為PDF格式創建渲染器
Aspose.Page.XPS.Presentation.Pdf.PdfDevice device = new Aspose.Page.XPS.Presentation.Pdf.PdfDevice(pdfStream);
document.Save(device, options);
}
結論
在本文中,您了解瞭如何使用 C# 以編程方式將 XPS 轉換為 PDF 以及將 OXPS 轉換為 PDF。您已經通過 C# 代碼示例和屏幕截圖了解了高保真、高效和快速的文件格式轉換。您可以在 API 文檔 和 API 參考 的幫助下進一步探索 Aspose.Page for .NET API。此外,請隨時在 免費支持論壇 上提出任何疑問。