PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Txt to pdf in vb.net
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3756
Page 1 of 1

Author:  Ballwin [ Mon Apr 02, 2018 12:39 am ]
Post subject:  Txt to pdf in vb.net

I am trying to write a simple vb.net program to convert text files to pdfs. The pgm creates the correct number of pages but displays all the text on the first page of the pdf. What am I missing? Thanks!

Code follows:

Code:
Imports System.IO
Imports PdfSharp
Imports PdfSharp.Drawing
Imports PdfSharp.Pdf

Public Class Form1
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Try
            Dim line As String
            Dim readFile As System.IO.TextReader = New StreamReader("c:\a\Test.txt")
            Dim yPoint As Integer = 0

            Dim pdf As PdfDocument = New PdfDocument
            pdf.Info.Title = "Text File to PDF"

            Dim pdfPage As PdfPage = pdf.AddPage
            Dim graph As XGraphics = XGraphics.FromPdfPage(pdfPage)

            Dim font As XFont = New XFont("courier new", 10, XFontStyle.Regular)

nextline:

            line = readFile.ReadLine()
            If line Is Nothing Then GoTo AllDone :
            If line <> Chr(12) Then GoTo Page1

            yPoint = 0

            ' pdfPage = pdf.
            pdfPage = pdf.AddPage()

Page1:
            graph.DrawString(line, font, XBrushes.Black,
            New XRect(10, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft)
            yPoint = yPoint + 10
            GoTo nextline


AllDone:

            Dim pdfFilename As String = "c:\a\newpdf.pdf"
            pdf.Save(pdfFilename)
            readFile.Close()
            readFile = Nothing
            Process.Start(pdfFilename)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        End
    End Sub
End Class

Author:  TH-Soft [ Mon Apr 02, 2018 6:37 am ]
Post subject:  Re: Txt to pdf in vb.net

Ballwin wrote:
What am I missing?
You missed that the Sample forum is for good code that works.

You missed that the XGraphics class does the drawing - you only create such an object for the first page, so all drawing goes there.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/