Click here to Skip to main content
15,896,432 members
Articles / Programming Languages / Visual Basic

Structured Print Document Utility

Rate me:
Please Sign up or sign in to vote.
4.79/5 (19 votes)
14 Jun 2014CPOL6 min read 152.8K   3.5K   95  
A set of classes for creating structured documents
Imports StructuredDocuments.StructuredDocuments


Public Class Form1
    Inherits System.Windows.Forms.Form

#Region "Private members"
    Private _Employees As New Employees
#End Region

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        Trace.WriteLine(GetType(System.String).FullName)
        Me.MainMenu1.MenuItems.Add("Test", AddressOf OnSelectSection)

        Call CreateStructuredDocument()

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents StructuredPrintDocumentProvider1 As StructuredDocuments.StructuredDocuments.StructuredPrintDocumentProvider
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
    Friend WithEvents PrintPreviewDialog1 As System.Windows.Forms.PrintPreviewDialog
    Friend WithEvents chkDesign As System.Windows.Forms.CheckBox
    Friend WithEvents TextStyleProvider1 As StructuredDocuments.StructuredDocuments.TextStyleProvider
    Friend WithEvents HeadingPicture As StructuredDocuments.StructuredDocuments.DocumentParagraphTextStyle
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents Button_DesignPage As System.Windows.Forms.Button
    Friend WithEvents StructuredPrintDocument1 As StructuredDocuments.StructuredDocuments.StructuredPrintDocument
    Friend WithEvents DesignPageTest As StructuredDocuments.StructuredDocuments.StructuredPrintPage
    Friend WithEvents StructuredPrintDocumentArea1 As StructuredDocuments.StructuredDocuments.StructuredPrintDocumentArea
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
        Me.TextStyleProvider1 = New StructuredDocuments.StructuredDocuments.TextStyleProvider(Me.components)
        Me.HeadingPicture = New StructuredDocuments.StructuredDocuments.DocumentParagraphTextStyle
        Me.StructuredPrintDocumentProvider1 = New StructuredDocuments.StructuredDocuments.StructuredPrintDocumentProvider(Me.components)
        Me.Button1 = New System.Windows.Forms.Button
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        Me.CheckBox1 = New System.Windows.Forms.CheckBox
        Me.PrintPreviewDialog1 = New System.Windows.Forms.PrintPreviewDialog
        Me.chkDesign = New System.Windows.Forms.CheckBox
        Me.MainMenu1 = New System.Windows.Forms.MainMenu
        Me.Button_DesignPage = New System.Windows.Forms.Button
        Me.StructuredPrintDocument1 = New StructuredDocuments.StructuredDocuments.StructuredPrintDocument("DesignPage")
        Me.DesignPageTest = New StructuredDocuments.StructuredDocuments.StructuredPrintPage("DesignPage", False, System.Drawing.Printing.PaperKind.A4)
        Me.StructuredPrintDocumentArea1 = New StructuredDocuments.StructuredDocuments.StructuredPrintDocumentArea("StructuredPrintDocumentArea1", "Test", GetType(StructuredDocuments.StructuredDocuments.StructuredPrintDocumentAreaTextWriter), New System.Drawing.Rectangle(1, 1, 100, 40), Nothing)
        Me.SuspendLayout()
        '
        'TextStyleProvider1
        '
        Me.TextStyleProvider1.Comment = "Test"
        Me.TextStyleProvider1.DocumentTextStyles.Add(Me.HeadingPicture)
        '
        'HeadingPicture
        '
        Me.HeadingPicture.BorderColor = System.Drawing.Color.Firebrick
        Me.HeadingPicture.BorderWidth = 1.0!
        Me.HeadingPicture.BottomBorder = True
        Me.HeadingPicture.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!)
        Me.HeadingPicture.Forecolour = System.Drawing.Color.Black
        Me.HeadingPicture.Highlight = System.Drawing.Color.IndianRed
        Me.HeadingPicture.HorizontalAlignment = StructuredDocuments.StructuredDocuments.DocumentParagraphTextStyle.CellTextHorizontalAlignment.CentreAlign
        Me.HeadingPicture.LeftSideBorder = True
        Me.HeadingPicture.RightSideBorder = True
        Me.HeadingPicture.StyleName = "HeadingPicture"
        Me.HeadingPicture.TextGutter = 5
        Me.HeadingPicture.TopBorder = True
        Me.HeadingPicture.VerticalAlignment = StructuredDocuments.StructuredDocuments.DocumentParagraphTextStyle.CellTextVerticalAlignment.TopAlign
        '
        'StructuredPrintDocumentProvider1
        '
        Me.StructuredPrintDocumentProvider1.DocumentName = "DesignPage"
        Me.StructuredPrintDocumentProvider1.StructuredPrintDocument = Me.StructuredPrintDocument1
        Me.StructuredPrintDocumentProvider1.TextStyleProvider = Me.TextStyleProvider1
        '
        'Button1
        '
        Me.Button1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Button1.Location = New System.Drawing.Point(192, 16)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Preview"
        '
        'PictureBox1
        '
        Me.PictureBox1.Image = CType(resources.GetObject("PictureBox1.Image"), System.Drawing.Image)
        Me.PictureBox1.Location = New System.Drawing.Point(32, 144)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(144, 56)
        Me.PictureBox1.TabIndex = 2
        Me.PictureBox1.TabStop = False
        Me.PictureBox1.Visible = False
        '
        'CheckBox1
        '
        Me.CheckBox1.Location = New System.Drawing.Point(8, 32)
        Me.CheckBox1.Name = "CheckBox1"
        Me.CheckBox1.Size = New System.Drawing.Size(72, 16)
        Me.CheckBox1.TabIndex = 3
        Me.CheckBox1.Text = "Stretch"
        '
        'PrintPreviewDialog1
        '
        Me.PrintPreviewDialog1.AutoScrollMargin = New System.Drawing.Size(0, 0)
        Me.PrintPreviewDialog1.AutoScrollMinSize = New System.Drawing.Size(0, 0)
        Me.PrintPreviewDialog1.ClientSize = New System.Drawing.Size(400, 300)
        Me.PrintPreviewDialog1.Enabled = True
        Me.PrintPreviewDialog1.Icon = CType(resources.GetObject("PrintPreviewDialog1.Icon"), System.Drawing.Icon)
        Me.PrintPreviewDialog1.Location = New System.Drawing.Point(387, 17)
        Me.PrintPreviewDialog1.MinimumSize = New System.Drawing.Size(375, 250)
        Me.PrintPreviewDialog1.Name = "PrintPreviewDialog1"
        Me.PrintPreviewDialog1.TransparencyKey = System.Drawing.Color.Empty
        Me.PrintPreviewDialog1.Visible = False
        '
        'chkDesign
        '
        Me.chkDesign.Checked = True
        Me.chkDesign.CheckState = System.Windows.Forms.CheckState.Checked
        Me.chkDesign.Location = New System.Drawing.Point(8, 16)
        Me.chkDesign.Name = "chkDesign"
        Me.chkDesign.Size = New System.Drawing.Size(112, 16)
        Me.chkDesign.TabIndex = 5
        Me.chkDesign.Text = "Design Mode"
        '
        'Button_DesignPage
        '
        Me.Button_DesignPage.Location = New System.Drawing.Point(192, 48)
        Me.Button_DesignPage.Name = "Button_DesignPage"
        Me.Button_DesignPage.Size = New System.Drawing.Size(72, 24)
        Me.Button_DesignPage.TabIndex = 6
        Me.Button_DesignPage.Text = "Design"
        '
        'StructuredPrintDocument1
        '
        Me.StructuredPrintDocument1.DocumentName = "DesignPage"
        Me.StructuredPrintDocument1.Mode = StructuredDocuments.StructuredDocuments.StructuredPrintDocument.PrintMode.DesignTime
        Me.StructuredPrintDocument1.Pages.Add(Me.DesignPageTest)
        '
        'DesignPageTest
        '
        Me.DesignPageTest.Landscape = False
        Me.DesignPageTest.PageName = "DesignPage"
        Me.DesignPageTest.PageNumber = 0
        Me.DesignPageTest.PaperKind = System.Drawing.Printing.PaperKind.A4
        Me.DesignPageTest.Sections.Add(Me.StructuredPrintDocumentArea1)
        '
        'StructuredPrintDocumentArea1
        '
        Me.StructuredPrintDocumentArea1.BoundaryRectangle = New System.Drawing.Rectangle(1, 1, 100, 40)
        Me.StructuredPrintDocumentArea1.DataSourceName = "Test"
        Me.StructuredPrintDocumentArea1.SectionName = "StructuredPrintDocumentArea1"
        Me.StructuredPrintDocumentArea1.StyleName = Nothing
        Me.StructuredPrintDocumentArea1.WriterType = GetType(StructuredDocuments.StructuredDocuments.StructuredPrintDocumentAreaTextWriter)
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 16)
        Me.ClientSize = New System.Drawing.Size(292, 77)
        Me.Controls.Add(Me.Button_DesignPage)
        Me.Controls.Add(Me.chkDesign)
        Me.Controls.Add(Me.CheckBox1)
        Me.Controls.Add(Me.PictureBox1)
        Me.Controls.Add(Me.Button1)
        Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Menu = Me.MainMenu1
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        Me.StructuredPrintDocumentProvider1.StructuredPrintDocument.Pages.Page(0).Sections().Item(0).SetExtraParameter("Stretch", Me.CheckBox1.Checked)
        If Me.chkDesign.Checked Then
            Me.StructuredPrintDocumentProvider1.StructuredPrintDocument.Mode = StructuredPrintDocument.PrintMode.DesignTime
        Else
            Me.StructuredPrintDocumentProvider1.StructuredPrintDocument.Mode = StructuredPrintDocument.PrintMode.RunTime
        End If
        Me.PrintPreviewDialog1.Document = Me.StructuredPrintDocumentProvider1.StructuredPrintDocument.PrintDocument
        Me.PrintPreviewDialog1.ShowDialog()

    End Sub

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

        Dim lblWriter As New StructuredPrintDocumentAreaTextWriter
        lblWriter.TextStyle = Me.TextStyleProvider1.DocumentTextStyles.Item("Heading")

        If lblWriter.CanDrawDataType(Me.Text.GetType) Then
            lblWriter.Draw(e.Graphics, New System.Drawing.Rectangle(4, 4, 100, 20), Me, "Text", StructuredPrintDocument.PrintMode.RunTime)
        End If

    End Sub

    Private Sub CreateStructuredDocument()


        With StructuredPrintDocumentProvider1
            .StructuredPrintDocument = New StructuredPrintDocument
            .StructuredPrintDocument.DocumentName = "Test structured document"
            'Add two new pages...
            .StructuredPrintDocument.AddPage(New StructuredPrintPage("Employee List"))
            .StructuredPrintDocument.AddPage(New StructuredPrintPage("Employee Details"))
            With .StructuredPrintDocument.Pages
                .Page("Employee List").Landscape = True
                .Page("Employee List").AddPicture("Heading", Me.PictureBox1.Image, "HeadingPicture", New System.Drawing.Rectangle(650, 300, 150, 300), False)

                .Page("Employee List").AddLabel("Heading", "Employee Phone List", "Subheading", New System.Drawing.Rectangle(150, 255, 800, 30))
                .Page("Employee List").AddSection(New StructuredPrintDocumentArea("Employee List"))
                With .Page("Employee List").Section("Employee List")
                    .WriterType = GetType(StructuredPrintDocumentAreaTableWriter)
                    .BoundaryRectangle = New System.Drawing.Rectangle(150, 300, 450, 300)
                    .DataSourceName = "Employee"
                    'Print another "Employee List" page if it overflows the first page
                    .AdditionalPageTests.Add(New AdditionalPageTest("Employee", AdditionalPageTest.AdditionalPageTestTypes.NotAtEndOfCollection))
                End With
                Dim EmployeeTelNoTableFormat As ColumnFormatCollection = ColumnFormatCollection.CreateDefaultColumnFormatCollection(GetType(Employee), New System.Drawing.Font(System.Drawing.FontFamily.GenericSerif, 12))
                EmployeeTelNoTableFormat.Column("Name").Width = 300
                EmployeeTelNoTableFormat.Column("Extension").Width = 150
                EmployeeTelNoTableFormat.Column("StartDate").PrintColumn = False
                'Make the header italic for extension...
                EmployeeTelNoTableFormat.Column("Extension").HeadingTextStyle.Font = New System.Drawing.Font(EmployeeTelNoTableFormat.Column("Extension").HeadingTextStyle.Font, Drawing.FontStyle.Italic Or Drawing.FontStyle.Bold)

                .Page("Employee List").Section("Employee List").SetExtraParameter("ColumnFormats", EmployeeTelNoTableFormat)

                With .Page(0).Sections
                    .Item(0).SetExtraParameter("Stretch", Me.CheckBox1.Checked)
                End With

                '\\Put a printed date thing at the bottom of the page
                .Page("Employee List").AddSection(New StructuredPrintDocumentArea("Todays Date"))
                With .Page("Employee List").Section("Todays Date")
                    .WriterType = GetType(StructuredPrintDocumentAreaTextWriter)
                    .BoundaryRectangle = New System.Drawing.Rectangle(150, 600, 450, 30)
                    .DataSourceName = "TodaysDate"
                    .StyleName = "Body"
                End With
                '\\ After page 1 is finished, reset the "Employee" pointer to the start 
                .Page(1).DataActionsBefore.Add(New DocumentDataAction("Employee", DocumentDataAction.DataSetMoveActions.MoveToFirstRecord))

                .Page(1).AddPicture("Heading", Me.PictureBox1.Image, "HeadingPicture", New System.Drawing.Rectangle(150, 150, 90, 150), False)
                .Page(1).Sections.Item(0).SetExtraParameter("Stretch", True)
                .Page(1).AddLabel("EmployeeName", "Employee Name", "Body", New System.Drawing.Rectangle(150, 350, 250, 30))
                .Page(1).AddSection(New StructuredPrintDocumentArea("Employee Name", "Employee.Name", GetType(StructuredDocuments.StructuredDocuments.StructuredPrintDocumentAreaTextWriter), New System.Drawing.Rectangle(300, 350, 250, 30)))
                .Page(1).Section("Employee Name").StyleName = "Body"

                .Page(1).AddLabel("EmployeeExtension", "Extension", "Body", New System.Drawing.Rectangle(150, 450, 250, 30))
                .Page(1).AddSection(New StructuredPrintDocumentArea("Employee Extension", "Employee.Extension", GetType(StructuredDocuments.StructuredDocuments.StructuredPrintDocumentAreaTextWriter), New System.Drawing.Rectangle(300, 450, 250, 30)))
                .Page(1).Section("Employee Extension").StyleName = "Body"

                .Page(1).AddLabel("EmployeeStartDate", "Joined", "Body", New System.Drawing.Rectangle(150, 550, 250, 30))
                .Page(1).AddSection(New StructuredPrintDocumentArea("Employee Start Date", "Employee.StartDate", GetType(StructuredDocuments.StructuredDocuments.StructuredPrintDocumentAreaTextWriter), New System.Drawing.Rectangle(300, 550, 250, 30)))
                .Page(1).Section("Employee Start Date").StyleName = "Body"

                'Make Page 2 repreat for each employee...
                .Page("Employee Details").DataActionsOnNextPage.Add(New DocumentDataAction("Employee", DocumentDataAction.DataSetMoveActions.MoveToNextRecord))
                .Page("Employee Details").AdditionalPageTests.Add(New AdditionalPageTest("Employee", AdditionalPageTest.AdditionalPageTestTypes.NotAtEndOfCollection))
            End With
        End With

        _Employees.Add(New Employee("Duncan Jones", "5951", #1/10/2002#))
        _Employees.Add(New Employee("Arthur Guinness", "5920", #11/15/2003#))
        _Employees.Add(New Employee("Stan Laurel", "5990", #10/15/2000#))
        _Employees.Add(New Employee("Jackie Chan", "5991", #10/15/2000#))
        _Employees.Add(New Employee("Lucy Martin", "5992", #10/15/2000#))
        _Employees.Add(New Employee("Dr C. Colon", "5993", #10/15/2000#))
        _Employees.Add(New Employee("Lisa Simpson", "5994", #10/15/2000#))
        _Employees.Add(New Employee("Patrick Laurent", "5995", #10/15/2000#))
        _Employees.Add(New Employee("David Davis", "5996", #10/15/2000#))
        _Employees.Add(New Employee("Carlton Palmer", "5997", #10/15/2000#))
        _Employees.Add(New Employee("Richard E. Grant", "5998", #10/15/2000#))
        _Employees.Add(New Employee("Grace Kelly", "5999", #10/15/2000#))
        _Employees.Add(New Employee("Lucretia Borger", "5980", #10/15/2000#))
        _Employees.Add(New Employee("Mike O'Shea", "5981", #10/15/2000#))
        _Employees.Add(New Employee("Ricky Gervais", "5922", #10/15/2000#))
        _Employees.Add(New Employee("Vincent Price", "5923", #10/15/2000#))

        Me.StructuredPrintDocumentProvider1.StructuredPrintDocument.LoadDataItem("Employee", "", _Employees)

    End Sub

    Private Sub StructuredPrintDocumentProvider1_DocumentContentQuery(ByVal sender As Object, ByVal e As System.EventArgs) Handles StructuredPrintDocumentProvider1.DocumentContentQuery

        With CType(e, DocumentContentQueryEventArgs)
            If .PropertyName = "TodaysDate" Then
                .Scope = DocumentContentQueryEventArgs.DocumentDataScope.GlobalScope
                .Value = Now.ToLongDateString
            End If
        End With

    End Sub


#Region "Selected Section"
    Private Sub OnSelectSection(ByVal sender As Object, ByVal e As EventArgs)

        With CType(sender, System.Windows.Forms.MenuItem)
            Trace.WriteLine(.Text)
        End With
    End Sub
#End Region

    Private Sub Button_DesignPage_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button_DesignPage.Click

        Dim fDesigner As New StructuredDocuments.StructuredDocuments.Design.DesignerForm_StructuredPrintPage

        fDesigner.CurrentPage = Me.DesignPageTest
        fDesigner.ShowDialog()

    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Ireland Ireland
C# / SQL Server developer
Microsoft MVP (Azure) 2017
Microsoft MVP (Visual Basic) 2006, 2007

Comments and Discussions