Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// New document (1 fixed document with 1 default size page)
auto doc = System::MakeObject<XpsDocument>();
// Add 2nd page on 1st document and set active
doc->AddPage();
// Add 2nd document with 1 page (3rd page in file)
doc->AddDocument(false);
// 1st document's 2nd page is still active
System::SharedPtr<XpsSolidColorBrush> textFill = doc->CreateSolidColorBrush(System::Drawing::Color::get_Black());
System::SharedPtr<XpsGlyphs> glyphs = doc->AddGlyphs(u"Arial", 12.0f, System::Drawing::FontStyle::Regular, 200.f, 500.f, u"Text on Page 2 (Document 1),");
glyphs->set_Fill(textFill);
// Activate 2nd document
doc->SelectActiveDocument(2);
glyphs = doc->AddGlyphs(u"Arial", 12.0f, System::Drawing::FontStyle::Regular, 200.f, 500.f, u"Text on Document 2 (Page #3 in file),");
glyphs->set_Fill(textFill);
// Save XPS file
doc->Save(u"Create-XPS.xps");
// Initialize PDF output stream
System::SharedPtr<System::Drawing::Imaging::ImageFormat> imageFormat = System::Drawing::Imaging::ImageFormat::get_Png();
// Initialize PostScript input stream
System::SharedPtr<System::IO::FileStream> psStream = System::MakeObject<System::IO::FileStream>(value + u"inputForImage.ps", System::IO::FileMode::Open, System::IO::FileAccess::Read);
System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(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::SharedPtr<ImageSaveOptions> options = System::MakeObject<ImageSaveOptions>(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<System::String>({ 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::SharedPtr<Aspose::Page::EPS::Device::ImageDevice> device = System::MakeObject<Aspose::Page::EPS::Device::ImageDevice>();
// 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::ArrayPtr<System::ArrayPtr<uint8_t>> imagesBytes = device->get_ImagesBytes();
int32_t i = 0;
{
for (System::ArrayPtr<uint8_t> imageBytes : 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::SharedPtr<System::IO::FileStream> fs = System::MakeObject<System::IO::FileStream>(imagePath, System::IO::FileMode::Create, System::IO::FileAccess::Write);
// Clearing resources under 'using' statement
System::Details::DisposeGuard<1> __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<PsConverterException>(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());
//}
}
// Initialize PDF output stream
System::SharedPtr<System::IO::FileStream> pdfStream = System::MakeObject<System::IO::FileStream>(value + u"PStoPDF.pdf", System::IO::FileMode::Create, System::IO::FileAccess::Write);
// Initialize PostScript input stream
System::SharedPtr<System::IO::FileStream> psStream = System::MakeObject<System::IO::FileStream>(value + u"input.ps", System::IO::FileMode::Open, System::IO::FileAccess::Read);
System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(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::SharedPtr<PdfSaveOptions> options = System::MakeObject<PdfSaveOptions>(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<System::String>({ u"{FONT_FOLDER}" }));
// Default page size is 595x842 and it is not mandatory to set it in PdfDevice
System::SharedPtr<Aspose::Page::EPS::Device::PdfDevice> device = System::MakeObject<Aspose::Page::EPS::Device::PdfDevice>(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<PsConverterException>(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());
}
}
// Input file
System::String inputFileName = u"input.xps";
// Output file
System::String outputFileName = u"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 form 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 options 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 PDF 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());
}
}
System::SharedPtr<System::IO::Stream> pdfStream = System::IO::File::Open(u"XPStoPDF.pdf", System::IO::FileMode::Create, System::IO::FileAccess::Write);
// Clearing resources under 'using' statement
System::Details::DisposeGuard<1> __dispose_guard_1({ pdfStream });
try {
System::SharedPtr<System::IO::Stream> xpsStream = System::IO::File::Open(u"input.xps", System::IO::FileMode::Open, System::IO::FileAccess::Read);
// Clearing resources under 'using' statement
System::Details::DisposeGuard<1> __dispose_guard_0({ xpsStream });
try
{
// Load XPS document form 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 options object with necessary parameters.
System::SharedPtr<Aspose::Page::XPS::Presentation::Pdf::PdfSaveOptions> options = [&] { auto tmp_0 = System::MakeObject<Aspose::Page::XPS::Presentation::Pdf::PdfSaveOptions>(); tmp_0->set_JpegQualityLevel(100); tmp_0->set_ImageCompression(Aspose::Page::XPS::Presentation::Pdf::PdfImageCompression::Jpeg); tmp_0->set_TextCompression(Aspose::Page::XPS::Presentation::Pdf::PdfTextCompression::Flate); tmp_0->set_PageNumbers(System::MakeArray<int32_t>({ 1, 2, 6 })); return tmp_0; }();
// Create rendering device for PDF format
System::SharedPtr<Aspose::Page::XPS::Presentation::Pdf::PdfDevice> device = System::MakeObject<Aspose::Page::XPS::Presentation::Pdf::PdfDevice>(pdfStream);
document->Save(device, options);
}
catch (...)
{
__dispose_guard_0.SetCurrentException(std::current_exception());
}
}
catch (...)
{
__dispose_guard_1.SetCurrentException(std::current_exception());
}
// Create a new XpsDocument object
auto doc = System::MakeObject<XpsDocument>();
// Add Image
System::SharedPtr<XpsPath> path = doc->AddPath(doc->CreatePathGeometry(u"M 30,20 l 450.24,0 0,150.64 -350.24,0 Z"));
// Create a matrix that can be used for proper positioning.
path->set_RenderTransform(doc->CreateMatrix(0.7f, 0.f, 0.f, 0.7f, 0.f, 20.f));
// Create Image Brush
path->set_Fill(doc->CreateImageBrush(u"QL_logo_color.tif", System::Drawing::RectangleF(0.f, 0.f, 450.24f, 150.64f), System::Drawing::RectangleF(50.f, 20.f, 450.68f, 150.48f)));
// Text as footer
System::SharedPtr<XpsSolidColorBrush> textFill = doc->CreateSolidColorBrush(System::Drawing::Color::get_Black());
System::SharedPtr<XpsGlyphs> glyphs = doc->AddGlyphs(u"Arial", 18.0f, System::Drawing::FontStyle::Regular, 40.f, 1015.f, u"Copyright &#xa9; 2006 QualityLogic, Inc.");
glyphs->set_Fill(textFill);
glyphs = doc->AddGlyphs(u"Arial", 15.0f, System::Drawing::FontStyle::Regular, 475.f, 1003.f, u"For information on QualityLogic XPS test products,");
glyphs->set_Fill(textFill);
// Save as XPS
doc->Save(u"Create-XPS.xps");
// Load XPS Document
auto doc = System::MakeObject<XpsDocument>(u"Created-XPS.xps");
// Add empty page at end of pages list
doc->AddPage();
// Insert an empty page at beginning of pages list
doc->InsertPage(1, true);
// Save XPS file
doc->Save(u"Updated-XPS.xps");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.