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); }
/// <summary> /// Adds the Unicode string as the last text element on the first page of the PDF. /// </summary> /// <param name="pdfDocument">An opened PDF document.</param> /// <param name="text">Unicode text to inject.</param> private static void InjectMessage(ImGearPDFDocument pdfDocument, string text) { // Hint: https://help.accusoft.com/ImageGear-Net/v24.11/Windows/HTML/webframe.html#topic6633.html // 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 = pdfDocument.CreateNewPage(-1, usLetterRectangle); // Get First Page of Document ImGearPDFPage igPdfPage = (ImGearPDFPage)pdfDocument.Pages[0]; // 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(text)) //{ // Get content that be able to fix the changes. ImGearPDEContent pdfContent = igPdfPage.GetContent(); // Integrate text to the current page. // ImGearPDEText textElement = CreateTextElement(text, font, 30, xPosition, pageHeight - yPosition, pageWidth - 40); ImGearPDEText textElement = CreateTextElement(text, font, 30, 0, 0, pageWidth); if (textElement != null) { Console.Write("Creating Text Element\n"); pdfContent.AddElement((int)ImGearPDEInsertElement.AFTER_LAST, textElement); //textElement.Dispose(); } else { Console.Write("Text Element Null\n"); } // Fix the changes on the page and release content. igPdfPage.SetContent(); igPdfPage.ReleaseContent(); // Integrate font to the document. if (textElement != null) { font.CreateToUnicodeNow(pdfDocument); font.EmbedNow(pdfDocument); font.SubsetNow(pdfDocument); return(false); } textElement.Dispose(); } //} return(true); }), null); // Release current PDF page. igPdfPage.Dispose(); }