itextsharp官网的PDF加密中pdfSrc,pdfDest,strength,permissions分别对应的是什么

posts - 198,&
comments - 1478,&
trackbacks - 6
在CodeProject上看到一篇不错的文章,所以在这里还是向大家推荐一下。&
Introduction
In this article I will present a simple source code allowing you to digitally sign a PDF document and modify its Meta data. I will use the excellent and free port of
library : iTextSharp that can be downloaded .You'll need Visual Studio 2005 to be able to open and build the project
If you don’t know what are digital signatures or how does they work, you can go
or simply ask Google :)
iTextSharp provides a lot of interesting features to create and manipulate PDF documents, but in this article we will only use digital signature functions. I will also use some function to manipulate pkcs#12 certificates, the only thing you need to know here is that our digital signature will use a private key extracted from a pkcs#12 certificate.
Getting started
So the first thing you have to do is to install a certificate on your browser, if you don’t have one you can install demo certificate from . Then extract the pkcs#12 certificate as described bellow :
Open Internet explorer and click on tools then internet options
Go to 'content' tab and click 'certificats'
Choose a certificate from the list and click Export
Follow the wizard and when asked choos to include private key with extracted certificate
Enter a password when prompted (dont give an empty one!!!)
You are now ready to use the code provided in this article. Using the signature example: 1 – compile and run the example2 – browse to the PDF source file you want to sign3 – browse and choose a destination pdf file4 – add/modify the PDF meta data if you want5 – browse to the certificate (the .pfx file) you just extracted and choose it6 – give the password you used to extract the certificate5 – add signature information if needed (reason, contact and location)6 – click sign buttonIn the debug box you’ll see operaion’s progressIf everything goes well open your explorer and browse to location you entered for Target file, open this file with Adobe Acrobat reader, your document is signed! =)
Now how all this work?
In the source code provided with this article, I wrote library called PDFSigner, it’s a helper package that use iTextSharp and do everything you need for digital signatures. It contains three classes
Cert class: this class is used to hold a certificate and extract needed information for signature, the most important methode in this class is processCert (will be explained bellow)
MetaData class : holds PDF meta data
PDFSigner class : the construction of this class takes a Cert object and if needed a MetaData object, the most important methode here is the sign methode (will be explained bellow)
processCet method
&&&&&&&&private&void&processCert()&&&&&&&&{&&&&&&&&&&&&&&&&string&alias&=&null;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&PKCS12Store&pk12;&&&&&&&&&&&&&&&&//First&we'll&read&the&certificate&file&&&&&&&&&&&&&&&&pk12&=&new&PKCS12Store(new&FileStream(this.Path,&FileMode.Open,&FileAccess.Read),&this.password.ToCharArray());&&&&&&&&&&&&&&&&//then&Iterate&throught&certificate&entries&to&find&the&private&key&entry&&&&&&&&&&&&&&&&IEnumerator&i&=&pk12.aliases();&&&&&&&&&&&&&&&&while&(i.MoveNext())&&&&&&&&&&&&&&&&{&&&&&&&&&&&&&&&&&&&&alias&=&((string)i.Current);&&&&&&&&&&&&&&&&&&&&if&(pk12.isKeyEntry(alias))&&&&&&&&&&&&&&&&&&&&&&&&break;&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&this.akp&=&pk12.getKey(alias).getKey();&&&&&&&&&&&&&&&&X509CertificateEntry[]&ce&=&pk12.getCertificateChain(alias);&&&&&&&&&&&&&&&&this.chain&=&new&org.bouncycastle.x509.X509Certificate[ce.Length];&&&&&&&&&&&&&&&&for&(int&k&=&0;&k&&&ce.L&++k)&&&&&&&&&&&&&&&&&&&&chain[k]&=&ce[k].getCertificate();&&&&&&&&&&&&}
This methode reads the certificate and iterate throught its entries to find the private key entry then extract it.it also construct the certificate's chain if available
sign method
public&void&Sign(string&SigReason,&string&SigContact,&string&SigLocation,&bool&visible){&&&&PdfReader&reader&=&new&PdfReader(this.inputPDF);&&&&//Activate&MultiSignatures&&&&PdfStamper&st&=&PdfStamper.CreateSignature(reader,&new&FileStream(this.outputPDF,&FileMode.Create,&FileAccess.Write),&'\0',&null,&true);&&&&//To&disable&Multi&signatures&uncomment&this&line&:&every&new&signature&will&invalidate&older&ones&!&&&&//PdfStamper&st&=&PdfStamper.CreateSignature(reader,&new&FileStream(this.outputPDF,&FileMode.Create,&FileAccess.Write),&'\0');&&&&&st.MoreInfo&=&this.metadata.getMetaData();&&&&st.XmpMetadata&=&this.metadata.getStreamedMetaData();&&&&PdfSignatureAppearance&sap&=&st.SignatureA&&&&&&&&sap.SetCrypto(this.myCert.Akp,&this.myCert.Chain,&null,&PdfSignatureAppearance.WINCER_SIGNED);&&&&sap.Reason&=&SigR&&&&sap.Contact&=&SigC&&&&sap.Location&=&SigL&&&&&&&&&&&&&&&&if&(visible)&&&&&&&&sap.SetVisibleSignature(new&iTextSharp.text.Rectangle(100,&100,&250,&150),&1,&null);&&&&&&&&st.Close();}
this function reads the content of a given pdf , then it use the read data to create a new PDF using PDFStamper.PDFStamper is a PDF Writer that can sign PDF documents, the signature appearence can be configured so you can add a reason, a contact and a location attributes to the signature.SetCrypto methode allows us to sign the document using the private key and chain certificate we extracted from the certificate file.And finally, the SetVisibleSignature is used if you need to add a visible signature to the documentPDFReader, PDFStamper and PdfSignatureAppearance are provided by iTextSharp library&From /useritems/Esignature.asp
阅读(...) 评论()&&iText in Action 2nd&&6.2节(Copying pages from existing PDF document
&&iText in Action 2nd&&6.2节(Copying pages from existing PDF document
[摘要:媒介 人人大概借记得我们正在第五节中建立d的超人pdf文档,阿谁文档是从包括了Pdf语法的文本文件中构建起去的,图片看起去蛮cool,但那没有是规范的构建体式格局。若是您愿望能够重用现有]
大家可能还记得我们在第五节中创建d的超人pdf文档,那个文档是从包含了Pdf语法的文本文件中构建起来的,图片看起来蛮cool,但那不是标准的构建方式。如果你希望可以重用现有文档内容,使用第五节中那些方法就比较危险。其实有更加安全的方法从现有文档中导入内容,这也是我们这一节要介绍的内容。
Importing pages
现在我们对第三节中创建的TimeTable文档进行一些操作。假设我们希望重用TimeTable文档中的内容,而且将其的每一页当作一个图片处理。下图就是如果用PdfPTable处理导出页面的效果。
代码如下:
listing 6.4 ImportingPages1.cs
document.Open();
PdfPTable table = new PdfPTable(2);
PdfReader reader = new PdfReader(new MovieTemplates().ContructFile());
int n = reader.NumberOfP
PdfImportedPage
for (int i = 1; i &= i++)
page = writer.GetImportedPage(reader, i);
table.AddCell(Image.GetInstance(page));
document.Add(table);
在以上代码中我们会发现使用了五步来构建PDF文档的过程。只是在这里我们通过PdfReader对象来循环现有文档的所有页面,然后通过GetImportedPage方法获取一个PdfImportedPage对象实例。但这些方法具体的含义是什么呢?
PAGE CONTENT AND RESOURCES
如果你浏览pdfReader类的API文档就会发现有一个GetPageContent方法,这个方法会返回一个页面的内容流。这个内容流和构建超人图片的hero.txt文件的内容很类似。一般情况下,这种内容流会包含一些外部的引用如图片和字体。
在3.4节中,画一个像素图的PDF语法如下:
q 232 0 0 362 25.5 27 cm /img0 Do Q
在上面的片段中/img0引用的是页面/Resources字典中的一个key,其对应的值就是包含图片的二进制内容。如果没有这些二进制内容,/img0这个pdf语法就没有意义了。
但在以前的超人列子有些特殊:画图片的语法都是自我描述和自我包含,这是很不寻常的情况。对其他的文档来说只要涉及到文本,那么我们至少有一个字体的引用。如果我们没有将字体copy过去,那么就会得到类似这样的警告:"Could not find a font in the Resoucres dictionary"。这样也是我们为什么从不用PdfReader类直接抽取页面的原因。相反我们应该将此PdfReader类传给PdfWriter类,然后通过PdfWriter(注意不是reader)去导出一个页面,这样就返回一个PdfImportedPage对象,在这种情况下所有必要的资源(比如图片和字体)都会被获取和copy过去。
使用PdfImportedPagel类时大家会发现页面所有的链接(links)全部都没有了。在这里我们要理解页面内容呈现需要的资源和页面交互特性之间的区别。这些特性一般也叫做注释(annotations),其中包括链接(links),文本注释(text annotations)和表单字段(form field)。大家要注意的是这些特性不属于页面内容流,他们没有包含在页面的资源字典中,而是在页面的注释字典中。这些交互的特性在使用PdfImportedPage对象时是不会被复制的。
PdfImportedPage对象继承于PdfTemplate,但是我们不能往其添加内容,这是一个只读XObject,我们只能通过AddTemplate方法去重用或者将其包裹在Image中,这些技巧都在3.4节中已经学习过。每一个导出页面的原始尺寸和页面初始media box一致,但PdfImportedPage对象会将其缩放以便添加到表格中。这里要注意的是原始页面的旋转通过PdfImportedPage对象时也是没有修改的,如果页面的旋转有问题我们还需要设置旋转相关的属性,代码如下:
listing 6.5 ImportingPages2.cs
document.Open();
PdfReader reader = new PdfReader(new MovieTemplates().ContructFile());
int n = reader.NumberOfP
PdfImportedPage
PdfPTable table = new PdfPTable(2);
for (int i = 1; i &= i++)
page = writer.GetImportedPage(reader, i);
table.DefaultCell.Rotation = -reader.GetPageRotation(i);
table.AddCell(Image.GetInstance(page));
document.Add(table);
以上代码就是将现有文档的页面旋转了,注意旋转的方向为逆时针,接下来我们会学习更多的转换。
Scaling and superimposing pages
在iText中我们可以将页面转换就像前几节中转换图片一样。下图的左边是我们学习内容层用的文档,一共有四页,现在我们会将其导出并整合成一页显示,效果在下图的右边:
listing 6.6& Layers.cs
PdfContentByte canvas = writer.DirectC
PdfImportedPage
BaseFont bf = BaseFont.CreateFont(BaseFont.ZAPFDINGBATS, "", BaseFont.EMBEDDED);
for (int i = 0; i & reader .NumberOfP )
page = writer.GetImportedPage(reader, ++i);
canvas.AddTemplate(page, 1f, 0, 0.4f, 0.4f, 72, 50 * i);
canvas.BeginText();
canvas.SetFontAndSize(bf, 20);
canvas.ShowTextAligned(Element.ALIGN_CENTER, ((char)(181 + i)).ToString(), 496, 150 + 50 * i, 0);
canvas.EndText();
在PDF文件中有一个比较常用的技巧:叠加(superimposing)。
SUPERIMPOSING PDF PAGES
叠加的意思在一页的pdf中我们将页面依次叠加起来,上一步中我们有4个页面,下图就是其4个页面叠加的效果图:
listing 6.7 Superimposing.cs
using (document)
document.Open();
PdfContentByte canvas = writer.DirectC
PdfImportedPage
for (int i = 1; i &= reader.NumberOfP i++)
page = writer.GetImportedPage(reader, i);
canvas.AddTemplate(page, 1f, 0, 0, 1, 0, 0);
叠加一般用来创建一个有标准页眉和页脚的文档。
IMPORTING COMPANY STATIONERY
有些公司会预印一些纸张,其中包含了公司的名称和logo或者还有水印,然后具体打印文档的时候就直接使用这些纸张打进行印。我们也可以通过PDF完成类似的效果:
上图的左边就是预印的页面,右边呈现的为添加了预印页面作为背景的一个新文档,添加的方法就是通过页面事件,具体代码如下:
listing 6.8 Stationery.cs
public void UseStationary(PdfWriter writer)
writer.PageEvent = this;
PdfReader reader = new PdfReader(stationery);
page = writer.GetImportedPage(reader, 1);
public void OnEndPage(PdfWriter writer, iTextSharp.text.Document document)
writer.DirectContentUnder.AddTemplate(page, 0, 0);
接下里我们会通过几个列子总结一下PdfImportedPage类的用法。
N-up copying and tiling PDF documents
当我们在google一些pdf工具时,你会发现有大量满足特定需求的小工具,比如创建一个N-UP布局的pdf文档。N-UP布局的意思就是将N页组合到一页中,下图就是2-UP,4-UP,8-UP,16-UP的效果图:
网上的这些小工具一般包含了iText类库,具体的代码如下:
listing 6.9 NUp.cs
public void ManipulatePdf(string src, string dest, int pow)
// reader for the src file
PdfReader reader = new PdfReader(src);
// initializations
Rectangle pageSize = reader.GetPageSize(1);
Rectangle newSize = (pow % 2) == 0 ? new Rectangle(pageSize.Width, pageSize.Height) : new Rectangle(pageSize.Height, pageSize.Width);
Rectangle unitSize = new Rectangle(pageSize.Width, pageSize.Height);
for (int i = 0; i & i++)
unitSize = new Rectangle(unitSize.Height / 2, unitSize.Width);
int n = (int)Math.Pow(2, pow);
int r = (int)Math.Pow(2, pow / 2);
int c = n /
Document document = new Document(newSize, 0, 0, 0, 0);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(string.Format(dest, pow), FileMode.Create));
using (document)
document.Open();
PdfContentByte cb = writer.DirectC
PdfImportedPage
Rectangle currentS
float offsetX, offsetY,
int total = reader.NumberOfP
for (int i = 0; i & )
if (i % n == 0)
document.NewPage();
currentSize = reader.GetPageSize(++i);
factor = Math.Min(unitSize.Width / currentSize.Width, unitSize.Height / currentSize.Height);
offsetX = unitSize.Width * ((i % n) % c) + (unitSize.Width - (currentSize.Width * factor)) / 2f;
offsetY = newSize.Height - (unitSize.Height * (((i % n) / c) + 1)) + (unitSize.Height - (currentSize.Height * factor)) / 2f;
page = writer.GetImportedPage(reader, i);
cb.AddTemplate(page, factor, 0, 0, factor, offsetX, offsetY);
和N-up布局相反的就是将一页分成多页,英文叫做TillPage,效果图如下:
这里列子我们在第五节的时候已经碰到了,这里我们使用PdfImportedPage类重新实现,具体代码如下:
listing 6.10 TilingHero.cs
PdfReader reader = new PdfReader(resouce);
Rectangle pagesize = reader.GetPageSizeWithRotation(1);
Document document = new Document(pagesize);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
using (document)
document.Open();
PdfContentByte content = writer.DirectC
PdfImportedPage page = writer.GetImportedPage(reader, 1);
// adding the same page 16 times with a different offset
for (int i = 0; i & 16; i++)
x = -pagesize.Width * (i % 4);
y = pagesize.Height * (i / 4 - 3);
content.AddTemplate(page, 4, 0, 0, 4, x, y);
document.NewPage();
在这一节中我们学会从现有文档中重用内容,其中介绍了PdfImportedPage类,并学习如何缩放和转换页面,还介绍了N-up布局和TillPage的方法,这些知识点在代码中都统一在AddTemplate方法中,此方法有大量的参数,但作者在这一节中对其梅雨哦说明,直到书的14节(坐标系统转换)时才会对此方法详细介绍。这里我们知道有这些功能并能依葫芦画瓢应该就可以了,最后就是代码下载。
此文章已同步到目录索引:iText in Action 2nd 读书笔记。
感谢关注 Ithao123精品文库频道,是专门为互联网人打造的学习交流平台,全面满足互联网人工作与学习需求,更多互联网资讯尽在 IThao123!
Laravel是一套简洁、优雅的PHP Web开发框架(PHP Web Framework)。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁、富于表达力。
Hadoop是一个由Apache基金会所开发的分布式系统基础架构。
用户可以在不了解分布式底层细节的情况下,开发分布式程序。充分利用集群的威力进行高速运算和存储。
Hadoop实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS。HDFS有高容错性的特点,并且设计用来部署在低廉的(low-cost)硬件上;而且它提供高吞吐量(high throughput)来访问应用程序的数据,适合那些有着超大数据集(large data set)的应用程序。HDFS放宽了(relax)POSIX的要求,可以以流的形式访问(streaming access)文件系统中的数据。
Hadoop的框架最核心的设计就是:HDFS和MapReduce。HDFS为海量的数据提供了存储,则MapReduce为海量的数据提供了计算。
产品设计是互联网产品经理的核心能力,一个好的产品经理一定在产品设计方面有扎实的功底,本专题将从互联网产品设计的几个方面谈谈产品设计
随着国内互联网的发展,产品经理岗位需求大幅增加,在国内,从事产品工作的大部分岗位为产品经理,其实现实中,很多从事产品工作的岗位是不能称为产品经理,主要原因是对产品经理的职责不明确,那产品经理的职责有哪些,本专题将详细介绍产品经理的主要职责
IThao123周刊c# PDF操作库iTextSharp源代码单文件版,适合项目开发 - 为程序员服务
为程序员服务
PDF操作库iTextSharp源代码单文件版,适合项目开发
PDF操作库iTextSharp源代码单文件版,仅依赖Bouncy Castle加密库。
从 PDF 文档获取内容
public void ExtractTextFromPDFPage(string pdfFile, int pageNumber)
PdfReader reader = new PdfReader(pdfFile);
string text = PdfTextExtractor.GetTextFromPage(reader, pageNumber);
try { reader.Close(); }
使用 iTextSharp 生成中文 PDF 文档
using System.D
using System.IO;
/// &summary&
/// Print a table to a Pdf file in tabular format,
/// with header image and text on every page
/// &/summary&
/// &param name=&PdfFileName&&This is pyhsical file name
/// of Pdf document that you wanto write to&/param&
/// &param name=&dt&&Datatable that contain the data to print&/param&
/// &param name=&DocTitle&&This is the title of the
/// document to be printed once on first page&/param&
/// &param name=&PageHeader&&Header text to go
with header image on every page&/param&
/// &param name=&HeaderImgPath&&the physical file path of a image
/// that you want to print out on every page header&/param&
/// &returns&&/returns&
public static bool ExportPdfTable(string PdfFileName, DataTable dt,
string DocTitle,string PageHeader,string HeaderImgPath)
Document doc = new Document(); //iTextSharp document
string physicalFile = PdfFileN
PdfWriter pw=PdfWriter.GetInstance(doc,
new FileStream(physicalFile, FileMode.Create));
//prepare for inserting header on every page, using Pageevent
//DocumentEevent e = new DocumentEevent(HeaderImgPath, PageHeader);
//pw.PageEvent =
doc.Open();
if (PageHeader.Length & 0)
doc.Add(new Paragraph(new Phrase(&&)));
if (DocTitle.Length & 0)
doc.Add(new Phrase(DocTitle));
if (dt.Columns.Count == 0)
int cols = dt.Columns.C
int rows = dt.Rows.C
//prepare the table object
PdfPTable t = new PdfPTable(cols);
t.WidthPercentage = 100;
//cell object
//Use BaseFont to load unicode fonts like Simplified Chinese font
string fontpath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath +
&\\includes\\fonts\\simsun.ttf&;
//&simsun.ttf& file was downloaded from web and placed in the folder
BaseFont bf = BaseFont.CreateFont(fontpath,BaseFont.IDENTITY_H,
BaseFont.EMBEDDED);
//create new font based on BaseFont
Font fontContent = new Font(bf, 11);
Font fontHeader = new Font(bf, 12);
//write header
for (int j = 0; j & j++)
Phrase pr=new Phrase((dt.Columns[j].Caption != null &&
dt.Columns[j].Caption.Length & 0) ? dt.Columns[j].Caption :
dt.Columns[j].ColumnName,fontHeader);
c = new PdfPCell(pr);
c.PaddingBottom = 5f;
c.PaddingTop = 5f;
c.PaddingLeft = 8f;
c.PaddingRight = 8f;
t.AddCell(c);
//write table content
for (int i = 0; i & i++)
for (int j = 0; j & j++)
//c = new Cell(dt.Rows[i][j].ToString());
//c.Header =
c = new PdfPCell(new Phrase(dt.Rows[i][j].ToString(),fontContent));
c.PaddingBottom = 5f;
c.PaddingTop = 5f;
c.PaddingLeft = 8f;
c.PaddingRight = 8f;
t.AddCell(c);
return doc.Add(t);
catch (Exception ex)
Console.WriteLine(ex.Message);
doc.Close();
解析PDF内容
using System.Collections.G
using System.IO;
using System.T
using System.
namespace iTextSharp.text.pdf.parser
* Tool that parses the content of a PDF document.
public class PdfContentReaderTool
* Shows the detail of a dictionary.
* This is similar to the PdfLister functionality.
* @param dic
the dictionary of which you want the detail
a String representation of the dictionary
public static String GetDictionaryDetail(PdfDictionary dic)
return GetDictionaryDetail(dic, 0);
* Shows the detail of a dictionary.
* @param dic
the dictionary of which you want the detail
* @param depth the depth of the current dictionary (for nested dictionaries)
a String representation of the dictionary
public static String GetDictionaryDetail(PdfDictionary dic, int depth)
StringBuilder builder = new StringBuilder();
builder.Append('(');
IList&PdfName& subDictionaries = new List&PdfName&();
foreach (PdfName key in dic.Keys)
PdfObject val = dic.GetDirectObject(key);
if (val.IsDictionary())
subDictionaries.Add(key);
builder.Append(key);
builder.Append('=');
builder.Append(val);
builder.Append(&, &);
builder.Length = builder.Length - 2;
builder.Append(')');
foreach (PdfName pdfSubDictionaryName in subDictionaries)
builder.Append('\n');
for (int i = 0; i & depth + 1; i++)
builder.Append('\t');
builder.Append(&Subdictionary &);
builder.Append(pdfSubDictionaryName);
builder.Append(& = &);
builder.Append(GetDictionaryDetail(dic.GetAsDict(pdfSubDictionaryName), depth + 1));
return builder.ToString();
* Displays a summary of the entries in the XObject dictionary for the stream
* @param resourceDic the resource dictionary for the stream
* @return a string with the summary of the entries
* @throws IOException
* @since 5.0.2
public static String GetXObjectDetail(PdfDictionary resourceDic)
StringBuilder sb = new StringBuilder();
PdfDictionary xobjects = resourceDic.GetAsDict(PdfName.XOBJECT);
if (xobjects == null)
return &No XObjects&;
foreach (PdfName entryName in xobjects.Keys)
PdfStream xobjectStream = xobjects.GetAsStream(entryName);
sb.Append(&------ & + entryName + & - subtype = & + xobjectStream.Get(PdfName.SUBTYPE) + & = & + xobjectStream.GetAsNumber(PdfName.LENGTH) + & bytes ------\n&);
if (!xobjectStream.Get(PdfName.SUBTYPE).Equals(PdfName.IMAGE))
byte[] contentBytes = ContentByteUtils.GetContentBytesFromContentObject(xobjectStream);
foreach (byte b in contentBytes)
sb.Append((char)b);
sb.Append(&------ & + entryName + & - subtype = & + xobjectStream.Get(PdfName.SUBTYPE) + &End of Content& + &------\n&);
return sb.ToString();
* Writes information about a specific page from PdfReader to the specified output stream.
* @since 2.1.5
* @param reader
the PdfReader to read the page content from
* @param pageNum
the page number to read
* @param out
the output stream to send the content to
* @throws IOException
public static void ListContentStreamForPage(PdfReader reader, int pageNum, TextWriter outp)
outp.WriteLine(&==============Page & + pageNum + &====================&);
outp.WriteLine(&- - - - - Dictionary - - - - - -&);
PdfDictionary pageDictionary = reader.GetPageN(pageNum);
outp.WriteLine(GetDictionaryDetail(pageDictionary));
outp.WriteLine(&- - - - - XObject Summary - - - - - -&);
outp.WriteLine(GetXObjectDetail(pageDictionary.GetAsDict(PdfName.RESOURCES)));
outp.WriteLine(&- - - - - Content Stream - - - - - -&);
RandomAccessFileOrArray f = reader.SafeF
byte[] contentBytes = reader.GetPageContent(pageNum, f);
f.Close();
outp.Flush();
foreach (byte b in contentBytes)
outp.Write((char)b);
outp.Flush();
outp.WriteLine(&- - - - - Text Extraction - - - - - -&);
String extractedText = PdfTextExtractor.GetTextFromPage(reader, pageNum, new LocationTextExtractionStrategy());
if (extractedText.Length != 0)
outp.WriteLine(extractedText);
outp.WriteLine(&No text found on page & + pageNum);
outp.WriteLine();
* Writes information about each page in a PDF file to the specified output stream.
* @since 2.1.5
* @param pdfFile
a File instance referring to a PDF file
* @param out
the output stream to send the content to
* @throws IOException
public static void ListContentStream(string pdfFile, TextWriter outp)
PdfReader reader = new PdfReader(pdfFile);
int maxPageNum = reader.NumberOfP
for (int pageNum = 1; pageNum &= maxPageN pageNum++)
ListContentStreamForPage(reader, pageNum, outp);
* Writes information about the specified page in a PDF file to the specified output stream.
* @since 2.1.5
* @param pdfFile
a File instance referring to a PDF file
* @param pageNum
the page number to read
* @param out
the output stream to send the content to
* @throws IOException
public static void ListContentStream(string pdfFile, int pageNum, TextWriter outp)
PdfReader reader = new PdfReader(pdfFile);
ListContentStreamForPage(reader, pageNum, outp);
* Writes information about each page in a PDF file to the specified file, or System.out.
* @param args
public static void Main(String[] args)
if (args.Length & 1 || args.Length & 3)
Console.WriteLine(&Usage:
PdfContentReaderTool &pdf file& [&output file&|stdout] [&page num&]&);
TextWriter writer = Console.O
if (args.Length &= 2)
if (!Util.EqualsIgnoreCase(args[1], &stdout&))
Console.WriteLine(&Writing PDF content to & + args[1]);
writer = new StreamWriter(args[1]);
int pageNum = -1;
if (args.Length &= 3)
pageNum = int.Parse(args[2]);
if (pageNum == -1)
ListContentStream(args[0], writer);
ListContentStream(args[0], pageNum, writer);
writer.Flush();
if (args.Length &= 2)
writer.Close();
Console.WriteLine(&Finished writing content to & + args[1]);
catch (Exception e)
Console.WriteLine(e.ToString());
您可能的代码
相关聚客文章
相关专栏文章}

我要回帖

更多关于 itextsharp.dll 下载 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信