[]
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.
To get the document properties from a particular PDF document:
Create an object of GcPdfDocument class.
Load any existing PDF file using the Load method of GcPdfDocument class.
Use the GcPdfDocument object to get the document properties of the PDF file.
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);
}
To set the document properties while generating a PDF document:
Create an object of GcPdfDocument class.
Set the document properties using the created object.
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);
}
GcPdf allows you to reduce the size of a document efficiently using RemoveDuplicateImages method of GcPdfDocument class. This method eliminates redundant instances of identical images internally within the document, retaining only a single instance across multiple locations, hence reducing the size of the document.
Refer to the following example code demonstrating how to optimize the file size of a document using RemoveDuplicateImages method:
// Initialize GcPdfDocument.
GcPdfDocument doc = new GcPdfDocument();
// Open PDF document in the file stream.
FileStream fs = File.OpenRead("Invoice.pdf");
// Load the PDF document.
doc.Load(fs);
// Remove duplicate images.
doc.RemoveDuplicateImages();
// Save PDF document.
doc.Save("RemovedDuplicateImages.pdf");
type=info
Note: You can also optimize the file size of a merged PDF document. See Remove Duplicate Images from Merged Document.
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:
//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");
type=info
Note: GcPdf 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.
GcPdf 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 Optimize Document Size.
Refer to the following example code to remove duplicate instances of the same images from a merged PDF document using RemoveDuplicateImages method:
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.