Home

Bytescout.PDF for .NET



Bytescout.PDF library for .NET - how to create a hyperlink URL to web-site in PDF

Download Free Trial Buy Now More Information
For HTML2PDF and XML2PDF solution check our PDFDoc Scout ActiveX library instead

How to set URL to text area to create a link to open web-site when user clicks the link

This example teaches how to add text and mark this text as hyperlink to www.live.com web-site in PDF document generated by Bytescout.PDF library

Download example source code: bytescoutpdf_create_url_link_in_pdf.zip (5 KB)

using System;
using System.Collections.Generic;
using System.Text;
using Bytescout.PDF;
using System.Diagnostics;

namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
// Create main PDF Doc Engine
PDFDocEngine engine = new PDFDocEngine("", "");

// Add new document
Document document = engine.AddDocument();

// Append new page to the document
Page page = document.AddPage(PageSizeType.A3, PageOrientationType.LandScape);

// Create new drawing
Drawing drawing = page.AddDrawing();

// Add standard font
uint font = document.AddFontStandard(StandardFontType.Courier, FontEncodingType.WinAnsi);

// font size constant
const int fontSize = 50;

// Set Active Font
drawing.SetActiveFont(font, fontSize, false, false);

// get text width
double textWidth = drawing.GetTextWidth("Click me to open www.live.com");

// X position of the text to draw
const int textX = 100;
// Y position of the text to draw
const int textY = 150;

// Draw Text
drawing.PlaceText(textX, textY, 0, "Click me to open www.live.com");

// Closing drawing on the page
drawing.Close();


// create new action opening URL to live.com
ActionURL actionLink = new ActionURL(document, "http://www.live.com/", false);
// comments: use slashes "/" as a separator.
// to open external PDF document located in the root directory ( for example "c:" drive root folder) just add slash "/" as it means "root folder"
// to open external PDF in up-level folder use path like "../ExternalPDF.pdf"

// create a rectangle holding information about link position
Rectangle rectLink = new Rectangle();
rectLink.Left = textX;
rectLink.Right = textX + textWidth;
// IMPORTANT: Y coordinate scale for annotations, links, actions etc is INVERTED (due to PDF format nature) and calculating as 0 point at left-bottom and maximum at left-top
rectLink.Top = drawing.Height - textY; // so we calculate RectLink.top = height - offset from top
rectLink.Bottom = rectLink.Top-fontSize; // so we calculate rectLink.bottom as inverted

// set new link to the given rectanlge assigning an action opening external PDF
AnnotationLink Link = new AnnotationLink(page, rectLink, actionLink, HighlightingModeType.Invert, false /* set to true to show rectangle for the link in the document*/);

// Save document
document.Save("TestLinkToURL.pdf");

// open generated PDF document in default PDF viewer installed in Windows
Process.Start("TestLinkToURL.pdf");
}
}
}

Download example source code: bytescoutpdf_create_url_link_in_pdf.zip (5 KB)

Download Free Trial Buy Now More Information
For HTML2PDF and XML2PDF solution check our PDFDoc Scout ActiveX library instead

"Hello, World!" quick start step-by-step tutorials:

Advanced Examples:
Products  |  Download  |  Purchase  |  Support  |  Contacts  |  Testimonials |  

ByteScout, 2003-2008. All other company and product names may be trademarks of their respective companies. Privacy Statement