PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Apr 27, 2024 12:21 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Thu Oct 12, 2023 1:58 pm 
Offline

Joined: Mon Oct 05, 2020 9:28 am
Posts: 16
Hi,

I've tried to add a link (PdfLinkAnnotation) to an embedded document but with no success.

The 'hello attachment!' rectangle is drawn on the page, but it is not clickable, and there is no comment (annotation) found when opening with Adobe Reader.

Below is a sample code. I use new PdfSharp 6.

Code:
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Drawing.Layout;
using PdfSharp.Fonts;
using PdfSharp.Pdf;
using PdfSharp.Pdf.Annotations;
using PdfSharp.Snippets.Font;

namespace PdfSharp6Attachments
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a new PDF document
            PdfDocument document = new PdfDocument();
            document.Info.Title = "Created with PDFsharp";

            // Create an empty page
            PdfPage page = document.AddPage();

            // Get an XGraphics object for drawing
            XGraphics gfx = XGraphics.FromPdfPage(page);

            // Create a font
            if (Capabilities.Build.IsCoreBuild)
                GlobalFontSettings.FontResolver = new FailsafeFontResolver();

            // Create a font.
            var font = new XFont("Arial", 20, XFontStyleEx.BoldItalic);

            // Draw the text
            gfx.DrawString("Hello, World!", font, XBrushes.Black,
              new XRect(0, 0, page.Width, page.Height),
              XStringFormats.Center);

            #region attach files
            AttachFile(document, "C:\\Users\\userxxx\\Downloads\\signed\\2729862.pdf", gfx, font);
            #endregion

            // Save the document...
            const string filename = "HelloWorld.pdf";
            document.Save(filename);
            // ...and start a viewer.
            //Process.Start(filename);
        }

        static void AttachFile(PdfDocument pdfDocument, string path, XGraphics gfx, XFont font)
        {
            string fileName = Path.GetFileName(path);

            pdfDocument.AddEmbeddedFile(fileName, File.OpenRead(path));

            // draw rectangle
            var annotationSystemRectangle = gfx.Transformer.WorldToDefaultPage(new XRect(new XPoint(30, 60), new XSize(300, 30)));
            XTextFormatter txtFormat = new XTextFormatter(gfx);
            gfx.DrawRectangle(XBrushes.White, annotationSystemRectangle);
            annotationSystemRectangle.X += 14;
            annotationSystemRectangle.Width -= 10;
            txtFormat.DrawString("hello attachment!",
                font,
                new XSolidBrush(XColor.FromKnownColor(XKnownColor.Black)),
                annotationSystemRectangle,
                XStringFormats.TopLeft);

            var page = pdfDocument.Pages[0];
            PdfLinkAnnotation annot = page.AddEmbeddedDocumentLink(new PdfRectangle(annotationSystemRectangle), fileName);
        }
    }
}


Could you provide a working example?
I found nothing on internet.

EDIT:
There actually is an area that seems clickable (cursor is differenton here), but when clicked or double-clicked, nothing happens.


Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 12, 2023 3:44 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 916
Location: CCAA
AIUI you have to call "WorldToDefaultPage" only for the rectangle you pass into "AddEmbeddedDocumentLink", but not for the rectangles you use with the XGraphics object.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 12, 2023 4:05 pm 
Offline

Joined: Mon Oct 05, 2020 9:28 am
Posts: 16
TH-Soft wrote:
AIUI you have to call "WorldToDefaultPage" only for the rectangle you pass into "AddEmbeddedDocumentLink", but not for the rectangles you use with the XGraphics object.

Yes indeed, but this is just a positioning issue.

The actual issue is that when I click on the clickable area, nothing happens. I think the /GoToE action is not properly built. Or I did not understand how to reference the attached file with 'fileName' in the line:
Code:
PdfLinkAnnotation annot = page.AddEmbeddedDocumentLink(new PdfRectangle(rectangleBottomBased), fileName);



Corrected code for the positioning:
Code:
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Drawing.Layout;
using PdfSharp.Fonts;
using PdfSharp.Pdf;
using PdfSharp.Pdf.Annotations;
using PdfSharp.Snippets.Font;

namespace PdfSharp6Attachments
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a new PDF document
            PdfDocument document = new PdfDocument();
            document.Info.Title = "Created with PDFsharp";

            // Create an empty page
            PdfPage page = document.AddPage();

            // Get an XGraphics object for drawing
            XGraphics gfx = XGraphics.FromPdfPage(page);

            // Create a font
            if (Capabilities.Build.IsCoreBuild)
                GlobalFontSettings.FontResolver = new FailsafeFontResolver();

            // Create a font.
            var font = new XFont("Arial", 20, XFontStyleEx.BoldItalic);

            // Draw the text
            gfx.DrawString("Hello, World!", font, XBrushes.Black,
              new XRect(0, 0, page.Width, page.Height),
              XStringFormats.Center);

            #region attach files
            AttachFile(document, @"C:\Users\userxxx\Downloads\avion2.jpg", gfx, font);
            #endregion

            // Save the document...
            const string filename = "HelloWorld.pdf";
            document.Save(filename);
            // ...and start a viewer.
            //Process.Start(filename);
        }

        static void AttachFile(PdfDocument pdfDocument, string path, XGraphics gfx, XFont font)
        {
            var page = pdfDocument.Pages[0];

            string fileName = Path.GetFileName(path);

            pdfDocument.AddEmbeddedFile(fileName, File.OpenRead(path));

            // draw rectangle           
            var rectangleTopBased = new XRect(30, 60, 300, 300);
            XTextFormatter txtFormat = new XTextFormatter(gfx);
            gfx.DrawRectangle(XBrushes.White, rectangleTopBased);
            txtFormat.DrawString("hello attachment!",
                font,
                new XSolidBrush(XColor.FromKnownColor(XKnownColor.Black)),
                rectangleTopBased,
                XStringFormats.TopLeft);

            var rectangleBottomBased = gfx.Transformer.WorldToDefaultPage(new XRect(new XPoint(30, 60), new XSize(300, 300)));
            PdfLinkAnnotation annot = page.AddEmbeddedDocumentLink(new PdfRectangle(rectangleBottomBased), fileName);
            //page.AddWebLink(new PdfRectangle(annotationSystemRectangle), "https://www.developpez.com/");

        }
    }
}


Anybody having a working example of a link to an attached file (PdfSharp6)?


Top
 Profile  
Reply with quote  
PostPosted: Fri Oct 13, 2023 3:43 pm 
Offline

Joined: Mon Oct 05, 2020 9:28 am
Posts: 16
OK I was confused about some concepts.

What I needed to do is to add a FileAttachment annotation that is clickable and that opens an attachment file.

That is done in this pull request: https://github.com/empira/PDFsharp/pull/38


Top
 Profile  
Reply with quote  
PostPosted: Fri Oct 13, 2023 10:38 pm 
Offline

Joined: Fri Oct 13, 2023 10:19 pm
Posts: 7
This is how you can use PdfSharp library to create a clickable link in a PDF document.
Ex..
Code:
using System;
using System.IO;
using PdfSharp.Pdf;
using PdfSharp.Pdf.Annotations;
using PdfSharp.Pdf.IO;

namespace PdfSharpLinkExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string filename = "Output.pdf";

            // Create a new PDF document
            PdfDocument document = new PdfDocument();

            // Add a page to the document
            PdfPage page = document.AddPage();
            var gfx = PdfSharp.Drawing.XGraphics.FromPdfPage(page);
           
            // Create a clickable link on the page
            PdfRectangle rect = new PdfRectangle(gfx.Transformer.WorldToDefaultPage(new PdfSharp.Drawing.XRect(30, 60, 300, 30)));
            PdfLinkAnnotation link = new PdfLinkAnnotation(rect)
            {
                Contents = "Click here for the embedded document!",
                Destination = new PdfExplicitDestination(page)
            };
            page.Annotations.Add(link);

            // Save the document
            document.Save(filename);
        }
    }
}


I hope it helps..


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 417 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group