翻譯|使用教程|編輯:李顯亮|2020-03-27 10:31:46.593|閱讀 230 次
概述:Aspose.Page for C ++是一個(gè)本機(jī)C ++庫,用于創(chuàng)建新的PostScript和XPS文件以及以編程方式修改和轉(zhuǎn)換現(xiàn)有文件。本文演示了如何將PS / EPS或XPS文檔轉(zhuǎn)換為PDF或光柵圖像格式,包括PNG,JPEG,TIFF和BMP。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
XPS是工作中常用的一種微軟的文檔保存和查看格式,針對(duì)XPS、EPS格式的管理控件Aspose.Page已經(jīng)推出C++版,將能夠在基于C ++的應(yīng)用程序中以編程方式創(chuàng)建,讀取,編輯,保存和轉(zhuǎn)換XPS文檔,該API還允許您處理XPS文檔中的頁面和元素,例如畫布和字形。此外,它支持將文檔轉(zhuǎn)換為PDF和光柵圖像。。你可以點(diǎn)擊下方按鈕下載測(cè)試體驗(yàn)。
與此同時(shí),.NET版和Java版Aspose.Page已更新至v20.3最新版,修復(fù)將圖像添加到XPS文件時(shí)發(fā)生的異常,點(diǎn)擊下方按鈕下載試用。
下載最新版Aspose.Page for .NET 下載最新版Aspose.Page for java
在本文中,將演示如何將PS / EPS或XPS文檔轉(zhuǎn)換為PDF或光柵圖像格式,包括PNG,JPEG,TIFF和BMP。本文的內(nèi)容包括:
以下是將PostScript PS / EPS文檔轉(zhuǎn)換為PDF的步驟:
為輸出PDF文件創(chuàng)建FileStream對(duì)象。
將輸入的PostScript文檔加載到FileStream對(duì)象中。
使用輸入流創(chuàng)建并初始化PsDocument對(duì)象。
使用輸出流創(chuàng)建并初始化PdfDevice對(duì)象。
使用PsDocument-> Save方法處理文檔并將其另存為PDF文件。
以下代碼示例顯示了如何在C ++中將PostScript PS文檔轉(zhuǎn)換為PDF。
// Initialize PDF output stream System::SharedPtrpdfStream = System::MakeObject(value + u"PStoPDF.pdf", System::IO::FileMode::Create, System::IO::FileAccess::Write); // Initialize PostScript input stream System::SharedPtrpsStream = System::MakeObject(value + u"input.ps", System::IO::FileMode::Open, System::IO::FileAccess::Read); System::SharedPtrdocument = System::MakeObject(psStream); // If you want to convert Postscript file despite of minor errors set this flag bool suppressErrors = true; // Initialize options object with necessary parameters. System::SharedPtroptions = System::MakeObject(suppressErrors); // If you want to add special folder where fonts are stored. Default fonts folder in OS is always included. options->set_AdditionalFontsFolders(System::MakeArray({ u"{FONT_FOLDER}" })); // Default page size is 595x842 and it is not mandatory to set it in PdfDevice System::SharedPtrdevice = System::MakeObject(pdfStream); // But if you need to specify size and image format use following line: // Aspose.Page.EPS.Device.PdfDevice device = new Aspose.Page.EPS.Device.PdfDevice(pdfStream, new System.Drawing.Size(595, 842)); { auto __finally_guard_0 = ::System::MakeScopeGuard([&psStream, &pdfStream]() { psStream->Close(); pdfStream->Close(); }); try { document->Save(device, options); } catch (...) { throw; } } // Review errors if (suppressErrors) { //auto ex_enumerator = (System::DynamicCastEnumerableTo(options->get_Exceptions()))->GetEnumerator(); auto ex_enumerator = (options->get_Exceptions())->GetEnumerator(); decltype(ex_enumerator->get_Current()) ex; while (ex_enumerator->MoveNext() && (ex = ex_enumerator->get_Current(), true)) { System::Console::WriteLine(ex->get_Message()); } }
以下是將PS / EPS轉(zhuǎn)換為圖像格式的步驟。
創(chuàng)建ImageFormat對(duì)象以設(shè)置輸出圖像的格式,即PNG。
將輸入的PostScript文檔加載到FileStream對(duì)象中。
使用輸入流創(chuàng)建并初始化PsDocument對(duì)象。
創(chuàng)建一個(gè)ImageDevice對(duì)象。
使用PsDocument-> Save方法處理文檔并將其另存為圖像。
下面的代碼示例演示如何將PostScript PS / EPS轉(zhuǎn)換為C ++中的圖像。
// Initialize PDF output stream System::SharedPtrimageFormat = System::Drawing::Imaging::ImageFormat::get_Png(); // Initialize PostScript input stream System::SharedPtrpsStream = System::MakeObject(value + u"inputForImage.ps", System::IO::FileMode::Open, System::IO::FileAccess::Read); System::SharedPtrdocument = System::MakeObject(psStream); // If you want to convert PostScript file despite of minor errors the set this flag bool suppressErrors = true; // Initialize options object with necessary parameters. System::SharedPtroptions = System::MakeObject(suppressErrors); // If you want to add special folder where fonts are stored. Default fonts folder in OS is always included. options->set_AdditionalFontsFolders(System::MakeArray({ u"{FONT_FOLDER}" })); // Default image format is PNG and it is not mandatory to set it in ImageDevice // Default image size is 595x842 and it is not mandatory to set it in ImageDevice System::SharedPtrdevice = System::MakeObject(); // But if you need to specify size and image format use constructor with parameters //ImageDevice device = new ImageDevice(new System.Drawing.Size(595, 842), System.Drawing.Imaging.ImageFormat.Jpeg); { auto __finally_guard_0 = ::System::MakeScopeGuard([&psStream]() { psStream->Close(); }); try { document->Save(device, options); } catch (...) { throw; } } System::ArrayPtrimagesBytes = device->get_ImagesBytes(); int32_t i = 0; { for (System::ArrayPtrimageBytes : imagesBytes) { System::String imagePath = System::IO::Path::GetFullPath(value + System::String(u"out_image") + System::Convert::ToString(i) + u"." + System::ObjectExt::ToString(imageFormat).ToLower()); { System::SharedPtrfs = System::MakeObject(imagePath, System::IO::FileMode::Create, System::IO::FileAccess::Write); // Clearing resources under 'using' statement System::Details::DisposeGuard__dispose_guard_1({ fs }); try { fs->Write(imageBytes, 0, imageBytes->get_Length()); } catch (...) { __dispose_guard_1.SetCurrentException(std::current_exception()); } } i++; } } // Review errors if (suppressErrors) { //auto ex_enumerator = (System::DynamicCastEnumerableTo(options->get_Exceptions()))->GetEnumerator(); //decltype(ex_enumerator->get_Current()) ex; //while (ex_enumerator->MoveNext() && (ex = ex_enumerator->get_Current(), true)) //{ // System::Console::WriteLine(ex->get_Message()); //} }
以下是將XPS文檔轉(zhuǎn)換為光柵圖像格式的步驟:
將輸入的XPS文檔加載到FileStream對(duì)象中。
創(chuàng)建XpsDocument對(duì)象,并使用輸入流對(duì)象對(duì)其進(jìn)行初始化。
通過創(chuàng)建PngSaveOptions類的對(duì)象來設(shè)置保存選項(xiàng)。
使用XpsDocument-> Save方法將XPS轉(zhuǎn)換為圖像。
以下代碼示例顯示了如何在C ++中將XPS轉(zhuǎn)換為PNG圖像。
// Input file System::String inputFileName = u"input.xps"; // Output file System::String outputFileName = u"XPStoImage_out.png"; // Initialize XPS input stream { System::SharedPtrxpsStream = System::IO::File::Open(inputFileName, System::IO::FileMode::Open, System::IO::FileAccess::Read); // Clearing resources under 'using' statement System::Details::DisposeGuard__dispose_guard_1({ xpsStream }); try { // Load XPS document form the stream System::SharedPtrdocument = System::MakeObject(xpsStream, System::MakeObject()); // 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::SharedPtroptions = [&] { auto tmp_0 = System::MakeObject(); tmp_0->set_SmoothingMode(System::Drawing::Drawing2D::SmoothingMode::HighQuality); tmp_0->set_Resolution(300); tmp_0->set_PageNumbers(System::MakeArray({ 1, 2, 6 })); return tmp_0; }(); // Create rendering device for PDF format System::SharedPtrdevice = System::MakeObject(); document->Save(device, options); // Iterate through document partitions (fixed documents, in XPS terms) for (int32_t i = 0; i < device->get_Result()->get_Length(); i++) { for (int32_t j = 0; j < device->get_Result()[i]->get_Length(); j++) { // Initialize image output stream { System::SharedPtrimageStream = System::IO::File::Open(System::IO::Path::GetDirectoryName(outputFileName) + u"\\" + System::IO::Path::GetFileNameWithoutExtension(outputFileName) + u"_" + (i + 1) + u"_" + (j + 1) + System::IO::Path::GetExtension(outputFileName), System::IO::FileMode::Create, System::IO::FileAccess::Write); // Clearing resources under 'using' statement System::Details::DisposeGuard__dispose_guard_0({ imageStream }); try { imageStream->Write(device->get_Result()[i][j], 0, device->get_Result()[i][j]->get_Length()); } catch (...) { __dispose_guard_0.SetCurrentException(std::current_exception()); } } } } } catch (...) { __dispose_guard_1.SetCurrentException(std::current_exception()); } }還想要更多嗎?您可以點(diǎn)擊閱讀【2019 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問或需求,請(qǐng)隨時(shí)加入Aspose技術(shù)交流群(642018183),我們很高興為您提供查詢和咨詢。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn