0

ターゲットページのセクションを含むインデックスページを含むPDFファイルがあります。セクション名(セクション1.1、セクション5.2)は取得できましたが、ターゲットページ番号を取得できません...

例: 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の9ページ目には、セクションを取得できなかったセクションが含まれています。だから私に解決策をください....

4

1 に答える 1

3

Aリンク注釈には、またはの2つのタイプがありますDest。これAはより強力なタイプですが、多くの場合やり過ぎです。この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
            }
        }
    }
}
于 2012-04-25T18:45:44.097 に答える