Document Class |
This class represents a PDF document in the PDF Creator framework. A Document object can be created from an existing PDF document or an empty document can be created using the default constructor of the Document class. When creating a Document object from an existing PDF document, the existing PDF document is passed to the Document class constructor as the path of the .pdf file on disk or as a stream containing the PDF document binary image. In either case the resulted Document object can be further modified using the PDF Creator API. There are also Document constructors accepting a password when creating a Document object form a password protected PDF document. Here are the Document class constructors:
public Document(); public Document(Stream pdfStream); public Document(string pdfFileName); public Document(Stream pdfStream, string pdfPassword); public Document(string pdfFileName, string pdfPassword);
The Document class offers access to the PDF pages collection, PDF fonts collection, bookmarks, security features, templates and viewer preferences. For example you can add a new page to the document pages collection using the AddPage method, adding a new font to the fonts collection using the AddFont(Font) method or adding a new template using the AddTemplate(RectangleF) method.
Below is a sample code to show a typical usage scenario of the PDF Creator API to instantiate a Document object and set various properties of the Document object. The code is taken from the HTML to PDF converter sample and it creates a Document object, adds a PDF page to the document and then adds a HTML to PDF converter element to the page. Finally the page is sent as response to the client browser.
protected void btnConvert_Click(object sender, EventArgs e) { //create a PDF document Document document = new Document(); //optional settings for the PDF document like margins,compression level, //security options, viewer preferences, document information, etc document.CompressionLevel = CompressionLevel.NormalCompression; document.Margins = new Margins(10, 10, 0, 0); document.Security.CanPrint = true; document.Security.UserPassword = ""; document.DocumentInformation.Author = "HTML to PDF Converter"; document.ViewerPreferences.HideToolbar = false; //Add a first page to the document. PdfPage page = document.Pages.AddNewPage(PageSize.A4, new Margins(10, 10, 0, 0), PageOrientation.Portrait); // add a font to the document that can be used for the text elements PdfFont font = document.Fonts.Add(new System.Drawing.Font( new System.Drawing.FontFamily("Times New Roman"), 10, System.Drawing.GraphicsUnit.Point)); // convert HTML to PDF HtmlToPdfElement htmlToPdfElement = new HtmlToPdfElement(xLocation, yLocation, width, height, urlToConvert); //optional settings for the HTML to PDF converter htmlToPdfElement.FitWidth = cbFitWidth.Checked; htmlToPdfElement.EmbedFonts = cbEmbedFonts.Checked; htmlToPdfElement.LiveUrlsEnabled = cbLiveLinks.Checked; htmlToPdfElement.RightToLeftEnabled = cbRTL.Checked; htmlToPdfElement.ScriptsEnabled = cbClientScripts.Checked; htmlToPdfElement.ActiveXEnabled = cbActiveXEnabled.Checked; // add the HTML to PDF converter element to page addResult = page.AddElement(htmlToPdfElement); // send the generated PDF document to client browser document.Save(Response, false, "HtmlConvert.pdf"); }
By default, the PDF Creator can generate PDF documents in conformance with PDF 1.4 standard. This standard is accepted by Adobe Reader 5.0 and the later versions of the Adobe Reader.
Using the Document class constructor with a parameter of PdfStandardSubset type, the library can be instructed to generate PDF documents in conformance with PDF/A, PDF/X and PDF/SiqQ and standards. These standards impose additional restrictions to the generate document.
The PDF/A-1b standard (ISO 19005-1), used for long-term archiving of PDF documents, requires that all the true type fonts used by the document to be embedded in the document, the http links are disabled, the document does not use transparent objects, the document information properties are disabled.
PDF/X-1a:2003 standard (ISO 15930-4), used to facilitate graphics exchange, requires that all the true type fonts used by the document to be embedded in the document, the http links are disabled, all the graphics are in CMYK color space, the document does not use transparent objects.
PDF/SiqQ is a standard used by Adobe to ensure that a digitally signed document does not contain items that could alter its appearance when viewed in different environments. A PDF/SiqQ compliant document requires that all the true type fonts used by the document to be embedded in the document and the http links to be disabled.
By default, the converter uses the RGB color space to draw graphics and images. Using the Document class constructor with a parameter of ColorSpace type, it is possible to instruct the converter to use the CMYK or Grayscale color space when the PDF document is rendered.