Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 + "/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.

like image 703
Sara Avatar asked May 22 '11 02:05

Sara


People also ask

Can I use iTextSharp for free?

Yes, as long as you don't try to sell it to your customer as your own product (;-)), you're free to use and deploy it as part of your own app.

Can I use iTextSharp in .NET core?

NET Core and the related UWP standard are not supported by any iText projects.

Is iTextSharp deprecated?

PLEASE NOTE: iTextSharp is EOL, and has been replaced by iText 7. Only security fixes will be added. We HIGHLY recommend customers use iText 7 for new projects, and to consider moving existing projects from iTextSharp to iText 7 to benefit from the many improvements such as: HTML to PDF (PDF/UA) conversion.


2 Answers

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.

like image 155
Mark Storer Avatar answered Oct 02 '22 17:10

Mark Storer


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
like image 37
Chris Fulstow Avatar answered Oct 02 '22 17:10

Chris Fulstow