演算子の使用

PDF演算子の紹介とその使用法

演算子は、ページ上にグラフィカルな形状を描画するなど、実行されるべきアクションを指定するPDFキーワードです。演算子キーワードは、初期のスラッシュ文字(2Fh)がないことで名前付きオブジェクトと区別されます。演算子はコンテンツストリーム内でのみ意味を持ちます。

コンテンツストリームは、ページ上に描画されるグラフィカル要素を説明する命令からなるデータを持つPDFストリームオブジェクトです。PDF演算子に関する詳細は、PDF仕様で確認できます。

実装の詳細

このトピックでは、Aspose.PDFで演算子を使用する方法について説明します。選択された例では、概念を示すためにPDFファイルに画像を追加します。PDFファイルに画像を追加するには、異なる演算子が必要です。この例では、GSaveConcatenateMatrixDo、および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演算子、XFormを配置するためのConcatenateMatrix演算子、およびページにXFormを描画するためのDo演算子の使用方法を示します。

以下のコードは、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ファイルに、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");
      }
  }