我正在尝试使用“li”和“image”标签转换简单的(现在)视图。在控制器中,我首先将视图作为字符串
ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, "Print");
var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
stringView = sw.GetStringBuilder().ToString();
}
之后,我尝试根据此文档解析字符串
Document document = new Document();
document.SetPageSize(iTextSharp.text.PageSize.A4);
document.Open();
PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
htmlContext.SetImageProvider(new ImageProvider(Server.MapPath(Config.UploadDirectory), Request.Url.ToString()));
//StyleAttrCSSResolver cssRevolver = new StyleAttrCSSResolver();
var cssRevolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);
cssRevolver.AddCssFile(Server.MapPath("~/Content/Site.css"), false);
IPipeline pipeline = new CssResolverPipeline(cssRevolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer)));
XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser parser = new XMLParser(worker);
var memViewStream = new MemoryStream(Encoding.UTF8.GetBytes(stringView));
memViewStream.Position = 0;
parser.Parse(memViewStream); //this line throw the exception
document.Close();
writer.Close();
Response.ContentType = "application/pdf";
return null;
但我收到此错误,无法弄清楚出了什么问题
你调用的对象是空的。
堆
at iTextSharp.text.pdf.PdfOutline.InitOutline(PdfOutline parent, String title, Boolean open)
at iTextSharp.text.pdf.PdfOutline..ctor(PdfOutline parent, PdfDestination destination, Paragraph title, Boolean open)
at iTextSharp.text.pdf.PdfOutline..ctor(PdfOutline parent, PdfDestination destination, Paragraph title)
at iTextSharp.tool.xml.html.Header.WriteH.Write(PdfWriter writer, Document doc)
at iTextSharp.text.pdf.PdfDocument.Add(IElement element)
at iTextSharp.text.Phrase.Process(IElementListener listener)
at iTextSharp.text.pdf.PdfDocument.Add(IElement element)
at iTextSharp.text.List.Process(IElementListener listener)
at iTextSharp.text.pdf.PdfDocument.Add(IElement element)
at iTextSharp.text.Document.Add(IElement element)
at iTextSharp.tool.xml.pipeline.end.PdfWriterPipeline.Write(IWorkerContext context, ProcessObject po)
at iTextSharp.tool.xml.pipeline.end.PdfWriterPipeline.Close(IWorkerContext context, Tag t, ProcessObject po)
at iTextSharp.tool.xml.XMLWorker.EndElement(String tag, String ns)
at iTextSharp.tool.xml.parser.XMLParser.EndElement()
我不知道为什么 InitOutline 会抛出异常。有什么想法吗?谢谢