我想用iText创建一个PDF / A-1A文件,但我无法解决经典的Helvetica错误,但我无法弄清楚如何修复它。 这是代码:
Imports iTextSharp.text.pdf
Imports iTextSharp.text
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim document As Document = New Document(PageSize.A4)
Dim writer As PdfAWriter = PdfAWriter.GetInstance(document, New FileStream("./Test.pdf", FileMode.Create), PdfAConformanceLevel.PDF_A_1A)
writer.CreateXmpMetadata()
document.Open()
'The sRGB File is located in my Resources.'
Dim icc = ICC_Profile.GetInstance(My.Resources.sRGB)
writer.SetOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc)
Dim fonts As Font = FontFactory.GetFont("./FreeSansBold.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED, 10)
Dim normal9 As Font = FontFactory.GetFont("./FreeSans.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED, 9)
Dim bold9 As Font = FontFactory.GetFont("./FreeSansBold.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED, 9)
Dim normal8 As Font = FontFactory.GetFont("./FreeSans.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED, 8)
document.Add(New Paragraph("Hello world"))
writer.Close()
document.Close()
End Sub
End Class
如果您有疑问,请问我。我希望你能帮助我。
此致
Nadix。
答案 0 :(得分:1)
简短的回答是你已经知道的,所有字体都必须嵌入,甚至是“核心PDF字体”。
更长的答案是iText不是PDF渲染器,因此不需要核心PDF字体,至少不完全。根据标准,所有符合标准的渲染器必须具有这些字体(或适当的替换),因此iText没有理由包含它们。 (更不用说任何潜在的许可问题。)它附带AFM文件,其中包含字体指标,但这只是为了弄清楚如何打开文本。
解决方案是始终声明,嵌入和使用您自己的字体。您可以购买嵌入式Helvetica的许可证,如果您愿意也可以使用。目前无法设置文档范围的默认字体,因此您需要始终指定字体或提供包装器。一种方法是只有一个辅助方法:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
''//Register our font
FontFactory.Register(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF"), "Arial Unicode MS")
End Sub
Public Shared Function CreateParagraph(text As String) As Paragraph
Static ArialUnicode = FontFactory.GetFont("Arial Unicode MS", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 20)
Return New Paragraph(text, ArialUnicode)
End Function
另一种方法是继承Paragraph
Public Class ParagraphArial
Inherits iTextSharp.text.Paragraph
Public Shared ReadOnly Property ThisFont As Font
Get
Return FontFactory.GetFont("Arial Unicode MS", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 20)
End Get
End Property
Public Sub New(text As String)
MyBase.New(text, ThisFont)
End Sub
End Class
两者都会像这样使用:
document.Add(CreateParagraph("Hello world"))
document.Add(New ParagraphArial("Second Test"))