Documents for PDF .NET Edition
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.

    GcPdf provides following document properties through GcPdfDocument class:

    Compression
    GcPdf 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
    GcPdf 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
    GcPdf 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
    GcPdf 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
    GcPdf contains OpenAction method which provides a value specifying an action that should be performed when a document is opened.

    Pdf Version
    GcPdf 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
    GcPdf 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 = "GcPdf Document Info Sample";
          doc.DocumentInfo.Author = "John Doe";
          doc.DocumentInfo.Subject = "GcPdfDocument.DocumentInfo";
          doc.DocumentInfo.Producer = "GcPdfWeb Producer";
          doc.DocumentInfo.Creator = "GcPdfWeb 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 = "GrapeCity 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 GcPdfWeb";
          // 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.

    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");
    

    Back to Top

    Save PDF as Image

    PDF pages often contain important information, which can be used for Powerpoint presentations, webpages or word processing documents. In such cases, you might want to make small changes in the PDF pages. With GcPdf library, you can save PDF documents as high quality image files, without turning to online PDF-to-Image converter tools.

    You can save a PDF document as an image by using the below methods:

    Using SaveAs methods

    GcPdf library provides methods to save the entire PDF document or a specific range as an image. The user can provide the file names and call the SaveAsBmp, SaveAsPng, SaveAsGif, SaveAsJpeg, and SaveAsTiff methods of the GcPdfDocument class.

    To save a PDF as an image, follow the steps given below:

    1. Create an instance of GcPdfDocument class.
    2. Load the PDF document using the Load method of GcPdfDocument class.
    3. Call the OutputRange method to define the pages of the document that need to be saved.
    4. Call the SaveAsImageOptions method to save the image in different formats (JPEG, BMP, PNG and GIF).
      C#
      Copy Code
      GcPdfDocument doc = new GcPdfDocument();
      var fs = new FileStream(Path.Combine("Wetlands.pdf"), FileMode.Open, FileAccess.Read);
      doc.Load(fs);  //Load the document
      
      //Create an output range object which defines which pages of the document should be saved
      //If no output range is defined then all the pages of the document will be saved
      OutputRange pageRange = new OutputRange(1, 2);
      
      //Specify the options that should be used while saving the document's pages to image 
      SaveAsImageOptions op = new SaveAsImageOptions();
      SaveAsImageOptions saveOptions = new SaveAsImageOptions()
      {
          BackColor = Color.LightCyan,
          DrawAnnotations = false,
          DrawFormFields = false,
          Resolution = 100
      };
      doc.SaveAsJpeg("WetlandsImage{0}.jpeg", pageRange, saveOptions); //Saves the document pages as images in JPEG format
      doc.SaveAsBmp("WetlandsImage{0}.bmp", pageRange, saveOptions); //Saves the document pages as images in BMP format
      doc.SaveAsGif("WetlandsImage{0}.gif", pageRange, saveOptions); //Saves the document pages as images in GIF format
      doc.SaveAsPng("WetlandsImage{0}.png", pageRange, saveOptions); //Saves the document pages as images in PNG format
      

    Back to Top

    GcPdf also enables a user to save PDF pages as images by simply calling methods of the Page class like SaveAsBmp, SaveAsPng, SaveAsGif, SaveAsTiff and SaveAsJpeg methods.

    To save a PDF page directly as an image, follow the steps given below:

    1. Create an instance of the GcPdfDocument class.
    2. Load the PDF document.
    3. Set BackColor and Resolution properties of the PDF page in SaveAsImageOptions class.
    4. Save the required page of the PDF document by invoking the appropriate method of Page class.
      C#
      Copy Code
      GcPdfDocument doc = new GcPdfDocument();
      var fs = new FileStream(Path.Combine("Wetlands.pdf"), FileMode.Open, FileAccess.Read);
      doc.Load(fs);  //Load the document
      
      //Specify the options that should be used while saving the page to image 
      SaveAsImageOptions saveOptions = new SaveAsImageOptions()
      {
          BackColor = Color.LightCyan,
          DrawAnnotations = false,
          DrawFormFields = false,
          Resolution = 100
      };
      
      //Saves the document's first page as an image to a file in JPEG format
      doc.Pages[0].SaveAsJpeg("WetlandsImage.jpeg",saveOptions);
      
      //Saves the document's first page as an image to a stream in JPEG format
      MemoryStream stream = new MemoryStream();
      doc.Pages[0].SaveAsJpeg(stream,saveOptions);
      

    Back to Top

    Render PDF pages on GcGraphics

    GcPdf allows a user to render specific PDF pages, annotations or a mix of PDF pages to images. The user can draw PDF pages on the image graphics using Draw method of the Page class. If the PDF pages contain annotations, you can draw the PDF page annotations on the graphics object with DrawAnnotations method of the Page class. After drawing on the graphics object, the bitmap image can be saved in any image format by calling the SaveAsPng, SaveAsJpeg or SaveAsTiff methods of the GcBitmap class.

    Note: To implement this method, you will need the license for GcImaging library.

    To save a PDF as an image, follow the steps given below:

    1. Create an instance of GcPdfDocument class.
    2. Load any existing PDF file using the Load method.
    3. Create an instance of GcBitmap class to render the PDF into an image.
    4. Call the Draw method of Page class to render the PDF file pages content with or without annotations and the DrawAnnotations method of Page class to render only the page annotations on the image graphics.
    5. Save the rendered PDF page into the required image format by calling SaveAsPng, SaveAsJpeg or SaveAsTiff methods of the GcBitmap class.

    The code snippet below illustrates how to save a PDF document as an image.

    C#
    Copy Code
    GcPdfDocument doc = new GcPdfDocument();
    doc.Load(new FileStream("SampleDoc.pdf", FileMode.Open, FileAccess.Read));
    
    var page = doc.Pages[0];
    var sz = page.Bounds;
    GcBitmap bmp1 = new GcBitmap((int)(sz.Width + 0.5f), (int)(sz.Height + 0.5f), true);
    using (GcGraphics g = bmp1.CreateGraphics(Color.White))
    {
        //render whole page content (including the annotations)
        page.Draw(g, new RectangleF(0, 0, sz.Width, sz.Height));
    }
    
    GcBitmap bmp2 = new GcBitmap((int)(sz.Width + 0.5f), (int)(sz.Height + 0.5f), true);
    using (GcGraphics g = bmp2.CreateGraphics(Color.White))
    {
        //render page content without annotations
        page.Draw(g, new RectangleF(0, 0, sz.Width, sz.Height), false);
    }
    
    GcBitmap bmp3 = new GcBitmap((int)(sz.Width + 0.5f), (int)(sz.Height + 0.5f), true);
    using (GcGraphics g = bmp3.CreateGraphics(Color.White))
    {
        //render only the page's annotations
        page.DrawAnnotations(g, new RectangleF(0, 0, sz.Width, sz.Height));
    }
    
    /*Once the PDF page has been rendered on GcGraphics, 
     *then the rendered PDF page can be saved as an image in various image formats
     *such as, JPEG, PNG, BMP, TIFF, and GIF.
    */
    bmp1.SaveAsPng("WholePageContents.png");
    bmp2.SaveAsJpeg("PageContentsWithoutAnnotations.jpeg");
    bmp3.SaveAsTiff("PageAnnotations.tiff");
                
    

    Back to Top

    Apart from the above, you can also render text in PDF and save it as an image by enabling TrueType hinting instructions as explained below:

    Support for TrueType Hinting Instructions

    GcPdf supports enabling TrueType hinting instructions while rendering text on GcPdfGraphics and saving it as an image.

    Hinting instructions are included in some TrueType fonts which improve their look by reusing some glyph parts in different glyphs regardless of their font size. TrueType hinting instructions, in GcPdf, supports drawing CJK characters as combinations of other smaller glyph pieces which enhances their final look.

    For fonts which include TrueType glyph hinting instructions, the EnableHinting property of the Font class is set to true, for the others it is set to False. Further, to apply the hinting instructions of the font, EnableFontHinting property of the SaveAsImageOptions class must be set to true (the default value).

    However, if the EnableHinting property is explicitly set to false, then the hinting instructions cannot be enabled.

    As the default value of both the properties is true, hence the hinting instructions are supported for any TrueType font which includes them.

    Disabled Hinting Intructions
    Disabled Hinting Intructions
    Disabled Hinting Intructions for Chinese string


    Enabled Hinting Intructions
    Enabled Hinting Intructions
    Enabled Hinting Intructions for Chinese string

    To enable TrueType hinting instructions for Chinese string:

    1. Load a Chinese Font file.
    2. Initialize the GcPdfGraphics class which represents a graphics object on a PDF page.
    3. Define a Chinese string and configure TextFormat properties.
    4. Draw the Chinese string.
    5. Create an instance of SaveAsImageOptions class and use it to set the EnableFontHinting property to true .
    6. Save the image.
    C#
    Copy Code
     var font = Font.FromFile("kaiu.ttf");
    GcPdfDocument doc = new GcPdfDocument();
    {
    
        GcPdfGraphics g = doc.NewPage().Graphics;
        {
            //Draw the string with hinting instructions set to true
            string s1 = @"入秋空污警報!這幾招遠離PM2.5學起來";
            //Define text formatting attributes
            var tf1 = new TextFormat()
            {
                Font = font,
                FontSize = 20,
    
            };
            g.DrawString(s1, tf1, new PointF(10, 110));
    
            SaveAsImageOptions imgOptions = new SaveAsImageOptions();
            imgOptions.EnableFontHinting = true;
    
            doc.SaveAsPng("ChineseFontwithHintingInstructions1.png", null, imgOptions);
        }
    }
    

    Back to Top

    Apply Redact Annotation

    GcPdf 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 Annotation Types.