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,如需转载请自行联系原作者
相关文章
|
1天前
|
数据采集 人工智能 安全
|
10天前
|
云安全 监控 安全
|
2天前
|
自然语言处理 API
万相 Wan2.6 全新升级发布!人人都能当导演的时代来了
通义万相2.6全新升级,支持文生图、图生视频、文生视频,打造电影级创作体验。智能分镜、角色扮演、音画同步,让创意一键成片,大众也能轻松制作高质量短视频。
910 150
|
15天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
1646 8
|
6天前
|
人工智能 前端开发 文件存储
星哥带你玩飞牛NAS-12:开源笔记的进化之路,效率玩家的新选择
星哥带你玩转飞牛NAS,部署开源笔记TriliumNext!支持树状知识库、多端同步、AI摘要与代码高亮,数据自主可控,打造个人“第二大脑”。高效玩家的新选择,轻松搭建专属知识管理体系。
365 152
|
7天前
|
人工智能 自然语言处理 API
一句话生成拓扑图!AI+Draw.io 封神开源组合,工具让你的效率爆炸
一句话生成拓扑图!next-ai-draw-io 结合 AI 与 Draw.io,通过自然语言秒出架构图,支持私有部署、免费大模型接口,彻底解放生产力,绘图效率直接爆炸。
604 152
|
9天前
|
人工智能 安全 前端开发
AgentScope Java v1.0 发布,让 Java 开发者轻松构建企业级 Agentic 应用
AgentScope 重磅发布 Java 版本,拥抱企业开发主流技术栈。
570 13
|
2天前
|
编解码 人工智能 机器人
通义万相2.6,模型使用指南
智能分镜 | 多镜头叙事 | 支持15秒视频生成 | 高品质声音生成 | 多人稳定对话