PDF: Working with embedded files of PDF document
In This Topic
PDF document may include embedded files. For example, a PDF portfolio includes other PDF documents as embedded files.
PdfEmbeddedFile class represents a file, which is stored in PDF document, and allows to:
All embedded files of PDF document can be gathered using
PdfDocument.EmbeddedFiles property.
Here is an example that demonstrates how to obtain information about all embedded files of PDF document:
/// <summary>
/// Prints information about all embedded files of PDF document.
/// </summary>
/// <param name="pdfFilename">The filename of PDF document.</param>
public static void PrintEmbeddedFilesInfo(string pdfFilename)
{
// open PDF document
using (Vintasoft.Imaging.Pdf.PdfDocument document =
new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename, true))
{
// get the dictionary of embedded file specifications
Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecificationDictionary embeddedFiles = document.EmbeddedFiles;
// if dictionary is empty
if (embeddedFiles == null || embeddedFiles.Count == 0)
{
System.Console.WriteLine("No embedded files.");
}
// if dictionary is NOT empty
else
{
System.Console.WriteLine(string.Format("There are {0} embedded files.", embeddedFiles.Count));
// counter of embedded files
int counter = 1;
// for each name-specification pair
foreach (System.Collections.Generic.KeyValuePair<string, Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecification>
embeddedFileInfo in embeddedFiles)
{
// write name of the file specification
System.Console.WriteLine(string.Format("{0}) Name: \"{1}\"", counter++, embeddedFileInfo.Key));
// get the file specification
Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecification embeddedFileSpecification = embeddedFileInfo.Value;
// write description of the file specification
System.Console.WriteLine(string.Format(" Description: \"{0}\"", embeddedFileSpecification.Description));
// write file name
System.Console.WriteLine(string.Format(" Filename: \"{0}\"", embeddedFileSpecification.Filename));
// write file system
System.Console.WriteLine(string.Format(" FileSystem: \"{0}\"", embeddedFileSpecification.FileSystem));
// get the embedded file
Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFile embeddedFile = embeddedFileSpecification.EmbeddedFile;
System.Console.WriteLine(string.Format(" EmbeddedFile:"));
// write compression of the embedded file
System.Console.WriteLine(string.Format(" Compression: {0}", embeddedFile.Compression));
// write creation date of the embedded file
System.Console.WriteLine(string.Format(" CreationDate: {0}", embeddedFile.CreationDate));
// write modification date of the embedded file
System.Console.WriteLine(string.Format(" ModifyDate: {0}", embeddedFile.ModifyDate));
// write uncompressed length of the embedded file
System.Console.WriteLine(string.Format(" UncompressedLength: {0} bytes", embeddedFile.UncompressedLength));
// write compressed length of the embedded file
System.Console.WriteLine(string.Format(" CompressedLength: {0} bytes", embeddedFile.Length));
System.Console.WriteLine();
}
}
}
}
''' <summary>
''' Prints information about all embedded files of PDF document.
''' </summary>
''' <param name="pdfFilename">The filename of PDF document.</param>
Public Shared Sub PrintEmbeddedFilesInfo(pdfFilename As String)
' open PDF document
Using document As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename, True)
' get the dictionary of embedded file specifications
Dim embeddedFiles As Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecificationDictionary = document.EmbeddedFiles
' if dictionary is empty
If embeddedFiles Is Nothing OrElse embeddedFiles.Count = 0 Then
System.Console.WriteLine("No embedded files.")
Else
' if dictionary is NOT empty
System.Console.WriteLine(String.Format("There are {0} embedded files.", embeddedFiles.Count))
' counter of embedded files
Dim counter As Integer = 1
' for each name-specification pair
For Each embeddedFileInfo As System.Collections.Generic.KeyValuePair(Of String, Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecification) In embeddedFiles
' write name of the file specification
System.Console.WriteLine(String.Format("{0}) Name: ""{1}""", System.Math.Max(System.Threading.Interlocked.Increment(counter),counter - 1), embeddedFileInfo.Key))
' get the file specification
Dim embeddedFileSpecification As Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecification = embeddedFileInfo.Value
' write description of the file specification
System.Console.WriteLine(String.Format(" Description: ""{0}""", embeddedFileSpecification.Description))
' write file name
System.Console.WriteLine(String.Format(" Filename: ""{0}""", embeddedFileSpecification.Filename))
' write file system
System.Console.WriteLine(String.Format(" FileSystem: ""{0}""", embeddedFileSpecification.FileSystem))
' get the embedded file
Dim embeddedFile As Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFile = embeddedFileSpecification.EmbeddedFile
System.Console.WriteLine(String.Format(" EmbeddedFile:"))
' write compression of the embedded file
System.Console.WriteLine(String.Format(" Compression: {0}", embeddedFile.Compression))
' write creation date of the embedded file
System.Console.WriteLine(String.Format(" CreationDate: {0}", embeddedFile.CreationDate))
' write modification date of the embedded file
System.Console.WriteLine(String.Format(" ModifyDate: {0}", embeddedFile.ModifyDate))
' write uncompressed length of the embedded file
System.Console.WriteLine(String.Format(" UncompressedLength: {0} bytes", embeddedFile.UncompressedLength))
' write compressed length of the embedded file
System.Console.WriteLine(String.Format(" CompressedLength: {0} bytes", embeddedFile.Length))
System.Console.WriteLine()
Next
End If
End Using
End Sub
PdfEmbeddedFileSpecificationDictionary class represents a dictionary of embedded files, which are stored in PDF document, and allows to add or remove embedded files.
Here is an example that demonstrates how to embed file into PDF document:
/// <summary>
/// Embeds specified file into specified PDF document.
/// </summary>
/// <param name="pdfFilename">The filename of PDF document.</param>
/// <param name="embeddingFilename">The filename of embedding file.</param>
public static void EmbedFileIntoPdfDocument(string pdfFilename, string embeddingFilename)
{
// open PDF document
using (Vintasoft.Imaging.Pdf.PdfDocument document =
new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
{
// if embedded files are absent
if (document.EmbeddedFiles == null)
// create new dictionary
document.EmbeddedFiles =
new Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecificationDictionary(document);
// create embedded file instance
Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFile embeddedFile =
new Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFile(
document, embeddingFilename, Vintasoft.Imaging.Pdf.PdfCompression.Zip);
// create embedded file specification
Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecification fileSpecification =
new Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecification(embeddingFilename, embeddedFile);
// set description of the embedded file specification
fileSpecification.Description = "Test embedding";
// add the embedded file specification to the dictionary
document.EmbeddedFiles.Add(embeddingFilename, fileSpecification);
// save changes to the source
document.SaveChanges();
}
}
''' <summary>
''' Embeds specified file into specified PDF document.
''' </summary>
''' <param name="pdfFilename">The filename of PDF document.</param>
''' <param name="embeddingFilename">The filename of embedding file.</param>
Public Shared Sub EmbedFileIntoPdfDocument(pdfFilename As String, embeddingFilename As String)
' open PDF document
Using document As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename)
' if embedded files are absent
If document.EmbeddedFiles Is Nothing Then
' create new dictionary
document.EmbeddedFiles = New Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecificationDictionary(document)
End If
' create embedded file instance
Dim embeddedFile As New Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFile(document, embeddingFilename, Vintasoft.Imaging.Pdf.PdfCompression.Zip)
' create embedded file specification
Dim fileSpecification As New Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecification(embeddingFilename, embeddedFile)
' set description of the embedded file specification
fileSpecification.Description = "Test embedding"
' add the embedded file specification to the dictionary
document.EmbeddedFiles.Add(embeddingFilename, fileSpecification)
' save changes to the source
document.SaveChanges()
End Using
End Sub
Here is an example that demonstrates how to remove embedded file from PDF document:
/// <summary>
/// Removes the embedded file from PDF document by the name of embedded file specification.
/// </summary>
/// <param name="pdfFilename">The filename of PDF document.</param>
/// <param name="embeddedFileName">Name of the embedded file specification.</param>
/// <returns><b>true</b> if the file is found and successfully removed; otherwise, <b>false</b>.</returns>
public static bool RemoveEmbeddedFileFromPdfDocument(string pdfFilename, string embeddedFileName)
{
// open PDF document
using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
{
// if embedded files are absent
if (document.EmbeddedFiles == null || document.EmbeddedFiles.Count == 0)
return false;
// try to remove embedded file specification
if (!document.EmbeddedFiles.Remove(embeddedFileName))
return false;
// save changes to the source
document.SaveChanges();
return true;
}
}
''' <summary>
''' Removes the embedded file from PDF document by the name of embedded file specification.
''' </summary>
''' <param name="pdfFilename">The filename of PDF document.</param>
''' <param name="embeddedFileName">Name of the embedded file specification.</param>
''' <returns><b>true</b> if the file is found and successfully removed; otherwise, <b>false</b>.</returns>
Public Shared Function RemoveEmbeddedFileFromPdfDocument(pdfFilename As String, embeddedFileName As String) As Boolean
' open PDF document
Using document As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename)
' if embedded files are absent
If document.EmbeddedFiles Is Nothing OrElse document.EmbeddedFiles.Count = 0 Then
Return False
End If
' try to remove embedded file specification
If Not document.EmbeddedFiles.Remove(embeddedFileName) Then
Return False
End If
' save changes to the source
document.SaveChanges()
Return True
End Using
End Function