0

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...

4

1 回答 1

7
  1. XMLWorker包含在您需要参考的单独库中,您可以在此处获取最新的
  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,但在转换过程中您无法真正调整任何内容。
于 2014-07-25T13:56:09.823 回答