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’을 사용하여 파일 입력 및 출력을 관리하여 JSON 파일로 필드를 내보냅니다.
// 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 형식으로 내보내고 이를 새 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 파일로 내보내는 방법을 보여줍니다.
// 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 문서의 특정 양식 필드로 값을 가져오는 방법을 보여줍니다.
// 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 파일에서 다른 필드의 값을 특정 필드로 가져오는 방법을 보여줍니다.
// 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");
}
}
이 예제는 Aspose.PDF를 사용하여 PDF에서 JSON 파일로 양식 필드를 내보내는 방법을 보여주며, 내보내기 과정에서 각 필드에 대한 다양한 상태를 처리하는 방법을 포함합니다.
이 Aspose.PDF 예제를 단계별로 살펴보겠습니다:
문서 로드. 지정된 디렉토리에서 “Sample.pdf"라는 PDF 문서를 로드합니다.
내보내기 옵션 설정. 여기에서 두 가지 설정이 있는 ExportFieldsToJsonOptions의 인스턴스를 생성합니다:
ExportPasswordValue
: 내보내기에 비밀번호 필드를 포함합니다.WriteIndented
: JSON 출력을 가독성을 위해 들여쓰기로 형식화합니다.JSON으로 양식 필드 내보내기. 지정된 매개변수를 사용하여 PDF 파일에서 “export.json"이라는 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.