问题
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