إنشاء AcroForm - إنشاء PDF قابل للتعبئة في C#
تعمل مقتطفات الشيفرة التالية أيضًا مع مكتبة Aspose.PDF.Drawing .
إنشاء نموذج من الصفر
إضافة حقل نموذج في مستند PDF
تقدم فئة Document مجموعة تُسمى Form والتي تساعدك في إدارة حقول النموذج في مستند PDF.
لإضافة حقل نموذج:
أنشئ حقل النموذج الذي تريد إضافته.
استدعِ طريقة Add لمجموعة Form .
إضافة TextBoxField
يوضح المثال أدناه كيفية إضافة TextBoxField .
.NET Core 3.1
Copy
private static void AddTextBoxFieldToPdf ( )
{
var dataDir = RunExamples . GetDataDir_AsposePdf_Forms ();
using ( var document = new Aspose . Pdf . Document ( dataDir + "TextField.pdf" ))
{
var textBoxField = new Aspose . Pdf . Forms . TextBoxField ( document . Pages [ 1 ], new Aspose . Pdf . Rectangle ( 100 , 200 , 300 , 300 ));
textBoxField . PartialName = "textbox1" ;
textBoxField . Value = "Text Box" ;
var border = new Aspose . Pdf . Annotations . Border ( textBoxField );
border . Width = 5 ;
border . Dash = new Aspose . Pdf . Annotations . Dash ( 1 , 1 );
textBoxField . Border = border ;
textBoxField . Color = Aspose . Pdf . Color . FromRgb ( System . Drawing . Color . Green );
document . Form . Add ( textBoxField , 1 );
document . Save ( dataDir + "TextBox_out.pdf" );
}
}
.NET 8
Copy
private static void AddTextBoxFieldToPdf ( )
{
var dataDir = RunExamples . GetDataDir_AsposePdf_Forms ();
using var document = new Aspose . Pdf . Document ( dataDir + "TextField.pdf" );
var textBoxField = new Aspose . Pdf . Forms . TextBoxField ( document . Pages [ 1 ], new Aspose . Pdf . Rectangle ( 100 , 200 , 300 , 300 ));
textBoxField . PartialName = "textbox1" ;
textBoxField . Value = "Text Box" ;
var border = new Aspose . Pdf . Annotations . Border ( textBoxField );
border . Width = 5 ;
border . Dash = new Aspose . Pdf . Annotations . Dash ( 1 , 1 );
textBoxField . Border = border ;
textBoxField . Color = Aspose . Pdf . Color . FromRgb ( System . Drawing . Color . Green );
document . Form . Add ( textBoxField , 1 );
document . Save ( dataDir + "TextBox_out.pdf" );
}
تظهر مقتطفات الشيفرة التالية كيفية إضافة RadioButtonField في مستند PDF.
.NET Core 3.1
Copy
private static void AddRadioButtonToPdf ( )
{
var dataDir = RunExamples . GetDataDir_AsposePdf_Forms ();
using ( var document = new Aspose . Pdf . Document ())
{
document . Pages . Add ();
var radio = new Aspose . Pdf . Forms . RadioButtonField ( document . Pages [ 1 ]);
radio . AddOption ( "Test" , new Aspose . Pdf . Rectangle ( 0 , 0 , 20 , 20 ));
radio . AddOption ( "Test1" , new Aspose . Pdf . Rectangle ( 20 , 20 , 40 , 40 ));
document . Form . Add ( radio );
document . Save ( dataDir + "RadioButton_out.pdf" );
}
}
.NET 8
Copy
private static void AddRadioButtonToPdf ( )
{
var dataDir = RunExamples . GetDataDir_AsposePdf_Forms ();
using var document = new Aspose . Pdf . Document ();
document . Pages . Add ();
var radio = new Aspose . Pdf . Forms . RadioButtonField ( document . Pages [ 1 ]);
radio . AddOption ( "Test" , new Aspose . Pdf . Rectangle ( 0 , 0 , 20 , 20 ));
radio . AddOption ( "Test1" , new Aspose . Pdf . Rectangle ( 20 , 20 , 40 , 40 ));
document . Form . Add ( radio );
document . Save ( dataDir + "RadioButton_out.pdf" );
}
يمكن إضافة TextBoxField مع بعض تعليقات الواجهة.
.NET Core 3.1
Copy
private static void AddTextBoxFieldToPdf ( )
{
string dataDir = RunExamples . GetDataDir_AsposePdf_Forms ();
using ( var document = new Aspose . Pdf . Document ())
{
var page = document . Pages . Add ();
var rects = new Aspose . Pdf . Rectangle []
{
new Aspose . Pdf . Rectangle ( 10 , 600 , 110 , 620 ),
new Aspose . Pdf . Rectangle ( 10 , 630 , 110 , 650 ),
new Aspose . Pdf . Rectangle ( 10 , 660 , 110 , 680 )
};
var defaultAppearances = new Aspose . Pdf . Annotations . DefaultAppearance []
{
new Aspose . Pdf . Annotations . DefaultAppearance ( "Arial" , 10 , System . Drawing . Color . DarkBlue ),
new Aspose . Pdf . Annotations . DefaultAppearance ( "Helvetica" , 12 , System . Drawing . Color . DarkGreen ),
new Aspose . Pdf . Annotations . DefaultAppearance ( Aspose . Pdf . Text . FontRepository . FindFont ( "TimesNewRoman" ), 14 , System . Drawing . Color . DarkMagenta )
};
var textBoxField = new Aspose . Pdf . Forms . TextBoxField ( page , rects );
short i = 0 ;
foreach ( Aspose . Pdf . Annotations . WidgetAnnotation wa in textBoxField )
{
wa . DefaultAppearance = defaultAppearances [ i ++];
}
textBoxField . Value = "Text" ;
document . Form . Add ( textBoxField );
document . Save ( dataDir + "TextBox_out.pdf" );
}
}
.NET 8
Copy
private static void AddTextBoxFieldToPdf ( )
{
string dataDir = RunExamples . GetDataDir_AsposePdf_Forms ();
using var document = new Aspose . Pdf . Document ();
var page = document . Pages . Add ();
var rects = new Aspose . Pdf . Rectangle []
{
new Aspose . Pdf . Rectangle ( 10 , 600 , 110 , 620 ),
new Aspose . Pdf . Rectangle ( 10 , 630 , 110 , 650 ),
new Aspose . Pdf . Rectangle ( 10 , 660 , 110 , 680 )
};
var defaultAppearances = new Aspose . Pdf . Annotations . DefaultAppearance []
{
new Aspose . Pdf . Annotations . DefaultAppearance ( "Arial" , 10 , System . Drawing . Color . DarkBlue ),
new Aspose . Pdf . Annotations . DefaultAppearance ( "Helvetica" , 12 , System . Drawing . Color . DarkGreen ),
new Aspose . Pdf . Annotations . DefaultAppearance ( Aspose . Pdf . Text . FontRepository . FindFont ( "TimesNewRoman" ), 14 , System . Drawing . Color . DarkMagenta )
};
var textBoxField = new Aspose . Pdf . Forms . TextBoxField ( page , rects );
short i = 0 ;
foreach ( Aspose . Pdf . Annotations . WidgetAnnotation wa in textBoxField )
{
wa . DefaultAppearance = defaultAppearances [ i ++];
}
textBoxField . Value = "Text" ;
document . Form . Add ( textBoxField );
document . Save ( dataDir + "TextBox_out.pdf" );
}
تظهر مقتطفات الشيفرة التالية الخطوات لإضافة RadioButtonField مع ثلاثة خيارات ووضعها داخل خلايا الجدول.
تظهر مقتطفات الشيفرة التالية كيفية إضافة عنوان سيتم ربطه بـ RadioButtonField:
خيار آخر لإضافة مربعات اختيار مجمعة
تظهر مقتطفات الشيفرة التالية كيفية إضافة حقول checkBox مجمعة في مستند PDF.
.NET Core 3.1
Copy
private static void AddGroupedCheckBoxFieldsToPdf ( )
{
var dataDir = RunExamples . GetDataDir_AsposePdf_Forms ();
using ( var document = new Aspose . Pdf . Document ())
{
var page = document . Pages . Add ();
var radioButtonField = new Aspose . Pdf . Forms . RadioButtonField ( page );
var opt1 = new Aspose . Pdf . Forms . RadioButtonOptionField ( page , new Aspose . Pdf . Rectangle ( 50 , 500 , 70 , 520 ));
var opt2 = new Aspose . Pdf . Forms . RadioButtonOptionField ( page , new Aspose . Pdf . Rectangle ( 100 , 500 , 120 , 520 ));
opt1 . OptionName = "Option1" ;
opt2 . OptionName = "Option2" ;
opt1 . Style = Aspose . Pdf . Forms . BoxStyle . Square ;
opt2 . Style = Aspose . Pdf . Forms . BoxStyle . Cross ;
opt1 . Border = new Aspose . Pdf . Annotations . Border ( opt1 );
opt1 . Border . Style = Aspose . Pdf . Annotations . BorderStyle . Dashed ;
opt1 . Border . Width = 1 ;
opt1 . Characteristics . Border = System . Drawing . Color . Blue ;
opt2 . Border = new Aspose . Pdf . Annotations . Border ( opt2 );
opt2 . Border . Style = Aspose . Pdf . Annotations . BorderStyle . Solid ;
opt2 . Border . Width = 1 ;
opt2 . Characteristics . Border = System . Drawing . Color . Black ;
radioButtonField . Add ( opt1 );
radioButtonField . Add ( opt2 );
document . Form . Add ( radioButtonField );
document . Save ( dataDir + "GroupedCheckboxFields_out.pdf" );
}
}
.NET 8
Copy
private static void AddGroupedCheckBoxFieldsToPdf ( )
{
var dataDir = RunExamples . GetDataDir_AsposePdf_Forms ();
using var document = new Aspose . Pdf . Document ();
var page = document . Pages . Add ();
var radioButtonField = new Aspose . Pdf . Forms . RadioButtonField ( page );
var opt1 = new Aspose . Pdf . Forms . RadioButtonOptionField ( page , new Aspose . Pdf . Rectangle ( 50 , 500 , 70 , 520 ));
var opt2 = new Aspose . Pdf . Forms . RadioButtonOptionField ( page , new Aspose . Pdf . Rectangle ( 100 , 500 , 120 , 520 ));
opt1 . OptionName = "Option1" ;
opt2 . OptionName = "Option2" ;
opt1 . Style = Aspose . Pdf . Forms . BoxStyle . Square ;
opt2 . Style = Aspose . Pdf . Forms . BoxStyle . Cross ;
opt1 . Border = new Aspose . Pdf . Annotations . Border ( opt1 );
opt1 . Border . Style = Aspose . Pdf . Annotations . BorderStyle . Dashed ;
opt1 . Border . Width = 1 ;
opt1 . Characteristics . Border = System . Drawing . Color . Blue ;
opt2 . Border = new Aspose . Pdf . Annotations . Border ( opt2 );
opt2 . Border . Style = Aspose . Pdf . Annotations . BorderStyle . Solid ;
opt2 . Border . Width = 1 ;
opt2 . Characteristics . Border = System . Drawing . Color . Black ;
radioButtonField . Add ( opt1 );
radioButtonField . Add ( opt2 );
document . Form . Add ( radioButtonField );
document . Save ( dataDir + "GroupedCheckboxFields_out.pdf" );
}
إضافة حقل ComboBox
تظهر مقتطفات الشيفرة التالية كيفية إضافة حقل ComboBox في مستند PDF.
إضافة CheckboxField
تظهر مقتطفات الشيفرة التالية كيفية إضافة CheckboxField في مستند PDF.
إضافة ListBoxField
تظهر مقتطفات الشيفرة التالية كيفية إضافة ListBoxField في مستند PDF.
استخدام SignatureField
تظهر مقتطفات الشيفرة التالية كيفية توقيع مستند PDF بواسطة SignatureField.
.NET Core 3.1
Copy private static void SignPdfBySignatureField ( )
{
var dataDir = RunExamples . GetDataDir_AsposePdf_Forms ();
using ( var document = new Aspose . Pdf . Document ( dataDir + "TextField.pdf" ))
{
var page = document . Pages . Add ();
var signatureField = new Aspose . Pdf . Forms . SignatureField ( page , new Aspose . Pdf . Rectangle ( 100 , 700 , 200 , 800 ));
document . Form . Add ( signatureField );
var pkcs = new Aspose . Pdf . Forms . PKCS7 ( "test1.pfx" , "test1" );
pkcs . Date = new DateTime ();
pkcs . ContactInfo = "Test" ;
pkcs . Location = "TestLocation" ;
pkcs . Reason = "Verify" ;
pkcs . ShowProperties = false ;
signatureField . Sign ( pkcs );
document . Save ( dataDir + "SignatureField_out.pdf" );
}
}
.NET 8
Copy private static void SignPdfBySignatureField ( )
{
var dataDir = RunExamples . GetDataDir_AsposePdf_Forms ();
using var document = new Aspose . Pdf . Document ( dataDir + "TextField.pdf" );
var page = document . Pages . Add ();
var signatureField = new Aspose . Pdf . Forms . SignatureField ( page , new Aspose . Pdf . Rectangle ( 100 , 700 , 200 , 800 ));
document . Form . Add ( signatureField );
var pkcs = new Aspose . Pdf . Forms . PKCS7 ( "test1.pfx" , "test1" );
pkcs . Date = new DateTime ();
pkcs . ContactInfo = "Test" ;
pkcs . Location = "TestLocation" ;
pkcs . Reason = "Verify" ;
pkcs . ShowProperties = false ;
signatureField . Sign ( pkcs );
document . Save ( dataDir + "SignatureField_out.pdf" );
}
إضافة تلميح إلى حقل النموذج
تقدم فئة Document مجموعة تُسمى Form والتي تدير حقول النموذج في مستند PDF. لإضافة تلميح إلى حقل النموذج، استخدم خاصية AlternateName في فئة Field. يستخدم Adobe Acrobat “الاسم البديل” كتلميح حقل.
تظهر مقتطفات الشيفرة التالية كيفية إضافة تلميح إلى حقل النموذج، أولاً باستخدام C# ثم Visual Basic.