我有这个在 .pdf 文件中生成表格的小脚本,但文本太大。如何为其添加其他字体大小/类型?
Dim form As New HtmlForm
form.Controls.Add(gvEvents)
Dim sw As New StringWriter
Dim htmlWriter As New HtmlTextWriter(sw)
Dim htmlContent As String = sw.ToString()
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=Holdkalender.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
form.Controls(0).RenderControl(htmlWriter)
Dim sr As New StringReader(sw.ToString())
Dim pdfDoc As New Document(PageSize.A4, 50, 50, 50, 50)
Dim htmlparser As New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
htmlparser.Parse(sr)
pdfDoc.Close()
Response.Write(pdfDoc)
Response.End()
编辑:
我已经尝试过了,但它给了我一个 Ielement 错误:
Protected Sub btnGeneratePDF_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnGeneratePDF.Click
BindEvents(Convert.ToInt32(ddlEventSize.SelectedValue))
Dim form As New HtmlForm
form.Controls.Add(gvEvents)
Dim sw As New StringWriter
Dim htmlWriter As New HtmlTextWriter(sw)
Dim htmlContent As String = sw.ToString()
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=Holdkalender.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
form.Controls(0).RenderControl(htmlWriter)
Dim sr As New StringReader(sw.ToString())
Dim pdfDoc As New Document(PageSize.A4, 50, 50, 50, 50) 'Paper Size and margin
Dim htmlparser As New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
Dim ftlbl As Font = Nothing
ftlbl = FontFactory.GetFont(FontFactory.HELVETICA, 5)
pdfDoc.Add(ftlbl)
Try
htmlparser.Parse(sr)
Catch ex As Exception
'Display parser errors in PDF.
Dim paragraph As New Paragraph("FEJL!" + ex.Message)
Dim text As Chunk = TryCast(paragraph.Chunks(0), Chunk)
If text IsNot Nothing Then
text.Font.Color = BaseColor.RED
End If
pdfDoc.Add(paragraph)
Finally
pdfDoc.Close()
Response.Write(pdfDoc)
Response.End()
End Try
End Sub
编辑2: