使用 C# 从印章中提取文本

从印章注释中提取文本

Aspose.PDF for NET 允许您从印章注释中提取文本。要从 PDF 中的印章注释提取文本,可以使用以下步骤。

  1. 创建一个 Document 类对象。
  2. 从页面的注释列表中获取所需的 Annotation
  3. 定义一个新的 TextAbsorber 类对象。
  4. 使用 TextAbsorber 的访问方法获取文本。

以下代码片段也适用于 Aspose.PDF.Drawing 库。

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

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "ExtractStampText.pdf"))
    {
        Aspose.Pdf.Annotations.Annotation item = document.Pages[1].Annotations[1];
        if (item is Aspose.Pdf.Annotations.StampAnnotation annot)
        {
            var absorber = new Aspose.Pdf.Text.TextAbsorber();
            Aspose.Pdf.XForm appearance = annot.Appearance["N"];
            absorber.Visit(appearance);
            Console.WriteLine(absorber.Text);
        }
    }
}