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...
XMLWorker
is contained in a separate library that you need to reference, you can pick up the latest here.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;
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.