PdfSharpCore icon indicating copy to clipboard operation
PdfSharpCore copied to clipboard

Add Attachment to PDF-File (.xml File)

Open schorges opened this issue 1 year ago • 3 comments

Is there a way to attach a xml file to the pdf ?

Like this: https://stackoverflow.com/questions/70597318/af-reference-to-file-embedded-into-a-pdf-with-itextsharp

schorges avatar Apr 24 '24 15:04 schorges

+1

hh-it-co avatar Sep 17 '24 01:09 hh-it-co

This works for me :

`PdfSharpCore.Pdf.PdfDocument document = new PdfSharpCore.Pdf.PdfDocument(); var p = new PdfSharpCore.Pdf.PdfPage(); document.Pages.Add(p);

var embeddedFile = new PdfSharpCore.Pdf.Advanced.PdfEmbeddedFile(document); embeddedFile.CreateStream(Encoding.Default.GetBytes("Hello World!")); embeddedFile.MimeType = "text/plain";

var fs = new PdfSharpCore.Pdf.Advanced.PdfFileSpecification(document, "Hello World.txt", embeddedFile); document.Internals.AddObject(embeddedFile); document.Internals.AddObject(fs);

var arrayName = new PdfArray(document);

var dicNames = new PdfDictionary(document); dicNames.Elements.Add("/Names", new PdfArray(document, new PdfItem[] { new PdfString(fs.FileName), fs.Reference })); document.Internals.AddObject(dicNames);

var dicEmbeddedFiles = new PdfDictionary(document); dicEmbeddedFiles.Elements.Add("/EmbeddedFiles", dicNames.Reference); document.Internals.AddObject(dicEmbeddedFiles);

document.Internals.Catalog.Elements.Add("/Names", dicEmbeddedFiles.Reference);

document.Save(filename); `

alex-pass avatar Oct 09 '24 21:10 alex-pass

This is not suitable if it has to be an embedded file, but for my use-case this worked:

PdfStringEncoding unicode = PdfStringEncoding.Unicode; document.CustomValues.Elements["/INVOICEJSON"] = new PdfString(invoice.Json(), unicode);

vonj avatar Apr 09 '25 21:04 vonj