dcsimg
Results 1 to 6 of 6

Thread: Merge 2 printing documents into one print file

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    13

    Question Merge 2 printing documents into one print file

    I use below code to do the print action of PrintDocument1 :

    PrintDialog1.show

    PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings

    PrintDocument1.Print()

    But this time I have 2 print Documents. I want to print them in one file , first page : printdocument1 , second page : printdocument2

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,118

    Re: Merge 2 printing documents into one print file

    Rather than have them as 2 print documents, do them as one (the first page being what you currently have for printdocument1, the second page what you have for printdocument2).

    If you show us the code for them both, we can help you combine them appropriately.

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    13

    Re: Merge 2 printing documents into one print file

    Quote Originally Posted by si_the_geek View Post
    Rather than have them as 2 print documents, do them as one (the first page being what you currently have for printdocument1, the second page what you have for printdocument2).

    If you show us the code for them both, we can help you combine them appropriately.
    well , long story to tell . I have a datagridview and a richbox . I want to print them both one after another in 2 pages.

    according to my previous thread , friends suggested to use Merrion's DataGridPrinter class to do datagridview printing , And I decided to print a doc file instead of Richtextbox. So here is the code of clicking "Print" from Merrion's form menu and code for printing doc file I added to it :


    Code:
     Private Sub MenuItem_File_print_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MenuItem_File_print.Click
            If GridPrinter Is Nothing Then
                GridPrinter = New DataGridPrinter(Me.DataGrid1)
            End If
    
            With GridPrinter
                .HeaderText = Me.TextBox1.Text
                .HeaderHeightPercent = CInt(Me.NumericUpDown_HeaderHeightPercentage.Value)
                .FooterHeightPercent = CInt(Me.NumericUpDown_FooterHeightPercent.Value)
                .InterSectionSpacingPercent = CInt(Me.NumericUpDown_InterSectionSpacingPercent.Value)
                .HeaderPen = New Pen(CType(Me.ComboBox_ColourHeaderLine.SelectedItem, System.Drawing.Color))
                .FooterPen = New Pen(CType(Me.ComboBox_ColourFooterLine.SelectedItem, System.Drawing.Color))
                .GridPen = New Pen(CType(Me.ComboBox_ColourBodyline.SelectedItem, System.Drawing.Color))
                .HeaderBrush = CType(Me.ComboBox_HeaderBrush.SelectedItem, Brush)
                .EvenRowBrush = CType(Me.ComboBox_EvenBrush.SelectedItem, Brush)
                .OddRowBrush = CType(Me.ComboBox_OddRowBrush.SelectedItem, Brush)
                .FooterBrush = CType(Me.ComboBox_FooterBrush.SelectedItem, Brush)
                .ColumnHeaderBrush = CType(Me.ComboBox_ColumnHeaderBrush.SelectedItem, Brush)
                .PagesAcross = CInt(Me.NumericUpDown_PagesAcross.Value)
            End With
    
    ' Printing doc file
    
            Dim p As New PrintDialog
            Dim app As Microsoft.Office.Interop.Word.Application
            Dim doc As Microsoft.Office.Interop.Word.Document
    
            'Open file and print dialogs to get desired document and printer
    
            If Dir("C:\MyFile.doc") <> "" Then
    
                If p.ShowDialog = Windows.Forms.DialogResult.OK Then
    
    
                    'Create instance of Microsoft.Office.Interop.Word Application
                    app = New Microsoft.Office.Interop.Word.Application
    
                    'Set Printer
    
    
                    app.WordBasic.FilePrintSetup(Printer:=p.PrinterSettings.PrinterName, DoNotSetAsSysDefault:=1)
    
    
                    'Set filename to object type
    
                    Dim m As Object = System.Reflection.Missing.Value
    
                    'Open document
                    doc = app.Documents.Open("C:\MyFile.doc", m, m, m, m, m, m, m, m, m, m, m)
    
                    'Print document
                    app.PrintOut()
    
                    'Close document
                    Try
                        app.Documents.Close()
                        'Quit Microsoft.Office.Interop.Word application
                        app.Quit()
    
                        'Release
                        app = Nothing
                    Catch ex As Exception
    
    
    
    
    
    
                    End Try
    
    
                    GridPrinter.PrintDocument.PrinterSettings = p.PrinterSettings
    
    
    
                    GridPrinter.Print()
    
    
    
                End If
    
    
    
    
    
            End If 
    end sub
    it actually prints both parts , but as 2 different files

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,780

    Re: Merge 2 printing documents into one print file

    I would suggest that you visit the VB.NET CodeBank forum and search for threads by Merrion, who is the local printing guru. They will include a beginners guide to printing and that will show you how to print multiple pages. It's pretty simple really. The PrintPage event is raised and you draw the contents of a printed page there. I you have more pages to print, you set e.HasMorePages to True and the event is raised again, allowing you to draw the contents of the next page. That goes on until there are no more pages to print, at which point you set e.HasMorePages to False, which is the default, and the event is no longer raised and the printing stops. You just need to keep track of where you're up to in the printing cycle. In your case, you just need a variable that says whether you're on the first page or the second so that you can draw the appropriate output.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2018
    Posts
    13

    Re: Merge 2 printing documents into one print file

    Quote Originally Posted by jmcilhinney View Post
    I would suggest that you visit the VB.NET CodeBank forum and search for threads by Merrion, who is the local printing guru. They will include a beginners guide to printing and that will show you how to print multiple pages. It's pretty simple really. The PrintPage event is raised and you draw the contents of a printed page there. I you have more pages to print, you set e.HasMorePages to True and the event is raised again, allowing you to draw the contents of the next page. That goes on until there are no more pages to print, at which point you set e.HasMorePages to False, which is the default, and the event is no longer raised and the printing stops. You just need to keep track of where you're up to in the printing cycle. In your case, you just need a variable that says whether you're on the first page or the second so that you can draw the appropriate output.
    That's it , but printing a doc file is not a control to be drawn into the print page like a datagridview

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,780

    Re: Merge 2 printing documents into one print file

    Quote Originally Posted by charlie-j View Post
    That's it , but printing a doc file is not a control to be drawn into the print page like a datagridview
    You hadn't posted about using a DOC file when I started writing that post. You won't really be able to combine that with the output from a PrintDcument. They are two completely separate mechanisms. If you want one output then you must use a single mechanism to generate it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width