QUESTIONS OR COMMENTS ABOUT PRODUCTS? WRITE US AT SUPPORT@BYTESCOUT.COM OR USE THIS FORM

How to add URL link to the text area in existing PDF document using Bytescout.PDF library for .NET

This sample code demonstartes how to set URL link to rectangle text area in the existing PDF document

This example teaches how to open existing PDF and set hyperlink URL to the rectangle area in text and then save modifed PDF as new using Bytescout.PDF library for NET



Download example source code: bytescoutpdf_add_url_in_text_in_existing_pdf.zip (6 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("Input.pdf");

// get the page

Page page = document.GetPage((uint)(document.PageCount - 1));

// 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 = 350;

// 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("WithURLAddedOnPage.pdf");

// open generated PDF document in default PDF viewer installed in Windows

Process.Start("WithURLAddedOnPage.pdf");

}

}

}

Download example source code: bytescoutpdf_add_url_in_text_in_existing_pdf.zip (6 KB)

Tutorials: