Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
schorges opened this issue Apr 24, 2024 · 2 comments
Open

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

schorges opened this issue Apr 24, 2024 · 2 comments

Comments

@schorges
Copy link

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
Copy link

+1

@alex-pass
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants