【问题标题】:ASP.NET Compiler errors with iTextSharp xmlworkerhelper带有 iTextSharp xmlworkerhelper 的 ASP.NET 编译器错误
【发布时间】:2014-09-17 08:49:42
【问题描述】:

我正在尝试编写一个应用程序来触发一系列我在 ASP.NET 中构建的网络报告。所以我有一个页面所在的 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();

// 以下几行表明我需要一个 Using 指令或一个引用 // XmlWorkerHelper 和 XmlWorker 声明。需要哪些参考资料??

            XmlWorkerHelper.GetInstance(doc, new FileStream("test.pdf", FileMode.Create));
            XmlWorker obj = new XmlWorker(doc);

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

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

    }
}

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

谢谢...

【问题讨论】:

    标签: c# asp.net pdf itextsharp xmlworker


    【解决方案1】:
    1. XMLWorker 包含在一个单独的库中,您需要参考,您可以pick up the latest here。
    2. 大小写很重要,XMLWorker的前四个字母需要大写。一旦你这样做(并添加上面的参考),你应该能够右键单击并让 VS 自动为你找出 using 指令。如果没有,您正在寻找using iTextSharp.tool.xml;
    3. 您只需尝试一下即可。如果您的目标是制作一个看起来与现有 Web 完全相同的 PDF 和/或您有相当复杂的 HTML 和 CSS,您最好尝试wkhtmltopdf。 iTextSharp 的 XMLWorker 大放异彩,因为它将 HTML 和 CSS 解析为 iTextSharp 对象,您可以在写入 PDF 之前选择操作这些对象。并非所有 HTML/CSS 范例/规则都可以用这种方式轻松表达,并且 XMLWorker 不支持这些。 wkhtmltopdf 可以制作非常漂亮的 PDF,但在转换过程中您无法真正调整任何内容。

    【讨论】:

    • 将 iTextSharp.XMLWorker 添加到引用中。这解决了 XMLWorker 问题...谢谢!为什么分发下载中不包含这个额外的 DLL?
    • using iTextSharp.tool.xml; 创造了奇迹!谢谢@Chris Haas
    • @AbhishekGhosh 但它得到错误工具不在 iTextSharp 中。
    猜你喜欢
    • 2015-08-01
    • 2016-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多