Group,

I'm converting a text file into a .pdf file using ItextSharp. The code below works perfectly to do this. Unfortunately the default font size is to large. I need to make it smaller. Here's my code:

Private Sub ConvertToPDF()
        '   Converting the Restran text file to a PDF file
        Dim row As Integer
        Dim lineEnd As String
        Dim txtLine As String
        saveRestran = hotelFolder & "\Restran\" & propertyNo & "Restran.txt"
        Dim newPDFName As String = hotelFolder & "\Restran\" & yearFolder & "\" & propertyNo & " - Restran " & formDate & ".pdf"
        Dim restranFile As String = hotelFolder & "\Restran\" & propertyNo & "Restran.txt"
        Dim restranText As String = File.ReadAllText(restranFile)
        Dim pdfDoc As New Document()
        Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(pdfDoc, New FileStream(newPDFName, FileMode.Create))
        Dim objReader As New System.IO.StreamReader(saveRestran)
        If System.IO.File.Exists(saveRestran) Then
            row = 1
            Do While objReader.Peek() <> -1
                txtLine = objReader.ReadLine()
                lineEnd = Trim(txtLine)
                pdfDoc.Open()
                pdfDoc.Add(New Paragraph(txtLine))
                row = row + 1
                If row = 61 Then
                    pdfDoc.NewPage()
                    row = 1
                End If
                If lineEnd = "End of Report" Then
                    pdfDoc.Close()
                    objReader.Close()
                    Exit Sub
                End If
            Loop
            pdfDoc.Close()
        End If

    End Sub

I've read multiple sites to find the appropriate VB.net code to change the font/font size. Every different code I've tried gives me an error. If you've got some thoughts, I would appreciate the help.

FYI.... I'm using Visual Studio 2010 (visual basic).

Thanks,

Don

One option that comes to mind is here, pdfDoc.Add(New Paragraph(txtLine)). One of the overloads for the Paragraph constructor allows you to set the font.

tinstaafl, Got it and tried it. It worked. Now the next issue: I need to change the font to "Arial Monospaced for SAP". Here's what I've tried:

Dim bf = FontFactory.GetFont("Arial Monospaced for SAP")
        Dim fFont = New Font(bf, 6)
        Dim objReader As New System.IO.StreamReader(saveRestran)
        If System.IO.File.Exists(saveRestran) Then
            row = 1
            Do While objReader.Peek() <> -1
                txtLine = objReader.ReadLine()
                lineEnd = Trim(txtLine)
                pdfDoc.Open()
                pdfDoc.Add(New Paragraph(txtLine, fFont))
                row = row + 1
                If row = 61 Then
                    pdfDoc.NewPage()
                    row = 1
                End If
                If lineEnd = "End of Report" Then
                    pdfDoc.Close()
                    objReader.Close()
                    Exit Sub
                End If
            Loop
            pdfDoc.Close()
        End If

It has accepted Dim bf = FontFactory.GetFont("Arial Monospaced for SAP"). However my error now comes on Dim fFont = New Font(bf, 6). Thoughts and ideas?

Thanks again,

Don

FYI... I've now eliminated the error message. However it's not picking up the correct font. Here's what I've done:

Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(pdfDoc, New FileStream(newPDFName, FileMode.Create))
Dim bf As Font = FontFactory.GetFont("C:\Windows\Arial Monospaced for SAP", 7)
Dim fFont = New Font(bf)

I was hoping the code would fine the path of the correct font I need along with the size. But no luck here. I'm guessing it's still giving me the default font, although the size has gotten smaller.

Thanks again.

Don

You're welcome, glad that you're making progress. Curious though why you're creating a new font object when the paragraph constructor will accept the one from FontFactory:

Dim bf = FontFactory.GetFont("C:\Windows\Arial Monospaced for SAP", 7)
pdfDoc.Add(New Paragraph(txtLine, bf))

My assumption is that it's required. I've been reading a good bit and the above code is the only thing that hasn't given me an error message. But keep in mind, this still didn't work. Given what I need to do, I have to divert from the default font and font size and change it to Arial Monospaced for SAP with a smaller font.

Thoughts?

Don

I think the problem might be that it's not recognizing Arial Monospaced for SAP as a valid font, but it doesn't generate an exception, it just leaves bf uninitialized. You might have to dig into the docs and see how to import an external font into the document.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.