VintaSoft Imaging .NET SDK 15.1: Documentation for .NET developer
Vintasoft.Imaging.Pdf.Tree Namespace / PdfFormXObjectResource Class / PdfFormXObjectResource Constructors / PdfFormXObjectResource Constructor(PdfDocument,PdfPage)
Syntax Example Requirements SeeAlso
In This Topic
PdfFormXObjectResource Constructor(PdfDocument,PdfPage)
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 page
The PDF page, which content must be copied into the new form resource.
As PdfPage _
)
public PdfFormXObjectResource(
Vintasoft.Imaging.Pdf.PdfDocument document,
PdfPage page
)

Parameters

document
The parent PDF document.
page
The PDF page, which content must be copied into the new form resource.
Example

Here is an example that shows how to draw one PDF page in the left upper corner of another PDF page:


''' <summary>
''' Draws one PDF page in the left upper corner of another PDF page
''' using Form XObject that represents the page.
''' </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 DrawingTemplateOnAnotherTemplate(pdfFilename As String)
    ' open PDF document
    Using pdfDocument As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename)
        If pdfDocument.Pages.Count = 1 Then
            Return
        End If

        ' get first page of the document
        Dim firstPage As Vintasoft.Imaging.Pdf.Tree.PdfPage = pdfDocument.Pages(0)
        ' get second page of the document
        Dim secondPage As Vintasoft.Imaging.Pdf.Tree.PdfPage = pdfDocument.Pages(1)
        ' create a form that represents the second page
        Dim form As New Vintasoft.Imaging.Pdf.Tree.PdfFormXObjectResource(pdfDocument, secondPage)
        ' get original dimensions of the form
        Dim formWidth As Single = form.BoundingBox.Width
        Dim formHeight As Single = form.BoundingBox.Height

        ' open PdfGraphics from the page
        Using pageGraphics As Vintasoft.Imaging.Pdf.Drawing.PdfGraphics = Vintasoft.Imaging.Pdf.Drawing.PdfGraphics.FromPage(firstPage)
            ' get the crop box of the page
            Dim cropBox As System.Drawing.RectangleF = firstPage.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.Y + 2 * cropBox.Height / 3, 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, cropBox.Y + cropBox.Height - actualFormAreaHeight, formAreaWidth, actualFormAreaHeight)
            End If
            ' set clip rectangle
            pageGraphics.IntersectClip(rect)
            ' fill a rectangle with white color as a background
            pageGraphics.FillRectangle(New Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.White), rect)
            ' draw red border
            pageGraphics.DrawRectangle(New Vintasoft.Imaging.Pdf.Drawing.PdfPen(System.Drawing.Color.Red, 2), rect)
            ' draw the form on the page
            pageGraphics.DrawForm(form, rect)
        End Using

        ' save changes to the source
        pdfDocument.SaveChanges()
    End Using
End Sub


/// <summary>
/// Draws one PDF page in the left upper corner of another PDF page
/// using Form XObject that represents the page.
/// </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 DrawingTemplateOnAnotherTemplate(string pdfFilename)
{
    // open PDF document
    using (Vintasoft.Imaging.Pdf.PdfDocument pdfDocument = 
        new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
    {
        if (pdfDocument.Pages.Count == 1)
            return;

        // get first page of the document
        Vintasoft.Imaging.Pdf.Tree.PdfPage firstPage = pdfDocument.Pages[0];
        // get second page of the document
        Vintasoft.Imaging.Pdf.Tree.PdfPage secondPage = pdfDocument.Pages[1];
        // create a form that represents the second page
        Vintasoft.Imaging.Pdf.Tree.PdfFormXObjectResource form = 
            new Vintasoft.Imaging.Pdf.Tree.PdfFormXObjectResource(pdfDocument, secondPage);
        // get original dimensions of the form
        float formWidth = form.BoundingBox.Width;
        float formHeight = form.BoundingBox.Height;

        // open PdfGraphics from the page
        using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics pageGraphics = 
            Vintasoft.Imaging.Pdf.Drawing.PdfGraphics.FromPage(firstPage))
        {
            // get the crop box of the page
            System.Drawing.RectangleF cropBox = firstPage.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.Y + 2 * cropBox.Height / 3,
                    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,
                    cropBox.Y + cropBox.Height - actualFormAreaHeight,
                    formAreaWidth,
                    actualFormAreaHeight);
            }
            // set clip rectangle
            pageGraphics.IntersectClip(rect);
            // fill a rectangle with white color as a background
            pageGraphics.FillRectangle(
                new Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.White), rect);
            // draw red border
            pageGraphics.DrawRectangle(
                new Vintasoft.Imaging.Pdf.Drawing.PdfPen(System.Drawing.Color.Red, 2), rect);
            // draw the form on the page
            pageGraphics.DrawForm(form, rect);
        }

        // save changes to the source
        pdfDocument.SaveChanges();
    }
}

Requirements

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

See Also