我如何使用iTextSharp的获得PDF文件部分目标页码?
我有一个PDF文件,其中包含索引页,其中包括与目标页面部分。
我能得到部分的名称(第1.1节,第5.2节),但我不能让目标页码...
I have a pdf file which contains Index Page that includes section with target page. I could get the section name(Section 1.1, Section 5.2) but i can not get the target page number...
有关例如:
http://www.mikesdotnetting.com/Article/84/iTextSharp-Links -and-书签
下面是我的code:
string FileName = AppDomain.CurrentDomain.BaseDirectory + "TestPDF.pdf";
PdfReader pdfreader = new PdfReader(FileName);
PdfDictionary PageDictionary = pdfreader.GetPageN(9);
PdfArray Annots = PageDictionary.GetAsArray(PdfName.ANNOTS);
if ((Annots == null) || (Annots.Length == 0))
return;
foreach (PdfObject oAnnot in Annots.ArrayList)
{
PdfDictionary AnnotationDictionary = (PdfDictionary)PdfReader.GetPdfObject(oAnnot);
if (AnnotationDictionary.Keys.Contains(PdfName.A))
{
PdfDictionary oALink = AnnotationDictionary.GetAsDict(PdfName.A);
if (oALink.Get(PdfName.S).Equals(PdfName.GOTO))
{
if (oALink.Keys.Contains(PdfName.D))
{
PdfObject objs = oALink.Get(PdfName.D);
if (objs.IsString())
{
string SectionName = objs.ToString(); // here i could see the section name...
}
}
}
}
}
我如何获得目标页码?
How do i get the target page number?
我也不能为某些PDF前访问该节名称:http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/pdf/pdfs/adobe_supplement_iso32000.pdf
also I couldn't access the Section name for some pdf ex: http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/pdf/pdfs/adobe_supplement_iso32000.pdf
在此PDF页面第九包含部分我不能让部分。
所以请给我的解决方案......
In this PDF 9th page contains a section I could not get the section. so please give me solution....
有两种可能的链接注释,无论是 A
或目的地
。在 A
是更强大的类型,但往往是矫枉过正。在目的地
只输入指定一些配件和缩放选项以及间接引用的页面。
There's two possible types of Link Annotations, either A
or Dest
. The A
is the more powerful type but is often overkill. The Dest
type just specifies an indirect reference to a page along with some fitting and zooming options.
的目的地
值可以是几个不同的事情,但通常是(据我所见过的)一个名为字符串目的地。你可以看一下命名目标文档的名称目的地字典。所以在你的主循环添加此,以便它可以在以后引用:
The Dest
value can be a couple of different things but is usually (as far as I've ever seen) a named string destination. You can look up named destinations in the document's name destination dictionary. So before your main loop add this so that it can be referenced later:
//Get all existing named destinations
Dictionary<string, PdfObject> dests = pdfreader.GetNamedDestinationFromStrings();
一旦你得到了目的地
作为一个字符串,你可以看看那个对象了在上述字典的关键。
Once you've got the Dest
as a string you can look that object up as a key in the above dictionary.
PdfArray thisDest = (PdfArray)dests[AnnotationDictionary.GetAsString(PdfName.DEST).ToString()];
返回的数组中的第一项就是你用来间接引用。 (实际上,第一个项目可能会重新$ P $远程文档中psenting页数量的整数,所以你可能要检查这一点。)
The first item in the array returned is the indirect reference that you're used to. (Actually, the first item could be an integer representing a page number in a remote document so you might have to check for that.)
PdfIndirectReference a = (PdfIndirectReference)thisDest[0];
PdfObject thisPage = PdfReader.GetPdfObject(a);
下面是code,它把最上面的一起,去掉了一些code,你已经拥有的。 A
和目的地
每所以没有标注应该永远都同时指定规范相互排斥的。
Below is code that puts most of the above together, omitting some of the code that you already have. A
and Dest
are mutually exclusive per the spec so no annotation should ever have both specified.
//Get all existing named desitnations
Dictionary<string, PdfObject> dests = pdfreader.GetNamedDestinationFromStrings();
foreach (PdfObject oAnnot in Annots.ArrayList) {
PdfDictionary AnnotationDictionary = (PdfDictionary)PdfReader.GetPdfObject(oAnnot);
if (AnnotationDictionary.Get(PdfName.SUBTYPE).Equals(PdfName.LINK)) {
if (AnnotationDictionary.Contains(PdfName.A)) {
//...Do normal A stuff here
} else if (AnnotationDictionary.Contains(PdfName.DEST)) {
if (AnnotationDictionary.Get(PdfName.DEST).IsString()) {//Named-based destination
if (dests.ContainsKey(AnnotationDictionary.GetAsString(PdfName.DEST).ToString())) {//See if it exists in the global name dictionary
PdfArray thisDest = (PdfArray)dests[AnnotationDictionary.GetAsString(PdfName.DEST).ToString()];//Get the destination
PdfIndirectReference a = (PdfIndirectReference)thisDest[0];//TODO, this could actually be an integer for the case of Remote Destinations
PdfObject thisPage = PdfReader.GetPdfObject(a);//Get the actual PDF object
}
} else if(AnnotationDictionary.Get(PdfName.DEST).IsArray()) {
//Technically possible, I think the array matches the code directly above but I don't have a sample PDF
}
}
}
}