1

I am struggling with attaching files to a PDF that I am generating at runtime.

I'm using C# ASP.net in the MVC framework. I originally created the PDF using ABCpdf from a HTML View but I then realised, I also needed to attach files to the PDF. I switched to using iText and I have managed to attach a file to the PDF using the solution at iTextSharp for PDF - how add file attachments?

The problem I have now is that I can't seem to reference these attachments as links within the PDF.

The "iText in Action" book suggests I can use annotations or document level attachments. I don't find the book easy to follow or the code easy to understand though. There also seems to be scarce help on this around in the way of articles on the internet but apologies is I have missed anything.

This is the first time I have asked a question here so my apologies if I have done this incorrectly.

Community
  • 1
  • 1
DuncanOppaz
  • 21
  • 1
  • 4
  • possible duplicate of [iTextSharp for PDF - how add file attachments?](http://stackoverflow.com/questions/3007218/itextsharp-for-pdf-how-add-file-attachments) – Bobrovsky May 22 '13 at 14:50

2 Answers2

1

I'm sorry you didn't like my book.

Did you read chapter 16? You want to embed a file as a document-level attachment like this:

PdfFileSpecification fs = PdfFileSpecification.FileEmbedded(writer, ... );
fs.AddDescription("specificname", false);
writer.AddFileAttachment(fs);

Inside the document, you want to create a link to that opens the PDF document described with the keyword "specificname". This is done through an action:

PdfTargetDictionary target = new PdfTargetDictionary(true);
target.EmbeddedFileName = "specificname";
PdfDestination dest = new PdfDestination(PdfDestination.FIT);
dest.AddFirst(new PdfNumber(1));
PdfAction action = PdfAction.GotoEmbedded(null, target, dest, true);

You can use this action for an annotation, a Chunk, etc... For instance:

Chunk chunk = new Chunk(" (see info)");
chunk.SetAction(action);

It is a common misconception to think that this will work for any attachment. However, ISO-32000-1 is very clear about the GotoE(mbedded) functionality:

12.6.4.4 Embedded Go-To Actions An embedded go-to action (PDF 1.6) is similar to a remote go-to action but allows jumping to or from a PDF file that is embedded in another PDF file (see 7.11.4, “Embedded File Streams"). Streams”).

If you meant to ask "I want to attach any file (such as a Docx, jpg,... file) to my PDF and add an action to the PDF that opens such a file upon clicking a link," then you're asking something that isn't supported in the PDF specification.

Feel free to read ISO-32000-1. If you didn't understand my book, you'll have to do an extra effort trying to read the PDF standard...

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Sorry - I didn't meant to imply I didn't like your book. I certainly do like your book and find it extremely helpful. The section on file embedding I did find complicated and hard to understand and thank you so much for taking the time to explain it so well here. I suspect I was being a bit to rushed with it all. You are completely right that I did mean any file and I didn't realise that. Thanks again. – DuncanOppaz May 25 '13 at 07:29
  • No problem. I've just started writing a third edition, or rather: 4 volumes of new documentation that will be offered for free on Leanpub: https://leanpub.com/u/itextsoftware Writing is very hard work, so all feedback that helps improving the book is welcome. – Bruno Lowagie May 25 '13 at 07:46
0

The ABCpdf Annotations example project found under the ABCpdf menu item shows how to add file attachments. These use a standard 'Pin' format link icon.

Because this icon is commonly understood I would suggest that you use that one rather than replace it with a generic link.

However if you should need to use a more generic link please contact us via email and I think we have some sample code you can use.

I work on the ABCpdf .NET software component so my replies may feature concepts based around ABCpdf. It's just what I know. :-)"

  • Thanks so much - I hadn't seen that despite looking several times at all your support documents. This is because I was searching for embed and attachments rather than annotations - now I feel an idiot. That is so helpful thanks. (Sorry I can't plus one it as I haven't yet got enough reputation to do that) – DuncanOppaz May 25 '13 at 07:33
  • Thanks again - this was really helpful. By the way on your page:http://www.websupergoo.com/helppdf9net/source/4-examples/18-annotations.htm I think there might be a slight error on the line: FileAttachmentAnnotation fileAttachMent = new FileAttachmentAnnotation(theDoc, "340 625 340 640", Server.MapPath("video.WMV") ); This means you can't see the push pin as the X coordinates are the same and so the rectangle has no width and presumably should be "340 625 360 640" instead. I hope that is helpful. – DuncanOppaz May 27 '13 at 09:51