ITextSharp - MVC / HTMLWorker one line to add to a paragraph

I send the contents of Telerik MVC Editor to the controller using Ajax as a string: I exit 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 in 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 paragraphs to the same page, this makes a new page.

I tried using the following to create 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 the following 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.

+4
source share
1 answer

I worked a lot on iTextSharp, and I usually take the approach of creating a table or nested tables; if necessary, with multiple rows and columns and using colspan to distribute 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 once as soon as you try in the question. otherwise, this approach will certainly not lead to a new page.

EDIT:

see 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); 

Download google to fill in, margins, borders and coloring, etc.

+3
source

Source: https://habr.com/ru/post/1481577/


All Articles