ststeiger/PdfSharpCore

Add Attachment to PDF-File (.xml File)

Opened this issue · 2 comments

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); `