使用iTextSharp xmlworkerhelper的ASP.NET编译器错误

kar*_*arn 0 c# pdf asp.net itextsharp xmlworker

我正在尝试编写一个应用程序,它将触发我在ASP.NET中构建的一系列Web报表.所以我有一个Web应用程序,其中页面存在,但我真的不想使用该网站查看页面,我想创建我可以提供给我的客户的PDF.

我正在尝试编写的应用程序是一个控制台应用程序,在c#中使用Visual Studio 2010编写并使用iTextSharp v5.5.2和.NET 4.5.它目前只是一个框架,因为我尚未完成"解雇PDF"部分.我正在从网上访问一个页面并试图将其保存为PDF,但我找不到任何有关我得到的编译错误的信息.我在应用程序中有以下Using指令:

using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.xml;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using iTextSharp.text.io;
using iTextSharp.text.xml.simpleparser;
using iTextSharp.text.html.simpleparser;

namespace ConsoleApplication1
{
    public partial class BuildReports : System.Web.UI.Page
    {
        static void Main(string[] args)
        {
            WebRequest wReq = WebRequest.Create("http://www.somesite.com/");
            WebResponse wRsp = null;

            try
            {
                wRsp = wReq.GetResponse();
            }
            catch (Exception e)
            {
                Console.WriteLine("{0} exception caught.", e);
            }

            Console.WriteLine(((HttpWebResponse)wRsp).StatusDescription);
            Stream sRsp = wRsp.GetResponseStream();
            StreamReader sRdr = new StreamReader(sRsp);
            string sHtml = string.Empty;
            sHtml = sRdr.ReadToEnd();
            Console.WriteLine(sHtml);
            StringReader sr = new StringReader(sHtml);
            Document doc = new Document();
Run Code Online (Sandbox Code Playgroud)

//以下行表示我需要在// XmlWorkerHelper和XmlWorker声明中使用Using指令或Reference.需要什么参考?

            XmlWorkerHelper.GetInstance(doc, new FileStream("test.pdf", FileMode.Create));
            XmlWorker obj = new XmlWorker(doc);
Run Code Online (Sandbox Code Playgroud)

// ------------------------------------------------ ---------------------------------------

            doc.Open();
            obj..Parse(sr);
            doc.Close();
            sRdr.Close();
            sRsp.Close();
        }
    }
    public class CustomHTTPModule : IHttpModule
    {
        public CustomHTTPModule()
        {
            // Class constructor.
        }

        // Classes that inherit IHttpModule  
        // must implement the Init and Dispose methods. 
        public void Init(HttpApplication app)
        {

            app.AcquireRequestState += new EventHandler(app_AcquireRequestState);
            app.PostAcquireRequestState += new EventHandler(app_PostAcquireRequestState);
        }

        public void Dispose()
        {
            // Add code to clean up the 
            // instance variables of a module.
        }

        // Define a custom AcquireRequestState event handler. 
        public void app_AcquireRequestState(object o, EventArgs ea)
        {
            HttpApplication httpApp = (HttpApplication)o;
            HttpContext ctx = HttpContext.Current;
            ctx.Response.Write(" Executing AcquireRequestState ");
        }

        // Define a custom PostAcquireRequestState event handler. 
        public void app_PostAcquireRequestState(object o, EventArgs ea)
        {
            HttpApplication httpApp = (HttpApplication)o;
            HttpContext ctx = HttpContext.Current;
            ctx.Response.Write(" Executing PostAcquireRequestState ");
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题:
1.我需要什么参考或Using指令来解决XmlWorkerHelper和XmlWorker声明的编译错误?
2.是否还有其他我错过的导致编译错误的东西?
我的报告主要由图形元素组成.iTextSharp是否能够在没有大量操作的情况下生成PDF?

谢谢...

Chr*_*aas 6

  1. XMLWorker包含在您需要引用的单独库中,您可以在此处获取最新信息.
  2. 套管很重要,前四个字母XMLWorker需要大写.一旦你这样做(并添加上面的参考),你应该能够右键单击并让VS自动using为你找出指令.如果没有,你正在寻找using iTextSharp.tool.xml;
  3. 你只需要尝试看看.如果你的目标是制作一个看起来与现有网页完全相同的PDF和/或你有相当复杂的HTML和CSS,那么你最好不要尝试wkhtmltopdf.iTextSharp的XMLWorker发光,因为它将HTML和CSS解析为iTextSharp对象,您可以在写入PDF之前选择对其进行操作.并非所有HTML/CSS范例/规则都可以通过这种方式轻松表达,而且XMLWorker不支持这些.wkhtmltopdf可以制作看起来很棒的PDF,但在转换过程中你无法真正调整任何内容.