VintaSoft Imaging .NET SDK 15.2: Documentation for .NET developer
Vintasoft.Imaging.Pdf.Tree Namespace / PdfFormXObjectResource Class / PdfFormXObjectResource Constructors / PdfFormXObjectResource Constructor(PdfDocument,RectangleF)
Syntax Example Requirements SeeAlso
In This Topic
PdfFormXObjectResource Constructor(PdfDocument,RectangleF)
In This Topic
Initializes a new instance of the PdfFormXObjectResource class.
Syntax
'Declaration

Public Function New( _
ByVal document
The parent PDF document.
As Vintasoft.Imaging.Pdf.PdfDocument, _
ByVal boundingBox
The bounding box of new form resource.
As System.Drawing.RectangleF _
)

Parameters

document
The parent PDF document.
boundingBox
The bounding box of new form resource.
Example

Here is an example that shows how to create an empty Form XObject, draw a text on the form and then draw the Form Xobject itself in the right lower corner of each page of PDF document:


''' <summary>
''' Creates an empty Form XObject, draws a text on it
''' and then draws the Form XObject itself in the right lower corner
''' of each page of PDF document.
''' </summary>
''' <param name="pdfFilename">The filename of PDF document.</param>
''' <remarks>
''' This method doesn't consider the rotation of the page.
''' </remarks>
Public Shared Sub DrawingOnTemplate(pdfFilename As String)
    ' open PDF document
    Using pdfDocument As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename)
        ' bounding box of the form
        Dim boundingBox As New System.Drawing.RectangleF(0, 0, 100, 50)
        ' create a form with specified bounding box
        Dim form As New Vintasoft.Imaging.Pdf.Tree.PdfFormXObjectResource(pdfDocument, boundingBox)
        ' open PdfGraphics from the form
        Using formGraphics As Vintasoft.Imaging.Pdf.Drawing.PdfGraphics = Vintasoft.Imaging.Pdf.Drawing.PdfGraphics.FromForm(form)
            ' text of the string
            Dim text As String = "Template"
            ' get standard font
            Dim font As Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont = pdfDocument.FontManager.GetStandardFont(Vintasoft.Imaging.Pdf.Tree.Fonts.PdfStandardFontType.TimesRoman)
            ' rectangle of text string on the form
            Dim textRect As New System.Drawing.RectangleF(10, 10, 80, 30)
            ' get font size for specified rectangle
            Dim fontSize As Single = formGraphics.MeasureFontSize(text, font, textRect.Width, textRect.Height)
            ' create brush for the text string
            Dim brush As New Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.Red)
            ' draw the text string on the form
            formGraphics.DrawString(text, font, fontSize, brush, textRect, Vintasoft.Imaging.Pdf.Drawing.PdfContentAlignment.Center, _
                False)
        End Using

        ' get original dimensions of the form
        Dim formWidth As Single = form.BoundingBox.Width
        Dim formHeight As Single = form.BoundingBox.Height

        ' for each page of the PDF document
        For Each page As Vintasoft.Imaging.Pdf.Tree.PdfPage In pdfDocument.Pages
            ' open PdfGraphics from the page
            Using pageGraphics As Vintasoft.Imaging.Pdf.Drawing.PdfGraphics = Vintasoft.Imaging.Pdf.Drawing.PdfGraphics.FromPage(page)
                ' get the crop box of the page
                Dim cropBox As System.Drawing.RectangleF = page.CropBox
                ' width of form area on the page
                Dim formAreaWidth As Single = cropBox.Width / 3
                ' height of form area on the page
                Dim formAreaHeight As Single = cropBox.Height / 3

                ' calculate ratios of form area dimensions to form dimensions
                Dim horizontalRatio As Single = formAreaWidth / formWidth
                Dim verticalRatio As Single = formAreaHeight / formHeight
                ' destination rectangle
                Dim rect As System.Drawing.RectangleF

                ' calculate destination rectanle that is fitted into the specified
                ' form area with the same width-to-height ratio as the form has

                If horizontalRatio > verticalRatio Then
                    ' calculate actual form area width
                    Dim actualFormAreaWidth As Single = formAreaWidth * (verticalRatio / horizontalRatio)
                    ' calculate the destination rectangle in the right lower corner of the page
                    rect = New System.Drawing.RectangleF(cropBox.X + cropBox.Width - actualFormAreaWidth, cropBox.Y, actualFormAreaWidth, formAreaHeight)
                Else
                    ' calculate actual form area height
                    Dim actualFormAreaHeight As Single = formAreaHeight * (horizontalRatio / verticalRatio)
                    ' calculate the destination rectangle in the right lower corner of the page
                    rect = New System.Drawing.RectangleF(cropBox.X + 2 * cropBox.Width / 3, cropBox.Y, formAreaWidth, actualFormAreaHeight)
                End If
                ' set clip rectangle
                pageGraphics.IntersectClip(rect)
                ' draw the form on the page
                pageGraphics.DrawForm(form, rect)
            End Using
        Next
        ' save changes to the source
        pdfDocument.SaveChanges()
    End Using
End Sub


/// <summary>
/// Creates an empty Form XObject, draws a text on it
/// and then draws the Form XObject itself in the right lower corner
/// of each page of PDF document.
/// </summary>
/// <param name="pdfFilename">The filename of PDF document.</param>
/// <remarks>
/// This method doesn't consider the rotation of the page.
/// </remarks>
public static void DrawingOnTemplate(string pdfFilename)
{
    // open PDF document
    using (Vintasoft.Imaging.Pdf.PdfDocument pdfDocument = new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
    {
        // bounding box of the form
        System.Drawing.RectangleF boundingBox = new System.Drawing.RectangleF(0, 0, 100, 50);
        // create a form with specified bounding box
        Vintasoft.Imaging.Pdf.Tree.PdfFormXObjectResource form = 
            new Vintasoft.Imaging.Pdf.Tree.PdfFormXObjectResource(pdfDocument, boundingBox);
        // open PdfGraphics from the form
        using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics formGraphics = 
            Vintasoft.Imaging.Pdf.Drawing.PdfGraphics.FromForm(form))
        {
            // text of the string
            string text = "Template";
            // get standard font
            Vintasoft.Imaging.Pdf.Tree.Fonts.PdfFont font = pdfDocument.FontManager.GetStandardFont(
                Vintasoft.Imaging.Pdf.Tree.Fonts.PdfStandardFontType.TimesRoman);
            // rectangle of text string on the form
            System.Drawing.RectangleF textRect = new System.Drawing.RectangleF(10, 10, 80, 30);
            // get font size for specified rectangle
            float fontSize = formGraphics.MeasureFontSize(text, font, textRect.Width, textRect.Height);
            // create brush for the text string
            Vintasoft.Imaging.Pdf.Drawing.PdfBrush brush = new Vintasoft.Imaging.Pdf.Drawing.PdfBrush(
                System.Drawing.Color.Red);
            // draw the text string on the form
            formGraphics.DrawString(text, font, fontSize, brush, textRect, 
                Vintasoft.Imaging.Pdf.Drawing.PdfContentAlignment.Center, false);
        }

        // get original dimensions of the form
        float formWidth = form.BoundingBox.Width;
        float formHeight = form.BoundingBox.Height;

        // for each page of the PDF document
        foreach (Vintasoft.Imaging.Pdf.Tree.PdfPage page in pdfDocument.Pages)
        {
            // open PdfGraphics from the page
            using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics pageGraphics = 
                Vintasoft.Imaging.Pdf.Drawing.PdfGraphics.FromPage(page))
            {
                // get the crop box of the page
                System.Drawing.RectangleF cropBox = page.CropBox;
                // width of form area on the page
                float formAreaWidth = cropBox.Width / 3;
                // height of form area on the page
                float formAreaHeight = cropBox.Height / 3;

                // calculate ratios of form area dimensions to form dimensions
                float horizontalRatio = formAreaWidth / formWidth;
                float verticalRatio = formAreaHeight / formHeight;
                // destination rectangle
                System.Drawing.RectangleF rect;

                // calculate destination rectanle that is fitted into the specified
                // form area with the same width-to-height ratio as the form has

                if (horizontalRatio > verticalRatio)
                {
                    // calculate actual form area width
                    float actualFormAreaWidth = formAreaWidth * (verticalRatio / horizontalRatio);
                    // calculate the destination rectangle in the right lower corner of the page
                    rect = new System.Drawing.RectangleF(
                        cropBox.X + cropBox.Width - actualFormAreaWidth,
                        cropBox.Y,
                        actualFormAreaWidth,
                        formAreaHeight);
                }
                else
                {
                    // calculate actual form area height
                    float actualFormAreaHeight = formAreaHeight * (horizontalRatio / verticalRatio);
                    // calculate the destination rectangle in the right lower corner of the page
                    rect = new System.Drawing.RectangleF(
                        cropBox.X + 2 * cropBox.Width / 3,
                        cropBox.Y,
                        formAreaWidth,
                        actualFormAreaHeight);
                }
                // set clip rectangle
                pageGraphics.IntersectClip(rect);
                // draw the form on the page
                pageGraphics.DrawForm(form, rect);
            }
        }
        // save changes to the source
        pdfDocument.SaveChanges();
    }
}

Requirements

Target Platforms: .NET 11; .NET 10; .NET 9; .NET 8; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5

See Also