iTextSharp在Winforms项目中无法正常运行

时间:2013-04-03 12:57:31

标签: c# winforms itextsharp

环境

C#2008 Express Edition,Windows Forms,iTextSharp 5.4.0。

我将iTextSharp.dll放在我的项目bin文件夹中,我在项目中添加了对它的引用,我有名称空间using iTextSharpiTextSharp.textiTextSharp.text.pdf我尝试了以下几段代码,但没有创建任何错误消息。

这是第一个块:

Document doc1 = new Document();
string path = "C:/Temp/Testing";
PdfWriter.GetInstance(doc1, new FileStream(path + "first.pdf", FileMode.Create));
doc1.Open();
doc1.Add(new Paragraph("Test"));
doc1.Close();

这是第二个块:

FileStream fs = new FileStream("C:\\Temp\\Testing" + "Test.pdf", FileMode.Create);
Document doc = new Document();
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
doc.Open();
doc.Add(new Paragraph("Hello"));
doc.Close();
writer.Close();
fs.Close();

1 个答案:

答案 0 :(得分:0)

此示例打开一个对话框,用户可以保存该文件。使用代码转换器将VB代码转换为C#。

Using ms As New System.IO.MemoryStream()
Using doc As New iTextSharp.text.Document(iTextSharp.text.PageSize.A4)
Using w As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, ms)

Dim FileName As String = "Name.pdf"


Here create your document end close it

Response.Clear()
Response.ContentType = "application/octet-stream"
Response.AddHeader("content-disposition", "attachment;filename= " & FileName)
Response.Buffer = True
Response.Clear()
Dim bytes = ms.ToArray()
Response.OutputStream.Write(bytes, 0, bytes.Length)
Response.OutputStream.Flush()


End Using
End Using
End Using