Form Objects

A PDFsharp form is a drawable object composed of graphics, text, images, and other forms. It is similar to the concept of Windows metafiles. Forms are useful when the same group of graphical output is needed in more than one place in a document. Instead of drawing the same shapes several times and thus increasing the size of the output file unnecessarily a form can be used. The content of a form is saved only once in the document and each invocation of the form references the same resources.

Note: This article has nothing to do with the concept of AcroForms which can be filled out.

Forms are represented by the class XForm. XForm is derived from XImage and is drawn with the DrawImage function.

A specialized form is the derived class XPdfForm. This form represents a page of an existing PDF document.

XPdfForm pdfForm = XPdfForm.FromFile("MyFile.pdf");

It is not possible to draw on such a form because the underlying document is opened read-only.

By default the form represents the first page of the document specified during initialization. If the document has more than one page the property PageNumber selects the page the form represents:

pdfForm.PageNumber = 7; // one-based page number

The XPdfForm object represents one page at any one time, but you can change the page number as often as you like. Drawing operations draw always the currently selected page. Also note that the page number is one-based.

A typical use of XPdfForm objects is a document that contains background graphics created with applications like Adobe Illustrator or InDesign. You open that document as an XPdfForm object and draw its pages as the background content of your own document.

In contrast to the class XForm, objects of type XPdfForm are not bound to a document when they are created. You can create an XPdfForm and reuse it for drawing in a series of documents.