ImageGear .NET - Updated
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;
}
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
See Also

Reference

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