ASP.NET Compiler errors with iTextSharp xmlworkerhelper

一世执手 提交于 2019-12-25 01:43:46

问题


I'm trying to write an application that will fire off a series of web reports that I've built in ASP.NET. So I have a web application where the pages live, but I don't really want to use the website to see the pages, I want to create PDFs that I can deliver to my clients.

The application I'm trying to write is a console app, written in Visual Studio 2010 in c# and using iTextSharp v5.5.2 and .NET 4.5. It's currently just a framework as I haven't got to the "firing off PDFs" part yet. I'm accessing a page from the web and trying to save it as a PDF, but I can't find any info on the compile errors I'm getting. I have the following Using directives in the application:

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();

// The following lines are indicating that I need a Using directive or a Reference on the // XmlWorkerHelper and XmlWorker declarations. What references are needed??

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

    }
}

My questions:
1. What reference or Using directive do I need to resolve the compile errors on the XmlWorkerHelper and XmlWorker declarations?
2. Is there something else I'm missing that's causing the compile errors?
3. My reports are mainly composed of graphical elements. Will iTextSharp be able to produce the PDFs without a lot of manipulations?

Thanks...


回答1:


  1. XMLWorker is contained in a separate library that you need to reference, you can pick up the latest here.
  2. Casing is important, the first four letters of XMLWorker need to be capitalized. Once you do that (and add the reference above) you should be able to right-click and have VS automatically figure out the using directive for you. If not, you're looking for using iTextSharp.tool.xml;
  3. You'll just have to try it and see. If your goal is to make a PDF that looks exactly like an existing web and/or you have fairly complicated HTML and CSS you might be better off trying wkhtmltopdf. iTextSharp's XMLWorker shines because it parses HTML and CSS into iTextSharp objects that you can optionally manipulate before writing to PDF. Not all HTML/CSS paradigms/rules can be easily expressed this way and XMLWorker doesn't support these. wkhtmltopdf can make really great looking PDFs but you can't really tweak anything during the conversion process.


来源:https://stackoverflow.com/questions/24956740/asp-net-compiler-errors-with-itextsharp-xmlworkerhelper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!