XPS 包内的跨包操作
通过 C++ 操作 XPS 包中的页面、颜色和字形 (Glyphs)
用于 C++ 的 Aspose.Page API 提供了用于处理 XPS 文件的独立类库,使您可以将它们作为一种独特的格式进行处理。该类库拥有多种有用的功能,包括合并 XPS 文档、将其转换为其他格式,以及在其内操纵图形。
XPS 文件的一个关键特性是它们能够在单个文档中包含多个文件。因此,Aspose.Page XPS 类库提供了管理这些内部文件及其页面的功能。这些称为“跨包操作 (cross-package operations)”的操作,涉及跨不同 XPS 文档操作内容。
本节将深入探讨跨包操作的具体示例,例如管理单个 XPS 文档中的页面,以及添加具有特定颜色的文本(字形)。
但是要尝试该功能,您首先需要获取解决方案:
打开 NuGet 包管理器 (Package Manager),搜索 Aspose.Page 并安装。您也可以在程序包管理器控制台 (Package Manager Console) 中使用以下命令。
在 XPS Package C++ 中操作页面的步骤。
- 设置文档目录的路径。
- 使用 XpsDocument 类 创建一个 XPS 文件。
- 要将活动页面从一个文档插入到另一个文档的开头,请使用 InsertPage() 方法。
- 要将活动页面从一个文档插入到另一个文档的末尾,请使用 AddPage() 方法。
- 要删除空白页,请使用 RemovePage() 方法。
- 要将页面从一个文档删除(移动)到另一个文档,请使用 InsertPage() 和 SelectActivePage() 方法。
- 使用 XPsDocument.Save 方法保存更改后的 XPS 文档。
操作页面
// The path to the documents directory.
System::String dataDir = RunExamples::GetDataDir_WorkingWithCrossPackageOperations();
// Create the first XPS Document
System::SharedPtr<XpsDocument> doc1 = System::MakeObject<XpsDocument>(dataDir + u"input1.xps");
// Create the second XPS Document
System::SharedPtr<XpsDocument> doc2 = System::MakeObject<XpsDocument>(dataDir + u"input2.xps");
// Create the third XPS Document
System::SharedPtr<XpsDocument> doc3 = System::MakeObject<XpsDocument>(dataDir + u"input3.xps");
// Create the fourth XPS Document
System::SharedPtr<XpsDocument> doc4 = System::MakeObject<XpsDocument>();
// Insert active page (1 in this case) from the second document to the beginning of the fourth document
doc4->InsertPage(1, doc2->get_Page(), false);
// Insert active page (1 in this case) from the third document to the end of the fourth document
doc4->AddPage(doc3->get_Page(), false);
// Remove page 2 from the fourth document. This is an empty page that was created when document has been created.
doc4->RemovePageAt(2);
// Insert page 3 from the first document to the second postion of the fourth document
doc4->InsertPage(2, doc1->SelectActivePage(3), false);
// Save the fourth XPS document
doc4->Save(dataDir + u"output/" + u"out.xps");
在 XPS Package C++ 内添加字形克隆的步骤。
- 设置文档目录的路径。
- 打开 XPS 文件的流 (stream)。
- 使用 XpsDocument 类创建一个 XPS 文件。
- 使用 AddGlyphs() 方法将字形添加到文档中。
- 使用 XpsDocument 类创建第二个 XPS 文件。
- 要将字形从第一个文件克隆(复制)到第二个文件,请使用 Add() 和 Clone() 方法。
- 通过 XPsDocument.Save() 方法保存两个 XPS 文档。
添加字形克隆并更改颜色
// The path to the documents directory.
System::String dataDir = RunExamples::GetDataDir_WorkingWithCrossPackageOperations();
// Create the first XPS Document
System::SharedPtr<XpsDocument> doc1 = System::MakeObject<XpsDocument>();
// Add glyphs to the first document
System::SharedPtr<XpsGlyphs> glyphs = doc1->AddGlyphs(u"Times New Roman", 200.0f, System::Drawing::FontStyle::Bold, 50.0f, 250.0f, u"Test");
// Fill glyphs in the first document with one color
glyphs->set_Fill(doc1->CreateSolidColorBrush(System::Drawing::Color::get_Green()));
// Create the second XPS Document
System::SharedPtr<XpsDocument> doc2 = System::MakeObject<XpsDocument>();
// Add glyphs cloned from the one's from the first document
glyphs = doc2->Add<System::SharedPtr<XpsGlyphs>>(glyphs->Clone());
// Fill glyphs in the second document with another color
(System::ExplicitCast<Aspose::Page::XPS::XpsModel::XpsSolidColorBrush>(glyphs->get_Fill()))->set_Color(doc2->CreateColor(System::Drawing::Color::get_Red()));
// Save the first XPS document
doc1->Save(dataDir + u"output/" + u"out1.xps");
// Save the second XPS document
doc2->Save(dataDir + u"output/" + u"out2.xps");
添加图像填充的字形 C++ 的步骤。
- 设置文档目录的路径。
- 打开 XPS 文件的流。
- 使用 XpsDocument 类创建一个 XPS 文件。
- 使用 AddGlyphs() 方法将字形添加到文档中。
- 要用图像笔刷 (image brush) 填充字形,请使用 CreateImageBrush() 方法。
- 使用 XpsDocument 类创建第二个 XPS 文件。
- 使用 AddGlyphs() 方法将带有第一个文档字体的字形添加到第二个文档中。
- 从第一个文档的填充创建图像笔刷,并使用 CreateImageBrush() 方法填充第二个文档中的字形。
- 通过 XPsDocument.Save() 方法保存两个 XPS 文档。
添加图像填充字形和外部图像
// The path to the documents directory.
System::String dataDir = RunExamples::GetDataDir_WorkingWithCrossPackageOperations();
// Create the first XPS Document
System::SharedPtr<XpsDocument> doc1 = System::MakeObject<XpsDocument>();
// Add glyphs to the first document
System::SharedPtr<XpsGlyphs> glyphs1 = doc1->AddGlyphs(u"Times New Roman", 200.0f, System::Drawing::FontStyle::Bold, 50.0f, 250.0f, u"Test");
// Fill the glyphs with an image brush
glyphs1->set_Fill(doc1->CreateImageBrush(dataDir + u"R08SY_NN.tif", System::Drawing::RectangleF(0.f, 0.f, 128.f, 192.f), System::Drawing::RectangleF(0.f, 0.f, 64.f, 96.f)));
(System::ExplicitCast<Aspose::Page::XPS::XpsModel::XpsImageBrush>(glyphs1->get_Fill()))->set_TileMode(Aspose::Page::XPS::XpsModel::XpsTileMode::Tile);
// Create the second XPS Document
System::SharedPtr<XpsDocument> doc2 = System::MakeObject<XpsDocument>();
// Add glyphs with the font from the first document to the second document
System::SharedPtr<XpsGlyphs> glyphs2 = doc2->AddGlyphs(glyphs1->get_Font(), 200.0f, 50.0f, 250.0f, u"Test");
// Create an image brush from the fill of the the first document and fill glyphs in the second document
glyphs2->set_Fill(doc2->CreateImageBrush((System::ExplicitCast<Aspose::Page::XPS::XpsModel::XpsImageBrush>(glyphs1->get_Fill()))->get_Image(), System::Drawing::RectangleF(0.f, 0.f, 128.f, 192.f), System::Drawing::RectangleF(0.f, 0.f, 128.f, 192.f)));
(System::ExplicitCast<Aspose::Page::XPS::XpsModel::XpsImageBrush>(glyphs2->get_Fill()))->set_TileMode(Aspose::Page::XPS::XpsModel::XpsTileMode::Tile);
// Save the first XPS document
doc1->Save(dataDir + u"output/" + u"out1.xps");
// Save the second XPS document
doc2->Save(dataDir + u"output/" + u"out2.xps");XPS 什么是XPS文件格式
XPS(XML Paper Specification)是 Microsoft 的 PDF 替代方案,基于 XML/HTML,跨平台保持布局一致,且不依赖操作系统。