ITextSharp for PDF - how to add file attachments?

I am using iTextSharp to create a PDF in C #. I would like to add another file to the PDF. I just have problems trying to do this. The examples here show some annotations, which are apparently attachments.

Here is what I tried:

writer.AddAnnotation(its.pdf.PdfAnnotation.CreateFileAttachment(writer, new iTextSharp.text.Rectangle(100,100,100,100), "File Attachment", its.pdf.PdfFileSpecification.FileExtern(writer, "C:\\test.xml"))); 

Well, what happens is adding an annotation to the PDF (appears as a small voice voice), which I don't want. test.xml is displayed in the attachment panel in Adobe Reader, but it cannot be read or saved, and its file size is unknown, so it is unlikely to be correctly attached.

Any suggestions?

+6
c # pdf pdf-generation itext itextsharp
source share
1 answer

Well, I have code for it to work:

 its.Document PDFD = new its.Document(its.PageSize.LETTER); its.pdf.PdfWriter writer; writer = its.pdf.PdfWriter.GetInstance(PDFD, new FileStream(targetpath, FileMode.Create)); its.pdf.PdfFileSpecification pfs = its.pdf.PdfFileSpecification.FileEmbedded(writer, "C:\\test.xml", "New.xml", null); writer.AddFileAttachment(pfs); 

where "its" = "iTextSharp.text"

Now to read the attachment!

+7
source share

All Articles