using System;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

/// <summary> List the name and position of the fields.</summary>
/// <author>  Paulo Soares
/// </author>
public class DumpFields
{
    
/// <param name="args">the command line arguments</param>
    [STAThread]
    
public static void  Main(string[] args)
    {
        
if (args.Length < 2)
        {
            System.Console.Error.WriteLine(
"Dumps the field positions of a PDF document.");
            System.Console.Error.WriteLine(
"The output format is a text file where the first line is the field");
            System.Console.Error.WriteLine(
"name and the second line is:");
            System.Console.Error.WriteLine(
"page x1 y1 x2 y2");
            System.Console.Error.WriteLine(
"This is repeated for each field.");
            System.Console.Error.WriteLine(
"usage: DumpFields pdf_file field_list");
            
return ;
        }
        
        
try
        {
            PdfReader reader 
= new PdfReader(args[0]);
            StreamWriter out_Renamed 
= new StreamWriter(new FileStream(args[1], FileMode.Create), System.Text.Encoding.Default);
            PRAcroForm form 
= reader.AcroForm;
            
if (form == null)
            {
                out_Renamed.Close();
                System.Console.Out.WriteLine(
"This document has no fields.");
                
return ;
            }
            
//            PdfLister list = new PdfLister(System.Console.OpenStandardOutput());
            System.Collections.Hashtable refToField = new System.Collections.Hashtable();
            System.Collections.ArrayList fields 
= form.Fields;
            
            
for (int k = 0; k < fields.Count; ++k)
            {
                
                PRAcroForm.FieldInformation field 
= (PRAcroForm.FieldInformation) fields[k];
                refToField[field.Ref.Number] 
= field;
            }
            
for (int page = 1; page <= reader.NumberOfPages; ++page)
            {
                PdfDictionary dPage 
= reader.GetPageN(page);
                PdfArray annots 
= (PdfArray) PdfReader.GetPdfObject((PdfObject) dPage.Get(PdfName.ANNOTS));
                
if (annots == null)
                    
continue;
                System.Collections.ArrayList ali 
= annots.ArrayList;
                
for (int annot = 0; annot < ali.Count; ++annot)
                {
                    PdfObject refObj 
= (PdfObject) ali[annot];
                    PRIndirectReference ref_Renamed 
= null;
                    PdfDictionary an 
= (PdfDictionary) PdfReader.GetPdfObject(refObj);
                    PdfName name 
= (PdfName) an.Get(PdfName.SUBTYPE);
                    
if (name == null || !name.Equals(PdfName.WIDGET))
                        
continue;
                    PdfArray rect 
= (PdfArray) PdfReader.GetPdfObject(an.Get(PdfName.RECT));
                    
string fName = "";
                    PRAcroForm.FieldInformation field 
= null;
                    
while (an != null)
                    {
                        PdfString tName 
= (PdfString) an.Get(PdfName.T);
                        
if (tName != null)
                            fName 
= tName.ToString() + "." + fName;
                        
if (refObj.Type == PdfObject.INDIRECT && field == null)
                        {
                            ref_Renamed 
= (PRIndirectReference) refObj;
                            
                            field 
= (PRAcroForm.FieldInformation) refToField[ref_Renamed.Number];
                        }
                        refObj 
= (PdfObject) an.Get(PdfName.PARENT);
                        an 
= (PdfDictionary) PdfReader.GetPdfObject(refObj);
                    }
                    
if (fName.EndsWith("."))
                        fName 
= fName.Substring(0, (fName.Length - 1- (0));
                    out_Renamed.WriteLine(fName);
                    out_Renamed.Write(page);
                    System.Collections.ArrayList arr 
= rect.ArrayList;
                    
for (int r = 0; r < 4++r)
                    {
                        PdfNumber num 
= (PdfNumber) PdfReader.GetPdfObject((PdfObject) arr[r]);
                        out_Renamed.Write(
" " + num.FloatValue);
                    }
                    out_Renamed.WriteLine();
                }
            }
            out_Renamed.Close();
            System.Console.Out.WriteLine(
"Finished.");
        }
        
catch (System.Exception e)
        {
            System.Console.Error.WriteLine(e.Message);
        }
    }
}
posted on 2006-07-19 10:05  RubyPDF  阅读(2379)  评论(1编辑  收藏  举报