ImageGear .NET v24.14 - Updated September 15, 2020
Add(ImGearPDETextFlags,Int32,String,ImGearPDEFont,ImGearPDEGraphicState,ImGearPDETextState,ImGearPDFFixedMatrix) Method
Example 




ImageGear24.Formats.Pdf Assembly > ImageGear.Formats.PDF Namespace > ImGearPDEText Class > Add Method : Add(ImGearPDETextFlags,Int32,String,ImGearPDEFont,ImGearPDEGraphicState,ImGearPDETextState,ImGearPDFFixedMatrix) Method
ImGearPDETextFlags options that specify what kind of text to add. Must be either:CHAR - for a text character, or RUN - for a text run.
Index after which to add character or text run.
String with Unicode characters to add.
Font for the element. Must be TrueType or Type 0 font.
The graphics state for the element.
The text state for the element.
The coordinates of text are specified in text space. TextMatrix defines the transformation from text space to the user space. See section '5.3.1 Text-Positioning Operators' of the PDF Reference for more details.
Adds a Unicode character or a Unicode text run to a PDE Text object.
Syntax
'Declaration
 
Public Overloads Sub Add( _
   ByVal options As ImGearPDETextFlags, _
   ByVal index As Integer, _
   ByVal text As String, _
   ByVal font As ImGearPDEFont, _
   ByVal graphicsState As ImGearPDEGraphicState, _
   ByVal textState As ImGearPDETextState, _
   ByVal textMatrix As ImGearPDFFixedMatrix _
) 
 
'Usage
 
Dim instance As ImGearPDEText
Dim options As ImGearPDETextFlags
Dim index As Integer
Dim text As String
Dim font As ImGearPDEFont
Dim graphicsState As ImGearPDEGraphicState
Dim textState As ImGearPDETextState
Dim textMatrix As ImGearPDFFixedMatrix
 
instance.Add(options, index, text, font, graphicsState, textState, textMatrix)

Parameters

options
ImGearPDETextFlags options that specify what kind of text to add. Must be either:CHAR - for a text character, or RUN - for a text run.
index
Index after which to add character or text run.
text
String with Unicode characters to add.
font
Font for the element. Must be TrueType or Type 0 font.
graphicsState
The graphics state for the element.
textState
The text state for the element.
textMatrix
The coordinates of text are specified in text space. TextMatrix defines the transformation from text space to the user space. See section '5.3.1 Text-Positioning Operators' of the PDF Reference for more details.
Remarks

This method should be used for adding text encoded with Unicode.

The Unicode API methods require a TrueType or Type 0 font that can be embedded. This font must be created from a font file on the local machine, because the lookup tables used by these API only exist in the font files. This is required even if you only check for representable text. This method will raise an exception, if used with other fonts, like Type 1 or font retrieved from an existing PDF document.

Example
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;
}
See Also

Reference

ImGearPDEText Class
ImGearPDEText Members
Overload List
ImGearPDETextFlags Enumeration
ImGearPDEFont Class
ImGearPDEGraphicState Class
ImGearPDETextState Class
ImGearPDFFixedMatrix Class