如何使用iTextSharp?

Sar*_*ara 7 vb.net pdf asp.net pdf-generation itextsharp

所以,我需要一个PDF生成器用于我的ASP.NET应用程序.我下载了iTextSharp,因为它似乎是最受欢迎的免费版本.但在搜索互联网后,我并没有真正找到让我开始的信息.到目前为止,我发现的一些教程太混乱了.我知道那里有一本书,但我是学生,不想花钱.我只需要真正基本的逐步信息,最好是VB中的代码.到目前为止,我发现的最基本的教程是http://www.mikesdotnetting.com/Article/80/Create-PDFs-in-ASP.NET-getting-started-with-iTextSharp,但它对我不起作用.我试着遵循它并想出了这段代码:

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();
}
}
Run Code Online (Sandbox Code Playgroud)

但它给了我一个错误:"CS1502:'iTextSharp.text.pdf.PdfWriter.GetInstance(iTextSharp.text.Document,System.IO.Stream)'的最佳重载方法匹配'有一些无效的参数"并且突出显示的行是PdfWriter.GetInstance ...

所以无论如何,我想知道是否有人知道我在本教程中做错了什么,或者我可以使用的其他教程.或者如果你想给我一个如何以你自己的话开始的基本解释,这将是伟大的.请记住,我很遗憾对此一无所知.:) 谢谢.

Mar*_*rer 4

很难说,但我猜你的doc不是 iTextSharp.text.Document;对于所有这些“using”命令,您很可能导入了多个名为“Document”的类,并且得到了错误的类。

您应该能够使用完全限定名称来查看这是否确实是问题所在:

var doc1 = new iTextSharp.text.Document();
Run Code Online (Sandbox Code Playgroud)

(公平警告:我不了解 vb.net,因此实际语法可能完全不同)

using垃圾邮件迟早会造成名称冲突问题。在这种情况下“更快”。