4

我正在使用 Ajax 作为字符串将 Telerik MVC 编辑器的内容发送到控制器:我出来为:

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

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

我尝试使用以下内容使用 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

我会很感激你的建议,在此先感谢。

4

1 回答 1

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

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

于 2013-05-21T19:51:42.113 回答