XPS 的代码画笔和渐变

用于处理 XPS 文件的画笔 (brushes) 和渐变 (gradients) 的 C++ API 解决方案。

 

Aspose.Page API for C++ 提供用于控制 XPS 文件的矢量图形 (vector graphics) 的功能。它允许您创建具有不同颜色和纹理 (textures) 的各种几何形状。您还可以通过 Aspose.Page 文档了解 如何使用画笔 。

为了处理 XPS 文件的画笔和渐变,我们需要:

  • Aspose.Page for C++ API,由于它功能丰富、强大且易于使用的文档操作和转换 API ,因此十分适合 C++ 平台。

  • 您可以直接下载其最新版本,只需打开 NuGet 包管理器 (NuGet package manager) , 搜索 Aspose.Page.Cpp 并安装。您也可以在包管理器控制台 (Package Manager Console) 中使用以下命令。

Package Manager Console Command


    PM> Install-Package Aspose.Page.Cpp

在 C++ 中使用可视画笔 (visual brushes) 的工作

Aspose.Page for C++ API 允许您使用几行代码将网格添加到 XPS 文件中。这可以应用于新文件和现有文件。XpsVisualBrush 类用于利用 XpsPathGeometry 和 XpsCanvas 对象来设置网格的属性。要向 XPS 文件添加网格,您需要执行以下步骤:

  1. 使用 XpsDocument 类创建一个 Document 对象。
  2. 创建一个具有必要定义段 (segments) 的 XpsPathGeometry 对象。
  3. 运用 XpsCanvas 类来设置呈现转换 (rendering transformation)。
  4. 根据需要调整填充 (Fill) 和不透明度 (Opacity) 属性。
  5. 通过调用 Save() 方法来保存文件。
Namespaces in use
using Aspose::Page::XPS;
using Aspose::Page::XPS::XpsModel;
using System::Drawing;
使用控制的可视画笔来创建网格 (grid)图案
    // 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++ 添加水平渐变 (horizontal gradient)。

Aspose.Page for C++ 解决方案允许您使用 XpsGradientBrush 类将不同类型的渐变添加到您的 XPS 文件中。此类用于将 XpsGradientStop 和 XpsPath 信息指定给代表 XPS 文件的 XpsDocument 对象。以下是执行此操作的一个示例:

  1. 初始化 (Initialize) 设定的 XpsDocument 对象。
  2. 为 XPS 文件定义 XpsGradientStop 和 XpsPath 参数。
  3. 设置呈现信息 (rendering information)。
  4. 通过调用 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(XML Paper Specification)是 Microsoft 的 PDF 替代方案,基于 XML/HTML,跨平台保持布局一致,且不依赖操作系统。