XPS格式处理控件Aspose.Page支持C ++平台!且看如何将EPS和XPS转换为C ++中的PDF和光栅图像

Aspose.Page for C ++是一个本机C ++库,用于创建新的PostScript和XPS文件以及以编程方式修改和转换现有文件。本文演示了如何将PS / EPS或XPS文档转换为PDF或光栅图像格式,包括PNG,JPEG,TIFF和BMP。

XPS是工作中常用的一种微软的文档保存和查看格式,针对XPS、EPS格式的管理控件Aspose.Page已经推出C++版,将能够在基于C ++的应用程序中以编程方式创建,读取,编辑,保存和转换XPS文档,该API还允许您处理XPS文档中的页面和元素,例如画布和字形。此外,它支持将文档转换为PDF和光栅图像。你可以点击下方按钮下载测试体验。


与此同时,.NET版和Java版Aspose.Page已更新至v20.3最新版,修复将图像添加到XPS文件时发生的异常,点击下方按钮下载试用。

                      

在本文中,将演示如何将PS / EPS或XPS文档转换为PDF或光栅图像格式,包括PNG,JPEG,TIFF和BMP。本文的内容包括:

  • 在C ++中将PostScript PS / EPS文档转换为PDF
  • 将PostScript PS / EPS文档转换为C ++中的图像
  • 在C ++中将XPS文档转换为PDF
  • 将XPS文档转换为C ++中的图像

在C ++中将PostScript PS / EPS文档转换为PDF

以下是将PostScript PS / EPS文档转换为PDF的步骤:

  • 为输出PDF文件创建FileStream对象。

  • 将输入的PostScript文档加载到FileStream对象中。

  • 使用输入流创建并初始化PsDocument对象。

  • 使用输出流创建并初始化PdfDevice对象。

  • 使用PsDocument-> Save方法处理文档并将其另存为PDF文件。

以下代码示例显示了如何在C ++中将PostScript PS文档转换为PDF。

// Initialize PDF output streamSystem::SharedPtrpdfStream = System::MakeObject(value + u"PStoPDF.pdf", System::IO::FileMode::Create, System::IO::FileAccess::Write);// Initialize PostScript input streamSystem::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 flagbool 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 PdfDeviceSystem::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 errorsif (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());}}

将PostScript PS / EPS文档转换为C ++图像

以下是将PS / EPS转换为图像格式的步骤。

  • 创建ImageFormat对象以设置输出图像的格式,即PNG。

  • 将输入的PostScript文档加载到FileStream对象中。

  • 使用输入流创建并初始化PsDocument对象。

  • 创建一个ImageDevice对象。

  • 使用PsDocument-> Save方法处理文档并将其另存为图像。

下面的代码示例演示如何将PostScript PS / EPS转换为C ++中的图像。

// Initialize PDF output streamSystem::SharedPtrimageFormat = System::Drawing::Imaging::ImageFormat::get_Png();// Initialize PostScript input streamSystem::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 flagbool 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 ImageDeviceSystem::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' statementSystem::Details::DisposeGuard__dispose_guard_1({ fs });try{fs->Write(imageBytes, 0, imageBytes->get_Length());}catch (...){__dispose_guard_1.SetCurrentException(std::current_exception());}}i++;}}// Review errorsif (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文档转换为C ++中的图像

以下是将XPS文档转换为光栅图像格式的步骤:

  • 将输入的XPS文档加载到FileStream对象中。

  • 创建XpsDocument对象,并使用输入流对象对其进行初始化。

  • 通过创建PngSaveOptions类的对象来设置保存选项。

  • 使用XpsDocument-> Save方法将XPS转换为图像。

以下代码示例显示了如何在C ++中将XPS转换为PNG图像。

// Input fileSystem::String inputFileName =  u"input.xps";// Output fileSystem::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' statementSystem::Details::DisposeGuard__dispose_guard_1({ xpsStream });try{// Load XPS document form the streamSystem::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 formatSystem::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' statementSystem::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技术交流群(),我们很高兴为您提供查询和咨询
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年1月24日
下一篇 2020年1月24日

相关推荐

发表回复

登录后才能评论