แปรงและการไล่เฉดสีใน XPS
โซลูชัน C# .NET API สำหรับทำงานกับแปรงและการไล่เฉดสีของไฟล์ XPS
โซลูชัน Aspose.Page สำหรับ .NET API ช่วยให้คุณจัดการกราฟิกเวกเตอร์ของไฟล์ XPS คุณสามารถสร้างรูปทรงเรขาคณิตต่างๆ ที่มีสีและพื้นผิวที่แตกต่างกัน ในหน้านี้จะอธิบายตัวอย่างบางส่วนเกี่ยวกับวิธีเพิ่มปริภูมิสี การไล่เฉดสี และวิธีทำงานกับแปรงแบบต่างๆ เมื่อทำงานในลักษณะเดียวกัน คุณจะสามารถสร้างรูปทรงเรขาคณิตที่คุณต้องการในสีใดก็ได้
ในการจัดการแปรงและการไล่เฉดสีของไฟล์ XPS เราต้องการ:
Aspose.Page สำหรับ .NET API ซึ่งเป็น API การจัดการและการแปลงเอกสารที่มีฟีเจอร์ครบถ้วน ทรงพลัง และใช้งานง่ายสำหรับแพลตฟอร์ม C#
เปิดตัวจัดการแพ็คเกจ NuGet ค้นหา Aspose.Page และติดตั้ง คุณยังสามารถใช้คำสั่งต่อไปนี้จาก Package Manager Console
Package Manager Console Command
PM> Install-Package Aspose.Page
ขั้นตอนการใช้ปริภูมิสีด้วย C# .NET
- กำหนดเส้นทางไปยังไดเรกทอรีเอกสาร
- สร้างไฟล์ XPS โดยใช้ XpsDocument Class
- หากต้องการสร้างสี่เหลี่ยมผืนผ้าที่เติมสีทึบแบบต่างๆ ให้ใช้เมธอดของคลาส XpsPath
- บันทึกเอกสาร XPS ที่เปลี่ยนแปลงโดยใช้เมธอด XPsDocument.Save()
โค้ด C# เพื่อกำหนดปริภูมิสีของไฟล์ 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");ขั้นตอนการทำงานกับ Visual Brush ด้วย C#
- กำหนดเส้นทางไปยังไดเรกทอรีเอกสาร
- สร้างไฟล์ XPS โดยใช้คลาส XpsDocument
- สร้างเรขาคณิตสำหรับ Visual Brush ตารางสีม่วงแดงโดยใช้คลาส XpsPathGeometry
- ในการกำหนดผืนผ้าใบสำหรับ Visual Brush ตารางสีม่วงแดง ให้ใช้เมธอด CreateCanvas()
- ในการสร้าง Visual Brush ให้ใช้เมธอด CreateVisualBrush()
- บันทึกเอกสาร XPS ที่เปลี่ยนแปลงโดยใช้เมธอด XPsDocument.Save()
โค้ด C# เพื่อจัดการ Visual Brush ของไฟล์ 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#
- กำหนดเส้นทางไปยังไดเรกทอรีเอกสาร
- สร้างไฟล์ XPS โดยใช้คลาส XpsDocument
- เตรียมใช้งานรายการ XpsGradentStop โดยใช้เมธอด CreateGradientStop() และ CreateColor()
- สร้างเส้นทางใหม่โดยกำหนดเรขาคณิตในรูปแบบย่อโดยใช้เมธอดของคลาส XpsPath
- บันทึกเอกสาร XPS ที่เปลี่ยนแปลงโดยใช้เมธอด XPsDocument.Save()
โค้ด C# เพื่อแทรกการไล่เฉดสีแนวนอนลงในไฟล์ 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");คำถามที่พบบ่อย
1. ฉันจะเพิ่มการไล่ระดับสีลงในไฟล์ XPS ได้อย่างไร
กำหนดเส้นทางไปยังไดเร็กทอรีเอกสาร หากต้องการเพิ่มการไล่ระดับสี ให้ใช้เมธอด CreateColor() และ CreateGradientStop()
2. จะทำงานกับ Visual Brush ในไฟล์ XPS ได้อย่างไร?
กำหนดเส้นทางไปยังไดเร็กทอรีเอกสาร หากต้องการสร้างแปรงภาพให้ใช้เมธอด CreateVisualBrush()
3. เปิดไฟล์ .XPS ได้อย่างไร
ใช้โซลูชัน Aspose.Page API เพื่อ เปิด XPS ไฟล์โดยทางโปรแกรมหรือโดยวิธีข้ามแพลตฟอร์ม XPS Viewer
XPS XPS รูปแบบไฟล์คืออะไร
XPS (XML Paper Specification) เป็นรูปแบบทางเลือกของ Microsoft ต่อ PDF พื้นฐานบน XML/HTML รักษาเลย์เอาต์ข้ามแพลตฟอร์มและไม่ขึ้นกับระบบปฏิบัติการ