// --------------------------------------------------------------------------- public byte[] FillTemplate(byte[] pdf, Movie movie) { using (MemoryStream ms = new MemoryStream()) { PdfReader reader = new PdfReader(pdf); using (PdfStamper stamper = new PdfStamper(reader, ms)) { AcroFields form = stamper.AcroFields; BaseColor color = WebColors.GetRGBColor( "#" + movie.entry.category.color ); PushbuttonField bt = form.GetNewPushbuttonFromField(POSTER); bt.Layout = PushbuttonField.LAYOUT_ICON_ONLY; bt.ProportionalIcon = true; bt.Image = Image.GetInstance(Path.Combine(IMAGE, movie.Imdb + ".jpg")); bt.BackgroundColor = color; form.ReplacePushbuttonField(POSTER, bt.Field); PdfContentByte canvas = stamper.GetOverContent(1); float size = 12; AcroFields.FieldPosition f = form.GetFieldPositions(TEXT)[0]; while (AddParagraph(CreateMovieParagraph(movie, size), canvas, f, true) && size > 6) { size -= 0.2f; } AddParagraph(CreateMovieParagraph(movie, size), canvas, f, false); form.SetField(YEAR, movie.Year.ToString()); form.SetFieldProperty(YEAR, "bgcolor", color, null); form.SetField(YEAR, movie.Year.ToString()); stamper.FormFlattening = true; } return(ms.ToArray()); } }
// --------------------------------------------------------------------------- /** * Manipulates a PDF file src with the file dest as result * @param src the original PDF */ public byte[] ManipulatePdf(byte[] src) { // create a reader PdfReader reader = new PdfReader(src); using (MemoryStream ms = new MemoryStream()) { // create a stamper using (PdfStamper stamper = new PdfStamper(reader, ms)) { // Add an open action PdfWriter writer = stamper.Writer; PdfAction action = PdfAction.JavaScript( Utilities.ReadFileToString( Path.Combine(Utility.ResourceJavaScript, "post_from_html.js") ), writer ); writer.SetOpenAction(action); // create a submit button that posts data to the HTML page PushbuttonField button1 = new PushbuttonField( stamper.Writer, new Rectangle(90, 660, 160, 690), "post" ); button1.Text = "POST TO HTML"; button1.BackgroundColor = new GrayColor(0.7f); button1.Visibility = PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT; PdfFormField submit1 = button1.Field; submit1.Action = PdfAction.JavaScript( Utilities.ReadFileToString(Path.Combine( Utility.ResourceJavaScript, "post_to_html.js" )), writer ); // add the button stamper.AddAnnotation(submit1, 1); } return ms.ToArray(); } }
// --------------------------------------------------------------------------- public void Write(Stream stream) { using (ZipFile zip = new ZipFile()) { NestedTables n = new NestedTables(); byte[] ntPdf = Utility.PdfBytes(n); Advertisement a = new Advertisement(); byte[] aPdf = a.ManipulatePdf(ntPdf); PdfReader reader = new PdfReader(aPdf); using (MemoryStream ms = new MemoryStream()) { using (PdfStamper stamper = new PdfStamper(reader, ms)) { AcroFields form = stamper.AcroFields; PushbuttonField ad = form.GetNewPushbuttonFromField("advertisement"); ad.Layout = PushbuttonField.LAYOUT_ICON_ONLY; ad.ProportionalIcon = true; ad.Image = Image.GetInstance(RESOURCE); form.ReplacePushbuttonField("advertisement", ad.Field); } zip.AddEntry(RESULT, ms.ToArray()); } zip.AddFile(RESOURCE, ""); zip.AddEntry(Utility.ResultFileName(a.ToString() + ".pdf"), aPdf); zip.Save(stream); } }
// --------------------------------------------------------------------------- /** * Manipulates a PDF file src with the file dest as result * @param src the original PDF */ public byte[] ManipulatePdf(byte[] src) { // Create a reader for the original document PdfReader reader = new PdfReader(src); // Create a reader for the advertisement resource PdfReader ad = new PdfReader(RESOURCE); using (MemoryStream ms = new MemoryStream()) { // Create a stamper using (PdfStamper stamper = new PdfStamper(reader, ms)) { // Create the advertisement annotation for the menubar Rectangle rect = new Rectangle(400, 772, 545, 792); PushbuttonField button = new PushbuttonField( stamper.Writer, rect, "click" ); button.BackgroundColor = BaseColor.RED; button.BorderColor = BaseColor.RED; button.FontSize = 10; button.Text = "Close this advertisement"; button.Image = Image.GetInstance( Path.Combine(Utility.ResourceImage, IMAGE) ); button.Layout = PushbuttonField.LAYOUT_LABEL_LEFT_ICON_RIGHT; button.IconHorizontalAdjustment = 1; PdfFormField menubar = button.Field; String js = "var f1 = getField('click'); f1.display = display.hidden;" + "var f2 = getField('advertisement'); f2.display = display.hidden;" ; menubar.Action = PdfAction.JavaScript(js, stamper.Writer); // Add the annotation stamper.AddAnnotation(menubar, 1); // Create the advertisement annotation for the content rect = new Rectangle(400, 550, 545, 772); button = new PushbuttonField( stamper.Writer, rect, "advertisement" ); button.BackgroundColor = BaseColor.WHITE; button.BorderColor = BaseColor.RED; button.Text = "Buy the book iText in Action 2nd edition"; button.Template = stamper.GetImportedPage(ad, 1); button.Layout = PushbuttonField.LAYOUT_ICON_TOP_LABEL_BOTTOM; PdfFormField advertisement = button.Field; advertisement.Action = new PdfAction( "http://www.1t3xt.com/docs/book.php" ); // Add the annotation stamper.AddAnnotation(advertisement, 1); } return(ms.ToArray()); } }
public byte[] CreateTemplate() { using (MemoryStream ms = new MemoryStream()) { using (Document document = new Document(new Rectangle( Utilities.MillimetersToPoints(35), Utilities.MillimetersToPoints(50) ))) { PdfWriter writer = PdfWriter.GetInstance(document, ms); writer.ViewerPreferences = PdfWriter.PageLayoutSinglePage; document.Open(); PushbuttonField poster = new PushbuttonField( writer, new Rectangle( Utilities.MillimetersToPoints(0), Utilities.MillimetersToPoints(25), Utilities.MillimetersToPoints(35), Utilities.MillimetersToPoints(50) ), POSTER ); poster.BackgroundColor = new GrayColor(0.4f); writer.AddAnnotation(poster.Field); TextField movie = new TextField( writer, new Rectangle( Utilities.MillimetersToPoints(0), Utilities.MillimetersToPoints(7), Utilities.MillimetersToPoints(35), Utilities.MillimetersToPoints(25) ), TEXT ); movie.Options = TextField.MULTILINE; writer.AddAnnotation(movie.GetTextField()); TextField screening = new TextField( writer, new Rectangle( Utilities.MillimetersToPoints(0), Utilities.MillimetersToPoints(0), Utilities.MillimetersToPoints(35), Utilities.MillimetersToPoints(7) ), YEAR ); screening.Alignment = Element.ALIGN_CENTER; screening.BackgroundColor = new GrayColor(0.4f); screening.TextColor = GrayColor.GRAYWHITE; writer.AddAnnotation(screening.GetTextField()); } return(ms.ToArray()); } }
// --------------------------------------------------------------------------- /** * Manipulates a PDF file src with the file dest as result * @param src the original PDF */ public byte[] ManipulatePdf(byte[] src) { // Create a reader PdfReader reader = new PdfReader(src); int n = reader.NumberOfPages; using (MemoryStream ms = new MemoryStream()) { // Create a stamper using (PdfStamper stamper = new PdfStamper(reader, ms)) { // Create pushbutton 1 PushbuttonField saveAs = new PushbuttonField( stamper.Writer, new Rectangle(636, 10, 716, 30), "Save" ); saveAs.BorderColor = BaseColor.BLACK; saveAs.Text = "Save"; saveAs.TextColor = BaseColor.RED; saveAs.Layout = PushbuttonField.LAYOUT_LABEL_ONLY; saveAs.Rotation = 90; PdfAnnotation saveAsButton = saveAs.Field; saveAsButton.Action = PdfAction.JavaScript( "app.execMenuItem('SaveAs')", stamper.Writer ); // Create pushbutton 2 PushbuttonField mail = new PushbuttonField( stamper.Writer, new Rectangle(736, 10, 816, 30), "Mail" ); mail.BorderColor = BaseColor.BLACK; mail.Text = "Mail"; mail.TextColor = BaseColor.RED; mail.Layout = PushbuttonField.LAYOUT_LABEL_ONLY; mail.Rotation = 90; PdfAnnotation mailButton = mail.Field; mailButton.Action = PdfAction.JavaScript( "app.execMenuItem('AcroSendMail:SendMail')", stamper.Writer ); // Add the annotations to every page of the document for (int page = 1; page <= n; page++) { stamper.AddAnnotation(saveAsButton, page); stamper.AddAnnotation(mailButton, page); } } return(ms.ToArray()); } }
//2.3 //print QRcode in button field private void _GenQRCode(PdfStamper stamper, string FieldName, string QrCodeContent) { AcroFields form = stamper.AcroFields; PushbuttonField ad = form.GetNewPushbuttonFromField(FieldName); BarcodeQRCode qrcode = new BarcodeQRCode(QrCodeContent, 2, 2, null); var qrcodeImg = qrcode.GetImage(); ad.Layout = PushbuttonField.LAYOUT_ICON_ONLY; ad.ProportionalIcon = true; ad.Image = qrcodeImg; form.ReplacePushbuttonField(FieldName, ad.Field); }
// --------------------------------------------------------------------------- /** * Manipulates a PDF file src with the file dest as result * @param src the original PDF */ public byte[] ManipulatePdf(byte[] src) { PdfReader reader = new PdfReader(src); using (MemoryStream ms = new MemoryStream()) { using (PdfStamper stamper = new PdfStamper(reader, ms)) { stamper.Writer.AddJavaScript(File.ReadAllText(RESOURCE)); AcroFields form = stamper.AcroFields; AcroFields.Item fd = form.GetFieldItem("married"); PdfDictionary dictYes = (PdfDictionary)PdfReader.GetPdfObject(fd.GetWidgetRef(0)); PdfDictionary yesAction = dictYes.GetAsDict(PdfName.AA); if (yesAction == null) { yesAction = new PdfDictionary(); } yesAction.Put( new PdfName("Fo"), PdfAction.JavaScript("ReadOnly = false);", stamper.Writer) ); dictYes.Put(PdfName.AA, yesAction); PdfDictionary dictNo = (PdfDictionary)PdfReader.GetPdfObject(fd.GetWidgetRef(1)); PdfDictionary noAction = dictNo.GetAsDict(PdfName.AA); if (noAction == null) { noAction = new PdfDictionary(); } noAction.Put( new PdfName("Fo"), PdfAction.JavaScript("ReadOnly = true);", stamper.Writer) ); dictNo.Put(PdfName.AA, noAction); PdfWriter writer = stamper.Writer; PushbuttonField button = new PushbuttonField( writer, new Rectangle(40, 690, 200, 710), "submit" ); button.Text = "validate and submit"; button.Options = PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT; PdfFormField validateAndSubmit = button.Field; validateAndSubmit.Action = PdfAction.JavaScript( "validate();", stamper.Writer ); stamper.AddAnnotation(validateAndSubmit, 1); } return(ms.ToArray()); } }
// --------------------------------------------------------------------------- /** * Adds a popup. * @param stamper the PdfStamper to which the annotation needs to be added * @param rect the position of the annotation * @param title the annotation title * @param contents the annotation content * @param imdb the IMDB number of the movie used as name of the annotation */ public void AddPopup(PdfStamper stamper, Rectangle rect, String title, String contents, String imdb) { // Create the text annotation PdfAnnotation text = PdfAnnotation.CreateText( stamper.Writer, rect, title, contents, false, "Comment" ); text.Name = string.Format("IMDB{0}", imdb); text.Flags = PdfAnnotation.FLAGS_READONLY | PdfAnnotation.FLAGS_NOVIEW; // Create the popup annotation PdfAnnotation popup = PdfAnnotation.CreatePopup( stamper.Writer, new Rectangle( rect.Left + 10, rect.Bottom + 10, rect.Left + 200, rect.Bottom + 100 ), null, false ); // Add the text annotation to the popup popup.Put(PdfName.PARENT, text.IndirectReference); // Declare the popup annotation as popup for the text text.Put(PdfName.POPUP, popup.IndirectReference); // Add both annotations stamper.AddAnnotation(text, 1); stamper.AddAnnotation(popup, 1); // Create a button field PushbuttonField field = new PushbuttonField( stamper.Writer, rect, string.Format("b{0}", imdb) ); PdfAnnotation widget = field.Field; // Show the popup onMouseEnter PdfAction enter = PdfAction.JavaScript( string.Format(JS1, imdb), stamper.Writer ); widget.SetAdditionalActions(PdfName.E, enter); // Hide the popup onMouseExit PdfAction exit = PdfAction.JavaScript( string.Format(JS2, imdb), stamper.Writer ); widget.SetAdditionalActions(PdfName.X, exit); // Add the button annotation stamper.AddAnnotation(widget, 1); }
//public void test() //{ // var fields = _pdfStamper.AcroFields; // PushbuttonField ad = fields.GetNewPushbuttonFromField("08"); // ad.Layout = PushbuttonField.LAYOUT_ICON_ONLY; // ad.ProportionalIcon = true; // //ad.Image = iTextSharp.text.Image.GetInstance(ValueType(; // fields.ReplacePushbuttonField("08", ad.Field); // //fields.ReplacePushbuttonField("", new PdfFormField()); //} public void SetBlobField(string fieldName, object value) { var fields = _pdfStamper.AcroFields; MemoryStream ms = new MemoryStream((byte[])value); System.Drawing.Image img = new System.Drawing.Bitmap(ms); PushbuttonField ad = fields.GetNewPushbuttonFromField(fieldName); ad.Layout = PushbuttonField.LAYOUT_ICON_ONLY; ad.ProportionalIcon = true; ad.Image = iTextSharp.text.Image.GetInstance(img, System.Drawing.Imaging.ImageFormat.Jpeg); fields.ReplacePushbuttonField(fieldName, ad.Field); }
public void AddSubmitButton() { Rectangle rect = new Rectangle(50, 50, 100, 10); PushbuttonField button = new PushbuttonField(writer, rect, "postSubmit") { FontSize = 8, BackgroundColor = BaseColor.LIGHT_GRAY, BorderColor = GrayColor.BLACK, BorderWidth = 1f, BorderStyle = PdfBorderDictionary.STYLE_BEVELED, TextColor = GrayColor.GREEN, Text = "Submit", Visibility = PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT }; PdfFormField field = button.Field; String javascript = "validate();"; field.Action = PdfAction.JavaScript(javascript, writer); stamper.AddAnnotation(field, 1); }
// --------------------------------------------------------------------------- /** * 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, PdfAction.SUBMIT_HTML_FORMAT ); stamper.AddAnnotation(submit, 1); } }
private static byte[] AddSubmitButton(byte[] pdfFile) { var pr = new PdfReader(pdfFile); var ims = new MemoryStream(); var ps = new PdfStamper(pr, ims); string url = AppSettings.URLForFormSubmission(); PushbuttonField button2 = new PushbuttonField(ps.Writer, new Rectangle(100, 1000, 150, 1030), "Submit"); button2.BackgroundColor = new GrayColor(0.7f); button2.Text = "Submit"; button2.Visibility = PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT; PdfFormField submit2 = button2.Field; submit2.Action = PdfAction.CreateSubmitForm(url, null, PdfAction.SUBMIT_PDF); ps.AddAnnotation(submit2, 1); ps.Close(); return(ims.ToArray()); }
// --------------------------------------------------------------------------- /** * 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); } }
private byte[] BindDataToPDFTemplate(List <customerinformationforpdf> listdata, PdfReader readPath, List <fn_getClientResourceData_Result> pdfHeaderData, IList <LogoDetails> LogoData) { byte[] bytes; using (MemoryStream memStream = new MemoryStream()) { using (PdfStamper stamper = new PdfStamper(readPath, memStream, '\0', true)) { var form = stamper.AcroFields; var fieldKeys = form.Fields.Keys; //Page 1 of PDF foreach (string fieldKey in fieldKeys) { if (fieldKey == "currentAddress_1") { foreach (fn_getClientResourceData_Result item in pdfHeaderData) { if (item.Name == "WOHeaderLine01") { form.SetField(fieldKey, item.Value); } } } if (fieldKey == "currentAddress_2") { foreach (fn_getClientResourceData_Result item in pdfHeaderData) { if (item.Name == "WOHeaderLine02") { form.SetField(fieldKey, item.Value); } } } if (fieldKey == "telephone_1") { foreach (fn_getClientResourceData_Result item in pdfHeaderData) { if (item.Name == "WOHeaderLine03") { form.SetField(fieldKey, item.Value); } } } if (fieldKey == "fax_1") { foreach (fn_getClientResourceData_Result item in pdfHeaderData) { if (item.Name == "WOHeaderLine04") { form.SetField(fieldKey, item.Value); } } } if (fieldKey == "websiteUrl_1") { foreach (fn_getClientResourceData_Result item in pdfHeaderData) { if (item.Name == "WOHeaderLine05") { form.SetField(fieldKey, item.Value); } } } if (fieldKey == "workOrderNumber_1") { form.SetField(fieldKey, listdata[0].WorkOrderNumber); } if (fieldKey == "currentDate_1") { form.SetField(fieldKey, Convert.ToString(DateTime.Now.ToString("MM/dd/yyyy"))); } if (fieldKey == "csr_1") { form.SetField(fieldKey, listdata[0].FirstName + " " + listdata[0].LastName); } if (fieldKey == "to") { form.SetField(fieldKey, listdata[0].VendorName); } if (fieldKey == "clientName") { form.SetField(fieldKey, listdata[0].ClientName); } if (fieldKey == "serviceCall_1") { form.SetField(fieldKey, listdata[0].WorkOrderNumber); } if (fieldKey == "workOrderNumber_2") { form.SetField(fieldKey, listdata[0].CustomerRefNumber); } if (fieldKey == "amount_1") { form.SetField(fieldKey, Convert.ToString("$" + listdata[0].NTE)); } if (fieldKey == "store_name") { form.SetField(fieldKey, listdata[0].CustomerName); } if (fieldKey == "storeAddress_1") { form.SetField(fieldKey, listdata[0].Address01); } if (fieldKey == "storeAddress_2") { form.SetField(fieldKey, listdata[0].City + " " + listdata[0].State + " " + listdata[0].Zip01); } if (fieldKey == "siteinfo") { form.SetField(fieldKey, "siteinfo"); } if (fieldKey == "telephone_2") { form.SetField(fieldKey, listdata[0].Telephone); } if (fieldKey == "contact") { form.SetField(fieldKey, "contact"); } if (fieldKey == "currentDate_3") { form.SetField(fieldKey, listdata[0].DateArriveFrom.Value.ToString("MM/dd/yyyy")); } if (fieldKey == "currentTime_2") { form.SetField(fieldKey, listdata[0].TimeArriveFrom); } if (fieldKey == "currentDate_4") { form.SetField(fieldKey, listdata[0].DateArriveTo.Value.ToString("MM/dd/yyyy")); } if (fieldKey == "currentTime_3") { form.SetField(fieldKey, Convert.ToString(listdata[0].TimeArriveTo)); } if (fieldKey == "description") { form.SetField(fieldKey, listdata[0].Description); } if (fieldKey == "IVRNumber_1") { form.SetField(fieldKey, listdata[0].IVRNumber); } if (fieldKey == "IVRNumber_2") { form.SetField(fieldKey, listdata[0].IVRNumber); } // Page 2 of PDF if (fieldKey == "currentAddress_3") { foreach (fn_getClientResourceData_Result item in pdfHeaderData) { if (item.Name == "WOHeaderLine01") { form.SetField(fieldKey, item.Value); } } } if (fieldKey == "currentAddress_4") { foreach (fn_getClientResourceData_Result item in pdfHeaderData) { if (item.Name == "WOHeaderLine02") { form.SetField(fieldKey, item.Value); } } } if (fieldKey == "telephone_3") { foreach (fn_getClientResourceData_Result item in pdfHeaderData) { if (item.Name == "WOHeaderLine03") { form.SetField(fieldKey, item.Value); } } } if (fieldKey == "fax_2") { foreach (fn_getClientResourceData_Result item in pdfHeaderData) { if (item.Name == "WOHeaderLine04") { form.SetField(fieldKey, item.Value); } } } if (fieldKey == "websiteUrl_2") { foreach (fn_getClientResourceData_Result item in pdfHeaderData) { if (item.Name == "WOHeaderLine05") { form.SetField(fieldKey, item.Value); } } } if (fieldKey == "serviceCall_2") { form.SetField(fieldKey, listdata[0].WorkOrderNumber); } if (fieldKey == "clientName_2") { form.SetField(fieldKey, listdata[0].ClientName); } if (fieldKey == "storeName_2") { form.SetField(fieldKey, listdata[0].CustomerName); } if (fieldKey == "storeAddress_3") { form.SetField(fieldKey, listdata[0].Address01 + " " + listdata[0].City + " " + listdata[0].State + " " + listdata[0].Zip01); } if (fieldKey == "workOrderNumber_3") { form.SetField(fieldKey, listdata[0].CustomerRefNumber); } if (fieldKey == "telephone_4") { form.SetField(fieldKey, listdata[0].Telephone); } if (fieldKey == "clientName_4") { form.SetField(fieldKey, listdata[0].ClientName); } if (fieldKey == "clientName_3") { form.SetField(fieldKey, listdata[0].ClientName); } if (fieldKey == "telephone_5") { foreach (fn_getClientResourceData_Result item in pdfHeaderData) { if (item.Name == "WOHeaderLine03") { form.SetField(fieldKey, item.Value); } } } } PushbuttonField ad = form.GetNewPushbuttonFromField("SubmitButton1"); ad.Layout = PushbuttonField.LAYOUT_ICON_ONLY; ad.ProportionalIcon = true; ad.Image = Image.GetInstance(LogoData[0].Logo); form.ReplacePushbuttonField("SubmitButton1", ad.Field); PushbuttonField ad2 = form.GetNewPushbuttonFromField("SubmitButton2"); ad2.Layout = PushbuttonField.LAYOUT_ICON_ONLY; ad2.ProportionalIcon = true; ad2.Image = Image.GetInstance(LogoData[0].Logo); form.ReplacePushbuttonField("SubmitButton2", ad2.Field); // "Flatten" the form so it wont be editable/usable anymore stamper.FormFlattening = false; form.GenerateAppearances = false; // You can also specify fields to be flattened, which // leaves the rest of the form still be editable/usable stamper.Close(); } bytes = memStream.ToArray(); return(bytes); } }
public TestCreatePDF() { filenameBlank = filename.Replace(".", "_Blank."); FileStream fs = new System.IO.FileStream(filenameBlank, System.IO.FileMode.Create); writer = PdfWriter.GetInstance(doc, fs); doc.Open(); cb = writer.DirectContent; Rectangle rect = new Rectangle(50, 700, 300, 600) { BorderWidth = .25f, Border = 255 }; cb.Rectangle(rect); cb.Stroke(); //Add text Font font3 = new Font(FontFactory.GetFont(FontFactory.HELVETICA, 1000, Font.NORMAL, BaseColor.BLACK)); Chunk chunk = new Chunk("Roopesh", new Font(FontFactory.GetFont(FontFactory.HELVETICA, 10, Font.NORMAL, BaseColor.BLUE))); Phrase phrase = new Phrase(chunk); ColumnText ctext = new ColumnText(cb); ctext.SetSimpleColumn(rect.Left + 5, rect.Top, rect.Right, rect.Bottom); ctext.SetText(phrase); ctext.Go(); this.doc.Close(); GetReader(); //Add TextBox rect = new Rectangle(rect.Left, rect.Top - 100, rect.Right, rect.Bottom - 100); TextField tf = new TextField(writer, rect, "text1") { Alignment = Element.ALIGN_LEFT | Element.ALIGN_TOP, BorderColor = BaseColor.BLACK, BorderStyle = PdfBorderDictionary.STYLE_SOLID, Text = "TextField" }; stamper.AddAnnotation(tf.GetTextField(), 1); //Add Radio rect = new Rectangle(rect.Left, rect.Top - 100, rect.Right, rect.Bottom - 25); PdfFormField group = PdfFormField.CreateRadioButton(writer, true); group.FieldName = "grp1"; for (int i = 0; i < 5; i++) { Rectangle radioRect = new Rectangle(rect.Left + i * 25, rect.Top, rect.Left + (i + 1) * 25, rect.Bottom); RadioCheckField radioField = new RadioCheckField(writer, radioRect, "chk" + i.ToString(), i.ToString()) { BorderColor = GrayColor.BLACK, CheckType = RadioCheckField.TYPE_CIRCLE, BorderStyle = PdfBorderDictionary.STYLE_SOLID }; group.AddKid(radioField.RadioField); } //group.SetAdditionalActions(PdfName.E, PdfAction.JavaScript("app.alert(validate);",writer)); stamper.AddAnnotation(group, 1); //Add submit button rect = new Rectangle(rect.Left, rect.Top - 100, rect.Right, rect.Bottom - 25); PushbuttonField button = new PushbuttonField(writer, rect, "postSubmit") { FontSize = 8, BackgroundColor = BaseColor.LIGHT_GRAY, BorderColor = GrayColor.BLACK, BorderWidth = 1f, BorderStyle = PdfBorderDictionary.STYLE_BEVELED, TextColor = GrayColor.GREEN, Text = "Submit", Visibility = PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT }; PdfFormField field = button.Field; String javascript = "validate();"; field.Action = PdfAction.JavaScript(javascript, writer); //field.Action = PdfAction.CreateSubmitForm( @"*****@*****.**", null, PdfAction.SUBMIT_HTML_FORMAT | PdfAction.SUBMIT_INCLUDE_NO_VALUE_FIELDS); stamper.AddAnnotation(field, 1); PdfAcroForm f = new PdfAcroForm(writer); //Add common Javascript code writer.AddJavaScript("var requiredFields = ['text1', 'grp1'];"); string validateFunction = "function validate () { " + " for (i=0; i<requiredFields.length; i++) {" + " var grp = this.getField(requiredFields[i]); if (!grp || grp.value === null || grp.value == ''|| grp.value=='Off') { " + " app.alert('please select this '+ requiredFields[i]); return false; }}" + "return true}"; writer.AddJavaScript(validateFunction); //Close all the streams stamper.Close(); reader.Close(); doc.Close(); }
// --------------------------------------------------------------------------- public void Write(Stream stream) { using (ZipFile zip = new ZipFile()) { MovieAds movieAds = new MovieAds(); byte[] pdf = movieAds.CreateTemplate(); zip.AddEntry(TEMPLATE, pdf); using (MemoryStream msDoc = new MemoryStream()) { using (Document document = new Document()) { using (PdfSmartCopy copy = new PdfSmartCopy(document, msDoc)) { document.Open(); PdfReader reader; PdfStamper stamper = null; AcroFields form = null; int count = 0; MemoryStream ms = null; using (ms) { foreach (Movie movie in PojoFactory.GetMovies()) { if (count == 0) { ms = new MemoryStream(); reader = new PdfReader(RESOURCE); stamper = new PdfStamper(reader, ms); stamper.FormFlattening = true; form = stamper.AcroFields; } count++; PdfReader ad = new PdfReader( movieAds.FillTemplate(pdf, movie) ); PdfImportedPage page = stamper.GetImportedPage(ad, 1); PushbuttonField bt = form.GetNewPushbuttonFromField( "movie_" + count ); bt.Layout = PushbuttonField.LAYOUT_ICON_ONLY; bt.ProportionalIcon = true; bt.Template = page; form.ReplacePushbuttonField("movie_" + count, bt.Field); if (count == 16) { stamper.Close(); reader = new PdfReader(ms.ToArray()); copy.AddPage(copy.GetImportedPage(reader, 1)); count = 0; } } if (count > 0) { stamper.Close(); reader = new PdfReader(ms.ToArray()); copy.AddPage(copy.GetImportedPage(reader, 1)); } } } } zip.AddEntry(RESULT, msDoc.ToArray()); } zip.AddFile(RESOURCE, ""); zip.Save(stream); } }
// --------------------------------------------------------------------------- /** * Creates a PDF document. */ public byte[] CreatePdf() { using (MemoryStream ms = new MemoryStream()) { using (Document document = new Document()) { PdfWriter writer = PdfWriter.GetInstance(document, ms); document.Open(); PdfFormField personal = PdfFormField.CreateEmpty(writer); personal.FieldName = "personal"; PdfPTable table = new PdfPTable(3); PdfPCell cell; table.AddCell("Your name:"); cell = new PdfPCell(); cell.Colspan = 2; TextField field = new TextField(writer, new Rectangle(0, 0), "name"); field.FontSize = 12; cell.CellEvent = new ChildFieldEvent( personal, field.GetTextField(), 1 ); table.AddCell(cell); table.AddCell("Login:"******"loginname"); field.FontSize = 12; cell.CellEvent = new ChildFieldEvent( personal, field.GetTextField(), 1 ); table.AddCell(cell); cell = new PdfPCell(); field = new TextField(writer, new Rectangle(0, 0), "password"); field.Options = TextField.PASSWORD; field.FontSize = 12; cell.CellEvent = new ChildFieldEvent( personal, field.GetTextField(), 1 ); table.AddCell(cell); table.AddCell("Your motivation:"); cell = new PdfPCell(); cell.Colspan = 2; cell.FixedHeight = 60; field = new TextField(writer, new Rectangle(0, 0), "reason"); field.Options = TextField.MULTILINE; field.FontSize = 12; cell.CellEvent = new ChildFieldEvent( personal, field.GetTextField(), 1 ); table.AddCell(cell); document.Add(table); writer.AddAnnotation(personal); PushbuttonField button1 = new PushbuttonField( writer, new Rectangle(90, 660, 140, 690), "post"); button1.Text = "POST"; button1.BackgroundColor = new GrayColor(0.7f); button1.Visibility = PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT; PdfFormField submit1 = button1.Field; submit1.Action = PdfAction.CreateSubmitForm( "/book/request", null, PdfAction.SUBMIT_HTML_FORMAT | PdfAction.SUBMIT_COORDINATES ); writer.AddAnnotation(submit1); } return(ms.ToArray()); } }
// --------------------------------------------------------------------------- /** * Creates a PDF document. */ public byte[] CreatePdf() { using (MemoryStream ms = new MemoryStream()) { using (Document document = new Document()) { PdfWriter writer = PdfWriter.GetInstance(document, ms); document.Open(); jsString = File.ReadAllText( Path.Combine(Utility.ResourceJavaScript, RESOURCE) ); writer.AddJavaScript(jsString); PdfContentByte canvas = writer.DirectContent; Font font = new Font(Font.FontFamily.HELVETICA, 18); Rectangle rect; PdfFormField field; PdfFormField radiogroup = PdfFormField.CreateRadioButton(writer, true); radiogroup.FieldName = "language"; RadioCheckField radio; for (int i = 0; i < LANGUAGES.Length; i++) { rect = new Rectangle(40, 806 - i * 40, 60, 788 - i * 40); radio = new RadioCheckField(writer, rect, null, LANGUAGES[i]); radio.BorderColor = GrayColor.GRAYBLACK; radio.BackgroundColor = GrayColor.GRAYWHITE; radio.CheckType = RadioCheckField.TYPE_CIRCLE; field = radio.RadioField; radiogroup.AddKid(field); ColumnText.ShowTextAligned( canvas, Element.ALIGN_LEFT, new Phrase(LANGUAGES[i], font), 70, 790 - i * 40, 0 ); } writer.AddAnnotation(radiogroup); PdfAppearance[] onOff = new PdfAppearance[2]; onOff[0] = canvas.CreateAppearance(20, 20); onOff[0].Rectangle(1, 1, 18, 18); onOff[0].Stroke(); onOff[1] = canvas.CreateAppearance(20, 20); onOff[1].SetRGBColorFill(255, 128, 128); onOff[1].Rectangle(1, 1, 18, 18); onOff[1].FillStroke(); onOff[1].MoveTo(1, 1); onOff[1].LineTo(19, 19); onOff[1].MoveTo(1, 19); onOff[1].LineTo(19, 1); onOff[1].Stroke(); RadioCheckField checkbox; for (int i = 0; i < LANGUAGES.Length; i++) { rect = new Rectangle(180, 806 - i * 40, 200, 788 - i * 40); checkbox = new RadioCheckField(writer, rect, LANGUAGES[i], "on"); // checkbox.CheckType = RadioCheckField.TYPE_CHECK; field = checkbox.CheckField; field.SetAppearance( PdfAnnotation.APPEARANCE_NORMAL, "Off", onOff[0] ); field.SetAppearance( PdfAnnotation.APPEARANCE_NORMAL, "On", onOff[1] ); writer.AddAnnotation(field); ColumnText.ShowTextAligned( canvas, Element.ALIGN_LEFT, new Phrase(LANGUAGES[i], font), 210, 790 - i * 40, 0 ); } rect = new Rectangle(300, 806, 370, 788); PushbuttonField button = new PushbuttonField(writer, rect, "Buttons"); button.BackgroundColor = new GrayColor(0.75f); button.BorderColor = GrayColor.GRAYBLACK; button.BorderWidth = 1; button.BorderStyle = PdfBorderDictionary.STYLE_BEVELED; button.TextColor = GrayColor.GRAYBLACK; button.FontSize = 12; button.Text = "Push me"; button.Layout = PushbuttonField.LAYOUT_ICON_LEFT_LABEL_RIGHT; button.ScaleIcon = PushbuttonField.SCALE_ICON_ALWAYS; button.ProportionalIcon = true; button.IconHorizontalAdjustment = 0; button.Image = Image.GetInstance( Path.Combine(Utility.ResourceImage, IMAGE) ); field = button.Field; field.Action = PdfAction.JavaScript("this.showButtonState()", writer); writer.AddAnnotation(field); } return(ms.ToArray()); } }
// --------------------------------------------------------------------------- /** * Manipulates a PDF file src with the file dest as result * @param src the original PDF */ // public byte[] ManipulatePdf(byte[] src) { public void ManipulatePdf(byte[] src, Stream stream) { string BaseUrl = Utility.GetServerBaseUrl(); // create a reader PdfReader reader = new PdfReader(src); // create a stamper using (PdfStamper stamper = new PdfStamper(reader, stream)) { // create a submit button that posts the form as an HTML query string PushbuttonField button1 = new PushbuttonField( stamper.Writer, new Rectangle(90, 660, 140, 690), "post" ); button1.Text = "POST"; button1.BackgroundColor = new GrayColor(0.7f); button1.Visibility = PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT; PdfFormField submit1 = button1.Field; string submit_url = new Uri( new Uri(BaseUrl), string.Format( "/iTextInAction2Ed/WebHandler.ashx?{0}=Chapter09&{1}=ShowData", Chapters.QS_CHAPTER, Chapters.QS_CLASS ) ).ToString(); submit1.Action = PdfAction.CreateSubmitForm( submit_url, null, PdfAction.SUBMIT_HTML_FORMAT | PdfAction.SUBMIT_COORDINATES ); // add the button stamper.AddAnnotation(submit1, 1); // create a submit button that posts the form as FDF PushbuttonField button2 = new PushbuttonField( stamper.Writer, new Rectangle(200, 660, 250, 690), "FDF" ); button2.BackgroundColor = new GrayColor(0.7f); button2.Text = "FDF"; button2.Visibility = PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT; PdfFormField submit2 = button2.Field; submit2.Action = PdfAction.CreateSubmitForm( submit_url, null, PdfAction.SUBMIT_EXCL_F_KEY ); // add the button stamper.AddAnnotation(submit2, 1); // create a submit button that posts the form as XFDF PushbuttonField button3 = new PushbuttonField( stamper.Writer, new Rectangle(310, 660, 360, 690), "XFDF" ); button3.BackgroundColor = new GrayColor(0.7f); button3.Text = "XFDF"; button3.Visibility = PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT; PdfFormField submit3 = button3.Field; submit3.Action = PdfAction.CreateSubmitForm( submit_url, null, PdfAction.SUBMIT_XFDF ); // add the button stamper.AddAnnotation(submit3, 1); // create a reset button PushbuttonField button4 = new PushbuttonField( stamper.Writer, new Rectangle(420, 660, 470, 690), "reset" ); button4.BackgroundColor = new GrayColor(0.7f); button4.Text = "RESET"; button4.Visibility = PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT; PdfFormField reset = button4.Field; reset.Action = PdfAction.CreateResetForm(null, 0); // add the button stamper.AddAnnotation(reset, 1); } }
private byte[] BindDataToQuotePDFTemplate(IList <customerinformationforpdf> listdata, PdfReader readPath, List <fn_getClientResourceData_Result> pdfHeaderData, List <AttachmentTypeData> WOInvoiceData, List <ServiceInvoice> item2, string InvoiceNumber, DateTime DateOfInvoice, IList <LogoDetails> LogoData) { byte[] bytes; using (MemoryStream memStream = new MemoryStream()) { using (PdfStamper stamper = new PdfStamper(readPath, memStream, '\0', true)) { var form = stamper.AcroFields; var fieldKeys = form.Fields.Keys; foreach (string fieldKey in fieldKeys) { if (fieldKey == "address_1") { foreach (fn_getClientResourceData_Result item in pdfHeaderData) { if (item.Name == "WOHeaderLine01") { form.SetField(fieldKey, item.Value); } } } if (fieldKey == "stateName_1") { foreach (fn_getClientResourceData_Result item in pdfHeaderData) { if (item.Name == "WOHeaderLine02") { form.SetField(fieldKey, item.Value); } } } if (fieldKey == "telephone_1") { foreach (fn_getClientResourceData_Result item in pdfHeaderData) { if (item.Name == "WOHeaderLine03") { form.SetField(fieldKey, item.Value); } } } if (fieldKey == "fax_1") { foreach (fn_getClientResourceData_Result item in pdfHeaderData) { if (item.Name == "WOHeaderLine04") { form.SetField(fieldKey, item.Value); } } } if (fieldKey == "siteAddress_1") { foreach (fn_getClientResourceData_Result item in pdfHeaderData) { if (item.Name == "WOHeaderLine05") { form.SetField(fieldKey, item.Value); } } } //Data for the Table in PDF Template //Data to bind Activity Column Data if (fieldKey == "Material_Activity") { foreach (ServiceInvoice i in item2) { if (i.Name == "MATERIALS" && i.SNotes != null) { form.SetField(fieldKey, i.SNotes + " " + i.Name); } } } if (fieldKey == "Trip_Activity") { foreach (ServiceInvoice i in item2) { if (i.Name == "TRIP" && i.SNotes != null) { form.SetField(fieldKey, i.SNotes + " " + i.Name); } } } if (fieldKey == "Labour_Activity") { foreach (ServiceInvoice i in item2) { if (i.Name == "Labor" && i.SNotes != null) { form.SetField(fieldKey, i.SNotes + " " + i.Name); } } } //Data to bind Amount Column if (fieldKey == "Material_Amount") { foreach (ServiceInvoice i in item2) { if (i.Name == "MATERIALS") { if (i.STotal != 0) { form.SetField(fieldKey, "$" + Convert.ToString(string.Format("{0:0.00}", i.SAmount))); } } } } if (fieldKey == "Trip_Amount") { foreach (ServiceInvoice i in item2) { if (i.Name == "TRIP") { if (i.STotal != 0) { form.SetField(fieldKey, "$" + Convert.ToString(string.Format("{0:0.00}", i.SAmount))); } } } } if (fieldKey == "Labour_Amount") { foreach (ServiceInvoice i in item2) { if (i.Name == "Labor") { if (i.STotal != 0) { form.SetField(fieldKey, "$" + Convert.ToString(string.Format("{0:0.00}", i.SAmount))); } } } } // Data to bind Estimated Tax Column if (fieldKey == "Material_ESTax") { foreach (ServiceInvoice i in item2) { if (i.Name == "MATERIALS") { if (i.STotal != 0) { form.SetField(fieldKey, "$" + Convert.ToString(string.Format("{0:0.00}", i.STax))); } } } } if (fieldKey == "Trip_ESTax") { foreach (ServiceInvoice i in item2) { if (i.Name == "TRIP") { if (i.STotal != 0) { form.SetField(fieldKey, "$" + Convert.ToString(string.Format("{0:0.00}", i.STax))); } } } } if (fieldKey == "Labour_ESTax") { foreach (ServiceInvoice i in item2) { if (i.Name == "Labor") { if (i.STotal != 0) { form.SetField(fieldKey, "$" + Convert.ToString(string.Format("{0:0.00}", i.STax))); } } } } // Data to bind Total column data if (fieldKey == "Material_Total") { foreach (ServiceInvoice i in item2) { if (i.Name == "MATERIALS") { if (i.STotal != 0) { form.SetField(fieldKey, "$" + Convert.ToString(string.Format("{0:0.00}", i.STotal))); } } } } if (fieldKey == "Trip_Total") { foreach (ServiceInvoice i in item2) { if (i.Name == "TRIP") { if (i.STotal != 0) { form.SetField(fieldKey, "$" + Convert.ToString(string.Format("{0:0.00}", i.STotal))); } } } } if (fieldKey == "Labour_Total") { foreach (ServiceInvoice i in item2) { if (i.Name == "Labor") { if (i.STotal != 0) { form.SetField(fieldKey, "$" + Convert.ToString(string.Format("{0:0.00}", i.STotal))); } } } } ////End of table data if (fieldKey == "description") { foreach (ServiceInvoice i in item2) { form.SetField(fieldKey, i.Description); } } if (fieldKey == "HeaderForQuote&Invoice") { foreach (ServiceInvoice i in item2) { form.SetField(fieldKey, i.ModalStatus); break; } } if (fieldKey == "total_2") { form.SetField(fieldKey, "$" + Convert.ToString(string.Format("{0:0.00}", item2[0]._TotalAmountAfterTaxForAllTypes))); } if (fieldKey == "serviceCall_1") { form.SetField(fieldKey, InvoiceNumber); } if (fieldKey == "date_1") { form.SetField(fieldKey, DateOfInvoice.ToString("MM/dd/yyyy")); } if (fieldKey == "billTo") { form.SetField(fieldKey, listdata[0].CustomerName); } if (fieldKey == "poNumber_1") { form.SetField(fieldKey, listdata[0].CustomerRefNumber); } if (fieldKey == "address_2") { form.SetField(fieldKey, listdata[0].Address01 + listdata[0].Address02); } if (fieldKey == "address_3") { form.SetField(fieldKey, listdata[0].City + " " + listdata[0].State + " " + listdata[0].Zip01 + " " + listdata[0].Zip02); } PushbuttonField ad = form.GetNewPushbuttonFromField("submitButton_1"); ad.Layout = PushbuttonField.LAYOUT_ICON_ONLY; ad.ProportionalIcon = true; ad.Image = Image.GetInstance(LogoData[0].Logo); form.ReplacePushbuttonField("submitButton_1", ad.Field); } // "Flatten" the form so it wont be editable/usable anymore stamper.FormFlattening = false; form.GenerateAppearances = false; // You can also specify fields to be flattened, which // leaves the rest of the form still be editable/usable //stamper.PartialFormFlattening("field1"); stamper.Close(); } //UOWWorkOrder wo = new UOWWorkOrder(); //wo.ConvertByteToMemory(memStream, listdata); bytes = memStream.ToArray(); return(bytes); } }
// --------------------------------------------------------------------------- public void Write(Stream stream) { // step 1 using (Document document = new Document()) { // step 2 PdfWriter writer = PdfWriter.GetInstance(document, stream); writer.SetPdfVersion(PdfWriter.PDF_VERSION_1_7); writer.AddDeveloperExtension( PdfDeveloperExtension.ADOBE_1_7_EXTENSIONLEVEL3 ); // step 3 document.Open(); // step 4 writer.AddJavaScript(File.ReadAllText(JS)); // we prepare a RichMediaAnnotation RichMediaAnnotation richMedia = new RichMediaAnnotation( writer, new Rectangle(36, 560, 561, 760) ); // we embed the swf file PdfFileSpecification fs = PdfFileSpecification.FileEmbedded( writer, RESOURCE, "FestivalCalendar2.swf", null ); // we declare the swf file as an asset PdfIndirectReference asset = richMedia.AddAsset( "FestivalCalendar2.swf", fs ); // we create a configuration RichMediaConfiguration configuration = new RichMediaConfiguration( PdfName.FLASH ); RichMediaInstance instance = new RichMediaInstance(PdfName.FLASH); instance.Asset = asset; configuration.AddInstance(instance); // we add the configuration to the annotation PdfIndirectReference configurationRef = richMedia.AddConfiguration( configuration ); // activation of the rich media RichMediaActivation activation = new RichMediaActivation(); activation.Configuration = configurationRef; richMedia.Activation = activation; // we add the annotation PdfAnnotation richMediaAnnotation = richMedia.CreateAnnotation(); richMediaAnnotation.Flags = PdfAnnotation.FLAGS_PRINT; writer.AddAnnotation(richMediaAnnotation); String[] days = new String[] { "2011-10-12", "2011-10-13", "2011-10-14", "2011-10-15", "2011-10-16", "2011-10-17", "2011-10-18", "2011-10-19" }; for (int i = 0; i < days.Length; i++) { Rectangle rect = new Rectangle(36 + (65 * i), 765, 100 + (65 * i), 780); PushbuttonField button = new PushbuttonField(writer, rect, "button" + i); button.BackgroundColor = new GrayColor(0.75f); button.BorderStyle = PdfBorderDictionary.STYLE_BEVELED; button.TextColor = GrayColor.GRAYBLACK; button.FontSize = 12; button.Text = days[i]; button.Layout = PushbuttonField.LAYOUT_ICON_LEFT_LABEL_RIGHT; button.ScaleIcon = PushbuttonField.SCALE_ICON_ALWAYS; button.ProportionalIcon = true; button.IconHorizontalAdjustment = 0; PdfFormField field = button.Field; RichMediaCommand command = new RichMediaCommand( new PdfString("getDateInfo") ); command.Arguments = new PdfString(days[i]); RichMediaExecuteAction action = new RichMediaExecuteAction( richMediaAnnotation.IndirectReference, command ); field.Action = action; writer.AddAnnotation(field); } TextField text = new TextField( writer, new Rectangle(36, 785, 559, 806), "date" ); text.Options = TextField.READ_ONLY; writer.AddAnnotation(text.GetTextField()); } }