Text file, as .txt file which a file format for files consisting of text usually without or containing very little formatting (ex: no bolding or italics). The precise definition of the .txt format is not specified, but typically matches the format accepted by the system terminal or simple text editor. Files with the .txt extension can easily be read or opened by any program that reads text and, for that reason, are considered universal (or platform independent).
How to Use C# Create PDF from Image?
Spire.PDF for .NET is a PDF document creation component that enables your .NET applications to read, write and manipulate PDF documents without using Adobe Acrobat. As .NET PDF component, it allows users to create PDF from Image by using C# code.
Download Spire.PDF (or Spire.Office which includes Spire.PDF) with .NET Framework together and use the sample code below to use C# create PDF fromText.
Create PDF from Text
private static void DrawText(PdfSection section)
{
PdfPageBase page = section.Pages.Add();
string[] lines = File.ReadAllLines("test.txt");
int y = 20;
foreach (string line in lines)
{
PdfSolidBrush brush = new PdfSolidBrush(Color.Blue);
Font textFont = new Font("Arial", 14f, FontStyle.Bold);
PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(textFont);
page.Canvas.DrawString(line, trueTypeFont, brush, 0, y);
y += 20;
//Add new page
if (y > page.Canvas.ClientSize.Height)
{
page = section.Pages.Add();
y = 20;
}
}
Set up Text font Style
PdfSolidBrush brush = new PdfSolidBrush(Color.Blue);
Font textFont = new Font("Arial", 14f, FontStyle.Bold);
PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(textFont);
Secure Your PDF Document
private static void Encryption(PdfDocument doc)
{
doc.Security.OwnerPassword = "OwnerPassword";
doc.Security.UserPassword = "UserPassword";
}
C# Create PDF from Text Full Code:
using System.IO;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace Test
{
class Program
{
static void Main(string[] args)
{
PdfDocument doc = new PdfDocument();
DrawText(doc.Sections.Add());
Encryption(doc);
doc.SaveToFile("result.pdf");
doc.Close();
}
///
/// Encryption the pdf
///
///
private static void Encryption(PdfDocument doc)
{
doc.Security.OwnerPassword = "OwnerPassword";
doc.Security.UserPassword = "UserPassword";
}
///
/// Draw text to pdf
///
///
private static void DrawText(PdfSection section)
{
PdfPageBase page = section.Pages.Add();
string[] lines = File.ReadAllLines("test.txt");
int y = 20;
foreach (string line in lines)
{
PdfSolidBrush brush = new PdfSolidBrush(Color.Blue);
Font textFont = new Font("Arial", 14f, FontStyle.Bold);
PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(textFont);
page.Canvas.DrawString(line, trueTypeFont, brush, 0, y);
y += 20;
//Add new page
if (y > page.Canvas.ClientSize.Height)
{
page = section.Pages.Add();
y = 20;
}
}
}
}
}
More about Spire.PDF
Download Spire.PDF
Purchase Spire.PDF