PDF: How to convert a text file to a PDF document?
In This Topic
Here is an example that demonstrates how to convert a text file to a PDF document:
' The project, which uses this code, must have references to the following assemblies:
' - Vintasoft.Imaging
' - Vintasoft.Imaging.Pdf
''' <summary>
''' Converts a text file to a PDF document.
''' </summary>
''' <param name="sourceTextFilename">The filename of source text file.</param>
''' <param name="destPdfFilename">The filename of destination PDF document.</param>
Public Shared Sub ConvertTextFileToPdfDocument(sourceTextFilename As String, destPdfFilename As String)
' get text of text file
Dim text As String = System.IO.File.ReadAllText(sourceTextFilename, System.Text.Encoding.UTF8)
' create PDF document
Using pdfDocument As New Vintasoft.Imaging.Pdf.PdfDocument(destPdfFilename, Vintasoft.Imaging.Pdf.PdfFormat.Pdf_14)
' create page of A4 size
Dim pdfPage As New Vintasoft.Imaging.Pdf.Tree.PdfPage(pdfDocument, Vintasoft.Imaging.ImageSize.FromPaperKind(Vintasoft.Imaging.PaperSizeKind.A4))
' add page to the PDF document
pdfDocument.Pages.Add(pdfPage)
' get PdfGrahics of page
Using graphics As Vintasoft.Imaging.Pdf.Drawing.PdfGraphics = Vintasoft.Imaging.Pdf.Drawing.PdfGraphics.FromPage(pdfPage)
' create a font that should be used for drawing a text
Dim font As New System.Drawing.Font(System.Drawing.FontFamily.GenericSerif, 14)
' create a brush that should be used for drawing a text
Dim brush As New Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.Black)
' specify a rectangle where text should be drawn
Dim rect As System.Drawing.RectangleF = pdfPage.MediaBox
' specify a string format that should be used for drawing a text
Dim stringFormat As System.Drawing.StringFormat = System.Drawing.StringFormat.GenericDefault
' draw text on the PDF page
graphics.DrawString(text, font, brush, rect, stringFormat)
End Using
' save PDF document
pdfDocument.SaveChanges()
End Using
End Sub
// The project, which uses this code, must have references to the following assemblies:
// - Vintasoft.Imaging
// - Vintasoft.Imaging.Pdf
/// <summary>
/// Converts a text file to a PDF document.
/// </summary>
/// <param name="sourceTextFilename">The filename of source text file.</param>
/// <param name="destPdfFilename">The filename of destination PDF document.</param>
public static void ConvertTextFileToPdfDocument(
string sourceTextFilename,
string destPdfFilename)
{
// get text of text file
string text = System.IO.File.ReadAllText(sourceTextFilename, System.Text.Encoding.UTF8);
// create PDF document
using (Vintasoft.Imaging.Pdf.PdfDocument pdfDocument =
new Vintasoft.Imaging.Pdf.PdfDocument(destPdfFilename, Vintasoft.Imaging.Pdf.PdfFormat.Pdf_14))
{
// create page of A4 size
Vintasoft.Imaging.Pdf.Tree.PdfPage pdfPage =
new Vintasoft.Imaging.Pdf.Tree.PdfPage(pdfDocument,
Vintasoft.Imaging.ImageSize.FromPaperKind(Vintasoft.Imaging.PaperSizeKind.A4));
// add page to the PDF document
pdfDocument.Pages.Add(pdfPage);
// get PdfGrahics of page
using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics graphics =
Vintasoft.Imaging.Pdf.Drawing.PdfGraphics.FromPage(pdfPage))
{
// create a font that should be used for drawing a text
System.Drawing.Font font =
new System.Drawing.Font(System.Drawing.FontFamily.GenericSerif, 14);
// create a brush that should be used for drawing a text
Vintasoft.Imaging.Pdf.Drawing.PdfBrush brush =
new Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.Black);
// specify a rectangle where text should be drawn
System.Drawing.RectangleF rect = pdfPage.MediaBox;
// specify a string format that should be used for drawing a text
System.Drawing.StringFormat stringFormat = System.Drawing.StringFormat.GenericDefault;
// draw text on the PDF page
graphics.DrawString(text, font, brush, rect, stringFormat);
}
// save PDF document
pdfDocument.SaveChanges();
}
}