PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Mar 19, 2024 8:58 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Txt to pdf in vb.net
PostPosted: Mon Apr 02, 2018 12:39 am 
Offline

Joined: Mon Apr 02, 2018 12:15 am
Posts: 1
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Txt to pdf in vb.net
PostPosted: Mon Apr 02, 2018 6:37 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 905
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 55 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group