You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用iText提取Foxit Reader文本框批注?PDF表单控件插入可行吗?

Answers to Your PDF Manipulation Questions

1. Extracting Data from Foxit Reader Text Box Annotations with iText

Foxit Reader's text box annotations are standard PDF text annotations, so iText (both Java and .NET/iTextSharp) can absolutely pull out their content. Here's how to implement this:

For iText 7 (Java):

Loop through each page's annotations, filter for text annotations, and grab the content:

PdfDocument pdfDoc = new PdfDocument(new PdfReader("input.pdf"));
for (int i = 1; i <= pdfDoc.getNumberOfPages(); i++) {
    PdfPage page = pdfDoc.getPage(i);
    List<PdfAnnotation> annotations = page.getAnnotations();
    for (PdfAnnotation annot : annotations) {
        if (annot instanceof PdfTextAnnotation) {
            PdfTextAnnotation textAnnot = (PdfTextAnnotation) annot;
            String annotationText = textAnnot.getContents().toString();
            System.out.println("Annotation on page " + i + ": " + annotationText);
        }
    }
}
pdfDoc.close();

For iTextSharp (.NET):

Same core logic using the .NET API:

using (PdfReader reader = new PdfReader("input.pdf"))
{
    for (int i = 1; i <= reader.NumberOfPages; i++)
    {
        PdfDictionary pageDict = reader.GetPageN(i);
        PdfArray annots = pageDict.GetAsArray(PdfName.Annots);
        if (annots != null)
        {
            foreach (PdfObject annotObj in annots)
            {
                PdfDictionary annotDict = (PdfDictionary)PdfReader.GetPdfObject(annotObj);
                if (annotDict.Get(PdfName.Subtype).Equals(PdfName.Text))
                {
                    string annotationText = annotDict.GetAsString(PdfName.Contents).ToString();
                    Console.WriteLine("Annotation on page " + i + ": " + annotationText);
                }
            }
        }
    }
}

Note: If Foxit uses any custom properties for its text boxes, you might need to check extra entries in the annotation dictionary, but the Contents key is the standard spot for annotation text.


2. Inserting Form Controls Below Specific Text with iTextSharp (Can It Be Done?)

Absolutely—iTextSharp is fully capable of this. Here's a step-by-step breakdown of how to make it happen:

Approach:

  1. Extract Text with Position Data: Use iTextSharp's LocationTextExtractionStrategy to get the bounding box coordinates of each target string ("Sam", "28", "april/18/2018").
  2. Calculate Field Position: For each target string's bounding box, compute the position of the text field directly below it (adjust the Y-coordinate to shift down).
  3. Add Text Fields: Create a TextField for each entry, set its position and size, and add it to the PDF's form.

Example Code (C#):

using (PdfReader reader = new PdfReader("input.pdf"))
using (PdfStamper stamper = new PdfStamper(reader, new FileStream("output.pdf", FileMode.Create)))
{
    AcroFields form = stamper.AcroFields;
    stamper.FormFlattening = false; // Keep fields editable

    // Map target strings to unique field names
    Dictionary<string, string> targets = new Dictionary<string, string>
    {
        {"Sam", "nameField"},
        {"28", "ageField"},
        {"april/18/2018", "dateField"}
    };

    foreach (KeyValuePair<string, string> target in targets)
    {
        // Extract text with location data
        LocationTextExtractionStrategy strategy = new LocationTextExtractionStrategy();
        PdfTextExtractor.GetTextFromPage(reader, 1, strategy);

        // Locate the target string's bounding box
        List<TextChunk> chunks = strategy.GetLocationTextChunks();
        foreach (TextChunk chunk in chunks)
        {
            if (chunk.Text.Equals(target.Key))
            {
                float x = chunk.GetStartLocation()[0];
                float y = chunk.GetStartLocation()[1];
                float width = chunk.GetEndLocation()[0] - x;
                float fieldHeight = 15; // Set your preferred field height

                // Position field just below the text (adjust offset as needed)
                float fieldY = y - fieldHeight - 2;

                // Create and add the text field
                TextField textField = new TextField(stamper.Writer, new Rectangle(x, fieldY, x + width, fieldY + fieldHeight), target.Value);
                textField.FontSize = 12;
                form.AddField(textField.GetTextField());
                break;
            }
        }
    }
}

Once this runs, the output PDF will have editable text fields below each target string. Later, you can read the field values using iTextSharp's AcroFields.GetField() method.

Are There Simpler Tools?

If you don't need automation (i.e., you're doing this once or a few times), Adobe Acrobat Pro has a "Prepare Form" feature that can auto-detect text and generate fields, or you can manually draw fields below the text. For automated workflows though, iTextSharp is the most flexible and reliable choice. Tools like PDFtk handle basic form tasks but don't support precise text-based field positioning like iTextSharp does.


内容的提问来源于stack exchange,提问作者sergio trajano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:26:18