Welcome
Guest
•
Login
PDFsharp and MigraDoc Wiki
Navigation
¶
Main Page
Random Page
Create a new Page
All Pages
Categories
Navigation Paths
File Management
Create Account
Quick Search
»
Visit the new
Website for PDFsharp & MigraDoc Foundation 6.0 for .NET 6
and find information about the new version for Windows, Linux, and other platforms.
Back
Form Objects
Modified on 2009/08/06 10:30
by
Thomas Hövel
Categorized as
Articles
,
PDFsharp Articles
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. {s:note}This article has nothing to do with the concept of AcroForms which can be filled out. Forms are represented by the class {s:className|XForm}. {s:className|XForm} is derived from {s:className|XImage} and is drawn with the {s:funcName|DrawImage} function. ==Creating a Form== The following code creates a new form: {s:beginCsharp} XForm form = new XForm(document, XUnit.FromMillimeter(70), XUnit.FromMillimeter(55)); {s:endCsharp} To make a form drawable on a particular document the form must be bound to it on its creation. This is why the first parameter is required. An XGraphics object for drawing the content of the form is created with the following code: {s:beginCsharp} XGraphics formGfx = XGraphics.FromForm(form); {s:endCsharp} With this graphics object the content of the form is drawn as usual: {s:beginCsharp} formGfx.DrawEllipse(XBrushes.Red, 20, 20, 50, 50); … {s:endCsharp} When finished with the drawing of the content the {s:className|XGraphics} object should be closed. {s:beginCsharp} formGfx.Dispose(); {s:endCsharp} Disposing is not strictly required. If you do not dispose the graphics object it is automatically disposed when the form gets used the first time for drawing. Now the form is ready to be used for drawing in a document. {s:beginCsharp} PdfPage page = document.AddPage(); XGraphics gfx = XGraphics.FromPdfPage(page); gfx.DrawImage(form, 20, 50); // Draw form like an image {s:endCsharp} Note that because the form was bound to a document during its creation you can draw the form only on pages of this document. ==PDF Form Objects== A specialized form is the derived class {s:className|XPdfForm}. This form represents a page of an existing PDF document. {s:beginCsharp} XPdfForm pdfForm = XPdfForm.FromFile("MyFile.pdf"); {s:endCsharp} 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: {s:beginCsharp} pdfForm.PageNumber = 7; // one-based page number {s:endCsharp} The {s:className|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 {s:className|XPdfForm} objects is a document that contains background graphics created with applications like Adobe Illustrator or InDesign. You open that document as an {s:className|XPdfForm} object and draw its pages as the background content of your own document. In contrast to the class {s:className|XForm}, objects of type {s:className|XPdfForm} are not bound to a document when they are created. You can create an {s:className|XPdfForm} and reuse it for drawing in a series of documents. ==Samples== The [XForms-sample|XForms sample] illustrates the use of the {s:className|XForm} and {s:className|XPdfForm} classes.
Meta Keywords:
Meta Description:
Change Comment:
Visit the new
Website for PDFsharp & MigraDoc Foundation 6.0 for .NET 6
and find information about the new version for Windows, Linux, and other platforms.
Miscellaneous
Home
PDFsharp
FAQ
Samples
Articles
MigraDoc
FAQ
Samples
Articles
ScrewTurn Wiki version 3.0.5.600. Some of the icons created by
FamFamFam
.
Impressum - Privacy Policy, Data Protection Declaration, Legal Notice