Hi mashispano,
Refer below sample. Add the reference of Windows.Forms from add reference window in your project for reading the RTF file.
Namespaces
C#
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using System.IO;
VB.Net
Imports PdfSharp.Drawing
Imports PdfSharp.Pdf
Imports System.IO
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
// Create a new PDF document.
using (PdfDocument document = new PdfDocument())
{
// Adding an empty page.
PdfPage page = document.AddPage();
// Get an XGraphics object for drawing.
using (XGraphics gfx = XGraphics.FromPdfPage(page))
{
// Creating font.
XFont font = new XFont("Time New Roman", 15, XFontStyle.Regular);
// Reading the RTF file using RichTextBox.
System.Windows.Forms.RichTextBox richTextBox = new System.Windows.Forms.RichTextBox();
richTextBox.Rtf = File.ReadAllText(Server.MapPath("~/templates/sample.rtf"));
string text = richTextBox.Text;
// Drawing the text.
gfx.DrawString(text, font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
// Saving the document.
string filename = Server.MapPath("~/export/" + "SAMPLE_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf");
document.Save(filename);
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
' Create a new PDF document.
Using document As PdfDocument = New PdfDocument()
' Adding an empty page.
Dim page As PdfPage = document.AddPage()
' Get an XGraphics object for drawing.
Using gfx As XGraphics = XGraphics.FromPdfPage(page)
' Creating font.
Dim font As XFont = New XFont("Time New Roman", 15, XFontStyle.Regular)
' Reading the RTF file using RichTextBox.
Dim richTextBox As Windows.Forms.RichTextBox = New Windows.Forms.RichTextBox()
richTextBox.Rtf = File.ReadAllText(Server.MapPath("~/templates/sample.rtf"))
Dim text As String = richTextBox.Text
' Drawing the text.
gfx.DrawString(text, font, XBrushes.Black, New XRect(0, 0, page.Width, page.Height), XStringFormats.Center)
' Saving the document.
Dim filename As String = Server.MapPath("~/export/" & "SAMPLE_" & DateTime.Now.ToString("yyyyMMddHHmmss") & ".pdf")
document.Save(filename)
End Using
End Using
End Sub