Results 1 to 10 of 10

Thread: Writting to Print Document

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2024
    Posts
    5

    Writting to Print Document

    Hi All,
    I am a retired man and I begin to code, using Visual Stutio Basic 2019,as an hobby. Visual basic is new to me. I try to answer this question myself, but no sucess.
    What I would like to write in the print document is the text, UNIFORM LOAD, and the string on UWriteText1 on lines, 1.5 * Font.Height + 0.025) * 17, and (1.5 * Font.Height + 0.025) * 18, but if the variable Wtl1 is equal to zero, then I would have two blank lines before writting the following lines. To avoid blank lines I use the if statement before writting the next lines. However, this method appears to be lengthy because I have many lines to writes. I think a better approach would be selecting the last line and move the cursor down one line and continue writting. I do not know the code for this.
    Also, I appologize English is confusing since English is my second language.

    Below are my lines of code:

    If Wtl > 0 Then

    UWrite1 = " UNIFORM LOAD:"
    UWrite2 = UWriteText1
    e.Graphics.DrawString(UWrite1, Font, Brush, LeftMargin, (1.5 * Font.Height + 0.025) * 17 + TopMargin)
    e.Graphics.DrawString(UWrite2, Font, Brush, LeftMargin, (1.5 * Font.Height + 0.025) * 18 + TopMargin)

    End If

    'Write uniform varying load *********************************************

    If (Wtl1 > 0 Or Wtl2 > 0) And (Wtl = 0) Then

    e.Graphics.DrawString(" UNIFORM VARYING LOAD:", Font, Brush, LeftMargin, (1.5 * Font.Height + 0.025) * 17 + TopMargin)
    e.Graphics.DrawString(UVWriteText1, Font, Brush, LeftMargin, (1.5 * Font.Height + 0.025) * 18 + TopMargin)
    e.Graphics.DrawString(UVWriteText2, Font, Brush, LeftMargin, (1.5 * Font.Height + 0.025) * 19 + TopMargin)
    e.Graphics.DrawString(" Start distance a = " & VRSDistA & " ft", Font, Brush, LeftMargin, (1.5 * Font.Height + 0.025) * 20 + TopMargin)
    e.Graphics.DrawString(" End distance b = " & VREDistB & " ft", Font, Brush, LeftMargin, (1.5 * Font.Height + 0.025) * 21 + TopMargin)


    ElseIf (Wtl1 > 0 Or Wtl2 > 0) And (Wtl > 0) Then
    'Uniform load exists in this case and occupies intlinenumber 17 to 18
    'So this load will occupied 19 to 23
    e.Graphics.DrawString(" UNIFORM VARYING LOAD:", Font, Brush, LeftMargin, (1.5 * Font.Height + 0.025) * 19 + TopMargin)
    e.Graphics.DrawString(UVWriteText1, Font, Brush, LeftMargin, (1.5 * Font.Height + 0.025) * 20 + TopMargin)
    e.Graphics.DrawString(UVWriteText2, Font, Brush, LeftMargin, (1.5 * Font.Height + 0.025) * 21 + TopMargin)
    e.Graphics.DrawString(" Start distance a = " & VRSDistA & " ft", Font, Brush, LeftMargin, (1.5 * Font.Height + 0.025) * 22 + TopMargin)
    e.Graphics.DrawString(" End distance b = " & VREDistB & " ft", Font, Brush, LeftMargin, (1.5 * Font.Height + 0.025) * 23 + TopMargin)


    End If

    'Write P1 load *************************************************************

    If (P1 > 0) And (Wtl = 0) And (Wtl1 = 0) And (Wtl2 = 0) Then

  2. #2
    Hyperactive Member
    Join Date
    Jul 2022
    Posts
    292

    Re: Writting to Print Document

    My first question is what type of print document? Where is the information coming from and what type of printout are you looking to get?

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2024
    Posts
    5

    Re: Writting to Print Document

    I am not sure how to answer your question. I guess the type of print document is whatever is in this event, Private Sub PrintDoc_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDoc.PrintPage and I would like to print it on a paper.

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2024
    Posts
    5

    Re: Writting to Print Document

    I am not sure how the answer your questions. I guess the type of print document is whatever is in the event, Private Sub PrintDoc_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDoc.PrintPage, and the printout will be on stardard 8.5x11 paper.

  5. #5
    Hyperactive Member
    Join Date
    Jul 2022
    Posts
    292

    Re: Writting to Print Document

    Okay, you can do something a bit easier using a NuGet package, I used FreeSpire.Doc, you can build the document (like a Word Document) then just print it without using all the drawing.

    this of course it a simple example, I had it print to the PDF printer but you can obviously name any one pf your printers.
    Code:
    Imports Spire.Doc
    Imports Spire.Doc.Documents
    Imports System.Drawing.Printing
    
    Public Class Form1
        Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
    
            ' use FreeSpire.Doc to print a document
            Dim doc As New Document
            Dim sec As Section
            Dim pg As Paragraph
            Dim printDoc As PrintDocument = doc.PrintDocument
    
            Dim UVWriteText1 As String
    
            sec = doc.AddSection()
            pg = sec.AddParagraph()
    
            UVWriteText1 = "sample text to print"
    
            pg.AppendText($"UNIFORM VARYING LOAD: {UVWriteText1}")
    
            With printDoc.PrinterSettings
                .PrinterName = "Microsoft Print to PDF"
                .FromPage = 1
                .ToPage = 1
                .Copies = 1
            End With
    
            printDoc.Print()
    
        End Sub
    
    
    End Class
    EDIT: forgot to add, they have extensive documentation https://www.e-iceblue.com/Tutorials/...e-Content.html
    Last edited by jdelano; May 24th, 2024 at 10:05 AM.

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2024
    Posts
    5

    Re: Writting to Print Document

    Hello Jdelano,
    I copied your code and pasted it on my Sub to see if it would work. There are three errror messages as follows:
    1. Document is not defined
    2. Section is not defined
    3. Paragraph is not defined
    Thank you very much for your help though.

  7. #7
    Hyperactive Member
    Join Date
    Jul 2022
    Posts
    292

    Re: Writting to Print Document

    Make sure to add the NuGet package FreeSprire.doc, see the link I placed there
    Edit: If you haven't used them before .. https://learn.microsoft.com/en-us/nuget/what-is-nuget

    You're welcome.

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2024
    Posts
    5

    Re: Writting to Print Document

    I will stick to Visual Basic for printing, because I will have some kind of drawings that I will draw on the printout page.

  9. #9
    Hyperactive Member
    Join Date
    Jul 2022
    Posts
    292

    Re: Writting to Print Document

    Sounds good.

  10. #10
    Lively Member
    Join Date
    Dec 2021
    Posts
    104

    Re: Writting to Print Document

    OK, I didn't understand your problem quite, but maybe this could help you. This is how I write each row of a DataGridView, making sure that rows don't overwrite each other.

    Code:
      For Each row As DataGridViewRow In DataGridView1.Rows
                Dim Yoffset As Integer = Yoffset + 40
    
                If Not row.IsNewRow Then
    
    
    
                    e.Graphics.DrawString(row.Cells(0).Value, f8, Brushes.Black, 75, 530 + Yoffset)
    
                    e.Graphics.DrawString(row.Cells(2).Value, f8, Brushes.Black, 250, 530 + Yoffset)
                    e.Graphics.DrawString(row.Cells(3).Value, f8, Brushes.Black, 600, 530 + Yoffset)
    
    
                    e.Graphics.DrawString("------------------------------------------------------------------------------------------", f8, Brushes.Black, 80, 560 + Yoffset)
    
    
                End If
            Next

Posting Permissions

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



Click Here to Expand Forum to Full Width