c# - iTextSharp 在 Winforms 项目中不起作用

标签 c# winforms itext

环境

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

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

这是第一个 block :

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();

这是第二 block :

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();

最佳答案

此示例打开一个对话框,用户可以保存文件。使用代码转换器将 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

关于c# - iTextSharp 在 Winforms 项目中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15787994/

相关文章:

c# - WPF DataGrid 中类似 Excel 的选择矩形

c# - 正确存储配置字符串

javascript - 如何在前端和后端验证必填字段

c# - 防止用户在后台处理时单击表单上的任何内容的最佳方法是什么?

c# - 如何创建从 System.Drawing.Bitmap 对象开始的 iTextSharp.text.Image 对象?

c# - 单声道 TextTransform.exe

c# - 在 Windows 8 Pro 平板电脑中使用 WinAPI 抛出 AccessViolationException

c# - 验证并打印小数点后两位的 double 值

Linux PDF/Postscript 优化

java - 如何使用 I-Text 为 CODE 128 A 插入 FNC1 符号标识符?