Вы находитесь на странице: 1из 2

using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using System.Collections;
using PdfSharp.Drawing;
public partial class SearchPDF_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// create an instance of the pdfparser class
//PDFParser pdfParser = new PDFParser();
// extract the text
// String result = ExtractText();
//Response.Write(result);
Bookmark();
}
public void Bookmark()
{
try
{
IList<string> Title = new List<string>();
Title.Add("FOREIGN FULBRIGHT GRANT APPLICATION COVER SHEET");
Title.Add("Application for foreign language teaching assistantship i
n the United States");
Title.Add("My Reason for Applying Page:");
Dictionary<int, int> RepeatedTitle = new Dictionary<int, int>();
Dictionary<int, string> PageTitles = new Dictionary<int, string>();

string inFileName = Server.MapPath("~/PDFBookmark/viewapp.pdf");

PdfDocument inputDocument = PdfReader.Open(inFileName, PdfDocumentOp


enMode.Modify);
// Create the root bookmark. You can set the style and the color.
PdfPage FirstPage = inputDocument.Pages[0];
PdfOutline outline = inputDocument.Outlines.Add("Application", First
Page, true,
PdfOutlineStyle.Bold, XColors.Red);
IEnumerable<KeyValuePair<int,string>> result=null;
for(int k=0;k<inputDocument.Pages.Count;k++)
{
PdfPage page = inputDocument.Pages[k];
for (int index = 0; index < page.Contents.Elements.Count; index+
+)
{
PdfDictionary.PdfStream stream = page.Contents.Elements.GetD
ictionary(index).Stream;
string Text = new PDFParser().ExtractTextFromPDFBytes(stream
.Value);
for (int j = 0; j < Title.Count; j++)
{
if (Text.ToLower().Contains(Title[j].ToLower()))
{
PageTitles.Add(k, Title[j]);
break;
}
}
}
}
for (int j = 0; j < Title.Count; j++)
{
IEnumerable<KeyValuePair<int, string>> final = PageTitles.Where(
p => p.Value == Title[j]);
if (final.Count() == 1)
{
// Create a bookmark
outline.Outlines.Add(Title[j], inputDocument.Pages[final.Ele
mentAt(0).Key], true);
}
else
{
KeyValuePair<int,string> pair=final.ElementAt(0);
PdfOutline parentoutline = outline.Outlines.Add(Title[j], in
putDocument.Pages[final.ElementAt(0).Key], true);
for (int p = 0; p < final.Count(); p++)
{
// Create a sub bookmark
parentoutline.Outlines.Add(Title[j]+" "+(p+1), inputDocu
ment.Pages[final.ElementAt(p).Key], true);
}
}
}
// Save the document...
inputDocument.Save(Server.MapPath("~/SearchPDF/Bookmarks.pdf"));
}
catch (Exception e)
{
Response.Write("Error:" + e.Message);
//PDF_ParseException oEx = new PDF_ParseException(this, e);
//oEx.Log();
//oEx.ToPdf(this._sDirectoryException);
}
}
}

Вам также может понравиться