←Select platform

WriteAnnotations Method

Summary
Writes annotations to an existing PDF file.
Syntax
C#
C++/CLI
Java
Python
public void WriteAnnotations( 
   IList<PDFAnnotation> annotations, 
   string destinationFileName 
) 
public void writeAnnotations( 
   java.util.List<PDFAnnotation> annotations, 
   java.lang.String destinationFileName 
); 
public: 
void WriteAnnotations(  
   IList<PDFAnnotation^>^ annotations, 
   String^ destinationFileName 
)  
def WriteAnnotations(self,annotations,destinationFileName): 

Parameters

annotations
A list of PDFAnnotation objects to write. This must not be null and must be a list of 1 or more items. An empty list (count is zero) will throw an exception.

destinationFileName
Name of the destination PDF file to be created. If the value of this parameter is null then the filename set in FileName will be updated.

Remarks

WriteAnnotations does not clear the existing annotations, only adds the new objects. To replace the annotations in an existing file with new ones, use ClearAnnotations first to delete any existing objects and then WriteAnnotations to add the new objects.

Example
C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Pdf; 
using Leadtools.WinForms; 
 
 
public void PDFDocumentPageExample() 
{ 
   string pdfFileName = Path.Combine(LEAD_VARS.ImagesDir, @"Leadtools.pdf"); 
   string txtFileName = Path.Combine(LEAD_VARS.ImagesDir, @"LEAD_pdf.txt"); 
 
   // Open the document 
   using (PDFDocument document = new PDFDocument(pdfFileName)) 
   { 
      // Parse everything and for all pages 
      PDFParsePagesOptions options = PDFParsePagesOptions.All; 
      document.ParsePages(options, 1, -1); 
 
      // Save the results to the text file for examining 
      using (StreamWriter writer = File.CreateText(txtFileName)) 
      { 
         foreach (PDFDocumentPage page in document.Pages) 
         { 
            writer.WriteLine("Page {0}", page.PageNumber); 
 
            IList<PDFObject> objects = page.Objects; 
            writer.WriteLine("Objects: {0}", objects.Count); 
            foreach (PDFObject obj in objects) 
            { 
               writer.WriteLine("  ObjectType: {0}", obj.ObjectType.ToString()); 
               writer.WriteLine("  Bounds: {0}, {1}, {2}, {3}", obj.Bounds.Left, obj.Bounds.Top, obj.Bounds.Right, obj.Bounds.Bottom); 
               WriteTextProperties(writer, obj.TextProperties); 
               writer.WriteLine("  Code: {0}", obj.Code); 
               writer.WriteLine("------"); 
            } 
            writer.WriteLine("---------------------"); 
 
            IList<PDFHyperlink> hyperlinks = page.Hyperlinks; 
            writer.WriteLine("Hyperlinks: {0}", hyperlinks.Count); 
            foreach (PDFHyperlink hyperlink in hyperlinks) 
            { 
               writer.WriteLine("  Hyperlink: {0}", hyperlink.Hyperlink); 
               writer.WriteLine("  Bounds: {0}, {1}, {2}, {3}", hyperlink.Bounds.Left, hyperlink.Bounds.Top, hyperlink.Bounds.Right, hyperlink.Bounds.Bottom); 
               WriteTextProperties(writer, hyperlink.TextProperties); 
            } 
            writer.WriteLine("---------------------"); 
         } 
      } 
   } 
} 
 
private static void WriteTextProperties(StreamWriter writer, PDFTextProperties textProperties) 
{ 
   writer.WriteLine("  TextProperties.FontHeight: {0}", textProperties.FontHeight.ToString()); 
   writer.WriteLine("  TextProperties.FontWidth: {0}", textProperties.FontWidth.ToString()); 
   writer.WriteLine("  TextProperties.FontIndex: {0}", textProperties.FontIndex.ToString()); 
   writer.WriteLine("  TextProperties.IsEndOfWord: {0}", textProperties.IsEndOfWord.ToString()); 
   writer.WriteLine("  TextProperties.IsEndOfLine: {0}", textProperties.IsEndOfLine.ToString()); 
   writer.WriteLine("  TextProperties.Color: {0}", textProperties.Color.ToString()); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 
import java.util.List; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.pdf.*; 
 
 
public void pdfDocumentPagesExample() throws IOException { 
 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   String pdfFileName = combine(LEAD_VARS_IMAGES_DIR, "Leadtools.pdf"); 
   String txtFileName = combine(LEAD_VARS_IMAGES_DIR, "LEAD_pdf.txt"); 
 
   // Open the document 
   PDFDocument document = new PDFDocument(pdfFileName); 
 
   // Parse everything and for all pages 
   PDFParsePagesOptions options = PDFParsePagesOptions.ALL; 
   document.parsePages(options.getValue(), 1, -1); 
 
   // Save the results to the text file for examining 
   OutputStream outStream = new FileOutputStream(txtFileName); 
   OutputStreamWriter writer = new OutputStreamWriter(outStream); 
 
   for (PDFDocumentPage page : document.getPages()) { 
 
      writer.write("Page " + page.getPageNumber() + "\n"); 
 
      List<PDFObject> objects = page.getObjects(); 
      writer.write("Objects: " + objects.size() + "\n"); 
 
      for (PDFObject obj : objects) { 
 
         writer.write(" ObjectType: " + obj.getObjectType() + "\n"); 
         writer.write(" Bounds: " + obj.getBounds().getLeft() + " " + obj.getBounds().getTop() + " " 
               + obj.getBounds().getRight() + " " + obj.getBounds().getBottom() + "\n"); 
         writeTextProperties(writer, obj.getTextProperties()); 
         writer.write(" Code: " + obj.getCode() + "\n"); 
         writer.write("------\n"); 
      } 
      writer.write("---------------------\n"); 
 
      List<PDFHyperlink> hyperlinks = page.getHyperlinks(); 
      writer.write("Hyperlinks: " + hyperlinks.size() + "\n"); 
 
      for (PDFHyperlink hyperlink : hyperlinks) { 
 
         writer.write(" Hyperlink: " + hyperlink.getHyperlink() + "\n"); 
         writer.write(" Bounds: " + hyperlink.getBounds().getLeft() + " " + hyperlink.getBounds().getTop() + " " 
               + hyperlink.getBounds().getRight() + " " + hyperlink.getBounds().getBottom() + "\n"); 
         writeTextProperties(writer, hyperlink.getTextProperties()); 
 
      } 
      writer.write("---------------------\n"); 
   } 
 
   assertTrue("Properties and info outputted", new File(txtFileName).exists()); 
   System.out.println("no errors or exceptions, output printed successfully"); 
   writer.close(); 
   document.dispose(); 
   outStream = null; 
 
} 
 
private void writeTextProperties(OutputStreamWriter writer, PDFTextProperties textProperties) 
      throws IOException { 
 
   writer.write(" TextProperties.FontHeight: " + textProperties.getFontHeight() + "\n"); 
   writer.write(" TextProperties.FontWidth: " + textProperties.getFontWidth() + "\n"); 
   writer.write(" TextProperties.FontIndex: " + textProperties.getFontIndex() + "\n"); 
   writer.write(" TextProperties.IsEndOfWord: " + textProperties.isEndOfWord() + "\n"); 
   writer.write(" TextProperties.IsEndOfLine: " + textProperties.isEndOfLine() + "\n"); 
   writer.write(" TextProperties.Color: " + textProperties.getColor() + "\n"); 
   System.out.println("no errors or exceptions, output printed successfully"); 
 
} 
Requirements

Target Platforms

See Also

Reference

PDFFile Class

PDFFile Members

Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Pdf Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.