Not long ago, I share a really easy method to Extract Text and Image from PDF Document with C#,VB.NET by using a PDF creation component Spire.PDF. Today, I want to introduce an equally simple way to convert text to PDF Document.
I have referred that there are many top advantages of PDF. Such as the software to view PDF is freeware, PDF files meet legal document requirements; PDF is “read-only”, practical and economical in that it allows the documents to be stored on a company's server. As such, it doesn't add hardware cost to the system; PDF can be used across platforms, and the compression of a scanned image into a PDF file is sizable etc.
That is to say, any format converted to PDF Document can benefit from PDF. So convert text to PDF is really indispensable. I always think that Spire.PDF is my best friend when I do conversion task, so it is rather necessary to install Spire.PDF in my system.
Freely Download Spire.PDF
Procedure:
1.Create a new PDF Document. Then, add one page in the PDF file.
2.Draw text and set the format of the text.
3.Encrypt the PDF Document by setting owner password and user password.
4.Save to the PDF file.
I have referred that there are many top advantages of PDF. Such as the software to view PDF is freeware, PDF files meet legal document requirements; PDF is “read-only”, practical and economical in that it allows the documents to be stored on a company's server. As such, it doesn't add hardware cost to the system; PDF can be used across platforms, and the compression of a scanned image into a PDF file is sizable etc.
That is to say, any format converted to PDF Document can benefit from PDF. So convert text to PDF is really indispensable. I always think that Spire.PDF is my best friend when I do conversion task, so it is rather necessary to install Spire.PDF in my system.
Freely Download Spire.PDF
Procedure:
1.Create a new PDF Document. Then, add one page in the PDF file.
2.Draw text and set the format of the text.
3.Encrypt the PDF Document by setting owner password and user password.
4.Save to the PDF file.
Main Code:
C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Graphics.Fonts;
namespace text_to_pdf
{
class Program
{
static void Main(string[] args)
{
PdfDocument doc = new PdfDocument();
DrawText(doc.Sections.Add());
Encryption(doc);
doc.SaveToFile("result.pdf");
doc.Close();
System.Diagnostics.Process.Start("result.pdf");
}
// Encryption the pdf
private static void Encryption(PdfDocument doc)
{
doc.Security.OwnerPassword = "OwnerPassword";
doc.Security.UserPassword = "UserPassword";
}
private static void DrawText(PdfSection section)
{
PdfPageBase page = section.Pages.Add();
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 14);
PdfStringFormat format = new PdfStringFormat();
float pageWidth = page.Canvas.ClientSize.Width;
PdfBrush brush = PdfBrushes.SpringGreen;
float y = 15;
//content
string text = "Spire.Office for .NET is a compilation of every .NET component offered by e-iceblue. It includes Spire.Doc, Spire.XLS, Spire.PDFViewer, Spire.PDF and Spire.DataExport. Spire.Office contains the most up-to-date versions of the components above. Using Spire.Office for .NET developers can create a wide range of applications." +
"Consistently offer high-quality, pure and stable .NET components for handling office format files. E-iceblue product lines include components for Word, Excel, PDF and component specially used for data exporting. For E-iceblue, customer service and products quality are the same most important and we guarantee 100% honesty to" +
"customers. E-iceblue Welcome any feedback from customers and promise timely and effectively response. E-iceblue .NET component products save your time and save your money from now on.";
PdfStringLayouter textLayouter = new PdfStringLayouter();
PdfStringLayoutResult result
= textLayouter.Layout(text, font, format, new SizeF(pageWidth, 0));
foreach (LineInfo line in result.Lines)
{
page.Canvas.DrawString(line.Text, font, brush, 0, y, format);
y = y + result.LineHeight;
}
}
}
}
VB.NET Code:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Drawing
Imports System.Drawing.Text
Imports System.IO
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Graphics.Fonts
Namespace text_to_pdf
Class Program
Private Shared Sub Main(args As String())
Dim doc As New PdfDocument()
DrawText(doc.Sections.Add())
Encryption(doc)
doc.SaveToFile("result.pdf")
doc.Close()
System.Diagnostics.Process.Start("result.pdf")
End Sub
' Encryption the pdf
Private Shared Sub Encryption(doc As PdfDocument)
doc.Security.OwnerPassword = "OwnerPassword"
doc.Security.UserPassword = "UserPassword"
End Sub
Private Shared Sub DrawText(section As PdfSection)
Dim page As PdfPageBase = section.Pages.Add()
Dim font As New PdfFont(PdfFontFamily.Helvetica, 14)
Dim format As New PdfStringFormat()
Dim pageWidth As Single = page.Canvas.ClientSize.Width
Dim brush As PdfBrush = PdfBrushes.SpringGreen
Dim y As Single = 15
'content
Dim text As String = "Spire.Office for .NET is a compilation of every .NET component offered by e-iceblue. It includes Spire.Doc, Spire.XLS, Spire.PDFViewer, Spire.PDF and Spire.DataExport. Spire.Office contains the most up-to-date versions of the components above. Using Spire.Office for .NET developers can create a wide range of applications." & "Consistently offer high-quality, pure and stable .NET components for handling office format files. E-iceblue product lines include components for Word, Excel, PDF and component specially used for data exporting. For E-iceblue, customer service and products quality are the same most important and we guarantee 100% honesty to" & "customers. E-iceblue Welcome any feedback from customers and promise timely and effectively response. E-iceblue .NET component products save your time and save your money from now on."
Dim textLayouter As New PdfStringLayouter()
Dim result As PdfStringLayoutResult = textLayouter.Layout(text, font, format, New SizeF(pageWidth, 0))
For Each line As LineInfo In result.Lines
page.Canvas.DrawString(line.Text, font, brush, 0, y, format)
y = y + result.LineHeight
Next
End Sub
End Class
End Namespace
Preview
Imports System.Linq
Imports System.Text
Imports System.Drawing
Imports System.Drawing.Text
Imports System.IO
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Graphics.Fonts
Namespace text_to_pdf
Class Program
Private Shared Sub Main(args As String())
Dim doc As New PdfDocument()
DrawText(doc.Sections.Add())
Encryption(doc)
doc.SaveToFile("result.pdf")
doc.Close()
System.Diagnostics.Process.Start("result.pdf")
End Sub
' Encryption the pdf
Private Shared Sub Encryption(doc As PdfDocument)
doc.Security.OwnerPassword = "OwnerPassword"
doc.Security.UserPassword = "UserPassword"
End Sub
Private Shared Sub DrawText(section As PdfSection)
Dim page As PdfPageBase = section.Pages.Add()
Dim font As New PdfFont(PdfFontFamily.Helvetica, 14)
Dim format As New PdfStringFormat()
Dim pageWidth As Single = page.Canvas.ClientSize.Width
Dim brush As PdfBrush = PdfBrushes.SpringGreen
Dim y As Single = 15
'content
Dim text As String = "Spire.Office for .NET is a compilation of every .NET component offered by e-iceblue. It includes Spire.Doc, Spire.XLS, Spire.PDFViewer, Spire.PDF and Spire.DataExport. Spire.Office contains the most up-to-date versions of the components above. Using Spire.Office for .NET developers can create a wide range of applications." & "Consistently offer high-quality, pure and stable .NET components for handling office format files. E-iceblue product lines include components for Word, Excel, PDF and component specially used for data exporting. For E-iceblue, customer service and products quality are the same most important and we guarantee 100% honesty to" & "customers. E-iceblue Welcome any feedback from customers and promise timely and effectively response. E-iceblue .NET component products save your time and save your money from now on."
Dim textLayouter As New PdfStringLayouter()
Dim result As PdfStringLayoutResult = textLayouter.Layout(text, font, format, New SizeF(pageWidth, 0))
For Each line As LineInfo In result.Lines
page.Canvas.DrawString(line.Text, font, brush, 0, y, format)
y = y + result.LineHeight
Next
End Sub
End Class
End Namespace
Preview
Note:1.Please set the Target framework to be .NET Framework 4.
2.Using both the owner password and user password can open the file, but if you want to paste the content of the PDF file, only owner password is available.
From the above method, it is not hard to find that it is really very easy to convert text to PDF document with C#/VB.NET by using Spire.PDF. Besides, Spire.PDF also can Convert HTML to PDF, Excel to PDF, Word to PDF, XML to PDF, Image to PDF, Rtf to PDF with C#,VB.NET.
2.Using both the owner password and user password can open the file, but if you want to paste the content of the PDF file, only owner password is available.
From the above method, it is not hard to find that it is really very easy to convert text to PDF document with C#/VB.NET by using Spire.PDF. Besides, Spire.PDF also can Convert HTML to PDF, Excel to PDF, Word to PDF, XML to PDF, Image to PDF, Rtf to PDF with C#,VB.NET.