iTextSharp - MVC / HTMLWorker一个要添加到段落的字符串

时间:2013-05-19 00:39:58

标签: asp.net itextsharp

我使用Ajax作为字符串将Telerik MVC Editor的内容发送到控制器: 我出来的时候:

"<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>"

在控制器中,我使用以下命令将字符串保存到会话变量:

        string comments = HttpUtility.HtmlDecode(Text);
        MySession.Current.pdfText = comments;

我可以使用

将其转换为PDF
.....
 HTMLWorker parser = new HTMLWorker(document);
.......

但是我无法将任何其他的paragraphes添加到同一页面,它会创建一个新页面。

我尝试使用以下内容使用HTMLWORK创建一个新段落:

    string PDFText = MySession.Current.pdfText;
    string PDFText1 = HTMLWorker.Parse(PDFText);
    StringReader reader = new StringReader(PDFText1);
    paragraph.Add(reader);

我收到了这些错误:

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

我很感激你的建议,谢谢你。

1 个答案:

答案 0 :(得分:3)

我在iTextSharp上做了很多工作,我通常遵循创建表或嵌套表的方法;如果需要,可以使用多行和多列,并使用colspan在页面上展开我的内容。

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
当你在尝试问题时,只需用段落试一试。否则这种方法肯定不会产生新的页面。

修改

请参阅更新后的代码

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

请谷歌填写填充,边距,边框和着色等。