العمل مع المشغلين

مقدمة إلى مشغلات PDF واستخدامها

المشغل هو كلمة مفتاحية في PDF تحدد بعض الإجراءات التي يجب تنفيذها، مثل رسم شكل رسومي على الصفحة. يتم تمييز كلمة المشغل عن كائن مسمى من خلال غياب حرف السوليدوس الأولي (2Fh). تكون المشغلين ذات معنى فقط داخل تدفق المحتوى.

تدفق المحتوى هو كائن تدفق PDF تتكون بياناته من تعليمات تصف العناصر الرسومية التي يجب رسمها على الصفحة. يمكن العثور على مزيد من التفاصيل حول مشغلات PDF في مواصفات PDF.

تفاصيل التنفيذ

يشرح هذا الموضوع كيفية استخدام المشغلين مع Aspose.PDF. يضيف المثال المختار صورة إلى ملف PDF لتوضيح المفهوم. لإضافة صورة في ملف PDF، هناك حاجة إلى مشغلين مختلفين. يستخدم هذا المثال GSave، ConcatenateMatrix، Do، و GRestore.

  • يقوم مشغل GSave بحفظ الحالة الرسومية الحالية لـ PDF.
  • يستخدم مشغل ConcatenateMatrix (دمج المصفوفة) لتحديد كيفية وضع الصورة على صفحة PDF.
  • يقوم مشغل Do برسم الصورة على الصفحة.
  • يقوم مشغل GRestore باستعادة الحالة الرسومية.

لإضافة صورة إلى ملف PDF:

  1. أنشئ كائن Document وافتح مستند PDF المدخل.
  2. احصل على الصفحة المحددة التي ستتم إضافة الصورة إليها.
  3. أضف الصورة إلى مجموعة موارد الصفحة.
  4. استخدم المشغلين لوضع الصورة على الصفحة:
    • أولاً، استخدم مشغل GSave لحفظ الحالة الرسومية الحالية.
    • ثم استخدم مشغل ConcatenateMatrix لتحديد مكان وضع الصورة.
    • استخدم مشغل Do لرسم الصورة على الصفحة.
  5. أخيرًا، استخدم مشغل GRestore لحفظ الحالة الرسومية المحدثة.

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

تظهر مقتطفات الكود التالية كيفية استخدام مشغلات PDF.

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

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "PDFOperators.pdf"))
    {
        // Set coordinates for the image placement
        int lowerLeftX = 100;
        int lowerLeftY = 100;
        int upperRightX = 200;
        int upperRightY = 200;

        // Get the page where the image needs to be added
        var page = document.Pages[1];

        // Load the image into a file stream
        using (var imageStream = new FileStream(dataDir + "PDFOperators.jpg", FileMode.Open))
        {
            // Add the image to the page's Resources collection
            page.Resources.Images.Add(imageStream);
        }

        // Save the current graphics state using the GSave operator
        page.Contents.Add(new Aspose.Pdf.Operators.GSave());

        // Create a rectangle and matrix for positioning the image
        var rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
        var matrix = new Aspose.Pdf.Matrix(new double[]
        {
            rectangle.URX - rectangle.LLX, 0,
            0, rectangle.URY - rectangle.LLY,
            rectangle.LLX, rectangle.LLY
        });

        // Define how the image must be placed using the ConcatenateMatrix operator
        page.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix));

        // Get the image from the Resources collection
        var ximage = page.Resources.Images[page.Resources.Images.Count];

        // Draw the image using the Do operator
        page.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name));

        // Restore the graphics state using the GRestore operator
        page.Contents.Add(new Aspose.Pdf.Operators.GRestore());

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

رسم XForm على الصفحة باستخدام المشغلين

يظهر هذا الموضوع كيفية استخدام مشغلات GSave/GRestore، ومشغل ConcatenateMatrix لتحديد موقع xForm ومشغل Do لرسم xForm على الصفحة.

تغلف الشيفرة أدناه محتويات ملف PDF الموجودة باستخدام زوج مشغلات GSave/GRestore. تساعد هذه الطريقة في الحصول على الحالة الرسومية الأولية في نهاية المحتويات الموجودة. بدون هذه الطريقة، قد تبقى تحويلات غير مرغوب فيها في نهاية سلسلة المشغلين الموجودة.

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

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "DrawXFormOnPage.pdf"))
    {
        var pageContents = document.Pages[1].Contents;

        // Wrap existing contents with GSave/GRestore operators to preserve graphics state
        pageContents.Insert(1, new Aspose.Pdf.Operators.GSave());
        pageContents.Add(new Aspose.Pdf.Operators.GRestore());

        // Add GSave operator to start new graphics state
        pageContents.Add(new Aspose.Pdf.Operators.GSave());

        // Create an XForm
        var form = Aspose.Pdf.XForm.CreateNewForm(document.Pages[1], document);
        document.Pages[1].Resources.Forms.Add(form);

        form.Contents.Add(new Aspose.Pdf.Operators.GSave());
        // Define image width and height
        form.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(200, 0, 0, 200, 0, 0));

        // Load image into stream
        using (var imageStream = new FileStream(dataDir + "aspose-logo.jpg", FileMode.Open))
        {
            // Add the image to the XForm's resources
            form.Resources.Images.Add(imageStream);
        }

        var ximage = form.Resources.Images[form.Resources.Images.Count];
        // Draw the image on the XForm
        form.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name));
        form.Contents.Add(new Aspose.Pdf.Operators.GRestore());

        // Place and draw the XForm at two different coordinates

        // Draw the XForm at (100, 500)
        pageContents.Add(new Aspose.Pdf.Operators.GSave());
        pageContents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(1, 0, 0, 1, 100, 500));
        pageContents.Add(new Aspose.Pdf.Operators.Do(form.Name));
        pageContents.Add(new Aspose.Pdf.Operators.GRestore());

        // Draw the XForm at (100, 300)
        pageContents.Add(new Aspose.Pdf.Operators.GSave());
        pageContents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(1, 0, 0, 1, 100, 300));
        pageContents.Add(new Aspose.Pdf.Operators.Do(form.Name));
        pageContents.Add(new Aspose.Pdf.Operators.GRestore());

        // Restore graphics state
        pageContents.Add(new Aspose.Pdf.Operators.GRestore());

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

إزالة كائنات الرسومات باستخدام فئات المشغلين

توفر فئات المشغلين ميزات رائعة لمعالجة PDF. عندما يحتوي ملف PDF على رسومات لا يمكن إزالتها باستخدام طريقة DeleteImage لفئة PdfContentEditor، يمكن استخدام فئات المشغلين لإزالتها بدلاً من ذلك.

تظهر مقتطفات الكود التالية كيفية إزالة الرسومات. يرجى ملاحظة أنه إذا كان ملف PDF يحتوي على تسميات نصية للرسومات، فقد تستمر في ملف PDF باستخدام هذه الطريقة. لذلك ابحث عن مشغلات الرسومات للحصول على طريقة بديلة لحذف مثل هذه الصور.

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

      // Open PDF document
      using (var document = new Aspose.Pdf.Document(dataDir + "RemoveGraphicsObjects.pdf"))
      {
          // Get the specific page (page 2 in this case)
          var page = document.Pages[2];

          // Get the operator collection from the page contents
          var oc = page.Contents;

          // Define the path-painting operators to be removed
          var operators = new Aspose.Pdf.Operator[]
          {
              new Aspose.Pdf.Operators.Stroke(),
              new Aspose.Pdf.Operators.ClosePathStroke(),
              new Aspose.Pdf.Operators.Fill()
          };

          // Delete the specified operators from the page contents
          oc.Delete(operators);

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