Group,

I'm using VB.net 2010 and would like to convert a text file into a PDF file.

In reading through the internet, it appears that I need to download a 3rd party .dll file. But is that needed? I see multiple PDF library references by right clicking the project name, choosing "Add Reference" and clicking the "COM" tab. These libraries include PDFFilter, PDFMAcad, PDFMaker for Visio, PDFMakerAPI, etc. Can (or should) any of these be used?

Thanks for your input!

Don

Dani AI

Generated

those COM entries you see (PDFMaker, PDFFilter, PDFMAcad, etc.) are Acrobat add-ins meant to automate Office/AutoCAD, not general-purpose PDF SDKs you can ship with your app. They depend on the host apps and Acrobat being installed, so they are the wrong tool for programmatic conversion from your VB.NET code. (learn.microsoft.com)

’s virtual-printer idea is great for interactive use. If you need to do it fully in code (no dialogs, reliable on any box), use a PDF library. Two solid options for a VB.NET 2010/.NET 4.0-era project are:

  • iTextSharp 5.x: mature and works for this scenario, but it is end-of-life and dual-licensed (AGPL/commercial). Verify licensing before deploying. (itextpdf.com)
  • PDFsharp/MigraDoc: open source (MIT) and actively maintained; good if you prefer a permissive license. (github.com)

Minimal example with iTextSharp that preserves line breaks using a monospaced font. Add a reference to iTextSharp.dll and call this method:

Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf

Public Sub TxtToPdf(txtPath As String, pdfPath As String)
    Dim bf = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
    Dim f As New iTextSharp.text.Font(bf, 10)

    Using fs As New FileStream(pdfPath, FileMode.Create, FileAccess.Write, FileShare.None)
        Using doc As New Document(PageSize.LETTER, 36, 36, 36, 36)
            PdfWriter.GetInstance(doc, fs)
            doc.Open()
            For Each line As String In File.ReadLines(txtPath)
                doc.Add(New Paragraph(line, f))
            Next
            doc.Close()
        End Using
    End Using
End Sub

Tips:

  • Use a monospaced font (as above) for fixed-width reports. For Unicode text, load a TTF and use Identity-H encoding.
  • If you prefer PDFsharp/MigraDoc, create a Document, add Paragraphs per line, then render with PdfDocumentRenderer. (github.com)

Recommended Answers

All 4 Replies

Thanks, Jim. I'm going to give that one a whirl!

You're the man!

Don

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.