iTextsharp html to pdf in .Net core Exception抛出:'System.MissingMethodException'在itextsharp.xmlworker.dll中

时间:2017-08-16 11:29:32

标签: c# asp.net-mvc itext pdf-generation

我正在使用iTextSharp使用外部css文件从HTML创建pdf。我的代码是:

string filepath = _configuration["RepositoryPath"];
FileName = FileName + ".pdf";
filepath = filepath + "\\" + FileName;
FileStream mFileStream = null;
PdfWriter pdfWr = null;
Document pdfDoc = null;
MyEvent myEvent = null;
try
{
    _log.WriteLogMessage("Request for Create PDF File: " + filepath);
    using (pdfDoc = new Document(PageSize))
    {
        if (PageMargin > 0)
            pdfDoc.SetMargins(PageMargin, PageMargin, PageMargin, 0);

        if (File.Exists(filepath))
            File.Delete(filepath);
        mFileStream = new FileStream(filepath, System.IO.FileMode.Create);
        pdfWr = PdfWriter.GetInstance(pdfDoc, mFileStream);

        myEvent = new MyEvent(Content, PageBorder);
        pdfWr.PageEvent = myEvent;

        pdfDoc.Open();
        pdfDoc.NewPage();
        var htmlContext = new HtmlPipelineContext(null);
        htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
        var cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
        if (CssFiles != null)
            foreach (var item in CssFiles)
            {
                cssResolver.AddCssFile(item, true);
            }
        var pipeline = new CssResolverPipeline(cssResolver,
            new HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDoc, pdfWr)));
        var worker = new XMLWorker(pipeline, true);
        var parser = new XMLParser(worker);
        for (int contentCount = 0; contentCount < Content.Length; contentCount++)
        {
            if (!String.IsNullOrEmpty(Content[contentCount].ToString()))
            {
                Content[contentCount] = Content[contentCount].Replace("<br>", "<br />").Replace("\r", "").Replace("\n", "");
            }
            using (var sr = new StringReader(Content[contentCount].ToString()))
            {
                parser.Parse(sr);//Exception occurs
            }

            if (contentCount != Content.Length - 1)
                pdfDoc.NewPage();
        }
    }

    if (PageSizeType == "BOOKLET")
    {
        manipulatePdf(filepath, filepath.Replace(".pdf", "_booklet.pdf"));
    }

}
catch (Exception ex)
{
    _log.WriteLogException(ex);
}
finally
{
    pdfDoc.Close();
    mFileStream.Close();
}

但我得到了例外

  

抛出异常:'System.MissingMethodException'中   抛出异常抛出异常:'System.IO.IOException'   itextsharp.dll

请帮助我在哪里做错了。

0 个答案:

没有答案