HiQPdf DocumentationPDF File LinksQuickly Create High Quality PDFs

You can create links in PDF pages to external files using the PdfDocumentCreateFileLink(PdfPage, RectangleF, String) method. The link will be represented by an icon in the PDF page. The file path can be given by a full or a relative path to the PDF document location.

PDF File Links Demo

In this demo you can learn how to attach external files and streams to a PDF document. When you create the attachment you can also give a page and a location in page where to create a link to the attachment marked with an icon.

The demo also creates a link to an external file from disk. The file path is relative to the PDF document location. When the link is clicked the file will be opened in an external viewer.

Demo Source Code

private void buttonCreatePdf_Click(object sender, EventArgs e)
    // create a PDF document
    PdfDocument document = new PdfDocument();

    // display the attachments when the document is opened
    document.Viewer.PageMode = PdfPageMode.Attachments;

    // create a page in document
    PdfPage page1 = document.AddPage();

    // create the true type fonts that can be used in document
    Font sysFont = new Font("Times New Roman", 10, System.Drawing.GraphicsUnit.Point);
    PdfFont pdfFont = document.CreateFont(sysFont);
    PdfFont pdfFontEmbed = document.CreateFont(sysFont, true);

    // create a reference Graphics used for measuring
    Bitmap refBmp = new Bitmap(1, 1);
    Graphics refGraphics = Graphics.FromImage(refBmp);
    refGraphics.PageUnit = GraphicsUnit.Point;

    // create an attachment with icon from file
    string filePath1 = Application.StartupPath + @"\DemoFiles\Attach\TextAttach1.txt";
    PdfAttachment pdfAttachment1 = document.CreateAttachmentFromFile(page1, new RectangleF(10, 30, 10, 20),
                PdfAttachIconType.PushPin, filePath1);
    pdfAttachment1.Description = "Attachment with icon from a file";

    // write a description at the right of the icon
    PdfText pdfAttachment1Descr = new PdfText(40, 35, pdfAttachment1.Description, pdfFontEmbed);

    // create an attachment with icon from a stream
    // The stream must remain opened until the document is saved 
    FileStream fileStream2 = new FileStream(filePath1, FileMode.Open, FileAccess.Read);
    PdfAttachment pdfAttachment2 = document.CreateAttachmentFromStream(page1, new RectangleF(10, 60, 10, 20), PdfAttachIconType.Paperclip,
                fileStream2, "AttachFromStream_WithIcon.txt");
    pdfAttachment2.Description = "Attachment with icon from a stream";

    // write a description at the right of the icon
    PdfText pdfAttachment2Descr = new PdfText(40, 65, pdfAttachment2.Description, pdfFontEmbed);

    // create an attachment without icon in PDF from a file
    string filePath2 = Application.StartupPath + @"\DemoFiles\Attach\TextAttach2.txt";

    // create an attachment without icon in PDF from a stream
    // The stream must remain opened until the document is saved 
    FileStream fileStream1 = new FileStream(filePath2, FileMode.Open, FileAccess.Read);
    document.CreateAttachmentFromStream(fileStream1, "AttachFromStream_NoIcon.txt");

    // create the external file link
    string extFilePath = @"DemoFiles\Attach\TextAttach1.txt";
    string linkText = String.Format("Click this link to open the '{0}' file from file system", extFilePath);
    PdfText clickHereText = new PdfText(10, 100, linkText, pdfFontEmbed);
    clickHereText.ForeColor = Color.Navy;

    SizeF textSize = refGraphics.MeasureString(linkText, sysFont);

    document.CreateFileLink(page1, new RectangleF(10,100,textSize.Width, textSize.Height), extFilePath);

    // dispose the graphics used for measuring

    Cursor = Cursors.WaitCursor;
    string pdfFile = Application.StartupPath + @"\DemoOutput\PdfAttachments.pdf";
    catch (Exception ex)
        MessageBox.Show(String.Format("Cannot create the PDF document. {0}", ex.Message));
        Cursor = Cursors.Arrow;

    // open the created PDF document
    catch (Exception ex)
        MessageBox.Show(String.Format("The PDF document was created but cannot open '{0}'. {1}", pdfFile, ex.Message));
See Also

Other Resources