-
Notifications
You must be signed in to change notification settings - Fork 1
2.8 Web Link Support
The PDF File Writer library provides support to web linking. This feature is one of the PDF interactive features described in the PDF reference manual in Section 8 Interactive Features. It is a combination of annotation and action. Annotation associates a web link with an area on the page. When the user clicks on the area, the PDF reader will activate the default web browser navigating to the desired web page.
The annotation area is a rectangle area defined by absolute coordinates relative to the bottom left corner of the page. To add a web link call AddWebLink
method of the PdfPage
class.
Page.AddWebLink(Double LeftPos, Double BottomPos, Double RightPos, Double TopPos, String WebLink);
Annotations are not part of the page contents. In order or the reader of your PDF document to know where to click you need to display appropriate text or graphics in the same area on the page. In other words you need to call two methods. The AddWebLink
method associated with the page and a second method associated with the contents. The second method can be a graphic object such as image or a rectangle, or text. Because AddWebLink
requires coordinates relative to the bottom left corner of the page, the coordinates of your graphic object must be the same. In other words, do not use translation, scaling or rotation. If you do, you need to make sure that the two areas will coincide.
The PDF File Writer has several PdfContents
methods supporting text annotation.
Draw a line of text with associated web link. The text will be left justified, underlined and blue. Text position is relative to bottom left corner of the page.
// font size in points
PdfContents.DrawWebLink(PdfPage Page, PdfFont Font, Double FontSize, Double TextAbsPosX, Double TextAbsPosY, String Text, String WebLink);
Draw a line of text with associated web link. Text position is relative to bottom left corner of the page.
// font size in points
PdfContents.DrawWebLink(PdfPage Page, PdfFont Font, Double FontSize, Double TextAbsPosX, Double TextAbsPosY, TextJustify Justify, DrawStyle DrawStyle, Color TextColor, String Text, String WebLink);
Drawing web link within TextBox
is a two step process. First you add the text and the web link string to the box using one of the AddText
methods of TextBox class. Second you draw the TextBox
to the page contents using one of the DrawText
methods of PdfContents
.
Add web link to TextBox
. The text will be displayed underlined and blue.
TextBox.AddText(PdfFont Font, Double FontSize, String Text, String WebLink);
Add web link to TextBox
. The text attributes are defined by DrawStyle
and FontColor
.
TextBox.AddText(PdfFont Font, Double FontSize, DrawStyle DrawStyle, Color FontColor, String Text, String WebLink);
Second step draw text to contents. This method assumes no extra line or paragraph spacing. Note, if you call DrawText
without PdfPage
argument on a TextBox
with WebLink
information, ApplicationException
will be thrown.
// note: PosYTop is by reference.
// On exit from the method the PosYTop will have the next Y position
PdfContents.DrawText(Double PosX, ref Double PosYTop, Double PosYBottom, Int32 LineNo, TextBox Box, PdfPage Page);
This method lets you define extra line or paragraph spacing. Note, if you call DrawText
without PdfPage
argument on a TextBox
with WebLink information, ApplicationException
will be thrown.
// note: PosYTop is by reference.
// On exit from the method the PosYTop will have the next Y position
PdfContents.DrawText(Double PosX, ref Double PosYTop, Double PosYBottom, Int32 LineNo, Double LineExtraSpace, Double ParagraphExtraSpace, Boolean FitTextToWidth, TextBox Box, PdfPage Page);
For coding examples please review 3.4 Draw Frame with Background Pattern, ArticleExample.cs and OtherExample.cs source code.
This page is a copy from https://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library by Uzi Granot. The article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL). All rights to the texts and source code remain with Uzi Granot.