3分钟学会使用Aspose.Page在C++上创建编辑PostScript和XPS文档!

与世无争的帅哥 提交于 2020-03-26 12:04:20

3 月,跳不动了?>>>

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

本文演示了如何使用Aspose.Page for C ++执行与PostScript和XPS文档有关的以下操作。

  • 用C ++创建一个新的XPS文档
  • 在C ++中编辑现有的XPS文档
  • 将页面或文档添加到C ++中的XPS文档中

用C ++创建XPS文档

以下是使用Aspose.Page for C ++创建包含文本和图像的XPS文档的简单步骤。

  • 创建XpsDocument类的对象。

  • 分别使用XpsGlyphs和XpsPath对象添加文本和图像。

  • 使用保存方法保存文档。

下面的代码示例演示如何使用C ++创建XPS文档。

// 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");

输出结果

在C ++中编辑XPS文档

以下是编辑现有XPS文档的步骤:

  • 创建XpsDocument类的对象,并使用XPS文档的路径对其进行初始化。

  • 使用XpsDocument对象访问文档的元素。

  • 使用保存方法保存更新的文档。

下面的代码示例演示如何使用C ++编辑现有的XPS文档。

// Load XPS Document
auto doc = System::MakeObject(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");

在C ++中将页面和文档添加到XPS

Aspose.Page for C ++还允许您在XPS文档中添加页面以及多个文档。以下是创建新XPS文档并添加其他页面和文档的步骤。

  • 创建XpsDocument类的对象。

  • 使用AddPage和AddDocument方法分别添加页面和文档。

  • 使用SelectActiveDocument方法选择要处理的活动文档。

  • 在文档中添加文本或图像。

  • 保存文档。

下面的代码示例演示如何使用C ++将其他页面和文档添加到XPS。

// New document (1 fixed document with 1 default size page)
auto doc = System::MakeObject();
// 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::SharedPtrtextFill = doc->CreateSolidColorBrush(System::Drawing::Color::get_Black());
System::SharedPtrglyphs = 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");

如果您有任何疑问或需求,请随时加入Aspose技术交流群(642018183),我们很高兴为您提供查询和咨询

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!