C# tutorial: PDF navigation


Add navigation pane to PDF document

Most PDF viewers allow you to navigate from one page to another page in a PDF document. However, in some cases, the built-in navigation panes are removed or hidden. Probably, you want to add an extra navigation page to every page of the PDF document to make it easy for the end user to go to the first, last, next, or previous page.

In the example PDP pages below, you can find arrows to jump to the first page (<=), the previous page (←), the next page (→), and the last page (=>).

pdf navigation


You can accomplish this task by using the PdfAction class. You will use constant values of this class to define links to the first, last, previous, or next page. These values are PdfAction.FIRSTPAGE, PdfAction.LASTPAGE, PdfAction.LASTPAGE, and PdfAction.PREVPAGE. These values need to be associated with Chunk objects by using the SetAction method.

In the example C# code below, a table of four columns is created to display the navigation links. The first cell shows the link to first page; the second cell shows the link to the previous page; the third cell shows the link to the next page; and the last cell shows the link to the last page. You can get the navigation symbols by defining a new Font object:

Font symbol = new Font(Font.FontFamily.SYMBOL,20);

Then apply this font object to each Chunk object. The table object is added at the bottom of every page of the PDF document by using PdfStamper and WriteSelelectedRows method.

//variables
String source_file="D:/jmf_tutorial.pdf";
String result = "d:/result.pdf";
//create PdfReader object to read from the source file
PdfReader reader = new PdfReader(source_file);
//create PdfStamper object to add content to the pdf file
PdfStamper stamper = new PdfStamper(reader, new FileStream(result, FileMode.Create));
//show navigation symbols in a table
Font symbol = new Font(Font.FontFamily.SYMBOL,20);
PdfPTable table = new PdfPTable(4);
table.DefaultCell.Border=Rectangle.NO_BORDER;
table.DefaultCell.HorizontalAlignment=Element.ALIGN_CENTER;
Chunk first = new Chunk(((char)220).ToString(), symbol);
first.SetAction(new PdfAction(PdfAction.FIRSTPAGE));
table.AddCell(new Phrase(first));
Chunk previous = new Chunk(((char)172).ToString(), symbol);
previous.SetAction(new PdfAction(PdfAction.PREVPAGE));
table.AddCell(new Phrase(previous));
Chunk next = new Chunk(((char)174).ToString(), symbol);
next.SetAction(new PdfAction(PdfAction.NEXTPAGE));
table.AddCell(new Phrase(next));
Chunk last = new Chunk(((char)222).ToString(), symbol);
last.SetAction(new PdfAction(PdfAction.LASTPAGE));
table.AddCell(new Phrase(last));
table.TotalWidth=120;
PdfContentByte pb;
for(int i=1;i<=reader.NumberOfPages;i++){
  //Get PdfContentByte object for every page of pdf file
  pb = stamper.GetOverContent(i);
  //add the table of navigation symbols at the bottom of every page
  table.WriteSelectedRows(0, 1,reader.GetPageSize(1).Width/2, table.TotalHeight+20,pb);
}
stamper.Close();


Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.