ブラシとグラデーションの操作

XPS ファイルのブラシとグラデーションを操作する C# .NET API ソリューション。

 

Aspose.Page for .NET API ソリューションを使用すると、XPS ファイルのベクター グラフィックスを操作できます。さまざまな色とテクスチャのさまざまな幾何学的形状を作成できます。このページでは、さまざまなカラー スペースとグラデーションを追加する方法と、さまざまなブラシを使用する方法の例をいくつか説明します。類推的に作業すると、必要な任意の色の幾何学的図形を作成できます。

XPS ファイルのブラシとグラデーションを操作するには、次のものが必要です。

  • Aspose.Page for .NET API は、C# プラットフォーム向けの機能豊富で強力で使いやすいドキュメント操作および変換 API です。

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

Package Manager Console Command


    PM> Install-Package Aspose.Page

C# .NET で色空間を適用する手順。

  1. ドキュメント ディレクトリへのパスを設定します。
  2. XpsDocument クラス を使用して XPS ファイルを作成します。
  3. さまざまな単色で塗りつぶされた四角形を作成するには、 XpsPath クラスのメソッドを使用します。
  4. XPsDocument.Save() メソッドを使用して、変更された XPS ドキュメントを保存します。

XPS ファイルの色空間を設定する C# コード

    using Aspose.Page.XPS;
    using Aspose.Page.XPS.XpsModel;
    using System.Drawing;
    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_WorkingWithShapes();
            
    // Create a new XPS Document
    XpsDocument doc = new XpsDocument();

    // ARGB solid color filled rectangle
    XpsPath rect1 = doc.AddPath(doc.CreatePathGeometry("M 20,10 L 220,10 220,100 20,100 Z"));
    rect1.Fill = doc.CreateSolidColorBrush(doc.CreateColor(Color.FromArgb(222, 12, 15, 159)));

    // ARGB solid color filled rectangle, in another way
    XpsPath rect2 = doc.AddPath(doc.CreatePathGeometry("M 20,210 L 220,210 220,300 20,300 Z"));
    rect2.Fill = doc.CreateSolidColorBrush(doc.CreateColor(222, 12, 15, 159));

    // sRGB solid color filled rectangle
    XpsPath rect3 = doc.AddPath(doc.CreatePathGeometry("M 20,410 L 220,410 220,500 20,500 Z"));
    rect3.Fill = doc.CreateSolidColorBrush(doc.CreateColor(12, 15, 159));

    // scRGB solid color filled rectangle
    XpsPath rect4 = doc.AddPath(doc.CreatePathGeometry("M 20,610 L 220,610 220,700 20,700 Z"));
    rect4.Fill = doc.CreateSolidColorBrush(doc.CreateColor(0.08706f, 0.04706f, 0.05882f, 0.62353f));

    // CMYK (blue) solid color filled rectangle
    XpsPath rect5 = doc.AddPath(doc.CreatePathGeometry("M 20,810 L 220,810 220,900 20,900 Z"));
    rect5.Fill = doc.CreateSolidColorBrush(
        doc.CreateColor(dataDir + "uswebuncoated.icc", 1.0f, 1.000f, 0.000f, 0.000f, 0.000f));
            
    // Save the resultant XPS document
    doc.Save(dataDir + "ApplyDifferentColorSpaces_out.xps");
次のコード スニペットは、Aspose.Page for .NET Api ソリューション内で XPS ファイルのビジュアル ブラシを使用する方法を示しています。

C# でビジュアル ブラシを操作する手順。

  1. ドキュメント ディレクトリへのパスを設定します。
  2. XpsDocument Class を使用して XPS ファイルを作成します。
  3. XpsPathGeometry クラスを使用して、マゼンタ グリッド ビジュアル ブラシのジオメトリを作成します。
  4. マゼンタ グリッドのビジュアル ブラシのキャンバスを設定するには、 CreateCanvas() メソッドを使用します。
  5. ビジュアル ブラシを作成するには、 CreateVisualBrush() メソッドを使用します。
  6. XPsDocument.Save() メソッドを使用して、変更された XPS ドキュメントを保存します。

XPS ファイルのビジュアル ブラシを操作する C# コード

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_WorkingWithVisualBrush();

    XpsDocument doc = new XpsDocument();
    // Geometry for the magenta grid VisualBrush
    XpsPathGeometry pathGeometry = doc.CreatePathGeometry();
    pathGeometry.AddSegment(doc.CreatePolyLineSegment(
        new PointF[] { new PointF(240f, 5f), new PointF(240f, 310f), new PointF(0f, 310f) }));
    pathGeometry[0].StartPoint = new PointF(0f, 5f);

    // Canvas for the magenta grid VisualBrush
    XpsCanvas visualCanvas = doc.CreateCanvas();

    XpsPath visualPath = visualCanvas.AddPath(
        doc.CreatePathGeometry("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.Fill = doc.CreateSolidColorBrush(doc.CreateColor(1f, .61f, 0.1f, 0.61f));

    XpsPath gridPath = doc.CreatePath(pathGeometry);
    // Create the Visual Brush, it is specified by some XPS fragment (vector graphics and glyphs)
    gridPath.Fill = doc.CreateVisualBrush(visualCanvas,
        new RectangleF(0f, 0f, 10f, 10f), new RectangleF(0f, 0f, 10f, 10f));
    ((XpsVisualBrush)gridPath.Fill).TileMode = XpsTileMode.Tile;
    // New canvas
    XpsCanvas canvas = doc.AddCanvas();
    canvas.RenderTransform = doc.CreateMatrix(1f, 0f, 0f, 1f, 268f, 70f);
    // Add a grid
    canvas.AddPath(pathGeometry);
    // Red transparent rectangle in the middle top
    XpsPath path = canvas.AddPath(doc.CreatePathGeometry("M 30,20 l 258.24,0 0,56.64 -258.24,0 Z"));
    path = canvas.AddPath(doc.CreatePathGeometry("M 10,10 L 228,10 228,100 10,100"));
    path.Fill = doc.CreateSolidColorBrush(doc.CreateColor(1.0f, 0.0f, 0.0f));
    path.Opacity = 0.7f;
    // Save the resultant XPS document
    doc.Save(dataDir + "AddGrid_out.xps");

C# で水平グラデーションを追加する手順。

  1. ドキュメント ディレクトリへのパスを設定します。
  2. XpsDocument Class を使用して XPS ファイルを作成します。
  3. CreateGradientStop() および CreateColor() メソッド。
  4. XpsPath クラスのメソッドを使用して省略形でジオメトリを定義することにより、新しいパスを作成します。
  5. XPsDocument.Save() メソッドを使用して、変更された XPS ドキュメントを保存します。

XPS ファイルに水平グラデーションを挿入する C# コード

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_WorkingWithGradient();
    // Create a new XPS Document
    XpsDocument doc = new XpsDocument();

    // Initialize the List of XpsGradentStop
    List<XpsGradientStop> stops = new List<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), 1f));

    // Create a new path by defining the geometery in the abbreviation form
    XpsPath path = doc.AddPath(doc.CreatePathGeometry("M 10,210 L 228,210 228,300 10,300"));
    path.RenderTransform = doc.CreateMatrix(1f, 0f, 0f, 1f, 20f, 70f);
    path.Fill = doc.CreateLinearGradientBrush(new PointF(10f, 0f), new PointF(228f, 0f));
    ((XpsGradientBrush)path.Fill).GradientStops.AddRange(stops);

    // Save the resultant XPS document
    doc.Save(dataDir + "AddHorizontalGradient_out.xps");



よくある質問

1. XPS ファイルにグラデーションを追加するにはどうすればよいですか?

ドキュメントディレクトリへのパスを設定します。グラデーションを追加するには、CreateColor() メソッドと CreateGradientStop() メソッドを使用します。

2. XPS ファイルでビジュアル ブラシを使用するにはどうすればよいですか?

ドキュメントディレクトリへのパスを設定します。ビジュアル ブラシを作成するには、CreateVisualBrush() メソッドを使用します。

3. XPSファイルを開くにはどうすればよいですか?

Aspose.Page API ソリューションを使用して、プログラムまたはクロスプラットフォームの XPS Viewer ファイルを XPS を開く します。

XPS XPS ファイル形式とは

XPS 形式は PDF 形式に似ています。どちらもページ記述言語 (PDL) 形式です。 EPS は PostScript 言語ではなく、HTML に基づいています。 .eps ファイルには、ドキュメントの構造のマークアップと、ドキュメントがどのように見えるかに関する情報を含めることができます。また、ドキュメントを印刷およびレンダリングする方法についての説明も追加されています。この形式の特徴は、ドキュメントの説明を修正することです。つまり、誰が、どのオペレーティング システムからドキュメントを開いたとしても、同じように表示されます。