public void SaveComments(string namePDF, int page) { string oldFile = namePDF; string newFile = "temporal.pdf"; PdfReader pdfReader = new PdfReader(oldFile); PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create)); Point[] finalcoor = coorComentario.ToArray(); for (int i = 1; i <= cantComentarios; i++) { string imageURL = directorio + "\\comment" + i + ".jpg"; Stream inputImageStream = new FileStream(imageURL, FileMode.Open, FileAccess.Read, FileShare.Read); iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(inputImageStream); //int xpos = finalcoor[i - 1].X; //int ypos = finalcoor[i - 1].Y; Console.WriteLine("estas son las coordenadas" + finalcoor[i - 1].X + " y " + finalcoor[i - 1].Y); int xpos = 100; int ypos = 100; iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(35 + xpos, 650 - ypos, 35 + xpos + Convert.ToSingle(image.Width * 0.5), 650 - ypos + Convert.ToSingle(image.Height * 0.5)); PdfAnnotation pdfStamp = PdfAnnotation.CreateStamp(pdfStamper.Writer, rect, null, Guid.NewGuid().ToString()); image.SetAbsolutePosition(0, 0); PdfAppearance app = pdfStamper.GetOverContent(1).CreateAppearance(Convert.ToSingle(image.Width * 0.5), Convert.ToSingle(image.Height * 0.5)); image.ScalePercent(50); app.AddImage(image); pdfStamp.SetAppearance(PdfName.N, app); pdfStamp.SetPage(); pdfStamp.Flags = PdfAnnotation.FLAGS_PRINT; pdfStamper.AddAnnotation(pdfStamp, 1); } pdfStamper.Close(); pdfReader.Close(); File.Replace(newFile, oldFile, @"backup.pdf.bac"); Console.WriteLine("Pdf modificado con exito, se ha guardado un backup de la versión anterior "); //axAcroPDF1.src = "C:\\Users\\Denisse\\Desktop\\prototipos\\prot2_ReconocerTexto\\prot2_ReconocerTexto\\bin\\Debug\\ejemploOK.pdf"; }
private void button1_Click(object sender, EventArgs e) { //pdf file string input = "..\\..\\..\\..\\..\\..\\..\\Data\\Sample5.pdf"; //open a pdf document PdfDocument document = new PdfDocument(input); //get the first page PdfPageBase page = document.Pages[0]; //create a rubber stamp annotation PdfRubberStampAnnotation loStamp = new PdfRubberStampAnnotation(new RectangleF(new PointF(0, 0), new SizeF(60, 60))); //create an instance of PdfAppearance PdfAppearance loApprearance = new PdfAppearance(loStamp); PdfImage image = PdfImage.FromFile("..\\..\\..\\..\\..\\..\\..\\Data\\image stamp.jpg"); PdfTemplate template = new PdfTemplate(160, 160); //draw a pdf image into pdf template template.Graphics.DrawImage(image, 0, 0); loApprearance.Normal = template; loStamp.Appearance = loApprearance; //add the rubber stamp annotation into pdf page.AnnotationsWidget.Add(loStamp); string output = "AddImageStamp.pdf"; //save pdf document document.SaveToFile(output); //Launching the Pdf file PDFDocumentViewer(output); }
private void button1_Click(object sender, EventArgs e) { //Load a Pdf document from disk PdfDocument document = new PdfDocument(); document.LoadFromFile("../../../../../../Data/PDFTemplate_N.pdf"); //Get the first page PdfPageBase page = document.Pages[0]; //Set the font and brush PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Regular), true); PdfSolidBrush brush = new PdfSolidBrush(Color.Blue); //Time text String timeString = DateTime.Now.ToString("MM/dd/yy hh:mm:ss tt "); //Create a template and rectangle, draw the string PdfTemplate template = new PdfTemplate(140, 15); RectangleF rect = new RectangleF(new PointF(page.ActualSize.Width - template.Width - 10, page.ActualSize.Height - template.Height - 10), template.Size); template.Graphics.DrawString(timeString, font, brush, new PointF(0, 0)); //Create stamp annoation PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(rect); PdfAppearance apprearance = new PdfAppearance(stamp); apprearance.Normal = template; stamp.Appearance = apprearance; page.AnnotationsWidget.Add(stamp); //Sabe the document string output = "AddDateTimeStamp_result.pdf"; document.SaveToFile(output, FileFormat.PDF); PDFDocumentViewer(output); }
private void button1_Click(object sender, EventArgs e) { PdfDocument doc = new PdfDocument(); doc.LoadFromFile(@"D:\DQ\Spire.Pdf.DEMO\WindowsFormsApp1\WindowsFormsApp1\bin\Debug\FromIMG.pdf"); PdfRubberStampAnnotation loStamp = new PdfRubberStampAnnotation(new RectangleF(new PointF(0, 0), new SizeF(160, 160))); PdfAppearance loApprearance = new PdfAppearance(loStamp); PdfImage image = PdfImage.FromFile(@"D:\\1.png"); PdfTemplate template = new PdfTemplate(160, 160); template.Graphics.DrawImage(image, 0, 0); loApprearance.Normal = template; loStamp.Appearance = loApprearance; foreach (PdfPageBase page in doc.Pages) { page.AnnotationsWidget.Add(loStamp); } string output = "ImageStamp.pdf"; doc.SaveToFile(output); }
// --------------------------------------------------------------------------- /** * 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()); } }
/// <summary> /// Highlights the PDF with Yellow annotation /// </summary> /// <param name="inputFile">File to read from</param> /// <param name="highLightFile">File to write to</param> /// <param name="pageno">Which page no to read</param> /// <param name="textToAnnotate">Texts to annotate</param> private void HighlightPDFAnnotation(string inputFile, string highLightFile, int pageno, params string[] textToAnnotate) { PdfReader reader = new PdfReader(inputFile); using (FileStream fs = new FileStream(highLightFile, FileMode.Create, FileAccess.Write, FileShare.None)) { using (PdfStamper stamper = new PdfStamper(reader, fs)) { MyLocationTextExtractionStrategy strategy = new MyLocationTextExtractionStrategy(); strategy.UndercontentHorizontalScaling = 100; string currentText = PdfTextExtractor.GetTextFromPage(reader, pageno, strategy); for (int i = 0; i < textToAnnotate.Length; i++) { if (string.IsNullOrWhiteSpace(textToAnnotate[i])) { continue; } var lstMatches = strategy.GetTextLocations(textToAnnotate[i].Trim(), StringComparison.CurrentCultureIgnoreCase); if (!this.chkAnnotation.Checked) { lstMatches = lstMatches.Take(1).ToList(); } foreach (iTextSharp.text.Rectangle rectangle in lstMatches) { float[] quadPoints = { rectangle.Left - 3.0f, rectangle.Bottom, rectangle.Right, rectangle.Bottom, rectangle.Left - 3.0f, rectangle.Top + 1.0f, rectangle.Right, rectangle.Top + 1.0f }; PdfAnnotation highlight = PdfAnnotation.CreateMarkup(stamper.Writer , rectangle, null , PdfAnnotation.MARKUP_HIGHLIGHT, quadPoints); highlight.Color = BaseColor.YELLOW; PdfGState state = new PdfGState(); state.BlendMode = new PdfName("Multiply"); PdfAppearance appearance = PdfAppearance.CreateAppearance(stamper.Writer, rectangle.Width, rectangle.Height); appearance.SetGState(state); appearance.Rectangle(0, 0, rectangle.Width, rectangle.Height); appearance.SetColorFill(BaseColor.YELLOW); appearance.Fill(); highlight.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, appearance); //Add the annotation stamper.AddAnnotation(highlight, pageno); } } } } reader.Close(); }
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(); }
public void CreatePDFFile(string strPDFPath, string[] selectedFiles, string password) { try { using (Document document = new Document(cPageSize.GetDocumentWithBackroundColor(cPageSize.A4, new BaseColor(245, 245, 245)), 0f, 0f, 0f, 17f)) { using (FileStream pdf = new FileStream(strPDFPath, FileMode.Create, FileAccess.Write, FileShare.None)) { using (PdfWriter pdfWriter = PdfWriter.GetInstance(document, pdf)) { document.Open(); Font titleFont = FontFactory.GetFont("Verdana", 12f, Font.BOLD); Font listFont = FontFactory.GetFont("Verdana", 9f, Font.NORMAL); Paragraph pMain = new Paragraph(); Chunk chunkTitle = new Chunk("Attached files:", titleFont); chunkTitle.SetUnderline(0.5f, -1.5f); pMain.Add(chunkTitle); for (int i = 0; i < selectedFiles.Length; i++) { string fileName = Path.GetFileName(selectedFiles[i]); var fs = PdfFileSpecification.FileEmbedded(pdfWriter, selectedFiles[i], fileName, null); //fs.AddDescription(fileName, false); //var rect = new Rectangle(10000f,10000f); Rectangle rect = new Rectangle(100, 400, 500, 800); rect.Border = Rectangle.BOX; rect.BorderWidth = 0.5f; rect.BorderColor = new BaseColor(0xFF, 0x00, 0x00); PdfAnnotation annotation = new PdfAnnotation(pdfWriter, rect); annotation.Put(PdfName.NAME, PdfName.ANNOT); annotation.Put(PdfName.SUBTYPE, PdfName.FILEATTACHMENT); annotation.Put(PdfName.CONTENTS, new PdfString(fileName)); annotation.Put(PdfName.FS, fs.Reference); //PdfTemplate tmp = PdfTemplate.CreateTemplate(pdfWriter, document.PageSize.Width, 100); PdfAppearance ap = pdfWriter.DirectContent.CreateAppearance(rect.Width, rect.Height); annotation.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, ap); Chunk linkChunk = new Chunk("(" + (i + 1).ToString() + ") " + fileName, listFont); linkChunk.SetAnnotation(annotation); Phrase phrase = new Phrase(); phrase.Add(new Chunk("\n")); phrase.Add(linkChunk); //document.Add(phrase); //document.Add(new Chunk("\n")); //document.Close(); //pdfWriter.Close(); //pdfWriter.Dispose(); pMain.Add(phrase); } pMain.IndentationLeft = 10f; document.Add(pMain); //pdf.Close(); //pdf.Dispose(); } //document.Dispose(); } } } catch (Exception ex) { } PasswordProtectPDF(strPDFPath, password); }