翻譯|使用教程|編輯:李顯亮|2020-03-27 10:31:46.593|閱讀 230 次
概述:Aspose.Page for C ++是一個本機C ++庫,用于創建新的PostScript和XPS文件以及以編程方式修改和轉換現有文件。本文演示了如何將PS / EPS或XPS文檔轉換為PDF或光柵圖像格式,包括PNG,JPEG,TIFF和BMP。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
XPS是工作中常用的一種微軟的文檔保存和查看格式,針對XPS、EPS格式的管理控件Aspose.Page已經推出C++版,將能夠在基于C ++的應用程序中以編程方式創建,讀取,編輯,保存和轉換XPS文檔,該API還允許您處理XPS文檔中的頁面和元素,例如畫布和字形。此外,它支持將文檔轉換為PDF和光柵圖像。。你可以點擊下方按鈕下載測試體驗。
與此同時,.NET版和Java版Aspose.Page已更新至v20.3最新版,修復將圖像添加到XPS文件時發生的異常,點擊下方按鈕下載試用。
下載最新版Aspose.Page for .NET 下載最新版Aspose.Page for java
在本文中,將演示如何將PS / EPS或XPS文檔轉換為PDF或光柵圖像格式,包括PNG,JPEG,TIFF和BMP。本文的內容包括:
以下是將PostScript PS / EPS文檔轉換為PDF的步驟:
為輸出PDF文件創建FileStream對象。
將輸入的PostScript文檔加載到FileStream對象中。
使用輸入流創建并初始化PsDocument對象。
使用輸出流創建并初始化PdfDevice對象。
使用PsDocument-> Save方法處理文檔并將其另存為PDF文件。
以下代碼示例顯示了如何在C ++中將PostScript PS文檔轉換為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轉換為圖像格式的步驟。
創建ImageFormat對象以設置輸出圖像的格式,即PNG。
將輸入的PostScript文檔加載到FileStream對象中。
使用輸入流創建并初始化PsDocument對象。
創建一個ImageDevice對象。
使用PsDocument-> Save方法處理文檔并將其另存為圖像。
下面的代碼示例演示如何將PostScript PS / EPS轉換為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文檔轉換為光柵圖像格式的步驟:
將輸入的XPS文檔加載到FileStream對象中。
創建XpsDocument對象,并使用輸入流對象對其進行初始化。
通過創建PngSaveOptions類的對象來設置保存選項。
使用XpsDocument-> Save方法將XPS轉換為圖像。
以下代碼示例顯示了如何在C ++中將XPS轉換為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()); } }還想要更多嗎?您可以點擊閱讀【2019 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問或需求,請隨時加入Aspose技術交流群(642018183),我們很高興為您提供查詢和咨詢。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn