在ITextSharp中更改文档字体大小

时间:2012-09-09 18:28:04

标签: asp.net vb.net itextsharp font-size

我有这个小脚本在.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()

修改

我试过这个,但它给了我一个错误:

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:

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用类似的内容为表标题添加格式

修改

//add a Paragraph to the document with an specify format
 Dim TableFont   = FontFactory.GetFont("Arial", 12, Font.BOLD)

 pdfDoc.Open()  
 pdfDoc.Add(New Paragraph(sr.ReadToEnd(),TableFont)