ststeiger / PdfSharpCore

Port of the PdfSharp library to .NET Core - largely removed GDI+ (only missing GetFontData - which can be replaced with freetype2)
Other
1.08k stars 237 forks source link

Add Attachment to PDF-File (.xml File) #438

Open schorges opened 7 months ago

schorges commented 7 months ago

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

hh-it-co commented 2 months ago

+1

alex-pass commented 1 month ago

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