编写 XPS 文件的矢量图形形状代码

处理 XPS 文件矢量图形的 C++ API 解决方案

 

与其他页面描述语言 (Page Description Languages) 一样,XPS 允许处理矢量图形。此 C++ API 解决方案包括操作 XPS 文件的矢量图形。它提供了广泛的功能,其中一部分在代码片段 (code snippet) 中描述。这里展示了几个示例,说明如何将椭圆和矩形形状添加到文件中。此外,您还将学习如何管理这些形状的颜色。通过遵循这种方法,您将能够创建任何需要的几何形状。

要使用 XPS 的矢量图形形状 (vector graphics shapes),我们需要:

  • Aspose.Page for C++ API,它是一款面向 C++ 平台、功能丰富、强大且易于使用的文档操作和转换 API。

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

Package Manager Console Command

    PM> Install-Package Aspose.Page.Cpp

使用 C++ 添加矩形的步骤。

  1. 首先,设置将保存文档的目录的路径。
  2. 通过实例化 (instantiating) XpsDocument 类 创建一个 XPS 文件。
  3. 使用 XpsPath 类的方法创建一个矩形。
  4. 最后,通过调用 XPsDocument.Save 方法来保存修改后的 XPS 文档。
添加一个矩形
    // The path to the documents directory.
    System::String dataDir = RunExamples::GetDataDir_WorkingWithShapes();
    // Create new XPS Document
    System::SharedPtr<XpsDocument> doc = System::MakeObject<XpsDocument>();
    // CMYK (blue) solid color stroked rectangle in the lower left
    System::SharedPtr<XpsPath> path = doc->AddPath(doc->CreatePathGeometry(u"M 20,10 L 220,10 220,100 20,100 Z"));
    path->set_Stroke(doc->CreateSolidColorBrush(doc->CreateColor(dataDir + u"uswebuncoated.icc", System::MakeArray<float>({1.0f, 1.000f, 0.000f, 0.000f, 0.000f}))));
    path->set_StrokeThickness(12.f);
    // Save resultant XPS document
    doc->Save(dataDir + u"output/" + u"AddRectangleXPS_out.xps");

使用 C++ 添加一个椭圆。

要将椭圆或其它图形添加到 XPS 文件中,您需要采取类似的步骤,并使用与添加矩形相同的方法实体 (entities)。

添加一个椭圆
    // The path to the documents directory.
    System::String dataDir = RunExamples::GetDataDir_WorkingWithShapes();
    // Create new XPS Document
    System::SharedPtr<XpsDocument> doc = System::MakeObject<XpsDocument>();
    // Radial gradient stroked ellipse in the lower left
    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(0, 0, 255), 0.f));
    stops->Add(doc->CreateGradientStop(doc->CreateColor(255, 0, 0), .25f));
    stops->Add(doc->CreateGradientStop(doc->CreateColor(0, 255, 0), .5f));
    stops->Add(doc->CreateGradientStop(doc->CreateColor(255, 255, 0), .75f));
    stops->Add(doc->CreateGradientStop(doc->CreateColor(255, 0, 0), 1.f));
    
    System::SharedPtr<XpsPath> path = doc->AddPath(doc->CreatePathGeometry(u"M 20,250 A 100,50 0 1 1 220,250 100,50 0 1 1 20,250"));
    path->set_Stroke(doc->CreateRadialGradientBrush(System::Drawing::PointF(575.f, 125.f), System::Drawing::PointF(575.f, 100.f), 75.f, 50.f));
    (System::ExplicitCast<Aspose::Page::XPS::XpsModel::XpsGradientBrush>(path->get_Stroke()))->set_SpreadMethod(Aspose::Page::XPS::XpsModel::XpsSpreadMethod::Reflect);
    (System::ExplicitCast<Aspose::Page::XPS::XpsModel::XpsGradientBrush>(path->get_Stroke()))->get_GradientStops()->AddRange(stops);
    stops->Clear();
    path->set_StrokeThickness(12.f);
    // Save resultant XPS document
    doc->Save(dataDir + u"output/" + u"AddEllipse_outXPS.xps");

XPS 什么是XPS文件格式

XPS(XML Paper Specification)是 Microsoft 的 PDF 替代方案,基于 XML/HTML,跨平台保持布局一致,且不依赖操作系统。