Pdf书签和链接的问题
问题描述:
如何在asp.net中获取PDF文档中的书签和链接..?
How to get Bookmarks and Links in a PDF Document in asp.net..?
答
嗨朋友这是获取书签和链接的解决方案来自pdf 使用 itextsharp.dll
Hi Friend this is solution for getting bookmarks and links from pdf using itextsharp.dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using System.Data;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
DataTable bookmarksTable = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string filename = FileUpload1.FileName;
//Save file temporary in server path
FileUpload1.SaveAs(Server.MapPath(filename));
//pass that method in sepearte pdf content read method
ReadPDFContent(Server.MapPath(filename));
}
}
private static List<string> GetPdfLinks(string file, int page)
{
//Open our reader
PdfReader pdfData = new PdfReader(file);
//Get the current page
PdfDictionary PageDictionary = pdfData.GetPageN(page);
//Get all of the annotations for the current page
PdfArray Annots = PageDictionary.GetAsArray(PdfName.ANNOTS);
//Make sure we have something
if ((Annots == null) || (Annots.Length == 0))
return null;
List<string> Ret = new List<string>();
//Loop through each annotation
foreach (PdfObject annotation in Annots.ArrayList)
{
//Convert the itext-specific object as a generic PDF object
PdfDictionary AnnotationDictionary = (PdfDictionary)PdfReader.GetPdfObject(annotation);
//Make sure this annotation has a link
if (!AnnotationDictionary.Get(PdfName.SUBTYPE).Equals(PdfName.LINK))
continue;
//Make sure this annotation has an ACTION
if (AnnotationDictionary.Get(PdfName.A) == null)
continue;
//Get the ACTION for the current annotation
PdfDictionary AnnotationAction = (PdfDictionary)AnnotationDictionary.Get(PdfName.A);
//Test if it is a URI action (There are tons of other types of actions, some of which might mimic URI, such as JavaScript, but those need to be handled seperately)
if (AnnotationAction.Get(PdfName.S).Equals(PdfName.URI))
{
PdfString Destination = AnnotationAction.GetAsString(PdfName.URI);
if (Destination != null)
Ret.Add(Destination.ToString());
}
}
return Ret;
}
protected DataTable TakeBookMarksFromList(PdfReader pdfFileReader, Dictionary<string, object> bookmarks)
{
string title = null, section = null;
DataRow bookmarkRow = bookmarksTable.NewRow();
foreach (KeyValuePair<string, object> dictionaryValue in bookmarks)
{
if (dictionaryValue.Key == "Named" || dictionaryValue.Key == "Page")
{
section = dictionaryValue.Value.ToString();
bookmarkRow["Section"] = section;
if (dictionaryValue.Key == "Page")
{
bookmarkRow["Destination"] = section.Substring(0, section.IndexOf(" "));
}
else
{
Dictionary<string, string> pages = SimpleNamedDestination.GetNamedDestination(pdfFileReader, false);
foreach (KeyValuePair<string, string> destPage in pages)
{
if (destPage.Key.StartsWith(section))
{
bookmarkRow["Destination"] = destPage.Value.Substring(0, destPage.Value.IndexOf(" "));
}
}
}
}
if (dictionaryValue.Key == "Title")
{
title = dictionaryValue.Value.ToString();
bookmarkRow["Title"] = title;
bookmarksTable.Rows.Add(bookmarkRow);
}
if (dictionaryValue.Key == "Kids")
{
IList<Dictionary<string, object>> innerDictionary = (IList<Dictionary<string, object>>)dictionaryValue.Value;
foreach (Dictionary<string, object> innerKids in innerDictionary)
{
if (innerKids.Count > 3)
{
TakeBookMarksFromList(pdfFileReader, innerKids);
}
else
{
bookmarkRow = bookmarksTable.NewRow();
foreach (KeyValuePair<string, object> childPair in innerKids)
{
if (childPair.Key == "Named" || childPair.Key == "Page")
{
section = childPair.Value.ToString();
bookmarkRow["Section"] = section;
if (childPair.Key == "Page")
{
bookmarkRow["Destination"] = section.Substring(0, section.IndexOf(" "));
}
else
{
Dictionary<string, string> pages = SimpleNamedDestination.GetNamedDestination(pdfFileReader, false);
foreach (KeyValuePair<string, string> destPage in pages)
{
if (destPage.Key.StartsWith(section))
{
bookmarkRow["Destination"] = destPage.Value.Substring(0, destPage.Value.IndexOf(" "));
}
}
}
}
if (childPair.Key == "Title")
{
title = childPair.Value.ToString();
bookmarkRow["Title"] = title;
bookmarksTable.Rows.Add(bookmarkRow);
}
}
}
}
}
}
return bookmarksTable;
}
void ReadPDFContent(string path)
{
bookmarksTable.Columns.Add("Section", typeof(string));
bookmarksTable.Columns.Add("Title", typeof(string));
bookmarksTable.Columns.Add("Destination", typeof(string));
if (File.Exists(path))
{
PdfReader pdfFileReader = new PdfReader(path);
IList<Dictionary<string, object>> bookmarks = SimpleBookmark.GetBookmark(pdfFileReader);
if (bookmarks != null)
{
foreach (Dictionary<string, object> item in bookmarks)
{
bookmarksTable = TakeBookMarksFromList(pdfFileReader, item);
}
}
#region Links in Pdf File
DataTable linksTable = new DataTable();
linksTable.Columns.Add("Link Text", typeof(string));
linksTable.Columns.Add("PageNumber", typeof(int));
for (int pageNum = 1; pageNum <= pdfFileReader.NumberOfPages; pageNum++)
{
List<string> Links = GetPdfLinks(path, pageNum);
if (Links != null)
{
if (Links.Count > 0)
{
for (int i = 0; i < Links.Count; i++)
{
DataRow linkRow = linksTable.NewRow();
linkRow["Link Text"] = Links[i].ToString();
linkRow["PageNumber"] = pageNum;
linksTable.Rows.Add(linkRow);
}
}
}
}
#endregion Links in Pdf File
if (linksTable.Rows.Count == 0)
{
DataRow linkRow = linksTable.NewRow();
linksTable.Rows.Add(linkRow);
gridLinks.DataSource = linksTable;
gridLinks.DataBind();
int gridLinksColCount = gridLinks.Rows[0].Cells.Count;
gridLinks.Rows[0].Cells.Clear();
gridLinks.Rows[0].Cells.Add(new TableCell());
gridLinks.Rows[0].Cells[0].ColumnSpan = gridLinksColCount;
gridLinks.Rows[0].Cells[0].Text = "No Links Found in Document";
}
else
{
gridLinks.DataSource = linksTable;
gridLinks.DataBind();
}
if (bookmarksTable.Rows.Count == 0)
{
DataRow bookmarkRow = bookmarksTable.NewRow();
bookmarksTable.Rows.Add(bookmarkRow);
gridBookmarks.DataSource = bookmarksTable;
gridBookmarks.DataBind();
int columncount = gridBookmarks.Rows[0].Cells.Count;
gridBookmarks.Rows[0].Cells.Clear();
gridBookmarks.Rows[0].Cells.Add(new TableCell());
gridBookmarks.Rows[0].Cells[0].ColumnSpan = columncount;
gridBookmarks.Rows[0].Cells[0].Text = "No Bookmarks Found in Document";
}
else
{
gridBookmarks.DataSource = bookmarksTable;
gridBookmarks.DataBind();
}
}
}
}