By Carmel ShvartzmanIn this tutorial we'll learn how to convert text into PDF online using the TinyMCE Rich Editor in an Asp.Net MVC application, and download the PDF file to the user's machine.
For this tutorial, we'll need an open source application named ITextSharp, a FREE download from SOURCEFORGE, and also another free plugin named TinyMCE Editor, which can be installed in your application using the NuGet Package Manager.
Then, we'll create an empty MVC application, and we will use ITextSharp and TinyMCE to create our PDF file and download it , showing as follows:
We'll need ITextSharp and TinyMCE. So install the TinyMCE using NuGet :
Then download ITextSharp from SourceFORGE :
Take a look at the files added by NuGet to your application:
Add the "usings" to the Controller :
Then, create an Action to handle HTTP POST requests, and return the PDF file over the response :
This is the code you need to create the simple PDF file :
public void CreatePdf(string text)
Document file = new Document();
FileStream stream = new FileStream(Server.MapPath("~/Images/") + "NewFile.pdf", FileMode.Create);
List<iTextSharp.text.IElement> parsedList = null;
parsedList = HTMLWorker.ParseToList(new StringReader(text), null);
for (int index = 0; index < parsedList.Count; index++)
Take care to CLOSE the stream before you leave the Action method.
Now that we're done with the PDF, we need to prepare the TinyMCE at client side. Browse to the official open source site of TinyMCE :
Therefore, open the HTML View where you want to add the Editor , and COPY-PASTE from the former example the <textarea> we need, and a reference to that script file :
That's all!!!!!!! (You expecting more ? ) Build the application and open the web page:
...And you get the PDF file to download :
That's all!!In this tutorial we've learn how to convert text into PDF online using the TinyMCE Rich Editor in an Asp.Net MVC application, and download the PDF file to the user's machine.
כתב: כרמל שוורצמן