Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
يمكن أن يكون التلاعب بحقول النماذج داخل مستندات PDF أمرًا حاسمًا عند التعامل مع عمليات إدخال وإخراج البيانات في تطبيقات مختلفة. توفر مكتبة Aspose.PDF وظائف قوية لتصدير واستيراد حقول النماذج بتنسيق JSON. أدناه، نستعرض عدة مقتطفات من التعليمات البرمجية توضح كيفية تنفيذ هذه المهام باستخدام طرق مختلفة.
منذ الإصدار 24.7، أصبح من الممكن إضافة استيراد وتصدير البيانات من النماذج بتنسيق JSON:
تقوم هذه الطريقة بتصدير جميع حقول النماذج من مستند PDF موجود إلى ملف JSON واستيرادها إلى مستند PDF جديد.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ExportAllFieldsToJson(string inputPdfPath, string outputJsonPath, string outputPdfPath)
{
// Create ExportFieldsToJsonOptions with indentation
var options = new Aspose.Pdf.ExportFieldsToJsonOptions { WriteIndented = true };
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "TextField.pdf"))
{
// Export fields to JSON
var exportResult = document.Form.ExportToJson(outputJsonPath, options);
// Optionally, save the document with fields to a new PDF
document.Save(dataDir + "TextBox_out.pdf");
}
}
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ImportFieldsFromJsonAndInsertToPdf(string inputPdfPath, string outputJsonPath, string outputPdfPath)
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "TextField.pdf"))
{
// Create PDF document to import fields into
var newDocument = new Aspose.Pdf.Document();
// Add page
var page = newDocument.Pages.Add();
// Import fields from JSON
var importResult = newDocument.Form.ImportFromJson(outputJsonPath);
// Save PDF document
newDocument.Save(dataDir + "TextBox_out.pdf");
}
}
يستخدم هذا المقتطف ‘FileStream’ لإدارة إدخال وإخراج الملف.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ExportFieldsToJson(string inputPdfPath, string outputJsonPath, string outputPdfPath)
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Create ExportFieldsToJsonOptions with indentation
var options = new Aspose.Pdf.ExportFieldsToJsonOptions { WriteIndented = true };
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "TextField.pdf"))
{
// Create a FileStream to write the JSON output
using (var fileStream = File.Create(outputJsonPath))
{
// Export fields to JSON using the FileStream
var exportResult = document.Form.ExportToJson(fileStream, options);
}
}
}
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ImportFieldsFromJsonToCreatedPdt(string inputPdfPath, string outputJsonPath, string outputPdfPath)
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "TextField.pdf"))
{
// Create PDF document to import fields into
var newDocument = new Aspose.Pdf.Document();
// Add page
var page = newDocument.Pages.Add();
// Open the JSON file for reading
using (var fileStream = File.OpenRead(outputJsonPath))
{
// Import fields from JSON using the FileStream
var importResult = newDocument.Form.ImportFromJson(fileStream);
}
// Save PDF document
newDocument.Save(dataDir + "TextBox_out.pdf");
}
}
في بعض السيناريوهات، قد تفضل العمل مع البيانات في الذاكرة بدلاً من الملفات على القرص. تستخدم هذه الطريقة ‘MemoryStream’ للتعامل مع عمليات التصدير والاستيراد بالكامل في الذاكرة.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ExportFieldsToJsonUsingMemoryStream(string inputPdfPath, string outputPdfPath)
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Create a MemoryStream to hold the JSON data
using (var memoryStream = new MemoryStream())
{
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "TextField.pdf"))
{
// Export fields to JSON and write to the MemoryStream
var exportResult = document.Form.ExportToJson(memoryStream);
}
// Save the MemoryStream content to a file
File.WriteAllBytes(outputPdfPath, memoryStream.ToArray());
}
}
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ImportFieldsFromJsonUsingMemoryStream(string inputPdfPath, string outputPdfPath)
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "TextField.pdf"))
{
// Create PDF document to import fields into
var newDocument = new Aspose.Pdf.Document();
// Add page
var page = newDocument.Pages.Add();
// Create a MemoryStream to hold the JSON data
using (var memoryStream = new MemoryStream())
{
// Export fields from the original document to the MemoryStream
document.Form.ExportToJson(memoryStream);
// Reset the MemoryStream position to the beginning
memoryStream.Position = 0;
// Import fields from the MemoryStream into the new document
var importResult = newDocument.Form.ImportFromJson(memoryStream);
}
// Save PDF document
newDocument.Save(dataDir + "TextBox_out.pdf");
}
}
أحيانًا، قد تحتاج إلى تصدير أو استيراد حقل محدد فقط بدلاً من جميع الحقول في المستند.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ExportFieldToJson(string inputPdfPath, string outputJsonPath, string outputPdfPath)
{
// Create ExportFieldsToJsonOptions with indentation
var options = new Aspose.Pdf.ExportFieldsToJsonOptions { WriteIndented = true };
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "TextField.pdf"))
{
// Get the specific field (WidgetAnnotation) from the form
if (document.Form[1] is Aspose.Pdf.Annotations.WidgetAnnotation field)
{
// Export the field to JSON
var exportResult = field.ExportToJson(outputJsonPath, options);
}
}
}
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ImportFieldFromJson(string inputPdfPath, string outputJsonPath, string outputPdfPath)
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "TextField.pdf"))
{
// Create PDF document to import fields into
var newDocument = new Aspose.Pdf.Document();
// Add page
var page = newDocument.Pages.Add();
// Import fields from JSON
var importResult = newDocument.Form.ImportFromJson(outputJsonPath);
// Save PDF document
newDocument.Save(dataDir + "TextBox_out.pdf");
}
}
هذا المثال مشابه للمثال أعلاه ولكنه يستخدم ‘FileStream’ للتعامل مع عمليات التصدير والاستيراد لحقل محدد.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ExportFieldToJson(string inputPdfPath, string outputJsonPath, string outputPdfPath)
{
// Create ExportFieldsToJsonOptions with indentation
var options = new Aspose.Pdf.ExportFieldsToJsonOptions { WriteIndented = true };
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "TextField.pdf"))
{
// Get the specific field (WidgetAnnotation) from the form
if (document.Form[1] is Aspose.Pdf.Annotations.WidgetAnnotation field)
{
// Create a FileStream to write the JSON output
using (var fileStream = File.Create(outputJsonPath))
{
// Export the field to JSON using the FileStream
var exportResult = field.ExportToJson(fileStream, options);
}
}
}
}
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ImportFieldFromJson(string inputPdfPath, string outputJsonPath, string outputPdfPath)
{
// Create PDF document to import fields into
using (var newDocument = new Aspose.Pdf.Document())
{
// Add page
var page = newDocument.Pages.Add();
// Open the JSON file for reading
using (var fileStream = File.OpenRead(outputJsonPath))
{
// Import fields from JSON using the FileStream
var importResult = newDocument.Form.ImportFromJson(fileStream);
}
// Save PDF document
newDocument.Save(dataDir + "TextBox_out.pdf");
}
}
يوضح هذا المقتطف كيفية تصدير حقل نموذج محدد من مستند PDF إلى تنسيق JSON باستخدام ‘MemoryStream’ ثم استيراده إلى مستند PDF جديد.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ExportFieldToJsonUsingMemoryStream(string inputPdfPath, string outputPdfPath)
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Create a MemoryStream to hold the JSON data
using (var memoryStream = new MemoryStream())
{
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "TextField.pdf"))
{
// Get the specific field (WidgetAnnotation) from the form
if (document.Form[1] is Aspose.Pdf.Annotations.WidgetAnnotation field)
{
// Export the field to JSON and write to the MemoryStream
var exportResult = field.ExportToJson(memoryStream);
}
}
// Optionally, you can save the MemoryStream content to a file
File.WriteAllBytes(outputPdfPath, memoryStream.ToArray());
}
}
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ImportFieldFromJsonUsingMemoryStream(string inputPdfPath, string outputPdfPath)
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "TextField.pdf"))
{
// Create PDF document to import fields into
var newDocument = new Aspose.Pdf.Document();
// Add page
var page = newDocument.Pages.Add();
// Create a MemoryStream to hold the JSON data
using (var memoryStream = new MemoryStream())
{
// Export fields from the original document to the MemoryStream
document.Form.ExportToJson(memoryStream);
// Reset the MemoryStream position to the beginning
memoryStream.Position = 0;
// Import fields from the MemoryStream into the new document
var importResult = newDocument.Form.ImportFromJson(memoryStream);
}
// Save PDF document
newDocument.Save(dataDir + "TextBox_out.pdf");
}
}
يوضح هذا المقتطف كيفية تصدير قيمة حقل نموذج محدد من مستند PDF إلى ملف JSON باستخدام FileStream.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ExportFieldValueToJson(string inputPdfPath, string outputJsonPath, string outputPdfPath)
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "TextField.pdf"))
{
// Get the specific field from the form
if (document.Form.Fields[1] is Aspose.Pdf.Forms.Field field)
{
// Create a FileStream to write the JSON output
using (var fileStream = File.Create(outputJsonPath))
{
// Export the field value to JSON using the FileStream
field.ExportValueToJson(fileStream);
}
}
}
}
يوضح هذا المقتطف كيفية استيراد قيمة من ملف JSON إلى حقل نموذج محدد في مستند PDF باستخدام FileStream.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ImportFieldValueFromJson(string inputPdfPath, string outputJsonPath, string outputPdfPath)
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "TextField.pdf"))
{
// Get the specific field from the form
if (document.Form.Fields[1] is Aspose.Pdf.Forms.Field field)
{
// Open the JSON file for reading
using (var fileStream = File.OpenRead(outputJsonPath))
{
// Import the field value from JSON using the FileStream
field.ImportValueFromJson(fileStream);
}
}
// Save PDF document
document.Save(dataDir + "TextBox_out.pdf");
}
}
يوضح هذا المقتطف كيفية استيراد قيمة حقل آخر إلى الحقل المحدد من ملف JSON باستخدام FileStream وAspose.PDF.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ImportFieldValueFromJson(string inputPdfPath, string outputJsonPath, string outputPdfPath, string fullNameOfOtherFieldInJson)
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "TextField.pdf"))
{
// Get the specific field from the form
if (document.Form.Fields[1] is Aspose.Pdf.Forms.Field field)
{
// Open the JSON file for reading
using (var fileStream = File.OpenRead(outputJsonPath))
{
// Import the field value from JSON using the FileStream and the full name of the other field in JSON
field.ImportValueFromJson(fileStream, fullNameOfOtherFieldInJson);
}
}
// Save PDF document
document.Save(dataDir + "TextBox_out.pdf");
}
}
يوضح هذا المثال كيفية تصدير حقول النماذج من PDF إلى ملف JSON باستخدام Aspose.PDF، بما في ذلك التعامل مع حالات مختلفة لكل حقل أثناء عملية التصدير.
دعونا نفصل هذا المثال من Aspose.PDF خطوة بخطوة:
تحميل المستند. نقوم بتحميل مستند PDF يسمى “Sample.pdf” من دليل محدد.
إعداد خيارات التصدير. هنا، نقوم بإنشاء مثيل من ExportFieldsToJsonOptions مع إعدادين:
ExportPasswordValue
: يتضمن حقول كلمة المرور في التصدير.WriteIndented
: يقوم بتنسيق مخرجات JSON لتكون متداخلة لسهولة القراءة.تصدير حقول النماذج إلى JSON. نقوم بتصدير حقول النماذج من ملف PDF إلى ملف JSON يسمى “export.json” باستخدام المعلمات المحددة.
معالجة نتائج التصدير: تقوم هذه الحلقة بالتكرار عبر نتائج التصدير وتطبع حالة كل حقل:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ExportFieldsToJsonWithOptions()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Forms();
// Open PDF document
using (var document = new Aspose.Pdf.Document(Path.Combine(dataDir, "Forms", "Sample.pdf")))
{
// Create ExportFieldsToJsonOptions with specific settings
var options = new Aspose.Pdf.ExportFieldsToJsonOptions
{
ExportPasswordValue = true,
WriteIndented = true,
};
var exportResults = document.Form.ExportToJson(File.OpenWrite("export.json"), options);
foreach (var result in exportResults)
{
Console.Write($"{result.FieldFullName} ");
switch (result.FieldSerializationStatus)
{
case Aspose.Pdf.FieldSerializationStatus.Success:
Console.WriteLine("Success");
break;
case Aspose.Pdf.FieldSerializationStatus.Warning:
foreach (var messages in result.WarningMessages)
{
Console.WriteLine(messages);
}
break;
case Aspose.Pdf.FieldSerializationStatus.Error:
foreach (var messages in result.ErrorMessages)
{
Console.WriteLine(messages);
}
break;
}
}
}
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.