using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HiQPdfClient;
namespace HiQPdf_Demo
{
public partial class PdfOutlinesAndLinks : System.Web.UI.Page
{
protected void buttonCreatePdf_Click(object sender, EventArgs e)
{
string serverIP = textBoxServerIP.Text;
uint serverPort = uint.Parse(textBoxServerPort.Text);
string serverPassword = textBoxServerPassword.Text;
// create a PDF document
PdfDocument document = new PdfDocument(serverIP, serverPort);
// use server password if necessary
if (serverPassword.Length > 0)
document.ServerPassword = serverPassword;
// set a demo serial number
document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";
// display the outlines when the document is opened
document.Viewer.PageMode = PdfPageMode.Outlines;
// create the true type fonts that can be used in document
PdfFont pdfFontEmbed = document.CreateFontFromName("Times New Roman", 10, true);
PdfFont pdfFontBoldEmbed = document.CreateFontFromName("Times New Roman", 12, true);
pdfFontBoldEmbed.Bold = true;
PdfFont pdfFontBigEmbed = document.CreateFontFromName("Times New Roman", 16, true);
pdfFontBigEmbed.Bold = true;
// create page 1
PdfPage page1 = document.AddPage();
float crtXPos = 10;
float crtYPos = 20;
#region Create Table of contents
// create table of contents title
PdfText tableOfContentsTitle = new PdfText(crtXPos, crtYPos, "Table of Contents", pdfFontBigEmbed);
page1.Layout(tableOfContentsTitle);
// create a top outline for the table of contents
PdfDestination tableOfContentsDest = new PdfDestination(page1, new PointFloat(crtXPos, crtYPos));
document.CreateTopOutline("Table of Contents", tableOfContentsDest);
// advance current Y position in page
crtYPos += pdfFontBigEmbed.Size + 10;
// create Chapter 1 in table of contents
PdfText chapter1TocTitle = new PdfText(crtXPos + 30, crtYPos, "Chapter 1", pdfFontBoldEmbed);
// layout the chapter 1 title in TOC
page1.Layout(chapter1TocTitle);
// get the bounding rectangle of the chapter 1 title in TOC
RectangleFloat chapter1TocTitleRectangle = new RectangleFloat(crtXPos + 30, crtYPos, 100, pdfFontBoldEmbed.Size);
// advance current Y position in page
crtYPos += pdfFontEmbed.Size + 10;
// create Subchapter 1 in table of contents
PdfText subChapter11TocTitle = new PdfText(crtXPos + 60, crtYPos, "Subchapter 1", pdfFontEmbed);
// layout the text
page1.Layout(subChapter11TocTitle);
// get the bounding rectangle of the subchapter 1 title in TOC
RectangleFloat subChapter11TocTitleRectangle = new RectangleFloat(crtXPos + 60, crtYPos, 100, pdfFontEmbed.Size);
// advance current Y position in page
crtYPos += pdfFontEmbed.Size + 10;
// create Subchapter 2 in table of contents
PdfText subChapter21TocTitle = new PdfText(crtXPos + 60, crtYPos, "Subchapter 2", pdfFontEmbed);
// layout the text
page1.Layout(subChapter21TocTitle);
// get the bounding rectangle of the subchapter 2 title in TOC
RectangleFloat subChapter21TocTitleRectangle = new RectangleFloat(crtXPos + 60, crtYPos, 100, pdfFontEmbed.Size);
// advance current Y position in page
crtYPos += pdfFontEmbed.Size + 10;
// create Chapter 2 in table of contents
PdfText chapter2TocTitle = new PdfText(crtXPos + 30, crtYPos, "Chapter 2", pdfFontBoldEmbed);
// layout the text
page1.Layout(chapter2TocTitle);
// get the bounding rectangle of the chapter 2 title in TOC
RectangleFloat chapter2TocTitleRectangle = new RectangleFloat(crtXPos + 30, crtYPos, 100, pdfFontBoldEmbed.Size);
// advance current Y position in page
crtYPos += pdfFontEmbed.Size + 10;
// create Subchapter 1 in table of contents
PdfText subChapter12TocTitle = new PdfText(crtXPos + 60, crtYPos, "Subchapter 1", pdfFontEmbed);
// layout the text
page1.Layout(subChapter12TocTitle);
// get the bounding rectangle of the subchapter 1 title in TOC
RectangleFloat subChapter12TocTitleRectangle = new RectangleFloat(crtXPos + 60, crtYPos, 100, pdfFontEmbed.Size);
// advance current Y position in page
crtYPos += pdfFontEmbed.Size + 10;
// create Subchapter 2 in table of contents
PdfText subChapter22TocTitle = new PdfText(crtXPos + 60, crtYPos, "Subchapter 2", pdfFontEmbed);
// layout the text
page1.Layout(subChapter22TocTitle);
// get the bounding rectangle of the subchapter 2 title in TOC
RectangleFloat subChapter22TocTitleRectangle = new RectangleFloat(crtXPos + 60, crtYPos, 100, pdfFontEmbed.Size);
// advance current Y position in page
crtYPos += pdfFontEmbed.Size + 10;
// create the website link in the table of contents
PdfText visitWebSiteText = new PdfText(crtXPos + 30, crtYPos, "Visit HiQPdf Website", pdfFontEmbed);
visitWebSiteText.ForeColor = PdfColor.Navy;
// layout the text
page1.Layout(visitWebSiteText);
// get the bounding rectangle of the website link in TOC
RectangleFloat visitWebsiteRectangle = new RectangleFloat(crtXPos + 30, crtYPos, 100, pdfFontEmbed.Size);
// create the link to website in table of contents
document.CreateUriLink(page1, visitWebsiteRectangle, "http://www.hiqpdf.com");
// advance current Y position in page
crtYPos += pdfFontEmbed.Size + 10;
// create a text note at the end of TOC
PdfTextNote textNote = document.CreateTextNote(page1, new PointFloat(crtXPos + 10, crtYPos),
"The table of contents contains internal links to chapters and subchapters");
textNote.IconType = PdfTextNoteIconType.Note;
textNote.IsOpen = true;
#endregion
#region Create Chapter 1 content and the link from TOC
// create page 2
PdfPage page2 = document.AddPage();
// create the Chapter 1 title
PdfText chapter1Title = new PdfText(crtXPos, 10, "Chapter 1", pdfFontBoldEmbed);
// layout the text
page2.Layout(chapter1Title);
// create the Chapter 1 root outline
PdfDestination chapter1Destination = new PdfDestination(page2, new PointFloat(crtXPos, 10));
PdfOutline chapter1Outline = document.CreateTopOutline("Chapter 1", chapter1Destination);
chapter1Outline.TitleColor = PdfColor.Navy;
// create the PDF link from TOC to chapter 1
document.CreatePdfLink(page1, chapter1TocTitleRectangle, chapter1Destination);
#endregion
#region Create Subchapter 1 content and the link from TOC
// create the Subchapter 1
PdfText subChapter11Title = new PdfText(crtXPos, 300, "Subchapter 1 of Chapter 1", pdfFontEmbed);
// layout the text
page2.Layout(subChapter11Title);
// create subchapter 1 child outline
PdfDestination subChapter11Destination = new PdfDestination(page2, new PointFloat(crtXPos, 300));
PdfOutline subchapter11Outline = document.CreateChildOutline("Subchapter 1", subChapter11Destination, chapter1Outline);
// create the PDF link from TOC to subchapter 1
document.CreatePdfLink(page1, subChapter11TocTitleRectangle, subChapter11Destination);
#endregion
#region Create Subchapter 2 content and the link from TOC
// create the Subchapter 2
PdfText subChapter21Title = new PdfText(crtXPos, 600, "Subchapter 2 of Chapter 1", pdfFontEmbed);
// layout the text
page2.Layout(subChapter21Title);
// create subchapter 2 child outline
PdfDestination subChapter21Destination = new PdfDestination(page2, new PointFloat(crtXPos, 600));
PdfOutline subchapter21Outline = document.CreateChildOutline("Subchapter 2", subChapter21Destination, chapter1Outline);
// create the PDF link from TOC to subchapter 2
document.CreatePdfLink(page1, subChapter21TocTitleRectangle, subChapter21Destination);
#endregion
#region Create Chapter 2 content and the link from TOC
// create page 3
PdfPage page3 = document.AddPage();
// create the Chapter 2 title
PdfText chapter2Title = new PdfText(crtXPos, 10, "Chapter 2", pdfFontBoldEmbed);
// layout the text
page3.Layout(chapter2Title);
// create chapter 2 to outline
PdfDestination chapter2Destination = new PdfDestination(page3, new PointFloat(crtXPos, 10));
PdfOutline chapter2Outline = document.CreateTopOutline("Chapter 2", chapter2Destination);
chapter2Outline.TitleColor = PdfColor.Green;
// create the PDF link from TOC to chapter 2
document.CreatePdfLink(page1, chapter2TocTitleRectangle, chapter2Destination);
#endregion
#region Create Subchapter 1 content and the link from TOC
// create the Subchapter 1
PdfText subChapter12Title = new PdfText(crtXPos, 300, "Subchapter 1 of Chapter 2", pdfFontEmbed);
// layout the text
page3.Layout(subChapter12Title);
// create subchapter 1 child outline
PdfDestination subChapter12Destination = new PdfDestination(page3, new PointFloat(crtXPos, 300));
PdfOutline subchapter12Outline = document.CreateChildOutline("Subchapter 1", subChapter12Destination, chapter2Outline);
// create the PDF link from TOC to subchapter 1
document.CreatePdfLink(page1, subChapter12TocTitleRectangle, subChapter12Destination);
#endregion
#region Create Subchapter 2 content and the link from TOC
// create the Subchapter 2
PdfText subChapter22Title = new PdfText(crtXPos, 600, "Subchapter 2 of Chapter 2", pdfFontEmbed);
// layout the text
page3.Layout(subChapter22Title);
// create subchapter 2 child outline
PdfDestination subChapter22Destination = new PdfDestination(page3, new PointFloat(crtXPos, 600));
PdfOutline subchapter22Outline = document.CreateChildOutline("Subchapter 2", subChapter22Destination, chapter2Outline);
// create the PDF link from TOC to subchapter 2
document.CreatePdfLink(page1, subChapter22TocTitleRectangle, subChapter22Destination);
#endregion
try
{
// write the PDF document to a memory buffer
byte[] pdfBuffer = document.WriteToMemory();
// inform the browser about the binary data format
HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");
// let the browser know how to open the PDF document and the file name
HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=PdfOutlines.pdf; size={0}",
pdfBuffer.Length.ToString()));
// write the PDF buffer to HTTP response
HttpContext.Current.Response.BinaryWrite(pdfBuffer);
// call End() method of HTTP response to stop ASP.NET page processing
HttpContext.Current.Response.End();
}
finally
{
document.Close();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Master.SelectNode("pdfOutlines");
Master.LoadCodeSample("PdfOutlinesAndLinks");
}
}
}
}