使用操作符

PDF 操作符及其使用简介

操作符是一个 PDF 关键字,指定要执行的某些操作,例如在页面上绘制图形形状。操作符关键字通过缺少初始斜杠字符 (2Fh) 与命名对象区分。操作符仅在内容流内有意义。

内容流是一个 PDF 流对象,其数据由描述要在页面上绘制的图形元素的指令组成。有关 PDF 操作符的更多详细信息,请参见 PDF 规范

实现细节

本主题解释了如何与 Aspose.PDF 一起使用操作符。所选示例将图像添加到 PDF 文件中以说明该概念。要在 PDF 文件中添加图像,需要不同的操作符。此示例使用 GSaveConcatenateMatrixDoGRestore

  • 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。

下面的代码使用 GSave/GRestore 操作符对 PDF 文件的现有内容进行包装。这种方法有助于在现有内容的末尾获取初始图形状态。如果没有这种方法,可能会在现有操作符链的末尾留下不必要的变换。

// 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 文件包含无法使用 PdfContentEditor 类的 DeleteImage 方法移除的图形时,可以使用操作符类来移除它们。

以下代码片段演示了如何移除图形。请注意,如果 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");
      }
  }