0

我正在尝试使用“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 会抛出异常。有什么想法吗?谢谢

4

1 回答 1

1

如果您想从视图中打印 pdf,则只需传递视图 (HTML) 的字符串格式

在此功能中,您将获得 pdf

String htmlText = html.ToString();   <== Html is a view page html string 
Document document = new Document();
string filePath = HostingEnvironment.MapPath("~/Content/Pdf/");
PdfWriter.GetInstance(document, new FileStream(filePath + "\\pdf-"+Name+".pdf", FileMode.Create));
document.Open();

iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
FontFactory.Register(Path.Combine(_webHelper.MapPath("~/App_Data/Pdf/arial.ttf")),  "Garamond");   // just give a path of arial.ttf 
StyleSheet css = new StyleSheet();
css.LoadTagStyle("body", "face", "Garamond");
css.LoadTagStyle("body", "encoding", "Identity-H");
css.LoadTagStyle("body", "size", "12pt");
hw.SetStyleSheet(css);

hw.Parse(new StringReader(htmlText));
于 2014-03-18T11:16:37.910 回答