public void DrawCheckBoxAppearences(PdfFormField field, string value, float llx, float lly, float urx, float ury) { BaseFont font = BaseFont.CreateFont(BaseFont.ZAPFDINGBATS, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); float size = (ury - lly); PdfContentByte cb = writer.DirectContent; PdfAppearance tpOn = cb.CreateAppearance(urx - llx, ury - lly); PdfAppearance tp2 = (PdfAppearance)tpOn.Duplicate; tp2.SetFontAndSize(font, size); tp2.ResetRGBColorFill(); field.DefaultAppearanceString = tp2; tpOn.DrawTextField(0f, 0f, urx - llx, ury - lly); tpOn.SaveState(); tpOn.ResetRGBColorFill(); tpOn.BeginText(); tpOn.SetFontAndSize(font, size); tpOn.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "4", (urx - llx) / 2, (ury - lly) / 2 - (size * 0.3f), 0); tpOn.EndText(); tpOn.RestoreState(); field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, value, tpOn); PdfAppearance tpOff = cb.CreateAppearance(urx - llx, ury - lly); tpOff.DrawTextField(0f, 0f, urx - llx, ury - lly); field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", tpOff); }
public void DrawRadioAppearences(PdfFormField field, string value, float llx, float lly, float urx, float ury) { PdfContentByte cb = writer.DirectContent; PdfAppearance tpOn = cb.CreateAppearance(urx - llx, ury - lly); tpOn.DrawRadioField(0f, 0f, urx - llx, ury - lly, true); field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, value, tpOn); PdfAppearance tpOff = cb.CreateAppearance(urx - llx, ury - lly); tpOff.DrawRadioField(0f, 0f, urx - llx, ury - lly, false); field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", tpOff); }
public void DrawSingleLineOfText(PdfFormField field, string text, BaseFont font, float fontSize, float llx, float lly, float urx, float ury) { PdfContentByte cb = writer.DirectContent; PdfAppearance tp = cb.CreateAppearance(urx - llx, ury - lly); PdfAppearance tp2 = (PdfAppearance)tp.Duplicate; tp2.SetFontAndSize(font, fontSize); tp2.ResetRGBColorFill(); field.DefaultAppearanceString = tp2; tp.DrawTextField(0f, 0f, urx - llx, ury - lly); tp.BeginVariableText(); tp.SaveState(); tp.Rectangle(3f, 3f, urx - llx - 6f, ury - lly - 6f); tp.Clip(); tp.NewPath(); tp.BeginText(); tp.SetFontAndSize(font, fontSize); tp.ResetRGBColorFill(); tp.SetTextMatrix(4, (ury - lly) / 2 - (fontSize * 0.3f)); tp.ShowText(text); tp.EndText(); tp.RestoreState(); tp.EndVariableText(); field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp); }
public void DrawMultiLineOfText(PdfFormField field, string text, BaseFont font, float fontSize, float llx, float lly, float urx, float ury) { PdfContentByte cb = writer.DirectContent; PdfAppearance tp = cb.CreateAppearance(urx - llx, ury - lly); PdfAppearance tp2 = (PdfAppearance)tp.Duplicate; tp2.SetFontAndSize(font, fontSize); tp2.ResetRGBColorFill(); field.DefaultAppearanceString = tp2; tp.DrawTextField(0f, 0f, urx - llx, ury - lly); tp.BeginVariableText(); tp.SaveState(); tp.Rectangle(3f, 3f, urx - llx - 6f, ury - lly - 6f); tp.Clip(); tp.NewPath(); tp.BeginText(); tp.SetFontAndSize(font, fontSize); tp.ResetRGBColorFill(); tp.SetTextMatrix(4, 5); System.util.StringTokenizer tokenizer = new System.util.StringTokenizer(text, "\n"); float yPos = ury - lly; while (tokenizer.HasMoreTokens()) { yPos -= fontSize * 1.2f; tp.ShowTextAligned(PdfContentByte.ALIGN_LEFT, tokenizer.NextToken(), 3, yPos, 0); } tp.EndText(); tp.RestoreState(); tp.EndVariableText(); field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp); }
public void DrawButton(PdfFormField button, string caption, BaseFont font, float fontSize, float llx, float lly, float urx, float ury) { PdfContentByte cb = writer.DirectContent; PdfAppearance pa = cb.CreateAppearance(urx - llx, ury - lly); pa.DrawButton(0f, 0f, urx - llx, ury - lly, caption, font, fontSize); button.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, pa); }
public PdfFormField AddMap(string name, string value, string url, PdfContentByte appearance, float llx, float lly, float urx, float ury) { PdfAction action = PdfAction.CreateSubmitForm(url, null, PdfAction.SUBMIT_HTML_FORMAT | PdfAction.SUBMIT_COORDINATES); PdfFormField button = new PdfFormField(writer, llx, lly, urx, ury, action); SetButtonParams(button, PdfFormField.FF_PUSHBUTTON, name, null); PdfContentByte cb = writer.DirectContent; PdfAppearance pa = cb.CreateAppearance(urx - llx, ury - lly); pa.Add(appearance); button.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, pa); AddFormField(button); return(button); }
// --------------------------------------------------------------------------- /** * Creates an appearance for a key * @param cb the canvas * @param btn the label for the key * @param color the color of the key * @param w the width * @param h the height * @return an appearance */ public PdfAppearance CreateAppearance( PdfContentByte cb, String btn, BaseColor color, float w, float h) { PdfAppearance app = cb.CreateAppearance(w, h); app.SetColorFill(color); app.Rectangle(2, 2, w - 4, h - 4); app.Fill(); app.BeginText(); app.SetColorFill(BaseColor.BLACK); app.SetFontAndSize(bf, h / 2); app.ShowTextAligned(Element.ALIGN_CENTER, btn, w / 2, h / 4, 0); app.EndText(); return(app); }
/** * @param field * @param llx * @param lly * @param urx * @param ury */ public void DrawSignatureAppearences(PdfFormField field, float llx, float lly, float urx, float ury) { PdfContentByte cb = writer.DirectContent; PdfAppearance tp = cb.CreateAppearance(urx - llx, ury - lly); tp.SetGrayFill(1.0f); tp.Rectangle(0, 0, urx - llx, ury - lly); tp.Fill(); tp.SetGrayStroke(0); tp.SetLineWidth(1); tp.Rectangle(0.5f, 0.5f, urx - llx - 0.5f, ury - lly - 0.5f); tp.ClosePathStroke(); tp.SaveState(); tp.Rectangle(1, 1, urx - llx - 2, ury - lly - 2); tp.Clip(); tp.NewPath(); tp.RestoreState(); field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp); }
// --------------------------------------------------------------------------- /** * 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()); } }
private void Button_Click(object sender, System.Windows.RoutedEventArgs e) { //TODO: THIS WILL BE DRIVEN BY A FORM FILLED BY THE USER TO ADD ITEMS FOR THE TEAM TO CHECK String[] LANGUAGES_gc = { "Scac", "Master Bill", "Comm Inv No", "Test Item", "Another" }; System.IO.FileStream fs = new FileStream(@"C:\Users\abuchanan.LII01\Desktop\First PDF document.pdf", FileMode.Create); // Create an instance of the document class which represents the PDF document itself. Document document = new Document(PageSize.A4, 25, 25, 30, 30); // Create an instance to the PDF file by creating an instance of the PDF // Writer class using the document and the filestrem in the constructor. PdfWriter writer = PdfWriter.GetInstance(document, fs); // Add meta information to the document document.AddAuthor("Anthony Buchanan"); document.AddKeywords("USeTeam EDI - Operations Team Testing Signoff"); document.AddSubject("Document subject - Describing the steps creating a PDF document"); document.AddTitle("The document title - PDF creation using iTextSharp"); // Open the document to enable you to write to the document document.Open(); PdfContentByte cb = writer.DirectContent; //TextField _text = new TextField(writer, new Rectangle(40, 806, 160, 788), "g1"); //_text.Alignment = Element.ALIGN_LEFT; //_text.Options = TextField.MULTILINE; //_text.Text = "abc"; //writer.AddAnnotation(_text.GetTextField()); Rectangle _rect; PdfFormField _Field1; PdfFormField _Field2; PdfAppearance[] onOff = new PdfAppearance[3]; onOff[0] = cb.CreateAppearance(20, 20); onOff[0].Rectangle(1, 1, 18, 18); onOff[0].Stroke(); //Pass Checkbox onOff[1] = cb.CreateAppearance(20, 20); onOff[1].SetRGBColorFill(0, 252, 0); 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(); //Fail Checkbox onOff[2] = cb.CreateAppearance(20, 20); onOff[2].SetRGBColorFill(252, 0, 0); onOff[2].Rectangle(1, 1, 18, 18); onOff[2].FillStroke(); onOff[2].MoveTo(1, 1); onOff[2].LineTo(19, 19); onOff[2].MoveTo(1, 19); onOff[2].LineTo(19, 1); onOff[2].Stroke(); RadioCheckField _checkbox1; RadioCheckField _checkbox2; document.Add(new Paragraph("Pass Fail Field Name Comments")); //Header Line cb.MoveTo(20f, 807f); cb.LineTo(560f, 807f); cb.ClosePath(); cb.Stroke(); cb.MoveTo(20f, 791f); cb.LineTo(560f, 791f); cb.ClosePath(); cb.Stroke(); for (int i = 0; i < LANGUAGES_gc.Length; i++) { _rect = new Rectangle(30, 789 - i * 20, 50, 771 - i * 20); _checkbox1 = new RadioCheckField(writer, _rect, LANGUAGES_gc[i], "on"); _Field1 = _checkbox1.CheckField; _Field1.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", onOff[0]); _Field1.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "On", onOff[1]); writer.AddAnnotation(_Field1); _rect = new Rectangle(60, 789 - i * 20, 80, 771 - i * 20); _checkbox2 = new RadioCheckField(writer, _rect, LANGUAGES_gc[i], "on"); _Field2 = _checkbox2.CheckField; _Field2.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", onOff[0]); _Field2.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "On", onOff[2]); writer.AddAnnotation(_Field2); ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase(LANGUAGES_gc[i], new Font(Font.FontFamily.HELVETICA, 12)), 90, 775 - i * 20, 0); TextField _text = new TextField(writer, new Rectangle(260, 789 - i * 20, 560, 771 - i * 20), "NoteBox"); _text.Alignment = Element.ALIGN_LEFT; _text.Text = "Add Notes here."; _text.SetExtraMargin(5, 0); writer.AddAnnotation(_text.GetTextField()); cb.MoveTo(20f, 770 - i * 20); cb.LineTo(560f, 770 - i * 20); cb.ClosePath(); cb.Stroke(); } //TODO: ADD LOGIC IN CASE THERE ARE NO ITEMS ENTERED. //Column Lines cb.MoveTo(20f, 807f); cb.LineTo(20f, 807 - (((LANGUAGES_gc.Length + 1) * 20) - 3)); cb.ClosePath(); cb.Stroke(); cb.MoveTo(55f, 807f); cb.LineTo(55f, 807 - (((LANGUAGES_gc.Length + 1) * 20) - 3)); cb.ClosePath(); cb.Stroke(); cb.MoveTo(85f, 807f); cb.LineTo(85f, 807 - (((LANGUAGES_gc.Length + 1) * 20) - 3)); cb.ClosePath(); cb.Stroke(); cb.MoveTo(259f, 807f); cb.LineTo(259f, 807 - (((LANGUAGES_gc.Length + 1) * 20) - 3)); cb.ClosePath(); cb.Stroke(); cb.MoveTo(560f, 807f); cb.LineTo(560f, 807 - (((LANGUAGES_gc.Length + 1) * 20) - 3)); cb.ClosePath(); cb.Stroke(); cb = writer.DirectContent; document.Close(); writer.Close(); fs.Close(); }