3

环境

C# 2008 速成版,Windows 窗体,iTextSharp 5.4.0。

我将 iTextSharp.dll 放在我的项目bin文件夹中,我在我的项目中添加了对它的引用,我有 namespace using iTextSharpiTextSharp.text并且iTextSharp.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();
4

1 回答 1

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
于 2013-04-03T13:55:11.783 回答