开发者

how to use iTextSharp?

So, I need a PDF generator for my ASP.NET application. I downloaded iTextSharp because it seems to be the most popular free one. But after searching the internet I am not really finding the information I need to get me started. The few tutorials I've found so far are too confusing. I know there's a book out there but I'm a student and don't want to spend the money. I just need really basic step-by-step information, preferably with code in VB. The most basic tutorial I've found so far is http://www.mikesdotnetting.com/Article/80/Create-PDFs-in-ASP.NET-getting-started-with-iTextSharp, but it's not working for me. I tried to follow it and came up with this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO; 


public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    var doc1 = new Document();
    string path = Server.MapPath("PDFs");
    PdfWriter.GetInstance(doc1, new FileStream(path + "/开发者_Go百科Doc1.pdf", FileMode.Create));
    doc1.Open();
    doc1.Add(new Paragraph("My first PDF"));
    doc1.Close();
}
}

But it gives me an error: "CS1502: The best overloaded method match for 'iTextSharp.text.pdf.PdfWriter.GetInstance(iTextSharp.text.Document, System.IO.Stream)' has some invalid arguments" and the line highlighted is PdfWriter.GetInstance...

So anyway, I was wondering if anyone knows either what I did wrong on this tutorial, or what other tutorials I can use. Or if you want to give me a basic explanation of how to get started in your own words, that would be great. Keep in mind I unfortunately know absolutely nothing about this. :) Thanks.


It's hard to tell, but I'm going to guess that your doc isn't an iTextSharp.text.Document; With all those "using" commands, it's quite possible you've imported multiple classes named "Document" and are getting the wrong one.

You should be able to use the fully qualified name to see if that's really the problem:

var doc1 = new iTextSharp.text.Document();

(Fair Warning: I don't know vb.net, so the actual syntax might be Quite Different)

using spam is going to create problems with name collisions sooner or later. "Sooner" in this case.


iTextSharp is a direct port from the Java iText library, so you can refer to any of the native iText docs and usually apply them to C# and .NET.

The best documentation is in the iText in Action book, but you can download the book's example code from the website, and the core API docs are also available online.

There are also some great downloadable .NET iTextSharp source code examples in this CodeProject article:

  • Tutorials on creating PDF files using C# 2.0
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜