如何检查pdf页面上是否有书签? [英] How to check if a pdf page has a bookmark?

查看:208
本文介绍了如何检查pdf页面上是否有书签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查pdf文件中的页面是否具有书签以及该书签中的内容,我正在使用"iTextSharp.text.pdf"来阅读和操作pdf,但是我找不到一种检查页面是否具有书签的方法.

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.

请帮助 谢谢!

我尝试获取书签,但是它获取了所有收藏夹,我不知道如何获取特定页面的书签,我使用了以下代码:

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

推荐答案

您可以从每个书签字典中的Page键提取每个书签的页面.

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

例如:

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

这篇关于如何检查pdf页面上是否有书签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆