Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/f6f507e3d65de7589ca5692ed42c60a5 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/f6f507e3d65de7589ca5692ed42c60a5 to your computer and use it in GitHub Desktop.
Convert XPS, OXPS to JPG or PNG Image using C++
Convert XPS, OXPS to JPG or PNG Image using C++
// Input file
System::String inputFileName = u"SourceDirectory\\sample.xps";
//Output file
System::String outputFileName = u"OutputDirectory\\XPStoImage_out.jpeg";
// Initialize XPS input stream
{
System::SharedPtr<System::IO::Stream> xpsStream = System::IO::File::Open(inputFileName, System::IO::FileMode::Open, System::IO::FileAccess::Read);
// Clearing resources under 'using' statement
System::Details::DisposeGuard<1> __dispose_guard_1({ xpsStream });
// ------------------------------------------
try
{
// Load XPS document from the stream
System::SharedPtr<XpsDocument> document = System::MakeObject<XpsDocument>(xpsStream, System::MakeObject<XpsLoadOptions>());
// or load XPS document directly from file. No xpsStream is needed then.
// XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());
// Initialize JpegSaveOptions object with necessary parameters.
System::SharedPtr<JpegSaveOptions> options = [&] {
auto tmp_0 = System::MakeObject<JpegSaveOptions>();
tmp_0->set_SmoothingMode(System::Drawing::Drawing2D::SmoothingMode::HighQuality);
tmp_0->set_Resolution(300);
tmp_0->set_PageNumbers(System::MakeArray<int32_t>({ 1, 2, 6 }));
return tmp_0;
}();
// Create rendering device for JPG format
System::SharedPtr<ImageDevice> device = System::MakeObject<ImageDevice>();
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::SharedPtr<System::IO::Stream> imageStream = 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<1> __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());
}
}
// Input file
System::String inputFileName = u"SourceDirectory\\sample.xps";
//Output file
System::String outputFileName = u"OutputDirectory\\XPStoImage_out.png";
// Initialize XPS input stream
{
System::SharedPtr<System::IO::Stream> xpsStream = System::IO::File::Open(inputFileName, System::IO::FileMode::Open, System::IO::FileAccess::Read);
// Clearing resources under 'using' statement
System::Details::DisposeGuard<1> __dispose_guard_1({ xpsStream });
// ------------------------------------------
try
{
// Load XPS document from the stream
System::SharedPtr<XpsDocument> document = System::MakeObject<XpsDocument>(xpsStream, System::MakeObject<XpsLoadOptions>());
// or load XPS document directly from file. No xpsStream is needed then.
// XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());
// Initialize PngSaveOptions object with necessary parameters.
System::SharedPtr<PngSaveOptions> options = [&] {
auto tmp_0 = System::MakeObject<PngSaveOptions>();
tmp_0->set_SmoothingMode(System::Drawing::Drawing2D::SmoothingMode::HighQuality);
tmp_0->set_Resolution(300);
tmp_0->set_PageNumbers(System::MakeArray<int32_t>({ 1, 2, 6 }));
return tmp_0;
}();
// Create rendering device for PNG format
System::SharedPtr<ImageDevice> device = System::MakeObject<ImageDevice>();
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::SharedPtr<System::IO::Stream> imageStream = 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<1> __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());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment