public virtual iText.Kernel.Pdf.PdfPage AddAnnotation(int index, PdfAnnotation annotation, bool tagAnnotation ) { if (GetDocument().IsTagged() && tagAnnotation) { TagTreePointer tagPointer = GetDocument().GetTagStructureContext().GetAutoTaggingPointer(); iText.Kernel.Pdf.PdfPage prevPage = tagPointer.GetCurrentPage(); // TODO what about if current tagging stream is set tagPointer.SetPageForTagging(this).AddAnnotationTag(annotation); if (prevPage != null) { tagPointer.SetPageForTagging(prevPage); } } PdfArray annots = GetAnnots(true); if (index == -1) { annots.Add(annotation.SetPage(this).GetPdfObject()); } else { annots.Add(index, annotation.SetPage(this).GetPdfObject()); } if (annots.GetIndirectReference() == null) { SetModified(); } return(this); }
// --------------------------------------------------------------------------- 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 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"; }