Example #1
0
        private NameToken GetAddedFont(PdfDocumentBuilder.AddedFont font)
        {
            if (!documentFonts.TryGetValue(font.Id, out NameToken value))
            {
                value = NameToken.Create($"F{nextFontId++}");
                var resources = pageDictionary.GetOrCreateDict(NameToken.Resources);
                var fonts     = resources.GetOrCreateDict(NameToken.Font);
                while (fonts.ContainsKey(value))
                {
                    value = NameToken.Create($"F{nextFontId++}");
                }

                documentFonts[font.Id] = value;
                fonts[value]           = font.Reference;
            }

            return(value);
        }
Example #2
0
        /// <summary>
        /// Draws the text in the provided font at the specified position and returns the letters which will be drawn.
        /// </summary>
        /// <param name="text">The text to draw to the page.</param>
        /// <param name="fontSize">The size of the font in user space units.</param>
        /// <param name="position">The position of the baseline (lower-left corner) to start drawing the text from.</param>
        /// <param name="font">
        /// A font added to the document using <see cref="PdfDocumentBuilder.AddTrueTypeFont"/>
        /// or <see cref="PdfDocumentBuilder.AddStandard14Font"/> methods.
        /// </param>
        /// <returns>The letters from the input text with their corresponding size and position.</returns>
        public IReadOnlyList <Letter> AddText(string text, decimal fontSize, PdfPoint position, PdfDocumentBuilder.AddedFont font)
        {
            if (font == null)
            {
                throw new ArgumentNullException(nameof(font));
            }

            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }

            if (!documentBuilder.Fonts.TryGetValue(font.Id, out var fontStore))
            {
                throw new ArgumentException($"No font has been added to the PdfDocumentBuilder with Id: {font.Id}. " +
                                            $"Use {nameof(documentBuilder.AddTrueTypeFont)} to register a font.", nameof(font));
            }

            if (fontSize <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(fontSize), "Font size must be greater than 0");
            }

            var fontName = GetAddedFont(font);

            var fontProgram = fontStore.FontProgram;

            var fm = fontProgram.GetFontMatrix();

            var textMatrix = TransformationMatrix.FromValues(1, 0, 0, 1, position.X, position.Y);

            var letters = DrawLetters(fontName, text, fontProgram, fm, fontSize, textMatrix);

            currentStream.Add(BeginText.Value);
            currentStream.Add(new SetFontAndSize(fontName, fontSize));
            currentStream.Add(new MoveToNextLineWithOffset((decimal)position.X, (decimal)position.Y));
            var bytesPerShow = new List <byte>();

            foreach (var letter in text)
            {
                if (char.IsWhiteSpace(letter))
                {
                    currentStream.Add(new ShowText(bytesPerShow.ToArray()));
                    bytesPerShow.Clear();
                }

                var b = fontProgram.GetValueForCharacter(letter);
                bytesPerShow.Add(b);
            }

            if (bytesPerShow.Count > 0)
            {
                currentStream.Add(new ShowText(bytesPerShow.ToArray()));
            }

            currentStream.Add(EndText.Value);

            return(letters);
        }
Example #3
0
        public PdfPageBuilder AddText(string text, decimal fontSize, PdfPoint position, PdfDocumentBuilder.AddedFont font)
        {
            if (font == null)
            {
                throw new ArgumentNullException(nameof(font));
            }

            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }

            if (!documentBuilder.Fonts.TryGetValue(font.Id, out var fontProgram))
            {
                throw new ArgumentException($"No font has been added to the PdfDocumentBuilder with Id: {font.Id}. " +
                                            $"Use {nameof(documentBuilder.AddTrueTypeFont)} to register a font.", nameof(font));
            }

            if (fontSize <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(fontSize), "Font size must be greater than 0");
            }

            var width = CalculateGlyphSpaceTextWidth(text, fontProgram);

            var fm = TransformationMatrix.FromValues(1 / 1000m, 0, 1 / 1000m, 0, 0, 0);

            var widthRect = fm.Transform(new PdfRectangle(0, 0, width, 0));

            try
            {
                var ctm = TransformationMatrix.FromValues(position.X, 0, position.Y, 0, 0, 0);

                var realWidth = ctm.Transform(widthRect).Width;

                if (realWidth + position.X > PageSize.Width)
                {
                    throw new InvalidOperationException("Text would exceed the bounds.");
                }

                operations.Add(new ModifyCurrentTransformationMatrix(new[]
                {
                    position.X, 0, position.Y, 0, 0, 0
                }));

                var beginText = BeginText.Value;

                operations.Add(beginText);
                operations.Add(new SetFontAndSize(font.Name, fontSize));
                operations.Add(new ShowText(text));
                operations.Add(EndText.Value);

                beginText = null;
            }
            catch (Exception ex)
            {
                throw;
            }

            return(this);
        }
Example #4
0
        /// <summary>
        /// Calculates the size and position of each letter in a given string in the provided font without changing the state of the page.
        /// </summary>
        /// <param name="text">The text to measure each letter of.</param>
        /// <param name="fontSize">The size of the font in user space units.</param>
        /// <param name="position">The position of the baseline (lower-left corner) to start drawing the text from.</param>
        /// <param name="font">
        /// A font added to the document using <see cref="PdfDocumentBuilder.AddTrueTypeFont"/>
        /// or <see cref="PdfDocumentBuilder.AddStandard14Font"/> methods.
        /// </param>
        /// <returns>The letters from the input text with their corresponding size and position.</returns>
        public IReadOnlyList <Letter> MeasureText(string text, decimal fontSize, PdfPoint position, PdfDocumentBuilder.AddedFont font)
        {
            if (font == null)
            {
                throw new ArgumentNullException(nameof(font));
            }

            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }

            if (!documentBuilder.Fonts.TryGetValue(font.Id, out var fontStore))
            {
                throw new ArgumentException($"No font has been added to the PdfDocumentBuilder with Id: {font.Id}. " +
                                            $"Use {nameof(documentBuilder.AddTrueTypeFont)} to register a font.", nameof(font));
            }

            if (fontSize <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(fontSize), "Font size must be greater than 0");
            }

            var fontProgram = fontStore.FontProgram;

            var fm = fontProgram.GetFontMatrix();

            var textMatrix = TransformationMatrix.FromValues(1, 0, 0, 1, position.X, position.Y);

            var letters = DrawLetters(null, text, fontProgram, fm, fontSize, textMatrix);

            return(letters);
        }
Example #5
0
        public List <Letter> AddText(string text, decimal fontSize, PdfPoint position, PdfDocumentBuilder.AddedFont font)
        {
            if (font == null)
            {
                throw new ArgumentNullException(nameof(font));
            }

            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }

            if (!documentBuilder.Fonts.TryGetValue(font.Id, out var fontProgram))
            {
                throw new ArgumentException($"No font has been added to the PdfDocumentBuilder with Id: {font.Id}. " +
                                            $"Use {nameof(documentBuilder.AddTrueTypeFont)} to register a font.", nameof(font));
            }

            if (fontSize <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(fontSize), "Font size must be greater than 0");
            }

            var fm         = TransformationMatrix.FromValues(1 / 1000m, 0, 0, 1 / 1000m, 0, 0);
            var textMatrix = TransformationMatrix.FromValues(1, 0, 0, 1, position.X, position.Y);

            var letters = DrawLetters(text, fontProgram, fm, fontSize, textMatrix);

            try
            {
                //var realWidth = widthRect.Width;

                //if (realWidth + position.X > PageSize.Width)
                //{
                //    throw new InvalidOperationException("Text would exceed the bounds.");
                //}

                var beginText = BeginText.Value;

                operations.Add(beginText);
                operations.Add(new SetFontAndSize(font.Name, fontSize));
                operations.Add(new MoveToNextLineWithOffset(position.X, position.Y));
                operations.Add(new ShowText(text));
                operations.Add(EndText.Value);

                beginText = null;
            }
            catch (Exception ex)
            {
                throw;
            }

            return(letters);
        }