Advertisement
vicos

iTextSharp

Jul 6th, 2016
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.41 KB | None | 0 0
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using Ionic.Zip;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using iTextSharp.text;
  13. using iTextSharp.text.pdf;
  14.  
  15. namespace kuujinbo.iTextInAction2Ed.Chapter08 {
  16.   public class TextFields : IWriter, IPdfPCellEvent {
  17. // ===========================================================================
  18.     public const string RESULT1 = "text_fields.pdf";
  19.     public const String RESULT2 = "text_filled.pdf";
  20.     protected int tf;
  21. // ---------------------------------------------------------------------------
  22.     public TextFields() {}
  23.     public TextFields(int tf) {
  24.       this.tf = tf;
  25.     }
  26. // ---------------------------------------------------------------------------
  27.     public void Write(Stream stream) {
  28.       using (ZipFile zip = new ZipFile()) {
  29.         TextFields example = new TextFields(0);
  30.         byte[] pdf = example.CreatePdf();
  31.         zip.AddEntry(RESULT1, pdf);      
  32.         zip.AddEntry(RESULT2, example.ManipulatePdf(pdf));      
  33.         zip.Save(stream);            
  34.       }
  35.     }
  36. // ---------------------------------------------------------------------------
  37.     /**
  38.      * Manipulates a PDF file src with the file dest as result
  39.      * @param src the original PDF
  40.      */
  41.     public byte[] ManipulatePdf(byte[] src) {
  42.       PdfReader reader = new PdfReader(src);
  43.       using (MemoryStream ms = new MemoryStream()) {
  44.         using (PdfStamper stamper = new PdfStamper(reader, ms)) {
  45.           AcroFields form = stamper.AcroFields;
  46.           form.SetField("text_1", "Bruno Lowagie");
  47.           form.SetFieldProperty("text_2", "fflags", 0, null);
  48.           form.SetFieldProperty("text_2", "bordercolor", BaseColor.RED, null);
  49.           form.SetField("text_2", "bruno");
  50.           form.SetFieldProperty("text_3", "clrfflags", TextField.PASSWORD, null);
  51.           form.SetFieldProperty("text_3", "setflags", PdfAnnotation.FLAGS_PRINT, null);
  52.           form.SetField("text_3", "12345678", "xxxxxxxx");
  53.           form.SetFieldProperty("text_4", "textsize", 12f, null);
  54.           form.RegenerateField("text_4");
  55.         }
  56.         return ms.ToArray();
  57.       }
  58.     }  
  59. // ---------------------------------------------------------------------------
  60.     /**
  61.      * Creates a PDF document.
  62.      */
  63.     public byte[] CreatePdf() {
  64.       using (MemoryStream ms = new MemoryStream()) {
  65.         using (Document document = new Document()) {
  66.           PdfWriter writer = PdfWriter.GetInstance(document, ms);
  67.           document.Open();
  68.  
  69.           PdfPCell cell;
  70.           PdfPTable table = new PdfPTable(2);
  71.           table.SetWidths(new int[]{ 1, 2 });
  72.  
  73.           table.AddCell("Name:");
  74.           cell = new PdfPCell();
  75.           cell.CellEvent = new TextFields(1);
  76.           table.AddCell(cell);
  77.          
  78.           table.AddCell("Loginname:");
  79.           cell = new PdfPCell();
  80.           cell.CellEvent = new TextFields(2);
  81.           table.AddCell(cell);
  82.          
  83.           table.AddCell("Password:");
  84.           cell = new PdfPCell();
  85.           cell.CellEvent = new TextFields(3);
  86.           table.AddCell(cell);
  87.          
  88.           table.AddCell("Reason:");
  89.           cell = new PdfPCell();
  90.           cell.CellEvent = new TextFields(4);
  91.           cell.FixedHeight = 60;
  92.           table.AddCell(cell);
  93.  
  94.           document.Add(table);
  95.         }
  96.         return ms.ToArray();
  97.       }
  98.     }
  99. // ---------------------------------------------------------------------------
  100.     public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) {
  101.       PdfWriter writer = canvases[0].PdfWriter;
  102.       TextField text = new TextField(
  103.         writer, rectangle, string.Format("text_{0}", tf)
  104.       );
  105.       text.BackgroundColor = new GrayColor(0.75f);
  106.       switch(tf) {
  107.         case 1:
  108.           text.BorderStyle = PdfBorderDictionary.STYLE_BEVELED;
  109.           text.Alignment = Element.ALIGN_RIGHT;
  110.           text.Text = "Enter your name here...";
  111.           text.FontSize = 0;
  112.           text.Alignment = Element.ALIGN_CENTER;
  113.           text.Options = TextField.REQUIRED;
  114.           break;
  115.         case 2:
  116.           text.MaxCharacterLength = 8;
  117.           text.Options = TextField.COMB;
  118.           text.BorderStyle = PdfBorderDictionary.STYLE_SOLID;
  119.           text.BorderColor = BaseColor.BLUE;
  120.           text.BorderWidth = 2;
  121.           break;
  122.         case 3:
  123.           text.BorderStyle = PdfBorderDictionary.STYLE_INSET;
  124.           text.Options = TextField.PASSWORD;
  125.           text.Visibility = TextField.VISIBLE_BUT_DOES_NOT_PRINT;
  126.           break;
  127.         case 4:
  128.           text.BorderStyle = PdfBorderDictionary.STYLE_DASHED;
  129.           text.BorderColor = BaseColor.RED;
  130.           text.BorderWidth = 2;
  131.           text.FontSize = 8;
  132.           text.Text = "Enter the reason why you want to win "
  133.               + "a free accreditation for the Foobar Film Festival";
  134.           text.Options = TextField.MULTILINE | TextField.REQUIRED;
  135.           break;
  136.       }
  137.       PdfFormField field = text.GetTextField();
  138.       if (tf == 3) {
  139.         field.UserName = "Choose a password";
  140.       }
  141.       writer.AddAnnotation(field);
  142.     }
  143. // ===========================================================================
  144.   }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement