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:
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 = "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); } |
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"); |
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:
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:
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 |
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:
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); |
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.
To save a PDF as an image, follow the steps given below:
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"); |
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:
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 |
![]() |
![]() |
Enabled Hinting Intructions |
![]() |
![]() |
To enable TrueType hinting instructions for Chinese string:
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); } } |
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.