XPS的代码刷和渐变
用于处理 XPS 文件的画笔和渐变的 C++ API 解决方案。
Aspose.Page API Solution for C++ 为您提供了操作 XPS 文件矢量图形的功能。它可以让您创建具有不同颜色和纹理的各种几何形状。还可以了解有关 如何使用画笔 的 Aspose.Page 文档。
要操作 XPS 文件的画笔和渐变,我们需要:
Aspose.Page for C++ API 是一个功能丰富、功能强大且易于使用的 C++ 平台文档操作和转换 API。
您可以直接下载其最新版本,只需打开NuGet包管理器,然后搜索Aspose.Page.Cpp并安装。您还可以从包管理器控制台使用以下命令。
Package Manager Console Command
PM> Install-Package Aspose.Page.Cpp
通过 C++ 使用视觉画笔
Aspose.Page for C++ API 允许您使用几行代码将网格添加到 XPS 文件中。对于新文档和现有文档都可以执行此操作。 XpsVisualBrush 类用于设置网格的属性,利用 XpsPathGeometry 和 XpsCanvas 对象。要将网格添加到 XPS 文档中,您需要执行以下步骤:
- 使用 XpsDocument 类创建一个 Document 对象
- 创建一个 XpsPathGeometry 对象并定义必要的段。
- 利用 XpsCanvas 类设置渲染转换。
- 根据需要调整填充和不透明度属性。
- 通过调用 Save() 方法保存文档。
Namespaces in use
using Aspose::Page::XPS;
using Aspose::Page::XPS::XpsModel;
using System::Drawing;
使用视觉画笔创建网格
// The path to the documents directory. | |
System::String dataDir = RunExamples::GetDataDir_WorkingWithVisualBrush(); | |
System::SharedPtr<XpsDocument> doc = System::MakeObject<XpsDocument>(); | |
// Geometry for magenta grid VisualBrush | |
System::SharedPtr<XpsPathGeometry> pathGeometry = doc->CreatePathGeometry(); | |
pathGeometry->AddSegment(doc->CreatePolyLineSegment(System::MakeArray<System::Drawing::PointF>({System::Drawing::PointF(240.f, 5.f), System::Drawing::PointF(240.f, 310.f), System::Drawing::PointF(0.f, 310.f)}))); | |
pathGeometry->idx_get(0)->set_StartPoint(System::Drawing::PointF(0.f, 5.f)); | |
// Canvas for magenta grid VisualBrush | |
System::SharedPtr<XpsCanvas> visualCanvas = doc->CreateCanvas(); | |
System::SharedPtr<XpsPath> visualPath = visualCanvas->AddPath(doc->CreatePathGeometry(u"M 0,4 L 4,4 4,0 6,0 6,4 10,4 10,6 6,6 6,10 4,10 4,6 0,6 Z")); | |
visualPath->set_Fill(doc->CreateSolidColorBrush(doc->CreateColor(1.f, .61f, 0.1f, 0.61f))); | |
System::SharedPtr<XpsPath> gridPath = doc->AddPath(pathGeometry); | |
//Create Visual Brush, it is specified by some XPS fragment (vector graphics and glyphs) | |
gridPath->set_Fill(doc->CreateVisualBrush(visualCanvas, System::Drawing::RectangleF(0.f, 0.f, 10.f, 10.f), System::Drawing::RectangleF(0.f, 0.f, 10.f, 10.f))); | |
(System::ExplicitCast<Aspose::Page::XPS::XpsModel::XpsVisualBrush>(gridPath->get_Fill()))->set_TileMode(Aspose::Page::XPS::XpsModel::XpsTileMode::Tile); | |
// New canvas | |
System::SharedPtr<XpsCanvas> canvas = doc->AddCanvas(); | |
canvas->set_RenderTransform(doc->CreateMatrix(1.f, 0.f, 0.f, 1.f, 268.f, 70.f)); | |
// Add grid | |
canvas->AddPath(pathGeometry); | |
// Red transparent rectangle in the middle top | |
System::SharedPtr<XpsPath> path = canvas->AddPath(doc->CreatePathGeometry(u"M 30,20 l 258.24,0 0,56.64 -258.24,0 Z")); | |
path = canvas->AddPath(doc->CreatePathGeometry(u"M 10,10 L 228,10 228,100 10,100")); | |
path->set_Fill(doc->CreateSolidColorBrush(doc->CreateColor(1.0f, 0.0f, 0.0f))); | |
path->set_Opacity(0.7f); | |
// Save resultant XPS document | |
doc->Save(dataDir + u"output/" + u"AddGrid_out.xps"); |
使用 C++ 添加水平渐变。
适用于 C++ 的 Aspose.Page API 解决方案允许您使用 XpsGradientBrush 类。此类用于将 XpsGradientStop 和 XpsPath 信息指定到代表 XPS 文件的 XpsDocument 对象。以下是如何执行此操作的示例:
- 初始化 XpsDocument 对象。
- 定义 XPS 文档的 XpsGradientStop 和 XpsPath 参数。
- 设置渲染信息。
- 使用 Save() 方法保存文档。
将水平渐变插入 XPS
// The path to the documents directory. | |
System::String dataDir = RunExamples::GetDataDir_WorkingWithGradient(); | |
// Create new XPS Document | |
System::SharedPtr<XpsDocument> doc = System::MakeObject<XpsDocument>(); | |
// Initialize List of XpsGradentStop | |
System::SharedPtr<System::Collections::Generic::List<System::SharedPtr<XpsGradientStop>>> stops = System::MakeObject<System::Collections::Generic::List<System::SharedPtr<XpsGradientStop>>>(); | |
stops->Add(doc->CreateGradientStop(doc->CreateColor(255, 244, 253, 225), 0.0673828f)); | |
stops->Add(doc->CreateGradientStop(doc->CreateColor(255, 251, 240, 23), 0.314453f)); | |
stops->Add(doc->CreateGradientStop(doc->CreateColor(255, 252, 209, 0), 0.482422f)); | |
stops->Add(doc->CreateGradientStop(doc->CreateColor(255, 241, 254, 161), 0.634766f)); | |
stops->Add(doc->CreateGradientStop(doc->CreateColor(255, 53, 253, 255), 0.915039f)); | |
stops->Add(doc->CreateGradientStop(doc->CreateColor(255, 12, 91, 248), 1.f)); | |
// Create new path by defining geometery in abbreviation form | |
System::SharedPtr<XpsPath> path = doc->AddPath(doc->CreatePathGeometry(u"M 10,210 L 228,210 228,300 10,300")); | |
path->set_RenderTransform(doc->CreateMatrix(1.f, 0.f, 0.f, 1.f, 20.f, 70.f)); | |
path->set_Fill(doc->CreateLinearGradientBrush(System::Drawing::PointF(10.f, 0.f), System::Drawing::PointF(228.f, 0.f))); | |
(System::ExplicitCast<Aspose::Page::XPS::XpsModel::XpsGradientBrush>(path->get_Fill()))->get_GradientStops()->AddRange(stops); | |
// Save resultant XPS document | |
doc->Save(dataDir + u"output/" + u"AddHorizontalGradient_outXPS.xps"); |
XPS 什么是XPS文件格式
XPS 格式类似于 PDF 格式。两者都是页面描述语言 (PDL) 格式。 EPS 基于 HTML 而不是 PostScript 语言。 .eps 文件能够包含文档结构的标记以及有关文档外观的信息。还添加了有关如何打印和呈现文档的说明。该格式的特点是它修复了文档的描述,这意味着无论谁以及从哪个操作系统打开它,它看起来都是一样的。