How to check if a pdf page has a bookmark?

假装没事ソ 提交于 2019-12-20 05:55:51

问题


I'm trying to check if a page in a pdf file has a bookmark and what is in that bookmark, I'm using "iTextSharp.text.pdf" for reading and manipulating a pdf, but I can't find a way to check if a page has a bookmark or not.

Pleas help Thanks!

I've tried to get the bookmarks, but it gets me the all collection and i don't know how to get for a specific page it's bookmark, i used this code:

public void Bookmarks(string pdfSourceFile)
    {
        PdfReader reader = new PdfReader(pdfSourceFile, new System.Text.ASCIIEncoding().GetBytes(""));
        IList<Dictionary<string, object>> bookmarks = SimpleBookmark.GetBookmark(reader);
        foreach (IDictionary<String, Object> bmProperty in bookmarks)
        {

            foreach (var fileProperty in bmProperty.Keys)
            {

                if (fileProperty == "File")
                {
                    // need the edit the value of Key-"File". Will it be possible to alter the value using pdfwriter
                }
            }
        }

回答1:


You can extract the page for each bookmark from the Page key in each bookmark dictionary.

For example:

public bool isBookmarked(string pdfSourceFile, int pageNumber)
{
    var reader = new PdfReader(pdfSourceFile, new System.Text.ASCIIEncoding().GetBytes(""));
    var bookmarks = SimpleBookmark.GetBookmark(reader);
    foreach (var bookmark in bookmarks)
        if (Int32.Parse(bookmark["Page"].ToString().Split(' ')[0]) == pageNumber)
            return true;

    return false;
}



回答2:


IList<Dictionary<string, object>> bookmarks = SimpleBookmark.GetBookmark(pdfReader);
foreach (Dictionary<string, object> bk in bookmarks)
{
string bjj = bk.Values.ToArray().GetValue(0).ToString();

Use This.



来源:https://stackoverflow.com/questions/9464627/how-to-check-if-a-pdf-page-has-a-bookmark

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!