Reputation: 2295
I am sending the contents of Telerik MVC Editor to the controller using Ajax as a string: I comes out as:
"<strong>Hello world!</strong> <object height=\"1\" id=\"plugin0\" style=\"position:absolute;z-index:1000;\" type=\"application/x-dgnria\" width=\"1\"><param name=\"tabId\" value=\"{84594B7B-865F-4AD7-A798-294A8B0EB376}\" /></object>"
In the controller, I save the string to a session variable using the following:
string comments = HttpUtility.HtmlDecode(Text);
MySession.Current.pdfText = comments;
I can convert it to PDF using
.....
HTMLWorker parser = new HTMLWorker(document);
.......
However I could not add any other paragraphes to the same page, it makes a new page.
I tried to use the following to make a new paragraph using HTMLWORK:
string PDFText = MySession.Current.pdfText;
string PDFText1 = HTMLWorker.Parse(PDFText);
StringReader reader = new StringReader(PDFText1);
paragraph.Add(reader);
I got these errors:
cannot convert from 'System.IO.StringReader' to 'string', and
The best overloaded method match for 'iTextSharp.text.html.simpleparser.HTMLWorker.Parse(System.IO.TextReader)' has some invalid arguments, and
The best overloaded method match for 'iTextSharp.text.Phrase.Add(string)' has some invalid arguments
I would appreciate your suggestions, thanks in advance.
Upvotes: 4
Views: 2446
Reputation: 9380
I have worked a lot on iTextSharp and I usually follow the approach of creating a table or nested tables; if needed, with multiple rows and columns and using colspan to spread out my content on the page.
PdfPTable table = new PdfPTable(1); //Create a new table with one column
PdfPCell cell = new PdfPCell(); //Create an empty cell
StyleSheet style = new StyleSheet(); //Declare a stylesheet
style.LoadTagStyle("h1", "color", "red"); //Create styles for your html tags which you think will be there in PDFText
ArrayList objects = HTMLWorker.ParseToList(new StringReader(PDFText),style); //This transforms your HTML to a list of PDF compatible objects
for (int k = 0; k < objects.Count; ++k)
{
cell.AddElement((IElement)objects[k]); //Add these objects to cell one by one
}
table.AddCell(cell); //Add cell to table
document.add(table) //Add table to the document
try it out with just the paragraph once as you were trying in the question. otherwise this approach will definitely not result in a new page.
EDIT:
please see the updated code
PdfPTable table = new PdfPTable(2); //Create a new table with one column
PdfPCell cellLeft = new PdfPCell(); //Create an empty cell
StyleSheet style = new StyleSheet(); //Declare a stylesheet
style.LoadTagStyle("h1", "color", "red"); //Create styles for your html tags which you think will be there in PDFText
List<IElement> objects = HTMLWorker.ParseToList(new StringReader(PDFText),style); //This transforms your HTML to a list of PDF compatible objects
for (int k = 0; k < objects.Count; ++k)
{
cellLeft.AddElement((IElement)objects[k]); //Add these objects to cell one by one
}
table.AddCell(cellLeft); //Add cell to table
string url = "http://localhost:1713/PDF/Images/sample.jpg"; //Image Path(can give local machine path for testing)
PdfPCell cellRight = new PdfPCell();
Image jpg = Image.GetInstance(new Uri(url));
cellRight.AddElement(jpg);
table.AddCell(cellRight);
document.add(table);
Please google for padding, margins, borders and coloring etc..
Upvotes: 3