Home
We need your feedback!

Bytescout.PDF for .NET


Subscribe to Our Bi-Monthly Newsletter (tips, new releases, specials):
 Subscribe in a reader
 PRODUCTS  |  DOWNLOAD  |  PURCHASE  |  SUPPORT  |  TESTIMONIALS   |  NEWS  | 

Bytescout.PDF library for .NET - how to set a link to web-site to the text rectangle in existing PDF

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

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)

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:

HOME  |  CONTACT US

Copyright © ByteScout, 2003-2010. Privacy Statement
Microsoft®, Windows®, Windows 2000®, Windows Server®, Windows Vista®, Internet Explorer®, .NET Framework®, ActiveX®, Visual Basic®, Visual C#®, ASP®, ASP.NET®, Excel®, PowerPoint®, are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. Adobe®, Flash® and Acrobat® are registered trademarks of Adobe Systems, Incorporated. Mozilla®, Firefox® and the Mozilla and Firefox Logos are registered trademarks of the Mozilla Foundation. Other product names or brandnames used herein are for identification purposes only and might be trademarks or registered trademarks of their respective companies. We disclaim any and all rights to those marks.