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:
To get the document properties from a particular PDF document:
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); } |
To set the document properties while generating a PDF document:
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); } |
DsPdf 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:
C# |
Copy Code
|
---|---|
// 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"); |
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"); |
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 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:
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"); |
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 Annotation Types.