Document Solutions for PDF
Features / Document
In This Topic
    Document
    In This Topic

    Apart from content, a PDF file holds some additional information in the form of document properties. These properties define various attributes of document as a whole.

    DsPdf provides following document properties through GcPdfDocument class:

    Compression
    DsPdf allows you to compress or reduce the original file size of the document using CompressionLevel property. The compression level can be set to Fastest, Nocompression or Optimal. The default value is System.IO.Compression.CompressionLevel.Fastest.

    Document Info
    DsPdf contains DocumentInfo property which includes basic information about the document such as title, author, subject etc., that helps in its identification. This data is generated automatically, if not set explicitly.

    Font Embedding
    DsPdf allows you to set the mode of font embedding using FontEmbedMode property. By default, font subsets are embedded in a document. However, you can change this property to embed whole fonts or not to embed fonts.

    Metadata
    DsPdf provides Metadata property which allows you to get the metadata associated with the document. Metadata such as keywords, descriptions are used by the search engines to narrow down the searches. This property provides a number of predefined accessors, such as contributors, creators, copyright, description, etc.

    Actions
    DsPdf contains OpenAction method which provides a value specifying an action that should be performed when a document is opened.

    Pdf Version
    DsPdf allows you to set the PDF version of the selected document using PdfVersion property. Although, the version of the document is determined automatically but it can be set explicitly.

    Viewer Preferences
    DsPdf provides ViewerPreferences property to specify how a document should be displayed on opening in a viewer. This property allows you to set the predominant reading order for text, set the number of copies to be printed when the print dialog is opened for this file, and more preferences.

    DocumentProperties

    Get Document Properties

    To get the document properties from a particular PDF document:

    1. Create an object of GcPdfDocument class.
    2. Load any existing PDF file using the Load method of GcPdfDocument class.
    3. Use the GcPdfDocument object to get the document properties of the PDF file.
      C#
      Copy Code
      static void Main(string[] args)
      {
          // Load an existing PDF using FileStream
          FileStream fileStream = File.OpenRead(args[0].ToString());
          GcPdfDocument doc = new GcPdfDocument();
          doc.Load(fileStream, null);
      
          // Get and Display the property values
          Console.WriteLine("Author of the document is {0}", doc.DocumentInfo.Author);
          Console.WriteLine("Document subject is {0}", doc.DocumentInfo.Subject);
          Console.WriteLine("Documentation title {0}", doc.DocumentInfo.Title);
      }
      
    Back to Top

    Set Document Properties

    To set the document properties while generating a PDF document:

    1. Create an object of GcPdfDocument class.
    2. Set the document properties using the created object.
      C#
      Copy Code
      public void PDFDoc(Stream stream)
      {
          const float In = 150;
          // Create a new PDF document:
          var doc = new GcPdfDocument();
          var page = doc.NewPage();
          var g = page.Graphics;
      
          var tf = new TextFormat() { Font = StandardFonts.Times, FontSize = 12 };
          // Set a PDF Version
          doc.PdfVersion = "1.7";
      
          doc.DocumentInfo.Title = "Document Info Sample";
          doc.DocumentInfo.Author = "John Doe";
          doc.DocumentInfo.Subject = "DsPdfDocument.DocumentInfo";
          doc.DocumentInfo.Producer = "DsPdfWeb Producer";
          doc.DocumentInfo.Creator = "DsPdfWeb Creator";
      
          // Set CreationDate
          doc.DocumentInfo.CreationDate = DateTime.Today;
      
          // Document metadata is available via the GcPdfDocument.Metadata property.
          // It provides a number of predefined accessors, such as:
          doc.Metadata.Contributors.Add("contributor 1");
          doc.Metadata.Contributors.Add("contributor 2");
          doc.Metadata.Copyright = "MESCIUS, Inc.";
          doc.Metadata.Creators.Add("Creator 1");
          doc.Metadata.Creators.Add("Creator 2");
          doc.Metadata.Description = "Sample document description";
          doc.Metadata.Keywords.Add("Keyword1");
          doc.Metadata.Keywords.Add("Keyword2");
          doc.Metadata.Source = "Sourced by DsPdfWeb";
          // Finally, add some text to the document and save the document
          g.DrawString("1. Test string. This is a sample text",tf, new PointF(In, In));
          doc.Save(stream);
      }
      
    Back to Top

    Merge Documents

    To merge two PDF documents into a single document, use MergeWithDocument method of the GcPdfDocument class which appends one PDF document into another.

    Refer to the following example code to append one PDF document to another:

    C#
    Copy Code
    //Create a basic pdf
    GcPdfDocument doc1 = new GcPdfDocument();
    GcPdfGraphics g = doc1.NewPage().Graphics;
    g.DrawString("Hello World!", new TextFormat() { Font = StandardFonts.Times,
                  FontSize = 12 }, new PointF(72, 72));
    
    //Create second pdf
    GcPdfDocument doc2 = new GcPdfDocument();
    GcPdfGraphics g1 = doc2.NewPage().Graphics;
    g1.DrawString("This PDF will be merged with another PDF.", new TextFormat()
    {
        Font = StandardFonts.Times, FontSize = 12
    },
    new PointF(72, 72));
    
    //Merge the two documents
    doc1.MergeWithDocument(doc2, new MergeDocumentOptions());
    
    doc1.Save("MergedDocument.pdf");
    
    Note: DsPdf also provides ClonePage method that can be used to clone a particular page from a specified index in the PDF file and insert it into a specified index of the same PDF file. For more information on cloning a page, see Clone a Page.

    Remove Duplicate Images from Merged Document

    DsPdf also allows you to remove duplicate instances of identical images when merging PDF documents using RemoveDuplicateImages property of MergeDocumentOptions class. Removing the duplicate instances helps reduce the size of the merged PDF document. The default value of RemoveDuplicateImages property is false.

    RemoveDuplicateImages property calls RemoveDuplicateImages method, which scans for duplicate instances during the merging, hence affecting the merge performance. To avoid this, you can merge the documents first and then remove the duplicate instances of the same images using RemoveDuplicateImages method. For more information, see Remove Duplicate Images.

    Refer to the following example code to remove duplicate instances of the same images from a merged PDF document using RemoveDuplicateImages method:

    C#
    Copy Code
    // Initialize GcPdfDocument for first PDF document.
    GcPdfDocument doc1 = new GcPdfDocument();
    
    // Open first PDF document in the file stream.
    FileStream fs1 = File.OpenRead("Invoice_Jan.pdf");
    
    // Load the first PDF document.
    doc1.Load(fs1);
    
    // Initialize GcPdfDocument for second PDF document.
    GcPdfDocument doc2 = new GcPdfDocument();
    
    // Open second PDF document in the file stream.
    FileStream fs2 = File.OpenRead("Invoice_Feb.pdf");
    
    // Load the second PDF document.
    doc2.Load(fs2);
    
    // Merge PDF document into current document.
    doc1.MergeWithDocument(doc2);
    
    // Remove duplicate images.
    doc1.RemoveDuplicateImages();
    
    // Save PDF document.
    doc1.Save("Merged.pdf");
    

    Back to Top

    Apply Redact Annotation

    DsPdf allows you to apply redaction in PDF documents through document.Redact method and remove content from the document. Redaction is performed firstly by adding Redact annotation on the PDF document that marks content for redaction and then using document.Redact method to remove the content from PDF. To see more details about how to apply redact annotations, refer Redact Annotation.