private void CreateOutput(string PDFInput) { try { iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(@PDFInput); FileStream fs = new FileStream(@PDFInput.Replace("Input", "Output"), FileMode.Create, FileAccess.Write); PdfStamper stamp = new PdfStamper(reader, fs); //Loop from here //MessageBox.Show(dataGridView1.RowCount.ToString()); for (int i = 0; i < dataGridView1.RowCount - 1; i++) { string comptype = dataGridView1.Rows[i].Cells[3].Value.ToString(); string complocation = dataGridView1.Rows[i].Cells[1].Value.ToString(); float llx = float.Parse(complocation.Split(' ')[0]); float lly = float.Parse(complocation.Split(' ')[1]); float urx = float.Parse(complocation.Split(' ')[2]); float ury = float.Parse(complocation.Split(' ')[3]); if (comptype == "TE") { int x = Int32.Parse(dataGridView1.Rows[i].Cells[2].Value.ToString()); PdfFormField ff = PdfFormField.CreateTextField(stamp.Writer, false, false, 50); ff.SetWidget(new iTextSharp.text.Rectangle(llx, lly, urx, ury), PdfAnnotation.HIGHLIGHT_INVERT); ff.SetFieldFlags(PdfAnnotation.FLAGS_PRINT); ff.FieldName = dataGridView1.Rows[i].Cells[0].Value.ToString(); stamp.AddAnnotation(ff, x); } else if (comptype == "CB") { int x = Int32.Parse(dataGridView1.Rows[i].Cells[2].Value.ToString()); RadioCheckField fCell = new RadioCheckField(stamp.Writer, new iTextSharp.text.Rectangle(llx, lly, urx, ury), dataGridView1.Rows[i].Cells[0].Value.ToString(), "Yes"); fCell.CheckType = RadioCheckField.TYPE_CROSS; PdfFormField footerCheck = null; footerCheck = fCell.CheckField; stamp.AddAnnotation(footerCheck, x); } else { } } //Loop Ends here stamp.Close(); fs.Close(); reader.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
// --------------------------------------------------------------------------- /** * Add a text field. * @param writer the PdfWriter * @param rect the position of the text field * @param name the name of the text field */ public void AddTextField(PdfWriter writer, Rectangle rect, String name) { PdfFormField field = PdfFormField.CreateTextField( writer, false, false, 0 ); field.FieldName = name; field.SetWidget(rect, PdfAnnotation.HIGHLIGHT_NONE); field.Quadding = PdfFormField.Q_RIGHT; field.SetFieldFlags(PdfFormField.FF_READ_ONLY); writer.AddAnnotation(field); }
private bool CreteTextField(string fieldName, float x, float y, float width, string fieldText, PdfStamper stamper) { var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); PdfFormField field = PdfFormField.CreateTextField(stamper.Writer, false, false, 50); TextField textField = new TextField(stamper.Writer, new Rectangle(x, y, x + width, y + 10), fieldName); textField.Text = fieldText; textField.FontSize = 8; textField.Font = baseFont; //field.SetWidget(new Rectangle(x, y - 10, x + width, y), PdfAnnotation.HIGHLIGHT_INVERT); //field.Flags = PdfAnnotation.FLAGS_PRINT; //field.RichValue = fieldText; //field.FieldName = fieldName; stamper.AddAnnotation(textField.GetTextField(), 1); return(true); }
/// <summary> /// Adds the field to PDF. /// </summary> /// <param name="pdfStamper">The PDF stamper.</param> /// <param name="numberOfPages">The number of pages.</param> /// TODO Edit XML Comment Template for AddFieldToPdf private void AddFieldToPdf(PdfStamper pdfStamper, int numberOfPages) { var parentField = PdfFormField.CreateTextField(pdfStamper.Writer, false, false, 0); parentField.FieldName = Field.Name; var pageNumber = Field.Pages == FieldPages.Last ? numberOfPages : FirstPageNumber; // ReSharper disable once ConvertIfStatementToSwitchStatement if (Field.Pages == FieldPages.First || Field.Pages == FieldPages.Last) { AddFieldToPage(pageNumber, pdfStamper, parentField); } else if (Field.Pages == FieldPages.Custom) { foreach (var customPageNumber in Field.CustomPageNumbers) { AddFieldToPage(customPageNumber, pdfStamper, parentField); } } else { var increment = Field.Pages == FieldPages.All ? EveryPageIncrement : AlternatingPageIncrement; if (Field.Pages == FieldPages.Even) { pageNumber += 1; } for (; pageNumber <= numberOfPages; pageNumber += increment) { AddFieldToPage(pageNumber, pdfStamper, parentField); } } pdfStamper.AddAnnotation(parentField, FirstPageNumber); }