استخراج وحفظ مرفق
الحصول على جميع المرفقات
مع Aspose.PDF، من الممكن الحصول على جميع المرفقات من مستند PDF. هذا مفيد إما عندما تريد حفظ المستندات بشكل منفصل عن PDF، أو إذا كنت بحاجة إلى إزالة المرفقات من PDF.
للحصول على جميع المرفقات من ملف PDF:
قم بالتكرار عبر مجموعة EmbeddedFiles لكائن Document . تحتوي مجموعة EmbeddedFiles على جميع المرفقات. يمثل كل عنصر في هذه المجموعة كائن FileSpecification . كل تكرار في حلقة foreach عبر مجموعة EmbeddedFiles يعيد كائن FileSpecification .
بمجرد توفر الكائن، استرجع إما جميع خصائص الملف المرفق أو الملف نفسه.
تظهر مقتطفات الكود التالية كيفية الحصول على جميع المرفقات من مستند PDF.
تعمل مقتطفات الكود التالية أيضًا مع مكتبة Aspose.PDF.Drawing .
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetAllAttachments ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_Attachments ();
// Open PDF document
using ( var document = new Aspose . Pdf . Document ( dataDir + "GetAlltheAttachments.pdf" ))
{
// Get embedded files collection
Aspose . Pdf . EmbeddedFileCollection embeddedFiles = document . EmbeddedFiles ;
// Get count of the embedded files
Console . WriteLine ( "Total files : {0}" , embeddedFiles . Count );
int count = 1 ;
// Loop through the collection to get all the attachments
foreach ( Aspose . Pdf . FileSpecification fileSpecification in embeddedFiles )
{
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
var fileContent = new byte [ fileSpecification . Contents . Length ];
fileSpecification . Contents . Read ( fileContent , 0 , fileContent . Length );
using ( var fileStream = new FileStream ( dataDir + count + "_out" + ".txt" , FileMode . Create ))
{
fileStream . Write ( fileContent , 0 , fileContent . Length );
}
count += 1 ;
}
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetAllAttachments ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_Attachments ();
// Open PDF document
using var document = new Aspose . Pdf . Document ( dataDir + "GetAlltheAttachments.pdf" );
// Get embedded files collection
Aspose . Pdf . EmbeddedFileCollection embeddedFiles = document . EmbeddedFiles ;
// Get count of the embedded files
Console . WriteLine ( "Total files : {0}" , embeddedFiles . Count );
int count = 1 ;
// Loop through the collection to get all the attachments
foreach ( Aspose . Pdf . FileSpecification fileSpecification in embeddedFiles )
{
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
var fileContent = new byte [ fileSpecification . Contents . Length ];
fileSpecification . Contents . Read ( fileContent , 0 , fileContent . Length );
using var fileStream = new FileStream ( dataDir + count + "_out" + ".txt" , FileMode . Create );
fileStream . Write ( fileContent , 0 , fileContent . Length );
count += 1 ;
}
}
الحصول على مرفق فردي
من أجل الحصول على مرفق فردي، يمكننا تحديد فهرس المرفق في كائن EmbeddedFiles
لمثيل Document. يرجى تجربة استخدام مقتطف الكود التالي.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetIndividualAttachment ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_Attachments ();
// Open PDF document
using ( var document = new Aspose . Pdf . Document ( dataDir + "GetIndividualAttachment.pdf" ))
{
// Get particular embedded file
Aspose . Pdf . FileSpecification fileSpecification = document . EmbeddedFiles [ 1 ];
// 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
var fileContent = new byte [ fileSpecification . Contents . Length ];
fileSpecification . Contents . Read ( fileContent , 0 , fileContent . Length );
using ( var fileStream = new FileStream ( dataDir + "test_out" + ".txt" , FileMode . Create ))
{
fileStream . Write ( fileContent , 0 , fileContent . Length );
}
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void GetIndividualAttachment ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_Attachments ();
// Open PDF document
using var document = new Aspose . Pdf . Document ( dataDir + "GetIndividualAttachment.pdf" );
// Get particular embedded file
Aspose . Pdf . FileSpecification fileSpecification = document . EmbeddedFiles [ 1 ];
// 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
var fileContent = new byte [ fileSpecification . Contents . Length ];
fileSpecification . Contents . Read ( fileContent , 0 , fileContent . Length );
using var fileStream = new FileStream ( dataDir + "test_out" + ".txt" , FileMode . Create );
fileStream . Write ( fileContent , 0 , fileContent . Length );
}
الحصول على المرفقات الموجودة في كائنات FileAttachmentAnnotation
بالإضافة إلى مجموعة EmbeddedFiles لكائن Document، يمكن أن تحتوي المرفقات أيضًا على كائنات FileAttachmentAnnotation. أدناه هو الكود لعرض عدد وتفاصيل مثل هذه المرفقات.