简体   繁体   中英

Text to PDF in VB.Net

Does anyone know how to take a string and save it to a PDF file?

Dim str As String
'Put it into PDF Document
'Save to PDF DOcument

Thanks,

Beginner

You could use itextSharp Library to create pdf document. I am using C#:

Your string is

str="Hello World!"

Use Library:

using iTextSharp.text;
using iTextSharp.text.pdf;

Create Document as:

Document pdfDocument = new Document(PageSize.A4, 20f, 20f, 20f, 20f);

Write document to disk:

PdfWriter.GetInstance(pdfDocument, new FileStream(filePathToDisk, FileMode.Create));

Set your string:

Paragraph newParagraph = new Paragraph(str);

pdfDocument.Open();
pdfDocument.Add(newParagraph);
pdfDocument.Close();
pdfDocument.Dispose();

This will create your Pdf File.

Try using Docotic.Pdf library for this (disclaimer: I am one of developers of the library).

Below is a sample that show how to draw a single line of text or multiple lines of text inscribed in specified rectangle.

Imports System.Diagnostics
Imports System.Drawing

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class DrawText
        Public Shared Sub Main()
            Dim pathToFile As String = "DrawText.pdf"

            Using pdf As New PdfDocument()
                Dim canvas As PdfCanvas = pdf.Pages(0).Canvas

                canvas.DrawString(10, 50, "Hello, world!")

                Const longString As String = "Lorem ipsum dolor sit amet, consectetur adipisicing elit"
                canvas.DrawString(longString, New RectangleF(10, 70, 40, 150), PdfTextAlign.Left, PdfVerticalAlign.Top)
                canvas.DrawText(longString, New RectangleF(70, 70, 40, 150), PdfTextAlign.Left, PdfVerticalAlign.Top)

                pdf.Save(pathToFile)
            End Using

            Process.Start(pathToFile)
        End Sub
    End Class
End Namespace

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM