import java.awt.Color; import java.io.IOException; import com.gnostice.pdfone.PDFOne; import com.gnostice.pdfone.PdfAction; import com.gnostice.pdfone.PdfAppearanceStream; import com.gnostice.pdfone.PdfDocument; import com.gnostice.pdfone.PdfException; import com.gnostice.pdfone.PdfFormPushButton; import com.gnostice.pdfone.PdfImage; import com.gnostice.pdfone.PdfMeasurement; import com.gnostice.pdfone.PdfPage; import com.gnostice.pdfone.PdfPageSize; import com.gnostice.pdfone.PdfRect; import com.gnostice.pdfone.PdfTextFormatter; import com.gnostice.pdfone.PdfWriter; import com.gnostice.pdfone.PdfAction.PdfEvent; import com.gnostice.pdfone.encodings.PdfEncodings; import com.gnostice.pdfone.fonts.PdfFont; import com.gnostice.pdfone.graphics.PdfBrush; import com.gnostice.pdfone.graphics.PdfPen; public class FormFieldsWithAppearanceStream { static { PDFOne.activate("T95VZE:W8HBPVA:74VQ8QV:LO4V8", "9B1HRZAP:X5853ERNE:5EREMEGRQ:TX1R10"); } public static void main(String[] args) throws IOException, PdfException { formFieldSimpleAppearanceDemo(); formFieldCustomAppearanceShapeDemo(); formFieldCustomAppearanceImageDemo(); } private static void formFieldSimpleAppearanceDemo() throws IOException, PdfException { // Create a PdfWriter instance for the PDF file PdfWriter w = PdfWriter.fileWriter("FormfieldSimpleAppearanceDemo.pdf"); // Create a PdfDocument instance with the PdfWriter PdfDocument d = new PdfDocument(w); /* * Create a font and set its color */ PdfFont font = PdfFont.create("Helvetica", 10, PdfEncodings.WINANSI); font.setColor(Color.BLACK); // Create a new PdfPage PdfPage p; p = new PdfPage(PdfPageSize.A4); p.setMeasurementUnit(PdfMeasurement.MU_INCHES); // X-Y coordinates of the PdfFormPushButton int xPos = 1; // One inch int yPos = 1; // One inch // Dimensions of the PdfFormPushButton double buttonWidth = 1.5; // One inch double buttonHeight = 0.5; // Half inch /* * Create an object for PdfFormPushButton * with above specified coordinates */ PdfFormPushButton jsButton = new PdfFormPushButton(new PdfRect( xPos, yPos, buttonWidth, buttonHeight)); // Set JavaScript action to the PdfFormPushButton jsButton.addAction(PdfAction.JAVASCRIPT, PdfEvent.ON_MOUSE_DOWN, "app.alert('Gnostice PDFOne on Mouse Down')"); // Set properties for the PdfFormPushButton jsButton.setBackgroundColor(Color.LIGHT_GRAY); jsButton.setBorderColor(Color.DARK_GRAY); jsButton.setName("button1"); jsButton.setNormalCaption("JavaScript-NormalAP"); jsButton.setRolloverCaption("JavaScript-RolloverAP"); jsButton.setDownCaption("Down"); jsButton.setFont(font); //X-Y coordinates for the second PdfFormPushButton xPos = 1; // One inch yPos = 2; // One inch /* * Create the second PdfFormPushButton object * with above specified coordinates */ PdfFormPushButton submitButton = new PdfFormPushButton(new PdfRect( xPos, yPos, buttonWidth, buttonHeight)); // Set Submit action to the PdfFormPushButton submitButton.addAction(PdfAction.URI, PdfEvent.ON_MOUSE_DOWN, "http://www.gnostice.com"); // Set properties for the PdfFormPushButton submitButton.setBackgroundColor(Color.LIGHT_GRAY); submitButton.setBorderColor(Color.DARK_GRAY); submitButton.setName("button2"); submitButton.setNormalCaption("Submit-NormalAP"); submitButton.setRolloverCaption("Submit-RolloverAP"); submitButton.setDownCaption("Down"); submitButton.setFont(font); // Add the jsButton to the page p.addFormField(jsButton); //Add the submitButton to the page p.addFormField(submitButton); // Add page to document d.add(p); d.setOpenAfterSave(true); d.write(); w.dispose(); } private static void formFieldCustomAppearanceShapeDemo() throws IOException, PdfException { //Create a PdfWriter instance for the PDF file PdfWriter w = PdfWriter.fileWriter( "FormfieldCustomAppearanceShapeDemo.pdf"); // Create a PdfDocument instance with the PdfWriter PdfDocument d = new PdfDocument(w); // Create a font and set its color PdfFont font = PdfFont.create("Helvetica", 10, PdfEncodings.WINANSI); font.setColor(Color.BLACK); // Create a new PdfPage PdfPage p; p = new PdfPage(PdfPageSize.A4); p.setMeasurementUnit(PdfMeasurement.MU_INCHES); // X-Y coordinates of the first PdfFormPushButton control int xPos = 1; // One inch int yPos = 1; // One inch // Dimensions of the PdfFormPushButton in Inches double buttonWidth = 1.5; // One inch double buttonHeight = 0.5; // Half inch // Dimensions of the PdfFormPushButton in Points double buttonWidth_Points = PdfMeasurement.convertToPdfUnit( PdfMeasurement.MU_INCHES, 1.5); double buttonHeight_Points = PdfMeasurement.convertToPdfUnit( PdfMeasurement.MU_INCHES, 0.5); /* * Create an instance of PdfPen which is used * to draw the shape inside the AppearanceStream */ PdfPen pen = new PdfPen(); // Change the pen settings pen.dashPhase = 3 ; pen.dashLength = 4; pen.dashGap = 2; pen.strokeColor = Color.GREEN; pen.width = 4; /* * Create an instance of PdfBrush which is used * to fill the color inside the shape */ PdfBrush brush = new PdfBrush(); //change the brush color brush.fillColor = Color.YELLOW; /* * Create a font for Normal AppearanceStream * and set its color */ PdfFont normal_ap_font = PdfFont.create("Arial", PdfFont.BOLD | PdfFont.ITALIC, 8, PdfEncodings.WINANSI); normal_ap_font.setColor(Color.RED); /* * Create a font for Rollover AppearanceStream * and set its color */ PdfFont rollover_ap_font = PdfFont.create("Helvetica", PdfFont.BOLD | PdfFont.ITALIC, 9, PdfEncodings.WINANSI); rollover_ap_font.setColor(Color.RED); /* * Create a rectangle of the same size as * the PushButton which will be drawn on * all AppearanceStreams */ PdfRect ap_rect = new PdfRect(0, 0, buttonWidth_Points, buttonHeight_Points); /* * Create a TextFormatter to format the text to be written * inside the shape of the AppearanceStream */ PdfTextFormatter tf = new PdfTextFormatter(); tf.setAlignment(PdfTextFormatter.CENTER); /* * Create an instance of PdfAppearanceStream for the * Normal Appearance of the first PdfFormPushButton */ PdfAppearanceStream js_normal_AP = new PdfAppearanceStream(ap_rect); // Draw the rectangle on the AppearanceStream js_normal_AP.drawRect(ap_rect, pen, brush); // Write the name of the Control inside the rectangle js_normal_AP.writeText("JavaScript-NormalAP", normal_ap_font, new PdfRect(0, 13, buttonWidth_Points, buttonHeight_Points), tf, PdfMeasurement.MU_POINTS); /* * Create an instance of PdfAppearanceStream for the * Normal Appearance of the second PdfFormPushButton */ PdfAppearanceStream submit_normal_AP = new PdfAppearanceStream(ap_rect); // Draw the rectangle on the AppearanceStream submit_normal_AP.drawRect(ap_rect, pen, brush); // Write the name of the Control inside the rectangle submit_normal_AP.writeText("Submit-NormalAP", normal_ap_font, new PdfRect(0, 13, buttonWidth_Points, buttonHeight_Points), tf, PdfMeasurement.MU_POINTS); // Change the pen stroke color pen.strokeColor = Color.RED; // Change the brush fill color brush.fillColor = Color.GREEN; /* * Create an instance of PdfAppearanceStream for the * Rollover Appearance of the first PdfFormPushButton */ PdfAppearanceStream js_rollover_AP = new PdfAppearanceStream(ap_rect); // Draw the rectangle on the AppearanceStream js_rollover_AP.drawRect(ap_rect, pen, brush); // Write the name of the Control inside the rectangle js_rollover_AP.writeText("JavaScript-RolloverAP", rollover_ap_font, new PdfRect(0, 11, buttonWidth_Points, buttonHeight_Points), tf, PdfMeasurement.MU_POINTS); /* * Create an instance of PdfAppearanceStream for the * Rollover Appearance of the second PdfFormPushButton */ PdfAppearanceStream submit_rollover_AP = new PdfAppearanceStream(ap_rect); // Draw the rectangle on the AppearanceStream submit_rollover_AP.drawRect(ap_rect, pen, brush); // Write the name of the Control inside the rectangle submit_rollover_AP.writeText("Submit-RolloverAP", rollover_ap_font, new PdfRect(0, 11, buttonWidth_Points, buttonHeight_Points), tf, PdfMeasurement.MU_POINTS); /* * Create the first PdfFormPushButton control * with the specified coordinates */ PdfFormPushButton jsButton = new PdfFormPushButton(new PdfRect( xPos, yPos, buttonWidth, buttonHeight)); // Set JavaScript action to the PdfFormPushButton jsButton.addAction(PdfAction.JAVASCRIPT, PdfEvent.ON_MOUSE_DOWN, "app.alert('Gnostice PDFOne on Mouse Down')"); // Set the properties for the PdfFormPushButton jsButton.setName("button1"); jsButton.setNormalCaption("JavaScript"); // Set the Normal Appearance jsButton.setNormalAppearance(js_normal_AP); // Set the Rollover Appearance jsButton.setRolloverAppearance(js_rollover_AP); jsButton.setFont(font); // Add the jsButton to the page p.addFormField(jsButton); //X-Y coordinates for another PdfFormPushButton xPos = 1; // One inch yPos = 2; // One inch /* * Create the second PdfFormPushButton control * with the specified coordinates */ PdfFormPushButton submitButton = new PdfFormPushButton(new PdfRect( xPos, yPos, buttonWidth, buttonHeight)); /* Set Submit action to the PdfFormPushButton */ submitButton.addAction(PdfAction.URI, PdfEvent.ON_MOUSE_DOWN, "http://www.gnostice.com"); // Set the properties for the PdfFormPushButton submitButton.setName("button2"); submitButton.setNormalCaption("Submit"); // Set the Normal Appearance submitButton.setNormalAppearance(submit_normal_AP); // Set the RolloverAppearance submitButton.setRolloverAppearance(submit_rollover_AP); submitButton.setFont(font); //Add the submitButton to the page p.addFormField(submitButton); // Add the page to the document d.add(p); d.setOpenAfterSave(true); d.write(); w.dispose(); } private static void formFieldCustomAppearanceImageDemo() throws IOException, PdfException { // Create a PdfWriter instance for the PDF file PdfWriter w = PdfWriter.fileWriter( "FormfieldCustomAppearanceImageDemo.pdf"); // Create a PdfDocument instance with the PdfWriter PdfDocument d = new PdfDocument(w); // Create a font and set its color PdfFont font = PdfFont.create("Helvetica", 10, PdfEncodings.WINANSI); font.setColor(Color.BLACK); // Create a new PdfPage PdfPage p; p = new PdfPage(PdfPageSize.A4); p.setMeasurementUnit(PdfMeasurement.MU_INCHES); // X-Y coordinates of the first PdfFormPushButton control int xPos = 1; // One inch int yPos = 1; // One inch // Dimensions of the PdfFormPushButton in Inches double buttonWidth = 1; // One inch double buttonHeight = 0.5; // Half inch // Dimensions of the PdfFormPushButton in Points double buttonWidth_Points = PdfMeasurement.convertToPdfUnit( PdfMeasurement.MU_INCHES, 1.5); double buttonHeight_Points = PdfMeasurement.convertToPdfUnit( PdfMeasurement.MU_INCHES, 0.5); /* * Create a rectangle of the same size as * the PushButton which will be drawn on * all AppearanceStreams */ PdfRect ap_rect = new PdfRect(0, 0, buttonWidth_Points, buttonHeight_Points); /* * Create a font for Normal AppearanceStream * and set its color */ PdfFont normal_ap_font = PdfFont.create("Arial", PdfFont.BOLD | PdfFont.ITALIC, 15, PdfEncodings.WINANSI); normal_ap_font.setColor(Color.BLACK); /* * Create a font for Rollover AppearanceStream * and set its color */ PdfFont rollover_ap_font = PdfFont.create("Helvetica", PdfFont.BOLD | PdfFont.ITALIC, 18, PdfEncodings.WINANSI); rollover_ap_font.setColor(Color.WHITE); /* * Create a TextFormatter to format the text to be written * inside the shape of the AppearanceStream */ PdfTextFormatter tf = new PdfTextFormatter(); tf.setAlignment(PdfTextFormatter.CENTER); /* * Create an image for Normal Appearance */ PdfImage imgNoraml = PdfImage.create("Normal_Appearance.bmp"); /* * Create an image for Rollover Appearance */ PdfImage imgRollover = PdfImage.create("Rollover_Appearance.bmp"); /* * Create an instance of PdfAppearanceStream for the * Normal Appearance of the first PdfFormPushButton */ PdfAppearanceStream js_normal_AP = new PdfAppearanceStream(ap_rect); // Draw an Image to be represented as Normal Appearance js_normal_AP.drawImage(imgNoraml); //Write the name of the Control inside the Image js_normal_AP.writeText("JavaScript", normal_ap_font, new PdfRect(0, 8, buttonWidth_Points, buttonHeight_Points), tf, PdfMeasurement.MU_POINTS); /* * Create an instance of PdfAppearanceStream for the * Normal Appearance of the second PdfFormPushButton */ PdfAppearanceStream submit_normal_AP = new PdfAppearanceStream(ap_rect); // Draw an Image to be represented as Normal Appearance submit_normal_AP.drawImage(imgNoraml); //Write the name of the Control inside the Image submit_normal_AP.writeText("Submit", normal_ap_font, new PdfRect(0, 8, buttonWidth_Points, buttonHeight_Points), tf, PdfMeasurement.MU_POINTS); /* * Create an instance of PdfAppearanceStream for the * Rollover Appearance of the first PdfFormPushButton */ PdfAppearanceStream js_rollover_AP = new PdfAppearanceStream(ap_rect); js_rollover_AP.drawImage(imgRollover); // Write the name of the Control inside the Image js_rollover_AP.writeText("JavaScript", rollover_ap_font, new PdfRect(0, 6, buttonWidth_Points, buttonHeight_Points), tf, PdfMeasurement.MU_POINTS); /* * Create an instance of PdfAppearanceStream for the * Rollover Appearance of the second PdfFormPushButton */ PdfAppearanceStream submit_rollover_AP = new PdfAppearanceStream(ap_rect); submit_rollover_AP.drawImage(imgRollover); // Write the name of the Control inside the Image submit_rollover_AP.writeText("Submit", rollover_ap_font, new PdfRect(0, 6, buttonWidth_Points, buttonHeight_Points), tf, PdfMeasurement.MU_POINTS); /* * Create the first PdfFormPushButton control * with the specified coordinates */ PdfFormPushButton jsButton = new PdfFormPushButton(new PdfRect( xPos, yPos, buttonWidth, buttonHeight)); // Set JavaScript action to the PdfFormPushButton jsButton.addAction(PdfAction.JAVASCRIPT, PdfEvent.ON_MOUSE_DOWN, "app.alert('Gnostice PDFOne on Mouse Down')"); // Set the properties for the PdfFormPushButton jsButton.setName("button1"); jsButton.setNormalCaption("JavaScript"); // Set the Normal Appearance jsButton.setNormalAppearance(js_normal_AP); // Set the Rollover Appearance jsButton.setRolloverAppearance(js_rollover_AP); jsButton.setFont(font); // Add the jsButton to the page p.addFormField(jsButton); //X-Y coordinates for the second PdfFormPushButton xPos = 1; // One inch yPos = 2; // One inch /* * Create the second PdfFormPushButton control * with the specified coordinates */ PdfFormPushButton submitButton = new PdfFormPushButton(new PdfRect( xPos, yPos, buttonWidth, buttonHeight)); /* Set Submit action to the PdfFormPushButton */ submitButton.addAction(PdfAction.URI, PdfEvent.ON_MOUSE_DOWN, "http://www.gnostice.com"); // Set the properties for the PdfFormPushButton submitButton.setName("button2"); submitButton.setNormalCaption("Submit"); // Set the Normal Appearance submitButton.setNormalAppearance(submit_normal_AP); // Set the RolloverAppearance submitButton.setRolloverAppearance(submit_rollover_AP); submitButton.setFont(font); //Add the submitButton to the page p.addFormField(submitButton); // Add the page to the document d.add(p); d.setOpenAfterSave(true); d.write(); w.dispose(); } }