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;
}