Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created December 15, 2020 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/d302a910cb473ea5b0c2ba72f777b13c to your computer and use it in GitHub Desktop.
Save aspose-com-gists/d302a910cb473ea5b0c2ba72f777b13c to your computer and use it in GitHub Desktop.
Add or remove attachments in PDF using C#
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// Open document
Document pdfDocument = new Document("document.pdf");
// Setup new file to be added as attachment
FileSpecification fileSpecification = new FileSpecification("test.txt", "Sample text file");
// Add attachment to document's attachment collection
pdfDocument.EmbeddedFiles.Add(fileSpecification);
// Save new output
pdfDocument.Save("output.pdf");
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// Open document
Document pdfDocument = new Document("document.pdf");
// Get particular embedded file
foreach(FileSpecification fileSpecification in pdfDocument.EmbeddedFiles)
{
// Get the file properties
Console.WriteLine("Name: {0}", fileSpecification.Name);
Console.WriteLine("Description: {0}", fileSpecification.Description);
Console.WriteLine("Mime Type: {0}", fileSpecification.MIMEType);
// Check if parameter object contains the parameters
if (fileSpecification.Params != null)
{
Console.WriteLine("CheckSum: {0}",
fileSpecification.Params.CheckSum);
Console.WriteLine("Creation Date: {0}",
fileSpecification.Params.CreationDate);
Console.WriteLine("Modification Date: {0}",
fileSpecification.Params.ModDate);
Console.WriteLine("Size: {0}", fileSpecification.Params.Size);
}
// Get the attachment and write to file or stream
byte[] fileContent = new byte[fileSpecification.Contents.Length];
fileSpecification.Contents.Read(fileContent, 0, fileContent.Length);
FileStream fileStream = new FileStream(fileSpecification.Name, FileMode.Create);
fileStream.Write(fileContent, 0, fileContent.Length);
fileStream.Close();
}
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// Open document
Document pdfDocument = new Document("document.pdf");
// Delete all attachments
pdfDocument.EmbeddedFiles.Delete();
// Save updated file
pdfDocument.Save("output.pdf");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment