130 likes | 298 Views
ASP.NET 產生 PDF. 建國科技大學 資管系 饒瑞佶 2007 年. ITEXTSHARP 類別. 下載類別 http://sourceforge.net/projects/itextsharp/ 在專案中參考它. 加入參考. 程式需要的 namespace. Imports System Imports System.IO Imports iTextSharp.text Imports iTextSharp.text.pdf.
E N D
ASP.NET產生PDF 建國科技大學 資管系 饒瑞佶 2007年
ITEXTSHARP類別 • 下載類別http://sourceforge.net/projects/itextsharp/ • 在專案中參考它
程式需要的namespace Imports SystemImports System.IOImports iTextSharp.textImports iTextSharp.text.pdf
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Request.QueryString("id") = 1 Then ShowHello() Else ShowTable() End If End Sub
Sub ShowHello() Dim doc As Document = New Document PdfWriter.GetInstance(doc, New FileStream(Request.PhysicalApplicationPath + "\1.pdf", FileMode.Create)) doc.Open() doc.Add(New Paragraph("Hello World")) doc.Close() Response.Redirect("~/1.pdf") End Sub
Sub ShowTable() Dim doc As Document = New Document PdfWriter.GetInstance(doc, New FileStream(Request.PhysicalApplicationPath + "\2.pdf", FileMode.Create)) doc.Open() Dim table As Table = New Table(3) table.BorderWidth = 1 table.BorderColor = New Color(0, 0, 255) table.Padding = 3 table.Spacing = 1 Dim cell As Cell = New Cell("header") cell.Header = True cell.Colspan = 3 table.AddCell(cell) cell = New Cell("example cell with colspan 1 and rowspan 2") cell.Rowspan = 2 cell.BorderColor = New Color(255, 0, 0) table.AddCell(cell) table.AddCell("1.1") table.AddCell("2.1") table.AddCell("1.2") table.AddCell("2.2") table.AddCell("cell test1") cell = New Cell("big cell") cell.Rowspan = 2 cell.Colspan = 2 cell.HorizontalAlignment = Element.ALIGN_CENTER cell.VerticalAlignment = Element.ALIGN_MIDDLE cell.BackgroundColor = New Color(192, 192, 192) table.AddCell(cell) table.AddCell("cell test2") doc.Add(table) doc.Close() Response.Redirect("~/2.pdf") End Sub
加入字型宣告 Dim bf As BaseFont = BaseFont.CreateFont("C:\WINDOWS\Fonts\KAIU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED) Dim font1 As iTextSharp.text.Font = New iTextSharp.text.Font(bf, 12, iTextSharp.text.Font.NORMAL) 將字型加入 doc.Add(New Paragraph(“小名22233", font1))