コード例 #1
0
ファイル: MeausureFont.cs プロジェクト: wtactics/arcmage
        public static Size MeasureString(string text, double fontSize, PdfFont font)
        {
            var width = font.GetWidth(text, (float)fontSize);

            float ascent  = font.GetAscent(text, (float)fontSize);
            float descent = font.GetDescent(text, (float)fontSize);

            var height = ascent - descent;

            return(new Size((int)Math.Round(width), (int)Math.Round(height)));
        }
コード例 #2
0
        protected Rectangle GetTextSize(string text, PdfFont pdfFont, float fontSize)
        {
            GlyphLine glyphLine = pdfFont.CreateGlyphLine(text);

            int width = 0;

            for (int i = 0; i < glyphLine.Size(); i++)
            {
                Glyph glyph = glyphLine.Get(i);
                width += glyph.GetWidth();
            }

            float userSpaceWidth = width * fontSize / 1000.0f;

            float ascent  = pdfFont.GetAscent(text, fontSize);
            float descent = pdfFont.GetDescent(text, fontSize);

            float userSpaceHeight = ascent - descent;

            return(new Rectangle(userSpaceWidth, userSpaceHeight));
        }
コード例 #3
0
ファイル: PdfBuilder.cs プロジェクト: myuzhang/PdfHandy.Net
        public int GetFontSizeFittingMultiLinesInAcroForm(string multiLineString, float targetFieldWidth, float targetFieldHeight, PdfFont pdfFont, int defaultFontSize)
        {
            if (string.IsNullOrWhiteSpace(multiLineString))
            {
                return(defaultFontSize);
            }

            List <string> stringLines = new List <string>(multiLineString.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));

            int lineNumRequired = 0;
            int targetSize      = defaultFontSize;

            while (targetSize > 1)
            {
                foreach (var stringLine in stringLines)
                {
                    float lineWidth     = pdfFont.GetWidth(stringLine, targetSize);
                    int   lineNumNeeded = (int)Math.Ceiling(lineWidth / targetFieldWidth);
                    lineNumRequired += lineNumNeeded;
                }

                float aboveBaseline   = pdfFont.GetAscent(stringLines[0], targetSize);
                float underBaseline   = pdfFont.GetDescent(stringLines[0], targetSize);
                float textHeight      = aboveBaseline - underBaseline;
                float lineHeight      = textHeight * 1.5f;
                int   lineNumProvided = (int)Math.Floor(targetFieldHeight / lineHeight);

                if (lineNumProvided > lineNumRequired)
                {
                    break;
                }

                lineNumRequired = 0;
                targetSize      = targetSize - 1;
            }

            return(targetSize);
        }
コード例 #4
0
ファイル: PdfBuilder.cs プロジェクト: myuzhang/PdfHandy.Net
        private void VerticalJustificationInAcroForm <T>(PdfFormField field, T detail, string propertyName, PdfFont pdfFont, PdfFontAttribute pdfFontAttribute)
        {
            var widgets = field.GetWidgets();

            if (widgets == null || widgets.Count == 0)
            {
                throw new ArgumentNullException($"no widgets to the field");
            }

            PdfArray position = widgets.First().GetRectangle();

            PdfPage page = field.GetWidgets().First().GetPage();

            if (page == null)
            {
                throw new ArgumentNullException(
                          $"field widget annotation is not associated with any page");
            }
            int pageNum = _pdfDoc.GetPageNumber(page);

            float width  = (float)(position.GetAsNumber(2).GetValue() - position.GetAsNumber(0).GetValue());
            float height = (float)(position.GetAsNumber(3).GetValue() - position.GetAsNumber(1).GetValue());
            float llx    = (float)position.GetAsNumber(0).GetValue();
            float lly    = (float)position.GetAsNumber(1).GetValue();
            float urx    = (float)position.GetAsNumber(2).GetValue();
            float ury    = (float)position.GetAsNumber(3).GetValue();

            Rectangle rect = new Rectangle(llx, lly, width, height);

            PdfCanvas canvas = new PdfCanvas(_pdfDoc, pageNum);

            canvas.SetLineWidth(0f).Rectangle(rect);

            string textValue = null;
            Type   type      = detail.GetType();

            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                if (property.Name == propertyName)
                {
                    textValue = property.GetValue(detail, null).ToString();
                }
            }
            if (string.IsNullOrWhiteSpace(textValue))
            {
                return;
            }

            float verticalAdjustment = 0;
            var   text = new Text(textValue);

            if (pdfFont != null)
            {
                text.SetFont(pdfFont);
                text.SetFontColor(pdfFontAttribute.Color);
                if (pdfFontAttribute.Size != 0)
                {
                    text.SetFontSize(pdfFontAttribute.Size);
                }

                verticalAdjustment = (pdfFont.GetWidth(textValue, pdfFontAttribute.Size) / (urx - llx)) * pdfFont.GetAscent(textValue, pdfFontAttribute.Size);
            }

            Paragraph p = new Paragraph(text);

            if (pdfFontAttribute.Justification == PdfTextAlignment.CenterLeft ||
                pdfFontAttribute.Justification == PdfTextAlignment.BottomLeft)
            {
                p.SetTextAlignment(iText.Layout.Properties.TextAlignment.LEFT);
            }
            if (pdfFontAttribute.Justification == PdfTextAlignment.CenterMiddle ||
                pdfFontAttribute.Justification == PdfTextAlignment.BottomMiddle)
            {
                p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
            }
            if (pdfFontAttribute.Justification == PdfTextAlignment.CenterRight ||
                pdfFontAttribute.Justification == PdfTextAlignment.BottomRight)
            {
                p.SetTextAlignment(iText.Layout.Properties.TextAlignment.RIGHT);
            }

            // set line space
            p.SetMultipliedLeading(1);

            if (pdfFontAttribute.Justification == PdfTextAlignment.CenterLeft ||
                pdfFontAttribute.Justification == PdfTextAlignment.CenterMiddle ||
                pdfFontAttribute.Justification == PdfTextAlignment.CenterRight)
            {
                new Canvas(canvas, _pdfDoc, rect)
                .Add(p.SetFixedPosition(llx, (ury + lly) / 2 - verticalAdjustment, urx - llx));
            }

            if (pdfFontAttribute.Justification == PdfTextAlignment.BottomLeft ||
                pdfFontAttribute.Justification == PdfTextAlignment.BottomMiddle ||
                pdfFontAttribute.Justification == PdfTextAlignment.BottomRight)
            {
                new Canvas(canvas, _pdfDoc, rect).Add(p);
            }

            _form.RemoveField(field.GetFieldName().ToString());
        }