Làm việc với bàn chải và gradient

Giải pháp C# .NET API để làm việc với cọ vẽ và độ dốc của tệp XPS.

 

Giải pháp API Aspose.Page cho .NET cho phép bạn thao tác đồ họa vector của các tệp XPS. Bạn có thể tạo ra nhiều hình dạng hình học với màu sắc và kết cấu khác nhau. Trên trang này sẽ được mô tả một vài ví dụ về cách thêm các khoảng màu và độ chuyển màu khác nhau, cũng như cách làm việc với các bàn chải khác nhau. Làm việc tương tự, bạn sẽ có thể tạo bất kỳ hình hình học nào bạn cần với bất kỳ màu nào.

Để thao tác với cọ vẽ và độ dốc của tệp XPS, chúng ta cần:

  • Aspose.Page for .NET API là một API chuyển đổi và thao tác tài liệu giàu tính năng, mạnh mẽ và dễ sử dụng cho nền tảng C#.

  • Mở trình quản lý gói NuGet, tìm kiếm Aspose.Page và cài đặt. Bạn cũng có thể sử dụng lệnh sau từ Bảng điều khiển Trình quản lý Gói.

Package Manager Console Command


    PM> Install-Package Aspose.Page

Các bước áp dụng không gian màu với C# .NET.

  1. Đặt đường dẫn đến thư mục tài liệu.
  2. Tạo tệp XPS bằng XpsDocument Class .
  3. Để tạo các hình chữ nhật tô màu đồng nhất khác nhau, hãy sử dụng các phương pháp của Lớp XpsPath .
  4. Lưu tài liệu XPS đã thay đổi bằng phương pháp XPsDocument.Save() .

Mã C# để đặt không gian màu của tệp XPS

    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");
Đoạn mã tiếp theo cho biết cách sử dụng bàn chải trực quan của tệp XPS trong Aspose.Page cho Giải pháp .NET Api.

Các bước làm việc với Visual Brush với C#.

  1. Đặt đường dẫn đến thư mục tài liệu.
  2. Tạo tệp XPS bằng XPsDocument Class.
  3. Tạo hình học cho lưới màu đỏ tươi Visual Brush bằng cách sử dụng Lớp XpsPathGeometry .
  4. Để đặt canvas cho lưới màu đỏ tươi Visual Brush, hãy sử dụng phương pháp CreateCanvas() .
  5. Để tạo Visual Brush, hãy sử dụng phương pháp CreateVisualBrush() .
  6. Lưu tài liệu XPS đã thay đổi bằng phương thức XPsDocument.Save().

Mã C# để thao tác bàn chải trực quan của tệp XPS

    // 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ác bước thêm gradient ngang với C#.

  1. Đặt đường dẫn đến thư mục tài liệu.
  2. Tạo tệp XPS bằng XPsDocument Class.
  3. Khởi tạo danh sách XpsGradentStop bằng cách sử dụng CreateGradientStop()CreateColor() Phương thức.
  4. Tạo một đường dẫn mới bằng cách xác định hình học ở dạng viết tắt bằng cách sử dụng các phương thức của Lớp XPsPath.
  5. Lưu tài liệu XPS đã thay đổi bằng phương thức XPsDocument.Save().

Mã C# để chèn gradient ngang vào tệp XPS

    // 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");



Câu hỏi thường gặp

1. Làm cách nào để thêm độ dốc vào tệp XPS?

Đặt đường dẫn đến thư mục tài liệu. Để thêm độ dốc, hãy sử dụng các phương thức CreateColor()CreateGradientStop().

2. Làm cách nào để làm việc với cọ vẽ trực quan trong tệp XPS?

Đặt đường dẫn đến thư mục tài liệu. Để tạo cọ vẽ trực quan, hãy sử dụng Phương thức CreateVisualBrush().

3. Làm cách nào để mở tệp XPS?

Sử dụng Giải pháp API Aspose.Page để mở XPS tệp theo chương trình hoặc bằng phương tiện đa nền tảng XPS Viewer .

XPS Những gì là XPS Tập Tin Định Dạng

Định dạng XPS tương tự như định dạng PDF. Cả hai đều là định dạng ngôn ngữ mô tả trang (PDL). EPS dựa trên HTML và không dựa trên ngôn ngữ PostScript. Tệp .eps có thể chứa phần đánh dấu cấu trúc của tài liệu cùng với thông tin về cách tài liệu sẽ trông như thế nào. Ngoài ra còn có các hướng dẫn bổ sung về cách in và kết xuất tài liệu. Đặc điểm của định dạng là nó sửa chữa mô tả của tài liệu, có nghĩa là nó sẽ trông giống nhau cho dù ai và từ hệ thống hoạt động nào mở nó ra.