public static byte[] GetPdf() { using (MemoryStream ms = new MemoryStream()) { using (Document document = new Document()) { PdfWriter writer = PdfWriter.GetInstance(document, ms); document.Open(); int i = 0; foreach (var fieldName in FieldNames) { var rectangle = new Rectangle(20, 800 - i * 40, 40, 780 - i * 40); var checkbox = new RadioCheckField( writer, rectangle, fieldName, ON_STATE ); checkbox.CheckType = RadioCheckField.TYPE_CHECK; PdfFormField field = checkbox.CheckField; writer.AddAnnotation(field); ++i; } // add textbox field for sanity-check var textField = new TextField( writer, new Rectangle(20, 800 - i * 40, 400, 780 - i * 40), TEXT_FIELD ); writer.AddAnnotation(textField.GetTextField()); } 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()); } }
public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { PdfWriter writer = canvases[0].PdfWriter; TextField text = new TextField(writer, position, name); writer.AddAnnotation(text.GetTextField()); }
// --------------------------------------------------------------------------- /** * Creates a page with specified font and appearance. * @param appearances sets the need appearances flag if true * @param font adds a substitution font if true */ public byte[] CreatePdf(bool appearances, bool font) { using (MemoryStream ms = new MemoryStream()) { using (Document document = new Document()) { PdfWriter writer = PdfWriter.GetInstance(document, ms); document.Open(); writer.AcroForm.NeedAppearances = appearances; TextField text = new TextField( writer, new Rectangle(36, 806, 559, 780), "description" ); text.Options = TextField.MULTILINE; if (font) { BaseFont unicode = BaseFont.CreateFont( "c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED ); text.ExtensionFont = BaseFont.CreateFont(); List <BaseFont> list = new List <BaseFont>(); list.Add(unicode); text.SubstitutionFonts = list; } text.Text = TEXT; writer.AddAnnotation(text.GetTextField()); } return(ms.ToArray()); } }
public void CreatePdf(String filename) { // step 1: Create a Document Document document = new Document(); // step 2: Create a PdfWriter PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filename, FileMode.Create)); // step 3: Open the Document document.Open(); // step 4: Add content document.Add(new Paragraph("Hello World!")); // create a signature form field PdfFormField field = PdfFormField.CreateSignature(writer); field.FieldName = SIGNAME; // set the widget properties field.SetPage(); field.SetWidget(new Rectangle(72, 732, 144, 780), PdfAnnotation.HIGHLIGHT_INVERT); field.Flags = PdfAnnotation.FLAGS_PRINT; // add it as an annotation writer.AddAnnotation(field); // maybe you want to define an appearance PdfAppearance tp = PdfAppearance.CreateAppearance(writer, 72, 48); tp.SetColorStroke(BaseColor.BLUE); tp.SetColorFill(BaseColor.LIGHT_GRAY); tp.Rectangle(0.5f, 0.5f, 71.5f, 47.5f); tp.FillStroke(); tp.SetColorFill(BaseColor.BLUE); ColumnText.ShowTextAligned(tp, Element.ALIGN_CENTER, new Phrase("SIGN HERE"), 36, 24, 25); field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp); // step 5: Close the Document document.Close(); }
/** * @see com.lowagie.text.pdf.PdfPageEvent#onGenericTag(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document, com.lowagie.text.Rectangle, java.lang.String) */ public override void OnGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) { rect.Bottom = rect.Bottom - 3; PdfFormField field = (PdfFormField)genericChunkFields[text]; if (field == null) { TextField tf = new TextField(writer, new Rectangle(rect.GetLeft(padding), rect.GetBottom(padding), rect.GetRight(padding), rect.GetTop(padding)), text); tf.FontSize = 14; field = tf.GetTextField(); } else { field.Put(PdfName.RECT, new PdfRectangle(rect.GetLeft(padding), rect.GetBottom(padding), rect.GetRight(padding), rect.GetTop(padding))); } if (parent == null) { writer.AddAnnotation(field); } else { parent.AddKid(field); } }
// --------------------------------------------------------------------------- public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) { PdfWriter writer = canvases[0].PdfWriter; TextField text = new TextField( writer, rectangle, string.Format("choice_{0}", cf) ); text.BackgroundColor = new GrayColor(0.75f); switch (cf) { case 1: text.Choices = LANGUAGES; text.ChoiceExports = EXPORTVALUES; text.ChoiceSelection = 2; writer.AddAnnotation(text.GetListField()); break; case 2: text.Choices = LANGUAGES; text.BorderColor = BaseColor.GREEN; text.BorderStyle = PdfBorderDictionary.STYLE_DASHED; text.Options = TextField.MULTISELECT; List <int> selections = new List <int>(); selections.Add(0); selections.Add(2); text.ChoiceSelections = selections; PdfFormField field = text.GetListField(); writer.AddAnnotation(field); break; case 3: text.BorderColor = BaseColor.RED; text.BackgroundColor = BaseColor.GRAY; text.Choices = LANGUAGES; text.ChoiceExports = EXPORTVALUES; text.ChoiceSelection = 4; writer.AddAnnotation(text.GetComboField()); break; case 4: text.Choices = LANGUAGES; text.Options = TextField.EDIT; writer.AddAnnotation(text.GetComboField()); break; } }
public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { PdfWriter writer = canvases[0].PdfWriter; field.SetPage(); field.SetWidget(position, PdfAnnotation.HIGHLIGHT_INVERT); writer.AddAnnotation(field); }
// --------------------------------------------------------------------------- public void Write(Stream stream) { // step 1 using (Document document = new Document()) { // step 2 PdfWriter writer = PdfWriter.GetInstance(document, stream); // step 3 document.Open(); // step 4 Rectangle rect = new Rectangle(100, 400, 500, 800); rect.Border = Rectangle.BOX; rect.BorderWidth = 0.5f; rect.BorderColor = new BaseColor(0xFF, 0x00, 0x00); document.Add(rect); PdfIndirectObject streamObject = null; using (FileStream fs = new FileStream(RESOURCE, FileMode.Open, FileAccess.Read)) { PdfStream stream3D = new PdfStream(fs, writer); stream3D.Put(PdfName.TYPE, new PdfName("3D")); stream3D.Put(PdfName.SUBTYPE, new PdfName("U3D")); stream3D.FlateCompress(); streamObject = writer.AddToBody(stream3D); stream3D.WriteLength(); } PdfDictionary dict3D = new PdfDictionary(); dict3D.Put(PdfName.TYPE, new PdfName("3DView")); dict3D.Put(new PdfName("XN"), new PdfString("Default")); dict3D.Put(new PdfName("IN"), new PdfString("Unnamed")); dict3D.Put(new PdfName("MS"), PdfName.M); dict3D.Put( new PdfName("C2W"), new PdfArray( new float[] { 1, 0, 0, 0, 0, -1, 0, 1, 0, 3, -235, 28 } ) ); dict3D.Put(PdfName.CO, new PdfNumber(235)); PdfIndirectObject dictObject = writer.AddToBody(dict3D); PdfAnnotation annot = new PdfAnnotation(writer, rect); annot.Put(PdfName.CONTENTS, new PdfString("3D Model")); annot.Put(PdfName.SUBTYPE, new PdfName("3D")); annot.Put(PdfName.TYPE, PdfName.ANNOT); annot.Put(new PdfName("3DD"), streamObject.IndirectReference); annot.Put(new PdfName("3DV"), dictObject.IndirectReference); PdfAppearance ap = writer.DirectContent.CreateAppearance( rect.Width, rect.Height ); annot.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, ap); annot.SetPage(); writer.AddAnnotation(annot); } }
// =========================================================================== public void Write(Stream stream) { // step 1 using (Document document = new Document()) { // step 2 PdfWriter writer = PdfWriter.GetInstance(document, stream); // step 3 document.Open(); // step 4 TextField date = new TextField( writer, new Rectangle(36, 806, 126, 780), "date" ); date.BorderColor = new GrayColor(0.2f); PdfFormField datefield = date.GetTextField(); // enter something that resembles a date for this to work; // if PDF reader doesn't recognize as a date, the field is cleared datefield.SetAdditionalActions( PdfName.V, PdfAction.JavaScript("AFDate_FormatEx( 'dd-mm-yyyy' );", writer) ); writer.AddAnnotation(datefield); TextField name = new TextField( writer, new Rectangle(130, 806, 256, 780), "name" ); name.BorderColor = new GrayColor(0.2f); PdfFormField namefield = name.GetTextField(); namefield.SetAdditionalActions( PdfName.FO, PdfAction.JavaScript( "app.alert('name field got the focus');", writer ) ); namefield.SetAdditionalActions( PdfName.BL, PdfAction.JavaScript("app.alert('name lost the focus');", writer) ); namefield.SetAdditionalActions( PdfName.K, PdfAction.JavaScript( "event.change = event.change.toUpperCase();", writer ) ); writer.AddAnnotation(namefield); } }
// --------------------------------------------------------------------------- /** Implementation of the cellLayout method. */ public void CellLayout( PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { writer.AddAnnotation(new PdfAnnotation( writer, position.Left, position.Bottom, position.Right, position.Top, action )); }
public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) { PdfWriter writer = canvases[0].PdfWriter; TextField text = new TextField( writer, rectangle, FieldName ?? ImageAddBase64Image.NAME ); PdfFormField field = text.GetTextField(); writer.AddAnnotation(field); }
private int CreateTableOfContent(PdfTableOfContent toc, PdfWriter writer, Document doc, bool reorderPage) { var numberOfPageBeforeInsertTOC = writer.PageNumber; doc.SetPageSize(PageSize.A4); doc.SetMargins(36, 36, 36, 36); doc.NewPage(); Paragraph p; PdfAction action = new PdfAction(PdfAction.FIRSTPAGE); PdfAnnotation link; if (!String.IsNullOrEmpty(toc.TitreDocument)) { p = new Paragraph(toc.TitreDocument, fontTitrePrincipal); p.Alignment = Element.ALIGN_CENTER; doc.Add(p); // Un espace ? peut mieux faire peut etre... doc.Add(new Paragraph(" ")); } foreach (var content in toc.Contents) { // action = PdfAction.GotoLocalPage("p" + content.RealPage, new PdfDestination(PdfDestination.XYZ, 0f, PageSize.A4.Top, 0f), writer); action = PdfAction.GotoLocalPage("p" + content.RealPage, false); p = new Paragraph(content.Titre, fontText); p.Add(new Chunk(new DottedLineSeparator())); p.Add(new Chunk(content.PageNumber.ToString(), fontText)); float topY = writer.GetVerticalPosition(false); doc.Add(p); float bottomYY = writer.GetVerticalPosition(false); if (bottomYY > topY) // on a changé de page { topY = _docContent.Top; } link = new PdfAnnotation(writer, doc.Left, bottomYY, doc.Right, topY, action); writer.AddAnnotation(link); } var numberOfPage = writer.PageNumber; if (reorderPage) { doc.NewPage(); var reorderArray = Enumerable.Range(numberOfPageBeforeInsertTOC + 1, numberOfPage - numberOfPageBeforeInsertTOC) .Concat(Enumerable.Range(1, numberOfPageBeforeInsertTOC)).ToArray(); writer.ReorderPages(reorderArray); } return(numberOfPage); }
public void AddFields() { PdfContentByte cb = writer.DirectContent; Font _bf = new Font(Font.FontFamily.HELVETICA, 9); PdfFormField _radioGroup = PdfFormField.CreateRadioButton(writer, true); _radioGroup.FieldName = "language_gc"; _radioGroup.PlaceInPage = 0; Rectangle _rect; RadioCheckField _radioG; PdfFormField _radioField1; for (int i = 0; i < LANGUAGES_gc.Length; i++) { _rect = new Rectangle(40, 806 - i * 40, 60, 788 - i * 40); _radioG = new RadioCheckField(writer, _rect, null, LANGUAGES_gc[i]) { BackgroundColor = new GrayColor(0.8f), BorderColor = GrayColor.BLACK, CheckType = RadioCheckField.TYPE_CIRCLE }; _radioField1 = _radioG.RadioField; _radioGroup.AddKid(_radioField1); ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase(LANGUAGES_gc[i], new Font(Font.FontFamily.HELVETICA, 18)), 70, 790 - i * 40, 0); } writer.AddAnnotation(_radioGroup); cb = writer.DirectContent; TextField _text = new TextField(writer, new Rectangle(ConvertX(103), ConvertY(190), ConvertX(182), ConvertY(199)), "g1"); _text.Alignment = Element.ALIGN_CENTER; _text.Options = TextField.MULTILINE; _text.Text = "abc"; PdfFormField textbox = _text.GetTextField(); textbox.PlaceInPage = 1; writer.AddAnnotation(textbox); cb = writer.DirectContentUnder; table.WriteSelectedRows(0, -1, ConvertX(25), ConvertY(133), cb); idTable.WriteSelectedRows(0, -1, ConvertX(25), ConvertY(205), cb); }
// --------------------------------------------------------------------------- /** * 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(); BaseFont bf = BaseFont.CreateFont(); PdfContentByte directcontent = writer.DirectContent; directcontent.BeginText(); directcontent.SetFontAndSize(bf, 12); directcontent.ShowTextAligned(Element.ALIGN_LEFT, "Married?", 36, 770, 0); directcontent.ShowTextAligned(Element.ALIGN_LEFT, "YES", 58, 750, 0); directcontent.ShowTextAligned(Element.ALIGN_LEFT, "NO", 102, 750, 0); directcontent.ShowTextAligned( Element.ALIGN_LEFT, "Name partner?", 36, 730, 0 ); directcontent.EndText(); PdfFormField married = PdfFormField.CreateRadioButton(writer, true); married.FieldName = "married"; married.ValueAsName = "yes"; Rectangle rectYes = new Rectangle(40, 766, 56, 744); RadioCheckField yes = new RadioCheckField(writer, rectYes, null, "yes"); yes.Checked = true; married.AddKid(yes.RadioField); Rectangle rectNo = new Rectangle(84, 766, 100, 744); RadioCheckField no = new RadioCheckField(writer, rectNo, null, "no"); no.Checked = false; married.AddKid(no.RadioField); writer.AddAnnotation(married); Rectangle rect = new Rectangle(40, 710, 200, 726); TextField partner = new TextField(writer, rect, "partner"); partner.BorderColor = GrayColor.GRAYBLACK; partner.BorderWidth = 0.5f; writer.AddAnnotation(partner.GetTextField()); } return(ms.ToArray()); } }
// --------------------------------------------------------------------------- /** * 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); }
// --------------------------------------------------------------------------- /** * 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); } return(ms.ToArray()); } }
// --------------------------------------------------------------------------- public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) { PdfWriter writer = canvases[0].PdfWriter; TextField text = new TextField( writer, rectangle, string.Format("text_{0}", tf) ); text.BackgroundColor = new GrayColor(0.75f); switch (tf) { case 1: text.BorderStyle = PdfBorderDictionary.STYLE_BEVELED; text.Alignment = Element.ALIGN_RIGHT; text.Text = "Enter your name here..."; text.FontSize = 0; text.Alignment = Element.ALIGN_CENTER; text.Options = TextField.REQUIRED; break; case 2: text.MaxCharacterLength = 8; text.Options = TextField.COMB; text.BorderStyle = PdfBorderDictionary.STYLE_SOLID; text.BorderColor = BaseColor.BLUE; text.BorderWidth = 2; break; case 3: text.BorderStyle = PdfBorderDictionary.STYLE_INSET; text.Options = TextField.PASSWORD; text.Visibility = TextField.VISIBLE_BUT_DOES_NOT_PRINT; break; case 4: text.BorderStyle = PdfBorderDictionary.STYLE_DASHED; text.BorderColor = BaseColor.RED; text.BorderWidth = 2; text.FontSize = 8; text.Text = "Enter the reason why you want to win " + "a free accreditation for the Foobar Film Festival"; text.Options = TextField.MULTILINE | TextField.REQUIRED; break; } PdfFormField field = text.GetTextField(); if (tf == 3) { field.UserName = "******"; } writer.AddAnnotation(field); }
// --------------------------------------------------------------------------- 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 // we prepare a RichMediaAnnotation RichMediaAnnotation richMedia = new RichMediaAnnotation( writer, new Rectangle(36, 400, 559, 806) ); // we embed the swf file PdfFileSpecification fs = PdfFileSpecification.FileEmbedded( writer, RESOURCE, "FestivalCalendar1.swf", null ); // we declare the swf file as an asset PdfIndirectReference asset = richMedia.AddAsset( "FestivalCalendar1.swf", fs ); // we create a configuration RichMediaConfiguration configuration = new RichMediaConfiguration( PdfName.FLASH ); RichMediaInstance instance = new RichMediaInstance(PdfName.FLASH); RichMediaParams flashVars = new RichMediaParams(); String vars = "&day=2011-10-13"; flashVars.FlashVars = vars; instance.Params = flashVars; 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); } }
private void VisibleTopChoiceHelper(int topVisible, int expected, String file) { Document document = new Document(); FileStream fos = new FileStream(OUTPUT_FOLDER + file, FileMode.Create); PdfWriter writer = PdfWriter.GetInstance(document, fos); document.Open(); TextField textField = new TextField(writer, new Rectangle(380, 560, 500, 610), "testListBox"); textField.Visibility = BaseField.VISIBLE; textField.Rotation = 0; textField.FontSize = 14f; textField.TextColor = BaseColor.MAGENTA; textField.BorderColor = BaseColor.BLACK; textField.BorderStyle = PdfBorderDictionary.STYLE_SOLID; textField.Font = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.EMBEDDED); textField.BorderWidth = BaseField.BORDER_WIDTH_THIN; writer.RunDirection = PdfWriter.RUN_DIRECTION_LTR; textField.Choices = new String[] { "one", "two", "three" }; textField.ChoiceExports = new String[] { "1", "2", "3" }; //choose the second item textField.ChoiceSelection = 1; //set the first item as the visible one textField.VisibleTopChoice = topVisible; Assert.AreEqual(expected, textField.VisibleTopChoice); PdfFormField field = textField.GetListField(); writer.AddAnnotation(field); document.Close(); // compare CompareTool compareTool = new CompareTool(); String errorMessage = compareTool.CompareByContent(OUTPUT_FOLDER + file, CMP_FOLDER + file, OUTPUT_FOLDER, "diff"); if (errorMessage != null) { Assert.Fail(errorMessage); } }
/** * @see com.lowagie.text.pdf.PdfPCellEvent#cellLayout(com.lowagie.text.pdf.PdfPCell, com.lowagie.text.Rectangle, com.lowagie.text.pdf.PdfContentByte[]) */ public void CellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] canvases) { if (cellField == null || (fieldWriter == null && parent == null)) { throw new ArgumentException("You have used the wrong constructor for this FieldPositioningEvents class."); } cellField.Put(PdfName.RECT, new PdfRectangle(rect.GetLeft(padding), rect.GetBottom(padding), rect.GetRight(padding), rect.GetTop(padding))); if (parent == null) { fieldWriter.AddAnnotation(cellField); } else { parent.AddKid(cellField); } }
/** * @see com.lowagie.text.pdf.PdfPCellEvent#cellLayout(com.lowagie.text.pdf.PdfPCell, com.lowagie.text.Rectangle, com.lowagie.text.pdf.PdfContentByte[]) */ public void CellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] canvases) { if (cellField == null || (fieldWriter == null && parent == null)) { throw new ArgumentException(MessageLocalization.GetComposedMessage("you.have.used.the.wrong.constructor.for.this.fieldpositioningevents.class")); } cellField.Put(PdfName.RECT, new PdfRectangle(rect.GetLeft(padding), rect.GetBottom(padding), rect.GetRight(padding), rect.GetTop(padding))); if (parent == null) { fieldWriter.AddAnnotation(cellField); } else { parent.AddKid(cellField); } }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <param name="writer"></param> /// <param name="pageNumber"></param> internal static void ExtractPdfComments(ref PdfReader reader, ref PdfWriter writer, int pageNumber) { if (reader == null || writer == null || pageNumber < 1) { return; } var annots = reader.GetPageN(pageNumber).GetAsArray(PdfName.ANNOTS); if (annots != null) { PdfDictionary dict; iTextSharp.text.Rectangle bounds; PdfAnnotation toAdd; System.Collections.Generic.IEnumerable <float> rect2; foreach (var item in annots) { dict = PdfReader.GetPdfObject(item) as PdfDictionary; //Evita di estrarre nuovamente gli allegati. if (dict != null && !PdfName.FILEATTACHMENT.Equals(dict.GetAsName(PdfName.SUBTYPE))) { //Non copiare MAI l'annotazione "/Rect" ! if (dict.Keys != null && dict.Keys.Any(x => x.Equals(PdfName.RECT))) { rect2 = dict.GetAsArray(PdfName.RECT).ArrayList .OfType <PdfNumber>() .Select(x => x.FloatValue); bounds = new iTextSharp.text.Rectangle(rect2.ElementAt(0), rect2.ElementAt(1), rect2.ElementAt(2), rect2.ElementAt(3)); toAdd = new PdfAnnotation(writer, bounds); foreach (var chiave in dict.Keys.OfType <PdfName>()) { //Non copiare MAI l'annotazione "/Rect" ! if (!chiave.Equals(PdfName.RECT)) { toAdd.Put(chiave, dict.Get(chiave)); } } // writer.AddAnnotation(toAdd); } } } } }
virtual public void TestGetLink() { string testFile = TestResourceUtils.GetResourceAsTempFile(TEST_RESOURCES_PATH, "getLinkTest1.pdf"); PdfReader currentReader = new PdfReader(testFile); Document document = new Document(PageSize.A4, 0, 0, 0, 0); PdfWriter writer = PdfWriter.GetInstance(document, new MemoryStream()); document.Open(); document.NewPage(); List <PdfAnnotation.PdfImportedLink> links = currentReader.GetLinks(1); PdfAnnotation.PdfImportedLink link = links[0]; writer.AddAnnotation(link.CreateAnnotation(writer)); document.Close(); currentReader.Close(); }
private void Form1_Load(object sender, EventArgs e) { string baseFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "StartFile.pdf"); string secondFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "SecondFile.pdf"); string TestImage = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.jpg"); //Create a very simple PDF with a single form field called "firstName" using (FileStream fs = new FileStream(baseFile, FileMode.Create, FileAccess.Write, FileShare.None)) { using (Document doc = new Document(PageSize.LETTER)) { using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) { doc.Open(); writer.AddAnnotation(new TextField(writer, new iTextSharp.text.Rectangle(0, 0, 100, 100), "firstName").GetTextField()); doc.Close(); } } } //Create a second file "filling out" the form above using (FileStream fs = new FileStream(secondFile, FileMode.Create, FileAccess.Write, FileShare.None)) { using (PdfStamper stamper = new PdfStamper(new PdfReader(baseFile), fs)) { //GetFieldPositions returns an array of field positions if you are using 5.0 or greater //This line does a lot and should really be broken up for null-checking iTextSharp.text.Rectangle rect = stamper.AcroFields.GetFieldPositions("firstName")[0].position; //Create an image iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(TestImage); //Scale it img.ScaleAbsolute(rect.Width, rect.Height); //Position it img.SetAbsolutePosition(rect.Left, rect.Bottom); //Add it to page 1 of the document stamper.GetOverContent(1).AddImage(img); stamper.Close(); } } this.Close(); }
/** * @see com.itextpdf.text.pdf.PdfPageEventHelper#onGenericTag( * com.itextpdf.text.pdf.PdfWriter, * com.itextpdf.text.Document, * com.itextpdf.text.Rectangle, java.lang.String) */ public override void OnGenericTag(PdfWriter writer, Document document, Rectangle rect, string text) { PdfAnnotation annotation = new PdfAnnotation(writer, new Rectangle( rect.Right + 10, rect.Bottom, rect.Right + 30, rect.Top ) ); annotation.Title = "Text annotation"; annotation.Put(PdfName.SUBTYPE, PdfName.TEXT); annotation.Put(PdfName.OPEN, PdfBoolean.PDFFALSE); annotation.Put(PdfName.CONTENTS, new PdfString(string.Format("Icon: {0}", text)) ); annotation.Put(PdfName.NAME, new PdfName(text)); writer.AddAnnotation(annotation); }
// --------------------------------------------------------------------------- /** * Implementation of the cellLayout method. * @see com.itextpdf.text.pdf.PdfPCellEvent#cellLayout( * com.itextpdf.text.pdf.PdfPCell, com.itextpdf.text.Rectangle, * com.itextpdf.text.pdf.PdfContentByte[]) */ public void CellLayout( PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { try { PdfAnnotation annotation = PdfAnnotation.CreateFileAttachment( writer, new Rectangle( position.Left - 20, position.Top - 15, position.Left - 5, position.Top - 5 ), description, fs ); annotation.Name = description; writer.AddAnnotation(annotation); } catch { throw; } }
// --------------------------------------------------------------------------- public void Write(Stream stream) { // step 1 using (Document document = new Document()) { // step 2 PdfWriter writer = PdfWriter.GetInstance(document, stream); // step 3 document.Open(); // step 4 PdfFileSpecification fs = PdfFileSpecification.FileEmbedded( writer, RESOURCE, "foxdog.mpg", null ); writer.AddAnnotation(PdfAnnotation.CreateScreen( writer, new Rectangle(200f, 700f, 400f, 800f), "Fox and Dog", fs, "video/mpeg", true )); } }
// --------------------------------------------------------------------------- public void Write(Stream stream) { // step 1 using (Document document = new Document()) { // step 2 PdfWriter writer = PdfWriter.GetInstance(document, stream); // step 3 document.Open(); // step 4 PdfContentByte cb = writer.DirectContent; BaseFont bf = BaseFont.CreateFont( BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED ); PdfFormField radiogroup = PdfFormField.CreateRadioButton( writer, true ); radiogroup.FieldName = "language"; Rectangle rect = new Rectangle(40, 806, 60, 788); RadioCheckField radio; PdfFormField radiofield; for (int page = 0; page < LANGUAGES.Length;) { radio = new RadioCheckField(writer, rect, null, LANGUAGES[page]); radio.BackgroundColor = new GrayColor(0.8f); radiofield = radio.RadioField; radiofield.PlaceInPage = ++page; radiogroup.AddKid(radiofield); } writer.AddAnnotation(radiogroup); for (int i = 0; i < LANGUAGES.Length; i++) { cb.BeginText(); cb.SetFontAndSize(bf, 18); cb.ShowTextAligned(Element.ALIGN_LEFT, LANGUAGES[i], 70, 790, 0); cb.EndText(); document.NewPage(); } } }
// --------------------------------------------------------------------------- /** * Create a pushbutton for a key * @param writer the PdfWriter * @param rect the position of the key * @param btn the label for the key * @param script the script to be executed when the button is pushed */ public void AddPushButton(PdfWriter writer, Rectangle rect, String btn, String script) { float w = rect.Width; float h = rect.Height; PdfFormField pushbutton = PdfFormField.CreatePushButton(writer); pushbutton.FieldName = "btn_" + btn; pushbutton.SetWidget(rect, PdfAnnotation.HIGHLIGHT_PUSH); PdfContentByte cb = writer.DirectContent; pushbutton.SetAppearance( PdfAnnotation.APPEARANCE_NORMAL, CreateAppearance(cb, btn, BaseColor.GRAY, w, h) ); pushbutton.SetAppearance( PdfAnnotation.APPEARANCE_ROLLOVER, CreateAppearance(cb, btn, BaseColor.RED, w, h) ); pushbutton.SetAppearance( PdfAnnotation.APPEARANCE_DOWN, CreateAppearance(cb, btn, BaseColor.BLUE, w, h) ); pushbutton.SetAdditionalActions( PdfName.U, PdfAction.JavaScript(script, writer) ); pushbutton.SetAdditionalActions( PdfName.E, PdfAction.JavaScript( "this.showMove('" + btn + "');", writer ) ); pushbutton.SetAdditionalActions( PdfName.X, PdfAction.JavaScript( "this.showMove(' ');", writer ) ); writer.AddAnnotation(pushbutton); }