DumpFields: Example code of Early Access iText

简介:
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);
        }
    }
}
 
本文转自 RubyPdf 的中文博客博客园博客,原文链接: http://www.cnblogs.com/hardrock/archive/2006/07/19/454386.html,如需转载请自行联系原作者
相关文章
|
8月前
|
安全 Java API
Eclipse 打开marketplace 报PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
网上有很多解决的帖子,我这里只是记录一下方便自己查阅 主要原因是java本身需要证书 1、按导入安全证书的方法解决
909 0
Eclipse 打开marketplace 报PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
|
开发者
上传苹果版本时错误解决办法:No suitable application records were found. Verify your bundle identifier
上传苹果版本时错误解决办法:No suitable application records were found. Verify your bundle identifier
130 2
|
程序员 iOS开发 开发者
iOS开发:程序打包提示Ignoring file XXX missing required architecture arm7 in file XXX错误
前段时间在开发过程中遇到一个iOS打包报错Ignoring file XXX missing required architecture arm7 in file XXX的问题,然后回想了一下问题所在,是因为集成了ijk播放器插件,然后ijk三方库不支持arm7造成的报错。
202 0
iOS开发:程序打包提示Ignoring file XXX missing required architecture arm7 in file XXX错误
The 'manifest_version' key must be present and set to 2 (without quotes)
The 'manifest_version' key must be present and set to 2 (without quotes)
94 0
|
Java API
Access restriction: The type 'BASE64Decoder' is not API (restriction on required library xxx)
版权声明:本文为 testcs_dn(微wx笑) 原创文章,非商用自由转载-保持署名-注明出处,谢谢。 https://blog.csdn.net/testcs_dn/article/details/79913071 ...
2859 0