For HTML2PDF and XML2PDF solution check our PDFDoc Scout ActiveX library instead
Add a link in the text to open external
PDF when user clicks the link
This example provides a Visual C# . NET
sample code that creates new PDF document (you can also
open existing PDF document by passing filename in Document()
constructor if you want to add text to an existing PDF
document) and adds text and a link area for the text to
open external PDF document (ExternalPDF.pdf) on page #2
when user clicks the link
Download example source code: bytescoutpdf_link_in_text_to_open_external_pdf.zip
(22 KB)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Bytescout.PDF;
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
external PDF");
// 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 external
PDF");
// Closing drawing on the page
drawing.Close();
// create a struct holding information about position
of the linked text in external PDF
DestinationInfo destInfo = new DestinationInfo();
destInfo.PageIndex = 2; // set index of the page to show in
external PDF document
destInfo.Type = DestinationType.FitPage; // set default view
mode for external PDF
// create Destination object to use in action to implement a
link
Destination linkDest = new Destination(document, destInfo, true,
true);
// create new action opening external remote document
ActionGoToRemote actionLink = new ActionGoToRemote(document,
"PDFs//ExternalPDF.pdf", linkDest, true /* in new
window or not*/);
// 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("TestLinkToExternalPDF.pdf");
// open in default PDF viewer application in Windows
Process.Start("TestLinkToExternalPDF.pdf");
}
}
}
Download example source code: bytescoutpdf_link_in_text_to_open_external_pdf.zip
(22 KB)
For HTML2PDF and XML2PDF solution check our PDFDoc Scout ActiveX library instead