В этом разделе
Предоставляет информацию о встроенном файловом PDF ресурсе документа.
Объектная модель
Синтаксис
Пример
Вот пример, показывающий, как добавить встроенный файл в PDF документ:
''' <summary>
''' Adds the large file attachment to PDF document.
''' </summary>
''' <param name="pdfFilename">The PDF filename.</param>
''' <param name="attachmentFilename">The attachment filename.</param>
Public Shared Sub AddLargeAttachment(pdfFilename As String, attachmentFilename As String)
' open PDF document
Using document As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename)
If document.EmbeddedFiles Is Nothing Then
document.EmbeddedFiles = New Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecificationDictionary(document)
End If
' open attachment file
Using attachmentStream As System.IO.Stream = System.IO.File.OpenRead(attachmentFilename)
' set ZIP compression level to 2 (fast)
Dim compressionSettings As New Vintasoft.Imaging.Pdf.PdfCompressionSettings()
compressionSettings.ZipCompressionLevel = 2
' create PDF embedded file
Dim embeddedFile As New Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFile(document, attachmentStream, False, Vintasoft.Imaging.Pdf.PdfCompression.Zip, compressionSettings)
' create PDF embedded file specification
Dim fileSpecification As New Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecification(System.IO.Path.GetFileName(attachmentFilename), embeddedFile)
' add PDF embedded file specification to PDF document
document.EmbeddedFiles.Add(fileSpecification)
' save changes in PDF document (file attachment will be encoded during saving of PDF document)
document.SaveChanges()
End Using
End Using
End Sub
''' <summary>
''' Adds the file attachment to PDF document.
''' </summary>
''' <param name="pdfFilename">The PDF filename.</param>
''' <param name="attachmentFilename">The attachment filename.</param>
Public Shared Sub AddAttachment(pdfFilename As String, attachmentFilename As String)
' open PDF document
Using document As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename)
If document.EmbeddedFiles Is Nothing Then
document.EmbeddedFiles = New Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecificationDictionary(document)
End If
' create PDF embedded file (file attachment will be encoded in constructor of PdfEmbeddedFile class)
Dim embeddedFile As New Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFile(document, attachmentFilename, Vintasoft.Imaging.Pdf.PdfCompression.Zip)
' create PDF embedded file specification
Dim fileSpecification As New Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecification(System.IO.Path.GetFileName(attachmentFilename), embeddedFile)
' add PDF embedded file specification to PDF document
document.EmbeddedFiles.Add(fileSpecification)
' save PDF document
document.SaveChanges()
End Using
End Sub
''' <summary>
''' Extracts the file attachments of PDF document in specified folder.
''' </summary>
''' <param name="pdfFilename">The PDF filename.</param>
''' <param name="attachmentOutputDir">The attachment output directory.</param>
Public Shared Sub ExtractFileAttachments(pdfFilename As String, attachmentOutputDir As String)
' open PDF document
Using document As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename)
' if PDF document has embedded files
If document.EmbeddedFiles IsNot Nothing Then
' for each file embedded in PDF document
For Each fileSpecification As Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecification In document.EmbeddedFiles.Values
If fileSpecification.EmbeddedFile IsNot Nothing Then
' save embedded file resource to a file in output directory
Dim filename As String = System.IO.Path.GetFileName(fileSpecification.Filename)
fileSpecification.EmbeddedFile.Save(System.IO.Path.Combine(attachmentOutputDir, filename))
End If
Next
End If
End Using
End Sub
/// <summary>
/// Adds the large file attachment to PDF document.
/// </summary>
/// <param name="pdfFilename">The PDF filename.</param>
/// <param name="attachmentFilename">The attachment filename.</param>
public static void AddLargeAttachment(string pdfFilename, string attachmentFilename)
{
// open PDF document
using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
{
if (document.EmbeddedFiles == null)
document.EmbeddedFiles = new Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecificationDictionary(document);
// open attachment file
using (System.IO.Stream attachmentStream = System.IO.File.OpenRead(attachmentFilename))
{
// set ZIP compression level to 2 (fast)
Vintasoft.Imaging.Pdf.PdfCompressionSettings compressionSettings = new Vintasoft.Imaging.Pdf.PdfCompressionSettings();
compressionSettings.ZipCompressionLevel = 2;
// create PDF embedded file
Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFile embeddedFile = new Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFile(
document, attachmentStream, false, Vintasoft.Imaging.Pdf.PdfCompression.Zip, compressionSettings);
// create PDF embedded file specification
Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecification fileSpecification =
new Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecification(System.IO.Path.GetFileName(attachmentFilename), embeddedFile);
// add PDF embedded file specification to PDF document
document.EmbeddedFiles.Add(fileSpecification);
// save changes in PDF document (file attachment will be encoded during saving of PDF document)
document.SaveChanges();
}
}
}
/// <summary>
/// Adds the file attachment to PDF document.
/// </summary>
/// <param name="pdfFilename">The PDF filename.</param>
/// <param name="attachmentFilename">The attachment filename.</param>
public static void AddAttachment(string pdfFilename, string attachmentFilename)
{
// open PDF document
using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
{
if (document.EmbeddedFiles == null)
document.EmbeddedFiles = new Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecificationDictionary(document);
// create PDF embedded file (file attachment will be encoded in constructor of PdfEmbeddedFile class)
Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFile embeddedFile = new Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFile(
document, attachmentFilename, Vintasoft.Imaging.Pdf.PdfCompression.Zip);
// create PDF embedded file specification
Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecification fileSpecification =
new Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecification(System.IO.Path.GetFileName(attachmentFilename), embeddedFile);
// add PDF embedded file specification to PDF document
document.EmbeddedFiles.Add(fileSpecification);
// save PDF document
document.SaveChanges();
}
}
/// <summary>
/// Extracts the file attachments of PDF document in specified folder.
/// </summary>
/// <param name="pdfFilename">The PDF filename.</param>
/// <param name="attachmentOutputDir">The attachment output directory.</param>
public static void ExtractFileAttachments(string pdfFilename, string attachmentOutputDir)
{
// open PDF document
using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
{
// if PDF document has embedded files
if (document.EmbeddedFiles != null)
{
// for each file embedded in PDF document
foreach (Vintasoft.Imaging.Pdf.Tree.PdfEmbeddedFileSpecification fileSpecification in document.EmbeddedFiles.Values)
{
if (fileSpecification.EmbeddedFile != null)
{
// save embedded file resource to a file in output directory
string filename = System.IO.Path.GetFileName(fileSpecification.Filename);
fileSpecification.EmbeddedFile.Save(System.IO.Path.Combine(attachmentOutputDir, filename));
}
}
}
}
}
Иерархия наследования
Требования
Целевые платформы: .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5
Смотрите также