string sampleText = "Russian: Русский Текст. Greek: Κείμενο στην ελληνική γλώσσα. Japanese: 日本 Chinese: 借用本院學術活動中";
static void AddUnicodeTextToDocument(ImGearPDFDocument igPdfDocument, string textToAdd)
{
// Initialize current page attributes.
const int pageWidth = (int)(8.5 * 300);
const int pageHeight = 11 * 300;
ImGearPDFFixedRect usLetterRectangle = new ImGearPDFFixedRect(0, 0, ImGearPDF.IntToFixed(pageWidth), ImGearPDF.IntToFixed(pageHeight));
// Create new PDF page in document.
ImGearPDFPage igPdfPage = igPdfDocument.CreateNewPage(-1, usLetterRectangle);
// This is current text position on the page.
int xPosition = 20, yPosition = 40;
// Type of encoding that will be used.
const string encodingName = "Identity-H";
ImGearPDFSysEncoding encoding = new ImGearPDFSysEncoding(new ImGearPDFAtom(encodingName));
// Enumerate all system fonts to find one that can represent our text. Add to the page.
ImGearPDFSysFont.Enumerate(new ImGearPDFSysFont.ImGearPDFSysFontEnumerate(
delegate (ImGearPDFSysFont systemFont, object userData)
{
// Check the possibility to create font by system font with given encoding.
ImGearPDEFontCreateFlags flags = systemFont.GetCreateFlags(encoding);
if (flags == ImGearPDEFontCreateFlags.CREATE_NOT_ALLOWED)
{
return true;
}
// Check the possibility to create font for unicode text, embedding, and embedding only a subset.
if ((flags & ImGearPDEFontCreateFlags.CREATE_TO_UNICODE) == 0 ||
(flags & ImGearPDEFontCreateFlags.CREATE_EMBEDDED) == 0 ||
(flags & ImGearPDEFontCreateFlags.CREATE_SUBSET) == 0)
{
return true;
}
// Create the font by system font.
using (ImGearPDEFont font = new ImGearPDEFont(systemFont, encoding, new ImGearPDFAtom(systemFont.Name.String), flags))
{
// Check this font is able to represent given string.
if (font.IsTextRepresentable(textToAdd))
{
// Get content that be able to fix the changes.
ImGearPDEContent pdfContent = igPdfPage.GetContent();
// Integrate text to the current page.
ImGearPDEText textElement = CreateTextElement(textToAdd, font, 30, xPosition, pageHeight - yPosition, pageWidth - 40);
if (textElement != null)
{
pdfContent.AddElement((int)ImGearPDEInsertElement.AFTER_LAST, textElement);
textElement.Dispose();
}
// Fix the changes on the page and release content.
igPdfPage.SetContent();
igPdfPage.ReleaseContent();
// Integrate font to the document.
if (textElement != null)
{
font.CreateToUnicodeNow(igPdfDocument);
font.EmbedNow(igPdfDocument);
font.SubsetNow(igPdfDocument);
return false;
}
}
}
return true;
}), null);
// Release current PDF page.
igPdfPage.Dispose();
}
static ImGearPDEText CreateTextElement(string Text, ImGearPDEFont Font, Int32 FontSize, int x, int y, int w)
{
ImGearPDEText textElement = null;
ImGearPDEColorSpace colorSpace =
new ImGearPDEColorSpace(new ImGearPDFAtom("DeviceGray"));
ImGearPDEGraphicState gState = new ImGearPDEGraphicState();
gState.StrokeColorSpec.Space = colorSpace;
gState.FillColorSpec.Space = colorSpace;
gState.MiterLimit = (int)ImGearPDFFixedValues.TEN;
gState.Flatness = (int)ImGearPDFFixedValues.ONE;
gState.LineWidth = (int)ImGearPDFFixedValues.ONE;
ImGearPDFFixedMatrix textMatrix = 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, null, textMatrix);
colorSpace.Dispose();
gState.Dispose();
return textElement;
}
Private sampleText As String = "Russian: Русский Текст. Greek: Κείμενο στην ελληνική γλώσσα. Japanese: 日本 Chinese: 借用本院學術活動中"
Private Shared Sub AddUnicodeTextToDocument(ByVal igPdfDocument As ImGearPDFDocument, ByVal textToAdd As String)
' Initialize current page attributes.
Const pageWidth As Integer = CInt((8.5 * 300))
Const pageHeight As Integer = 11 * 300
Dim usLetterRectangle As ImGearPDFFixedRect = New ImGearPDFFixedRect(0, 0, ImGearPDF.IntToFixed(pageWidth), ImGearPDF.IntToFixed(pageHeight))
' Create new PDF page in document.
Dim igPdfPage As ImGearPDFPage = igPdfDocument.CreateNewPage(-1, usLetterRectangle)
' This is current text position on the page.
Dim xPosition As Integer = 20, yPosition As Integer = 40
Const encodingName As String = "Identity-H"
Dim encoding As ImGearPDFSysEncoding = New ImGearPDFSysEncoding(New ImGearPDFAtom(encodingName))
' Enumerate all system fonts to find one that can represent our text. Add to the page.
ImGearPDFSysFont.Enumerate(New ImGearPDFSysFont.ImGearPDFSysFontEnumerate(Function(ByVal systemFont As ImGearPDFSysFont, ByVal userData As Object)
' // Check the possibility to create font by system font with given encoding.
Dim flags As ImGearPDEFontCreateFlags = systemFont.GetCreateFlags(encoding)
If flags = ImGearPDEFontCreateFlags.CREATE_NOT_ALLOWED Then
Return True
End If
' Check the possibility to create font for unicode text, embedding, and embedding only a subset.
If (flags And ImGearPDEFontCreateFlags.CREATE_TO_UNICODE) = 0 Or
(flags And ImGearPDEFontCreateFlags.CREATE_EMBEDDED) = 0 Or
(flags And ImGearPDEFontCreateFlags.CREATE_SUBSET) = 0 Then
Return True
End If
' Create the font by system font.
Using font As ImGearPDEFont = New ImGearPDEFont(systemFont, encoding, New ImGearPDFAtom(systemFont.Name.String), flags)
' Check this font is able to represent given string.
If font.IsTextRepresentable(textToAdd) Then
' Get content that be able to fix the changes.
Dim pdfContent As ImGearPDEContent = igPdfPage.GetContent()
' Integrate text to the current page.
Dim textElement As ImGearPDEText = CreateTextElement(textToAdd, font, 30, xPosition, pageHeight - yPosition, pageWidth - 40)
If textElement IsNot Nothing Then
pdfContent.AddElement(CInt(ImGearPDEInsertElement.AFTER_LAST), textElement)
textElement.Dispose()
End If
' Fix the changes on the page and release content.
igPdfPage.SetContent()
igPdfPage.ReleaseContent()
' Integrate font to the document.
If textElement IsNot Nothing Then
font.CreateToUnicodeNow(igPdfDocument)
font.EmbedNow(igPdfDocument)
font.SubsetNow(igPdfDocument)
Return False
End If
End If
End Using
Return True
End Function), Nothing)
' Release current PDF page.
igPdfPage.Dispose()
End Sub
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 ImGearPDEColorSpace = New ImGearPDEColorSpace(New ImGearPDFAtom("DeviceGray"))
Dim gState As ImGearPDEGraphicState = 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 ImGearPDFFixedMatrix = 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