同时生成多个PDF - itextsharp

时间:2016-06-09 23:25:10

标签: .net vb.net pdf itextsharp itext

我在.net网络应用中使用iTextSharp来创建一个保存在本地计算机上的PDF文件。我正在为这个测试创建随机文件名,所以我知道它不会覆盖同一个文件。

我想要做的是当用户点击按钮时,同时创建多个PDF(具有来自数据库的唯一名称和数据)。

此代码适用于1个文件。我怎么能让它循环?我需要对流做些什么吗?

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    'Loop syntax, to test creation of 5 unique pdfs
    'For i As Integer = 0 To 5 

    Randomize()
    'How I'm creating a unique filename for this example
    Dim r As Integer = CInt(Int((600000 * Rnd()) + 1))


    Using ms As New MemoryStream()
        Dim document As New Document(PageSize.A4, 25, 25, 30, 30)
        Dim writer As PdfWriter = PdfWriter.GetInstance(document, ms)
        document.Open()
        document.Add(New Paragraph("Hello World"))
        document.Close()
        writer.Close()
        Response.ContentType = "pdf/application"
        Response.AddHeader("content-disposition", "attachment;filename=" + r.ToString + ".pdf")
        Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length)
    End Using


    'Next

End Sub

0 个答案:

没有答案