// =========================================================================== public void Write(Stream stream) { // step 1 using (Document document = new Document()) { // step 2 PdfWriter writer = PdfWriter.GetInstance(document, stream); // step 3 document.Open(); // step 4 TextField date = new TextField( writer, new Rectangle(36, 806, 126, 780), "date" ); date.BorderColor = new GrayColor(0.2f); PdfFormField datefield = date.GetTextField(); // enter something that resembles a date for this to work; // if PDF reader doesn't recognize as a date, the field is cleared datefield.SetAdditionalActions( PdfName.V, PdfAction.JavaScript("AFDate_FormatEx( 'dd-mm-yyyy' );", writer) ); writer.AddAnnotation(datefield); TextField name = new TextField( writer, new Rectangle(130, 806, 256, 780), "name" ); name.BorderColor = new GrayColor(0.2f); PdfFormField namefield = name.GetTextField(); namefield.SetAdditionalActions( PdfName.FO, PdfAction.JavaScript( "app.alert('name field got the focus');", writer ) ); namefield.SetAdditionalActions( PdfName.BL, PdfAction.JavaScript("app.alert('name lost the focus');", writer) ); namefield.SetAdditionalActions( PdfName.K, PdfAction.JavaScript( "event.change = event.change.toUpperCase();", writer ) ); writer.AddAnnotation(namefield); } }
// --------------------------------------------------------------------------- /** * Create a pushbutton for a key * @param writer the PdfWriter * @param rect the position of the key * @param btn the label for the key * @param script the script to be executed when the button is pushed */ public void AddPushButton(PdfWriter writer, Rectangle rect, String btn, String script) { float w = rect.Width; float h = rect.Height; PdfFormField pushbutton = PdfFormField.CreatePushButton(writer); pushbutton.FieldName = "btn_" + btn; pushbutton.SetWidget(rect, PdfAnnotation.HIGHLIGHT_PUSH); PdfContentByte cb = writer.DirectContent; pushbutton.SetAppearance( PdfAnnotation.APPEARANCE_NORMAL, CreateAppearance(cb, btn, BaseColor.GRAY, w, h) ); pushbutton.SetAppearance( PdfAnnotation.APPEARANCE_ROLLOVER, CreateAppearance(cb, btn, BaseColor.RED, w, h) ); pushbutton.SetAppearance( PdfAnnotation.APPEARANCE_DOWN, CreateAppearance(cb, btn, BaseColor.BLUE, w, h) ); pushbutton.SetAdditionalActions( PdfName.U, PdfAction.JavaScript(script, writer) ); pushbutton.SetAdditionalActions( PdfName.E, PdfAction.JavaScript( "this.showMove('" + btn + "');", writer ) ); pushbutton.SetAdditionalActions( PdfName.X, PdfAction.JavaScript( "this.showMove(' ');", writer ) ); writer.AddAnnotation(pushbutton); }
// --------------------------------------------------------------------------- /** * Show keys and values passed to the query string with GET */ protected void DoGet(byte[] pdf, Stream stream) { // We get a resource from our web app PdfReader reader = new PdfReader(pdf); // Now we create the PDF using (PdfStamper stamper = new PdfStamper(reader, stream)) { // We add a submit button to the existing form PushbuttonField button = new PushbuttonField( stamper.Writer, new Rectangle(90, 660, 140, 690), "submit" ); button.Text = "POST"; button.BackgroundColor = new GrayColor(0.7f); button.Visibility = PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT; PdfFormField submit = button.Field; submit.Action = PdfAction.CreateSubmitForm( WebContext.Request.RawUrl, null, 0 ); stamper.AddAnnotation(submit, 1); // We add an extra field that can be used to upload a file TextField file = new TextField( stamper.Writer, new Rectangle(160, 660, 470, 690), "image" ); file.Options = TextField.FILE_SELECTION; file.BackgroundColor = new GrayColor(0.9f); PdfFormField upload = file.GetTextField(); upload.SetAdditionalActions(PdfName.U, PdfAction.JavaScript( "this.getField('image').browseForFileToSubmit();" + "this.getField('submit').setFocus();", stamper.Writer ) ); stamper.AddAnnotation(upload, 1); } }