XPSパッケージ内のパッケージ間操作

C++を介してXPSパッケージ内のページ、色、グリフ(文字)を操作する

 

C++用のAspose.Page APIは、XPSファイルを操作するための個別のライブラリを提供しており、XPSファイルを個別のフォーマットとして扱うことができます。このライブラリには、XPSドキュメントのマージ、他のフォーマットへの変換、その中のグラフィックの操作など、さまざまな便利な機能が備わっています。

XPSファイルの重要な側面の1つは、単一のドキュメント内に複数のファイルを含めることができる点です。その結果、Aspose.Page XPSライブラリは、これらの内部ファイルとそのページを管理するための機能を提供します。これらの操作は、「パッケージ間操作(cross-package operations)」と呼ばれ、異なるXPSドキュメント間でコンテンツを操作することを伴います。

このセクションでは、単一のXPSドキュメント内のページの管理や、特定の色を持つテキスト(グリフ)の追加など、パッケージ間操作の具体的な例について詳しく説明します。

ただし、機能を試すには、まずソリューションを取得する必要があります:

  • NuGetパッケージマネージャーを開き、Aspose.Pageを検索してインストールします。 パッケージマネージャーコンソール(Package Manager Console)から次のコマンドを使用することもできます。

XPSパッケージC++内でページを操作する手順。

  1. ドキュメントディレクトリへのパスを設定します。
  2. XpsDocument クラス を使用してXPSファイルを作成します。
  3. あるドキュメントのアクティブなページを別のドキュメントの先頭に挿入するには、 InsertPage() メソッドを使用します。
  4. あるドキュメントのアクティブなページを別のドキュメントの最後に挿入するには、 AddPage() メソッドを使用します。
  5. 空のページを削除するには、 RemovePage() メソッドを使用します。
  6. あるドキュメントから別のドキュメントにページを削除(移動)するには、InsertPage() および SelectActivePage() メソッドを使用します。
  7. 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パッケージC++内にグリフのクローンを追加する手順。

  1. ドキュメントディレクトリへのパスを設定します。
  2. XPSファイルのストリームを開きます。
  3. XpsDocument クラスを使用してXPSファイルを作成します。
  4. AddGlyphs() メソッドを使用して、ドキュメントにグリフを追加します。
  5. XpsDocument クラスを使用して2番目のXPSファイルを作成します。
  6. 最初のファイルから2番目のファイルにグリフをクローン(複製)するには、Add() および Clone() メソッドを使用します。
  7. *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++を追加する手順。

  1. ドキュメントディレクトリへのパスを設定します。
  2. XPSファイルのストリームを開きます。
  3. XpsDocument クラスを使用してXPSファイルを作成します。
  4. *AddGlyphs()*メソッドを使用して、ドキュメントにグリフを追加します。
  5. グリフをイメージブラシで塗りつぶすには、 CreateImageBrush() メソッドを使用します。
  6. XpsDocument クラスを使用して2番目のXPSファイルを作成します。
  7. *AddGlyphs()*メソッドを使用して、最初のドキュメントのフォントを使用して2番目のドキュメントにグリフを追加します。
  8. 最初のドキュメントの塗りつぶしからイメージブラシを作成し、*CreateImageBrush()*メソッドを使用して2番目のドキュメントのグリフを塗りつぶします。
  9. *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 ベースで、プラットフォームに依存せずレイアウトを保持します。