2012-04-25 88 views
0

我有一个pdf文件,其中包含索引页,其中包括目标页面的部分。 我能得到的节名(第1.1节,第5.2节),但我不能让目标页码...如何在使用iTextSharp的pdf文件中获取节目标页码?

对于前: http://www.mikesdotnetting.com/Article/84/iTextSharp-Links-and-Bookmarks

这里是我的代码:

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... 
       } 
      } 
     } 
    } 
} 

我如何获得目标页码?

我也不能为某些PDF前访问该科名:http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/pdf/pdfs/adobe_supplement_iso32000.pdf

在这个PDF第九页包含一个部分,我不能让部分。 所以请给我解决方案....

+0

查看我的帖子,http://stackoverflow.com/a/6599734/231316。基本上做一个'oALink.GetAsArray(PdfName.D)',然后在返回的ArrayList的第一项上调用PdfReader.GetPdfObject()。返回的对象应该是对页面的引用。我找不到找到页面“数字”的方法,然后循环遍历每个页面并与我的参考进行比较。 – 2012-04-25 13:00:29

+0

oALink.GetAsArray(PdfName.D)仅返回NULL值。但它与其他pdf一起工作,除了我发布的pdf ... wy? – 2012-04-25 14:09:55

回答

3

有两种可能的链接注释类型,或者ADestA是更强大的类型,但往往矫枉过正。 Dest类型只是指定对页面的间接引用以及一些拟合和缩放选项。

Dest值可以是几个不同的东西,但通常是(据我所见)一个命名的字符串目的地。您可以在文档的名称目标字典中查找指定的目标。所以,你的主循环前加上这一点,以便以后可以参考:

//Get all existing named destinations 
Dictionary<string, PdfObject> dests = pdfreader.GetNamedDestinationFromStrings(); 

一旦你得到了Dest作为一个字符串,你可以看看那个对象了,如上述辞典的键

PdfArray thisDest = (PdfArray)dests[AnnotationDictionary.GetAsString(PdfName.DEST).ToString()]; 

返回的数组中的第一项是您习惯的间接引用。 (实际上,第一项可能是较远程文档中的页面数的整数,所以你可能要检查这一点。)

PdfIndirectReference a = (PdfIndirectReference)thisDest[0]; 
PdfObject thisPage = PdfReader.GetPdfObject(a); 

下面的代码,把最上面的一起,省略了一些你已有的代码。根据规范,ADest是互斥的,因此不应该同时指定任何注释。

//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 
      } 
     } 
    } 
} 
+0

谢谢你.........克里斯哈斯。工作很好............精湛 – 2012-04-26 06:37:33

相关问题