-
public float GetFontSize (Dictionary<string, EMS.Base.Common.FieldsValue> fieldvalues, byte[] pdfIn) { if (pdfIn == null || fieldvalues == null || fieldvalues.Keys.Count == 0) return null; MemoryStream pdfInMS = new MemoryStream(pdfIn); MemoryStream outMS = new MemoryStream(); PdfDocument pdfDoc = new PdfDocument(new PdfReader(pdfInMS), new PdfWriter(outMS)); var fFont = GetArialFont(); PdfAcroForm pdfAcroForm = PdfAcroForm.GetAcroForm(pdfDoc, true); IDictionary<String, PdfFormField> formFields = pdfAcroForm.GetFormFields(); float fontSize = 11f; foreach (string key in fieldvalues.Keys) { var currentFieldvalues = fieldvalues[key]; if (currentFieldvalues.Value != null) { string value = currentFieldvalues.Value.ToString(); PdfFormField field = pdfAcroForm.GetField(key); if (field != null) { PdfDictionary fieldObject = field.GetPdfObject(); Rectangle rectangle = fieldObject.GetAsRectangle(PdfName.Rect); float textBoxWidth = rectangle.GetWidth(); float textBoxHeight = rectangle.GetHeight(); if (textBoxHeight > textBoxWidth) { textBoxWidth = textBoxHeight; } textBoxWidth = textBoxWidth - 1f; float ascent = fFont.GetAscent(value, fontSize); float textWidth = fFont.GetWidth(value, fontSize); while (textWidth > textBoxWidth) { fontSize = fontSize - 0.1f; textWidth = fFont.GetWidth(value, fontSize); } } } } return fontSize; }
iText根据文本域长度动态改变文本字体大小
最新推荐文章于 2024-03-28 16:30:12 发布