翻譯|使用教程|編輯:李顯亮|2020-06-12 09:30:28.647|閱讀 661 次
概述:XPS和OXPS文件因其分辨率獨立性而經(jīng)常用于打印。但是,有時我們需要將XPS轉(zhuǎn)換為PDF或?qū)XPS轉(zhuǎn)換為PDF。在本文中,將學(xué)習(xí)使用C#以編程方式將OXPS或XPS轉(zhuǎn)換為PDF。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
XPS和OXPS文件因其分辨率獨立性而經(jīng)常用于打印。但是,有時我們需要將XPS轉(zhuǎn)換為PDF或?qū)XPS轉(zhuǎn)換為PDF。Aspose.Page可讓您以高保真度和快速渲染執(zhí)行這些轉(zhuǎn)換。
在本文中,將學(xué)習(xí)使用C#以編程方式將OXPS或XPS轉(zhuǎn)換為PDF。以下是將在此博客中討論的功能列表:
目前,.NET版Aspose.page升級到v20.4版,將XPS文件轉(zhuǎn)換為PDF時不會釋放內(nèi)存,同時修復(fù)PS->圖像的背景,感興趣的朋友可點擊下方按鈕下載最新版。
使用Aspose.Page for .NET API,將XPS轉(zhuǎn)換為PDF很簡單。我們將學(xué)習(xí)以下方法來執(zhí)行XPS文件轉(zhuǎn)換:
要將XPS文檔的選定頁面轉(zhuǎn)換為PDF,請按照以下步驟操作:
下面的代碼段遵循這些步驟,并顯示如何使用C#將XPS轉(zhuǎn)換為PDF:
// Initialize PDF output stream using (System.IO.Stream pdfStream = System.IO.File.Open(dataDir + "XPStoPDF.pdf", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write)) // Initialize XPS input stream //using (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)) { // Load XPS document form the stream Aspose.Page.XPS.XpsDocument document = new Aspose.Page.XPS.XpsDocument(xpsStream, new Aspose.Page.XPS.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. 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} }; // Create rendering device for PDF format Aspose.Page.XPS.Presentation.Pdf.PdfDevice device = new Aspose.Page.XPS.Presentation.Pdf.PdfDevice(pdfStream); document.Save(device, options); }
此代碼段使用包含多個頁面的XPS文檔作為輸入文件。而只有頁碼1和3會按照代碼段中的指定轉(zhuǎn)換為PDF。以下屏幕截圖顯示了渲染到PDF文檔的2頁:
可以將整個XPS文件轉(zhuǎn)換為PDF。請按照以下步驟操作,XPS文件的所有頁面都將轉(zhuǎn)換為PDF文件:
下面的代碼段基于所有這些步驟,這些步驟顯示了如何使用C#語言將XPS文件轉(zhuǎn)換為PDF:
// Initialize PDF output stream using (System.IO.Stream pdfStream = System.IO.File.Open(dataDir + "XPStoPDF.pdf", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write)) // Initialize XPS input stream //using (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)) { // Load XPS document form the stream Aspose.Page.XPS.XpsDocument document = new Aspose.Page.XPS.XpsDocument(xpsStream, new Aspose.Page.XPS.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. 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 rendering device for PDF format Aspose.Page.XPS.Presentation.Pdf.PdfDevice device = new Aspose.Page.XPS.Presentation.Pdf.PdfDevice(pdfStream); document.Save(device, options); }
OXPS格式是XPS文件格式的更新和高級形式。但是,某些舊的操作系統(tǒng)不支持此類文件。.NET API的Aspose.Page也能夠轉(zhuǎn)換OXPS文件。讓我們繼續(xù)學(xué)習(xí)以下使用場景:
OXPS文件可能包含許多頁面,并且可以通過以下步驟將任意數(shù)量的頁面轉(zhuǎn)換為PDF:
以下代碼段顯示了如何使用C#將OXPS轉(zhuǎn)換為PDF。如代碼片段中所述,它將OXPS文件的第一頁轉(zhuǎn)換為PDF。
// Initialize PDF output stream using (System.IO.Stream pdfStream = System.IO.File.Open(dataDir + "OXPStoPDF.pdf", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write)) // Initialize OXPS input stream //using (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)) { // Load OXPS document form the stream Aspose.Page.XPS.XpsDocument document = new Aspose.Page.XPS.XpsDocument(xpsStream, new Aspose.Page.XPS.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. 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} }; // Create rendering device for PDF format Aspose.Page.XPS.Presentation.Pdf.PdfDevice device = new Aspose.Page.XPS.Presentation.Pdf.PdfDevice(pdfStream); document.Save(device, options); }
轉(zhuǎn)換OXPS的所有頁面都很簡單,并且與我們上面考慮的示例有關(guān)。讓我們按照以下步驟操作,將OXPS文件的所有頁面轉(zhuǎn)換為一個PDF文檔:
下面的代碼段按以下步驟一步一步進行,并使用C#將OXPS轉(zhuǎn)換為PDF:
// Initialize PDF output stream using (System.IO.Stream pdfStream = System.IO.File.Open(dataDir + "OXPStoPDF.pdf", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write)) // Initialize OXPS input stream //using (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)) { // Load OXPS document form the stream Aspose.Page.XPS.XpsDocument document = new Aspose.Page.XPS.XpsDocument(xpsStream, new Aspose.Page.XPS.XpsLoadOptions()); // or load OXPS document directly from file. No xpsStream is needed then. // XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions()); // Initialize options object with 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 rendering device for PDF format Aspose.Page.XPS.Presentation.Pdf.PdfDevice device = new Aspose.Page.XPS.Presentation.Pdf.PdfDevice(pdfStream); document.Save(device, options); }
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn