VintaSoft Imaging .NET SDK 12.4: Documentation for .NET developer
In This Topic
    How to annotate PDF file?
    In This Topic
    Here is C#/VB.NET code that shows how to load PDF document, annotate it, save annotated document to new PDF document:
    private static void AnnotatePdfUsingTemporaryFile()
    {
        // create image collection
        Vintasoft.Imaging.ImageCollection images = new Vintasoft.Imaging.ImageCollection();
        // create annotation controller associated with image collection
        Vintasoft.Imaging.Annotation.AnnotationDataController annotations = 
            new Vintasoft.Imaging.Annotation.AnnotationDataController(images);
        // load PDF file into collection
        images.Add("sourcePdfDocument.pdf");
    
        // get the annotation collection for selected image
        Vintasoft.Imaging.Annotation.AnnotationDataCollection imageAnnotations = annotations[images.Count - 1];
        // create new annotation
        Vintasoft.Imaging.Annotation.RectangleAnnotationData anno = 
            new Vintasoft.Imaging.Annotation.RectangleAnnotationData();
        anno.Size = new System.Drawing.SizeF(300, 300);
        anno.FillBrush = new Vintasoft.Imaging.Annotation.AnnotationSolidBrush(System.Drawing.Color.AliceBlue);
        anno.Location = new System.Drawing.PointF(0, 0);
        // add new annotation into annotation collection
        imageAnnotations.Add(anno);
    
        Vintasoft.Imaging.Codecs.Encoders.PdfEncoder encoder = 
            new Vintasoft.Imaging.Codecs.Encoders.PdfEncoder();
        // specify that annotations must be saved with image collection
        encoder.AnnotationsFormat = Vintasoft.Imaging.AnnotationsFormat.VintasoftBinary;
        // save image collection synchronously to new file
        images.SaveSync("destPdfDocument.pdf", encoder);
    }
    
    Private Shared Sub AnnotatePdfUsingTemporaryFile()
        ' create image collection
        Dim images As New Vintasoft.Imaging.ImageCollection()
        ' create annotation controller associated with image collection
        Dim annotations As New Vintasoft.Imaging.Annotation.AnnotationDataController(images)
        ' load PDF file into collection
        images.Add("sourcePdfDocument.pdf")
    
        ' get the annotation collection for selected image
        Dim imageAnnotations As Vintasoft.Imaging.Annotation.AnnotationDataCollection = annotations(images.Count - 1)
        ' create new annotation
        Dim anno As New Vintasoft.Imaging.Annotation.RectangleAnnotationData()
        anno.Size = New System.Drawing.SizeF(300, 300)
        anno.FillBrush = New Vintasoft.Imaging.Annotation.AnnotationSolidBrush(System.Drawing.Color.AliceBlue)
        anno.Location = New System.Drawing.PointF(0, 0)
        ' add new annotation into annotation collection
        imageAnnotations.Add(anno)
    
        Dim encoder As New Vintasoft.Imaging.Codecs.Encoders.PdfEncoder()
        ' specify that annotations must be saved with image collection
        encoder.AnnotationsFormat = Vintasoft.Imaging.AnnotationsFormat.VintasoftBinary
        ' save image collection synchronously to new file
        images.SaveSync("destPdfDocument.pdf", encoder)
    End Sub
    


    Here is C#/VB.NET code that shows how to load PDF document, annotate it, save changes back to PDF document. Temporary file is not used.
    private static void AnnotatePdfWithoutUsingTemporaryFile()
    {
        // open stream of the PDF document
        System.IO.FileStream stream = new System.IO.FileStream(
            "sourcePdfDocument.pdf", System.IO.FileMode.Open);
    
        // create image collection
        Vintasoft.Imaging.ImageCollection images = new Vintasoft.Imaging.ImageCollection();
        // create annotation controller associated with image collection
        Vintasoft.Imaging.Annotation.AnnotationDataController annotations = 
            new Vintasoft.Imaging.Annotation.AnnotationDataController(images);
        // add images (pages of PDF document) from the stream to the image collection
        images.Add(stream);
    
        // get annotation collection for selected image
        Vintasoft.Imaging.Annotation.AnnotationDataCollection imageAnnotations = annotations[images.Count - 1];
        // create new annotation
        Vintasoft.Imaging.Annotation.RectangleAnnotationData anno = 
            new Vintasoft.Imaging.Annotation.RectangleAnnotationData();
        anno.Size = new System.Drawing.SizeF(300, 300);
        anno.FillBrush = new Vintasoft.Imaging.Annotation.AnnotationSolidBrush(System.Drawing.Color.AliceBlue);
        anno.Location = new System.Drawing.PointF(0, 0);
        // add new annotation into annotation collection
        imageAnnotations.Add(anno);
    
        Vintasoft.Imaging.Codecs.Encoders.PdfEncoder encoder = 
            new Vintasoft.Imaging.Codecs.Encoders.PdfEncoder();
        // specify that annotations must be saved with image collection
        encoder.AnnotationsFormat = Vintasoft.Imaging.AnnotationsFormat.VintasoftBinary;
        // specify that image collection must be saved to the source stream
        encoder.SaveAndSwitchSource = true;
        // save image collection synchronously to the source stream (save changes in PDF document)
        images.SaveSync(stream, encoder);
    
        // close the stream of PDF document
        stream.Close();
    }
    
    Private Shared Sub AnnotatePdfWithoutUsingTemporaryFile()
        ' open stream of the PDF document
        Dim stream As New System.IO.FileStream("sourcePdfDocument.pdf", System.IO.FileMode.Open)
    
        ' create image collection
        Dim images As New Vintasoft.Imaging.ImageCollection()
        ' create annotation controller associated with image collection
        Dim annotations As New Vintasoft.Imaging.Annotation.AnnotationDataController(images)
        ' add images (pages of PDF document) from the stream to the image collection
        images.Add(stream)
    
        ' get annotation collection for selected image
        Dim imageAnnotations As Vintasoft.Imaging.Annotation.AnnotationDataCollection = annotations(images.Count - 1)
        ' create new annotation
        Dim anno As New Vintasoft.Imaging.Annotation.RectangleAnnotationData()
        anno.Size = New System.Drawing.SizeF(300, 300)
        anno.FillBrush = New Vintasoft.Imaging.Annotation.AnnotationSolidBrush(System.Drawing.Color.AliceBlue)
        anno.Location = New System.Drawing.PointF(0, 0)
        ' add new annotation into annotation collection
        imageAnnotations.Add(anno)
    
        Dim encoder As New Vintasoft.Imaging.Codecs.Encoders.PdfEncoder()
        ' specify that annotations must be saved with image collection
        encoder.AnnotationsFormat = Vintasoft.Imaging.AnnotationsFormat.VintasoftBinary
        ' specify that image collection must be saved to the source stream
        encoder.SaveAndSwitchSource = True
        ' save image collection synchronously to the source stream (save changes in PDF document)
        images.SaveSync(stream, encoder)
    
        ' close the stream of PDF document
        stream.Close()
    End Sub