XPSパッケージ内でのパッケージ間操作
C#によるXPSパッケージ内のページ、色、グリフの操作
Aspose.Page API Solution for .NETには、他の形式に加えて、XPSファイルを操作するための個別のライブラリとしてXPSパッケージが含まれています。その豊富な機能には、ファイルの結合、変換、グラフィックスの操作など、多くの便利で人気のある機能が含まれています。
XPSは1つのドキュメントに複数のファイルを保持できます。そのため、すべてのXPSパッケージには、ドキュメント内および異なるXPSドキュメント間でそれらのファイルとそのページを操作する機能が必要です。このような操作はパッケージ間操作と呼ばれます。これらについては別途説明する必要があります。
ここでは、ページの操作、グリフや色の追加などのパッケージ間操作の例を紹介します。
C#でXPSパッケージ内のページを操作する手順。
- ドキュメントディレクトリへのパスを設定します。
- XpsDocumentクラス を使用してXPSファイルを作成します。
- あるドキュメントのアクティブなページを別のドキュメントの最初に挿入するには、 InsertPage() メソッドを使用します。
- あるドキュメントのアクティブなページを別のドキュメントの最後に追加するには、 AddPage() メソッドを使用します。
- 空のページを削除するには、 RemovePage() メソッドを使用します。
- あるドキュメントから別のドキュメントへページを移動するには、*InsertPage()*と SelectActivePage() メソッドを使用します。
- XPsDocument.Save を使用して変更されたXPSドキュメントを保存します。
ページを跨いだパッケージ操作のC#コード
using Aspose.Page.XPS;
using Aspose.Page.XPS.XpsModel;
using System.Drawing; // The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithCrossPackageOperations();
// Create the first XPS Document
XpsDocument doc1 = new XpsDocument(dataDir + "input1.xps");
// Create the second XPS Document
XpsDocument doc2 = new XpsDocument(dataDir + "input2.xps");
// Create the third XPS Document
XpsDocument doc3 = new XpsDocument(dataDir + "input3.xps");
// Create the fourth XPS Document
XpsDocument doc4 = new XpsDocument();
// Insert the active page (1 in this case) from the second document to the beginning of the fourth document
doc4.InsertPage(1, doc2.Page, false);
// Insert the active page (1 in this case) from the third document to the end of the fourth document
doc4.AddPage(doc3.Page, false);
// Remove page 2 from the fourth document. This is an empty page that was created when the document had 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 + "out.xps");C#でXPSパッケージ内にグリフのクローンを追加する手順。
- ドキュメントディレクトリへのパスを設定します。
- XPSファイルのストリームを開きます。
- XpsDocumentクラスを使用してXPSファイルを作成します。
- AddGlyphs() メソッドを使用してドキュメントにグリフを追加します。
- 2つ目のXPSファイルをXpsDocumentクラスを使用して作成します。
- 最初のファイルから2つ目のファイルへグリフをクローンするには、*Add()*と Clone() メソッドを使用します。
- *XPsDocument.Save()*メソッドを使用して両方のXPSドキュメントを保存します。
XPSパッケージ内でグリフをコピーするC#コード
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithCrossPackageOperations();
// Create the first XPS Document
XpsDocument doc1 = new XpsDocument();
// Add glyphs to the first document
XpsGlyphs glyphs = doc1.AddGlyphs("Times New Roman", 200, FontStyle.Bold, 50, 250, "Test");
// Fill glyphs in the first document with one color
glyphs.Fill = doc1.CreateSolidColorBrush(Color.Green);
// Create the second XPS Document
XpsDocument doc2 = new XpsDocument();
// Add glyphs cloned from the one's from the first document
glyphs = doc2.Add(glyphs.Clone());
// Fill glyphs in the second document with another color
((XpsSolidColorBrush)glyphs.Fill).Color = doc2.CreateColor(Color.Red);
// Save the first XPS document
doc1.Save(dataDir + "out1.xps");
// Save the second XPS document
doc2.Save(dataDir + "out2.xps");C#で画像が塗りつぶされたグリフを追加する手順。
- ドキュメントディレクトリへのパスを設定します。
- XPSファイルのストリームを開きます。
- XpsDocumentクラスを使用してXPSファイルを作成します。
- *AddGlyphs()*メソッドを使用してドキュメントにグリフを追加します。
- グリフをイメージブラシで塗りつぶすには、 CreateImageBrush() メソッドを使用します。
- 2つ目のXPSファイルをXpsDocumentクラスを使用して作成します。
- *AddGlyphs()*メソッドを使用して、最初のドキュメントのフォントを使用して2つ目のドキュメントにグリフを追加します。
- 最初のドキュメントの塗りつぶしからイメージブラシを作成し、*CreateImageBrush()*メソッドを使用して2つ目のドキュメントのグリフを塗りつぶします。
- *XPsDocument.Save()*メソッドを使用して両方のXPSドキュメントを保存します。
XPSパッケージ内で画像が塗りつぶされたグリフを作成するC#コード
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithCrossPackageOperations();
// Create the first XPS Document
XpsDocument doc1 = new XpsDocument();
// Add glyphs to the first document
XpsGlyphs glyphs1 = doc1.AddGlyphs("Times New Roman", 200, FontStyle.Bold, 50, 250, "Test");
// Fill the glyphs with an image brush
glyphs1.Fill = doc1.CreateImageBrush(dataDir + "R08SY_NN.tif", new RectangleF(0f, 0f, 128f, 192f),
new RectangleF(0f, 0f, 64f, 96f));
((XpsImageBrush)glyphs1.Fill).TileMode = XpsTileMode.Tile;
// Create the second XPS Document
XpsDocument doc2 = new XpsDocument();
// Add glyphs with the font from the first document to the second document
XpsGlyphs glyphs2 = doc2.AddGlyphs(glyphs1.Font, 200, 50, 250, "Test");
// Create an image brush from the fill of the the first document and fill glyphs in the second document
glyphs2.Fill = doc2.CreateImageBrush(((XpsImageBrush)glyphs1.Fill).Image, new RectangleF(0f, 0f, 128f, 192f),
new RectangleF(0f, 0f, 128f, 192f));
((XpsImageBrush)glyphs2.Fill).TileMode = XpsTileMode.Tile;
// Save the first XPS document
doc1.Save(dataDir + "out1.xps");
// Save the second XPS document
doc2.Save(dataDir + "out2.xps");よくある質問
1. XPSパッケージとは何ですか?
XPS パッケージは、XPS ファイルを管理するための別個のライブラリです。これを使用して、XPS を編集するための独自のコンバーター、リーダー、またはその他のアプリを作成します。
2. XPS パッケージを入手するにはどうすればよいですか?
XPS パッケージは、 Aspose.Page ソリューションに含まれています。
3. どのようなパッケージ間操作が利用できますか?
Aspose XPS パッケージを使用すると、あるドキュメントから別のドキュメントにページを転送したり、グリフ、スタイル、設定などのオブジェクトのクローンを作成したりできます。
4. XPS ドキュメント間のページを操作するにはどうすればよいですか?
この XPS パッケージでファイルを転送するには、XpsDocument クラスの InsertPage()、AddPage()、RemovePage()、および SelectActivePage() メソッドを使用します。
XPS What is XPS File Format
XPS (XML Paper Specification) は、Microsoft が提供する PDF の代替フォーマットです。XML/HTML ベースで、プラットフォームに依存せずレイアウトを保持します。