Private Sub AddUnicodeTextToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddUnicodeTextToolStripMenuItem.Click
If TypeOf igDoc Is ImGearPDFDocument Then
Dim textToAdd As String = "Russian: Русский Текст. Greek: Κείμενο στην ελληνική γλώσσα. Japanese (unrepresentable): 日本"
Dim pdfPage As ImGearPDFPage = DirectCast(igDoc.Pages(0), ImGearPDFPage)
Dim pdfContent As ImGearPDEContent = pdfPage.GetContent()
Dim fontSerif As ImGearPDEFont = CreateFont("Times-Roman", "Type0", "Identity-H")
Dim embedSerifFont As Boolean = False
' Ensure that the text is fully representable by the font.
' Add a warning if not.
If Not fontSerif.IsTextRepresentable(textToAdd) Then
Dim unrepresentableChars As System.Collections.Generic.IEnumerable(Of Char) = fontSerif.FindUnrepresentableCharacters(textToAdd)
Dim unrepresentableCharsString As String = [String].Empty
For Each unrepresentableChar As Char In unrepresentableChars
If unrepresentableCharsString <> [String].Empty Then
unrepresentableCharsString += ","
End If
unrepresentableCharsString += unrepresentableChar
Next
MessageBox.Show(String.Format("The selected font, {0}, does not support the following characters: [{1}]", fontSerif.GetAttributes().Name.[String], unrepresentableCharsString))
End If
embedSerifFont = True
Dim text As ImGearPDEText = CreateTextElement(textToAdd, fontSerif, 11, 10, 30, 300)
If text IsNot Nothing Then
pdfContent.AddElement(CInt(ImGearPDEInsertElement.AFTER_LAST), text)
text.Dispose()
End If
' Finalize the content
pdfPage.SetContent()
' Embed and subset any fonts that were used. This is only
' required when using Unicode fonts, and only in case if the font was actually used.
If embedSerifFont Then
Dim pdfDocument As ImGearPDFDocument = TryCast(igDoc, ImGearPDFDocument)
fontSerif.CreateToUnicodeNow(pdfDocument)
fontSerif.CreateWidthsNow(pdfDocument)
fontSerif.SubsetNow(pdfDocument)
End If
pdfPage.ReleaseContent()
' Clean up editing objects
If fontSerif IsNot Nothing Then
fontSerif.Dispose()
End If
If pdfContent IsNot Nothing Then
pdfContent.Dispose()
End If
pdfPage.ResetDisplayCache()
Me.ImGearPageView1.Update()
End If
End Sub
Private Shared Function CreateFont(ByVal FontName As String, ByVal FontType As String, ByVal sysEncodingName As String) As ImGearPDEFont
Dim findAttrs As New ImGearPDEFontAttrs()
findAttrs.Name = New ImGearPDFAtom(FontName)
findAttrs.Type = New ImGearPDFAtom(FontType)
Dim sysFont As ImGearPDFSysFont = ImGearPDFSysFont.FindSysFont(findAttrs, 0)
Dim encoding As New ImGearPDFSysEncoding(New ImGearPDFAtom(sysEncodingName))
Dim font As New ImGearPDEFont(sysFont, encoding, findAttrs.Name, ImGearPDEFontCreateFlags.CREATE_EMBEDDED Or ImGearPDEFontCreateFlags.WILL_SUBSET Or ImGearPDEFontCreateFlags.CREATE_TO_UNICODE Or ImGearPDEFontCreateFlags.ENCODE_BY_GID)
Return font
End Function
Private Shared Function CreateTextElement(ByVal Text As String, ByVal Font As ImGearPDEFont, ByVal FontSize As Int32, ByVal x As Integer, ByVal y As Integer, ByVal w As Integer) As ImGearPDEText
Dim textElement As ImGearPDEText = Nothing
Dim colorSpace As New ImGearPDEColorSpace(New ImGearPDFAtom("DeviceGray"))
Dim gState As New ImGearPDEGraphicState()
gState.StrokeColorSpec.Space = colorSpace
gState.FillColorSpec.Space = colorSpace
gState.MiterLimit = CInt(ImGearPDFFixedValues.TEN)
gState.Flatness = CInt(ImGearPDFFixedValues.ONE)
gState.LineWidth = CInt(ImGearPDFFixedValues.ONE)
Dim textMatrix As New ImGearPDFFixedMatrix()
textMatrix.A = ImGearPDF.IntToFixed(FontSize)
textMatrix.D = ImGearPDF.IntToFixed(FontSize)
textMatrix.H = ImGearPDF.IntToFixed(x)
textMatrix.V = ImGearPDF.IntToFixed(y)
textElement = New ImGearPDEText()
textElement.Add(ImGearPDETextFlags.RUN, 0, Text, Font, gState, Nothing, _
textMatrix)
colorSpace.Dispose()
gState.Dispose()
Return textElement
End Function