【问题标题】:Change document Font Size in ITextSharp在 ITextSharp 中更改文档字体大小
【发布时间】:2015-06-04 22:06:11
【问题描述】:

我有一个在 .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:

【问题讨论】:

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


    【解决方案1】:

    您可以使用类似的方法为表格标题指定格式

    编辑

    //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)
    

    【讨论】:

    • 嗨,我试过 Dim TableFont = FontFactory.GetFont("Arial", 12, Font.BOLD) pdfDoc.Open()
    • 和 Dim 样式 As StyleSheet = New iTextSharp.text.html.simpleparser.StyleSheet() pdfDoc.Open() styles.LoadTagStyle("body", "font-family", "tahona") 样式.LoadTagStyle("body", "font-size", "6px")
    • 但我仍然得到了与 A4 纸格式的 pdf 相同的大字体。
    • @ThomasBøgPetersen 我已经更新了代码,现在应该可以使用了
    • 如果我添加这个 pdfDoc.Add(New Paragraph(sr, TableFont)) 或这个 pdfDoc.Add(New Paragraph(TableFont)) 然后我得到你在邮件问题中看到的图像。
    猜你喜欢
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多