إضافة كائن مستطيل إلى ملف PDF

تعمل مقتطفات الكود التالية أيضًا مع مكتبة Aspose.PDF.Drawing .

إضافة كائن مستطيل

يدعم Aspose.PDF for .NET ميزة إضافة كائنات الرسم (مثل الرسم، الخط، المستطيل، إلخ) إلى مستندات PDF. يمكنك أيضًا الاستفادة من إضافة كائن Rectangle حيث تقدم أيضًا ميزة ملء كائن المستطيل بلون معين، والتحكم في ترتيب Z، وإضافة ملء تدرج لوني، وما إلى ذلك.

أولاً، دعنا نلقي نظرة على إمكانية إنشاء كائن مستطيل.

اتبع الخطوات أدناه:

  1. إنشاء مستند PDF جديد Document.

  2. إضافة Page إلى مجموعة الصفحات في ملف PDF.

  3. إضافة Text fragment إلى مجموعة الفقرات في مثيل الصفحة.

  4. إنشاء مثيل Graph .

  5. تعيين الحدود لكائن Drawing object .

  6. إنشاء مثيل مستطيل.

  7. إضافة كائن Rectangle إلى مجموعة الأشكال في كائن Graph.

  8. إضافة كائن الرسم إلى مجموعة الفقرات في مثيل الصفحة.

  9. إضافة Text fragment إلى مجموعة الفقرات في مثيل الصفحة.

  10. وحفظ ملف PDF الخاص بك.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddRectangle(Aspose.Pdf.Page page, float x, float y, float width, float height, Aspose.Pdf.Color color, int zIndex)
{
    // Create a Graph object with dimensions matching the specified rectangle
    var graph = new Aspose.Pdf.Drawing.Graph(width, height)
    {
        // Prevent the graph from repositioning automatically
        IsChangePosition = false,
        // Set the Left coordinate position for the Graph instance
        Left = x,
        // Set the Top coordinate position for the Graph instance
        Top = y
    };

    // Create a Rectangle object inside the Graph
    var rect = new Aspose.Pdf.Drawing.Rectangle(0, 0, width, height)
    {
        // Set the fill color of the rectangle
        GraphInfo =
        {
            FillColor = color,
            // Set the border color of the rectangle
            Color = color
        }
    };

    // Add the rectangle to the Shapes collection of the Graph
    graph.Shapes.Add(rect);

    // Set the Z-Index for the Graph object to control layering
    graph.ZIndex = zIndex;

    // Add the Graph object to the Paragraphs collection of the page
    page.Paragraphs.Add(graph);
}

إنشاء مستطيل

إنشاء كائن مستطيل مملوء

يقدم Aspose.PDF for .NET أيضًا ميزة ملء كائن المستطيل بلون معين.

تظهر مقتطفات الكود التالية كيفية إضافة كائن Rectangle مملوء بلون.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void RectangleFilled()
{
    // The path to the documents directory directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_Images();

    // Create PDF document
    using (var document = new Aspose.Pdf.Document())
    {
        // Add page
        var page = document.Pages.Add();

        // Create Graph instance
        var graph = new Aspose.Pdf.Drawing.Graph(100, 400);

        // Add graph object to paragraphs collection of page instance
        page.Paragraphs.Add(graph);

        // Create Rectangle instance with specified dimensions
        var rect = new Aspose.Pdf.Drawing.Rectangle(100, 100, 200, 120)
        {
            // Specify fill color for the Rectangle object
            GraphInfo = 
            { 
                FillColor = Aspose.Pdf.Color.Red 
            }
        };

        // Add rectangle object to shapes collection of Graph object
        graph.Shapes.Add(rect);

        // Save PDF document
        document.Save(dataDir + "CreateFilledRectangle_out.pdf");
    }
}

انظر إلى نتيجة المستطيل المملوء بلون صلب:

مستطيل مملوء

إضافة رسم مع ملء تدرج

يدعم Aspose.PDF for .NET ميزة إضافة كائنات الرسم إلى مستندات PDF وأحيانًا يتطلب ملء كائنات الرسم بتدرج لوني. لملء كائنات الرسم بتدرج لوني، نحتاج إلى تعيين setPatterColorSpace مع كائن gradientAxialShading كما يلي.

تظهر مقتطفات الكود التالية كيفية إضافة كائن Rectangle مملوء بتدرج لوني.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreateFilledRectangleGradientFill()
{
    // The path to the documents directory directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_Images();

    // Create PDF document
    using (var document = new Aspose.Pdf.Document())
    {
        // Add page
        var page = document.Pages.Add();

        // Create Graph instance
        var graph = new Aspose.Pdf.Drawing.Graph(400, 400);

        // Add graph object to paragraphs collection of page instance
        page.Paragraphs.Add(graph);

        // Create Rectangle instance
        var rect = new Aspose.Pdf.Drawing.Rectangle(0, 0, 300, 300);

        // Create a gradient fill color
        var gradientColor = new Aspose.Pdf.Color();
        var gradientSettings = new Aspose.Pdf.Drawing.GradientAxialShading(Aspose.Pdf.Color.Red, Aspose.Pdf.Color.Blue)
        {
            Start = new Aspose.Pdf.Point(0, 0),
            End = new Aspose.Pdf.Point(350, 350)
        };
        gradientColor.PatternColorSpace = gradientSettings;

        // Apply gradient fill color to the rectangle
        rect.GraphInfo.FillColor = gradientColor;

        // Add rectangle object to shapes collection of Graph object
        graph.Shapes.Add(rect);

        // Save PDF document
        document.Save(dataDir + "CreateFilledRectangleGradient_out.pdf");
    }
}

مستطيل بتدرج لوني

إنشاء مستطيل مع قناة لون ألفا

يدعم Aspose.PDF for .NET ملء كائن المستطيل بلون معين. يمكن أن يحتوي كائن المستطيل أيضًا على قناة لون ألفا لإعطاء مظهر شفاف. تظهر مقتطفات الكود التالية كيفية إضافة كائن Rectangle مع قناة لون ألفا.

يمكن أن تخزن بكسلات الصورة معلومات حول شفافيتها جنبًا إلى جنب مع قيمة اللون. وهذا يسمح بإنشاء صور تحتوي على مناطق شفافة أو شبه شفافة.

بدلاً من جعل لون شفاف، يخزن كل بكسل معلومات حول مدى تعتيمه. تُسمى هذه البيانات الشفافية قناة ألفا وعادة ما يتم تخزينها بعد قنوات لون البكسل.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void RectangleFilled_AlphaChannel()
{
    // The path to the documents directory directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_Images();

    // Create PDF document
    using (var document = new Aspose.Pdf.Document())
    {
        // Add page
        var page = document.Pages.Add();

        // Create Graph instance
        var graph = new Aspose.Pdf.Drawing.Graph(100, 400);

        // Add graph object to paragraphs collection of page instance
        page.Paragraphs.Add(graph);

        // Create first rectangle with alpha channel fill color
        var rect = new Aspose.Pdf.Drawing.Rectangle(100, 100, 200, 120)
        {
            GraphInfo = 
            { 
                FillColor = Aspose.Pdf.Color.FromArgb(128, 244, 180, 0) 
            }
        };

        // Add the first rectangle to the shapes collection of the Graph object
        graph.Shapes.Add(rect);

        // Create second rectangle with different alpha channel fill color
        var rect1 = new Aspose.Pdf.Drawing.Rectangle(200, 150, 200, 100)
        {
            GraphInfo = 
            { 
                FillColor = Aspose.Pdf.Color.FromArgb(160, 120, 0, 120) 
            }
        };

        // Add the second rectangle to the shapes collection of the Graph object
        graph.Shapes.Add(rect1);

        // Save PDF document
        document.Save(dataDir + "CreateFilledRectangle_out.pdf");
    }
}

مستطيل لون قناة ألفا

التحكم في ترتيب Z للمستطيل

يدعم Aspose.PDF for .NET ميزة إضافة كائنات الرسم (مثل الرسم، الخط، المستطيل، إلخ) إلى مستندات PDF. عند إضافة أكثر من مثيل لنفس الكائن داخل ملف PDF، يمكننا التحكم في عرضها عن طريق تحديد ترتيب Z. يُستخدم ترتيب Z أيضًا عندما نحتاج إلى عرض الكائنات فوق بعضها البعض.

تظهر مقتطفات الكود التالية الخطوات اللازمة لعرض كائنات Rectangle فوق بعضها البعض.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddRectangleZOrder()
{
    // The path to the documents directory directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_Images();

    // Create PDF document
    using (var document = new Aspose.Pdf.Document())
    {
        // Add page
        var page = document.Pages.Add();

        // Set size of PDF page
        page.SetPageSize(375, 300);

        // Set left and top margins for the page object as 0
        page.PageInfo.Margin.Left = 0;
        page.PageInfo.Margin.Top = 0;

        // Create rectangles with different colors and Z-Order values
        AddRectangle(page, 50, 40, 60, 40, Aspose.Pdf.Color.Red, 2);
        AddRectangle(page, 20, 20, 30, 30, Aspose.Pdf.Color.Blue, 1);
        AddRectangle(page, 40, 40, 60, 30, Aspose.Pdf.Color.Green, 0);

        // Save PDF document
        document.Save(dataDir + "ControlRectangleZOrder_out.pdf");
    }
}

التحكم في ترتيب Z