DumpFields: Example code of Early Access iText
2006-07-19 10:05
387 查看
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);
}
}
}
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);
}
}
}
相关文章推荐
- field_merge: Example code of Early Access iText(how to fill PDF form with iTextSharp)
- form_checkbox: Example code of Early Access iText
- form_radio: Example code of Early Access iText
- fdf_write: Example code of Early Access iText
- align2: Example code of Early Access iText
- font_selector: Example code of Early Access iText
- form_combo: Example code of Early Access iText
- About itextSharp version example codes of Early Access iText(updated on 07/31/2006)
- Understand Code Access Security of SPS
- Class com.googlecode.jsonplugin.JSONWriter can not access a member of class*
- A real example of vioplot in R (sample data and code attached)
- Class com.googlecode.jsonplugin.JSONWriter can not access a member of class*
- Class com.googlecode.jsonplugin.JSONWriter can not access a member of class*
- The target assembly contains no service types. You may need to adjust the Code Access Security policy of this assembly." 目标程序集不包含服务类型。可能需要调整此程序集的代码访问安全策略。
- Class com.googlecode.jsonplugin.JSONWriter can not access a member of class*
- Question 29: The C++ code below generates a compiler error. Which of the following solutions can be used to correctly access the
- example code of Ajax
- Example code of searching in WordNet Dictionary with JAWS library
- A example code of multithread communication in Python
- Example Code of Using XML Parser in Symbian OS