public void DrawMultiLineOfText(PdfFormField field, string text, BaseFont font, float fontSize, float llx, float lly, float urx, float ury) { PdfAppearance tp = PdfAppearance.CreateAppearance(writer, urx - llx, ury - lly); PdfAppearance tp2 = (PdfAppearance)tp.Duplicate; tp2.SetFontAndSize(font, fontSize); tp2.ResetRGBColorFill(); field.DefaultAppearanceString = tp2; tp.DrawTextField(0f, 0f, urx - llx, ury - lly); tp.BeginVariableText(); tp.SaveState(); tp.Rectangle(3f, 3f, urx - llx - 6f, ury - lly - 6f); tp.Clip(); tp.NewPath(); tp.BeginText(); tp.SetFontAndSize(font, fontSize); tp.ResetRGBColorFill(); tp.SetTextMatrix(4, 5); var tokenizer = new StringTokenizer(text, "\n"); float yPos = ury - lly; while (tokenizer.HasMoreTokens()) { yPos -= fontSize * 1.2f; tp.ShowTextAligned(PdfContentByte.ALIGN_LEFT, tokenizer.NextToken(), 3, yPos, 0); } tp.EndText(); tp.RestoreState(); tp.EndVariableText(); field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp); }
/** * @param field * @param llx * @param lly * @param urx * @param ury */ public void DrawSignatureAppearences(PdfFormField field, float llx, float lly, float urx, float ury) { PdfAppearance tp = PdfAppearance.CreateAppearance(writer, urx - llx, ury - lly); tp.SetGrayFill(1.0f); tp.Rectangle(0, 0, urx - llx, ury - lly); tp.Fill(); tp.SetGrayStroke(0); tp.SetLineWidth(1); tp.Rectangle(0.5f, 0.5f, urx - llx - 0.5f, ury - lly - 0.5f); tp.ClosePathStroke(); tp.SaveState(); tp.Rectangle(1, 1, urx - llx - 2, ury - lly - 2); tp.Clip(); tp.NewPath(); tp.RestoreState(); field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp); }
public void DrawSingleLineOfText(PdfFormField field, string text, BaseFont font, float fontSize, float llx, float lly, float urx, float ury) { PdfAppearance tp = PdfAppearance.CreateAppearance(writer, urx - llx, ury - lly); PdfAppearance tp2 = (PdfAppearance)tp.Duplicate; tp2.SetFontAndSize(font, fontSize); tp2.ResetRGBColorFill(); field.DefaultAppearanceString = tp2; tp.DrawTextField(0f, 0f, urx - llx, ury - lly); tp.BeginVariableText(); tp.SaveState(); tp.Rectangle(3f, 3f, urx - llx - 6f, ury - lly - 6f); tp.Clip(); tp.NewPath(); tp.BeginText(); tp.SetFontAndSize(font, fontSize); tp.ResetRGBColorFill(); tp.SetTextMatrix(4, (ury - lly) / 2 - (fontSize * 0.3f)); tp.ShowText(text); tp.EndText(); tp.RestoreState(); tp.EndVariableText(); field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp); }
/** * Get the <code>PdfAppearance</code> of a list field * @throws IOException on error * @throws DocumentException on error * @return A <code>PdfAppearance</code> */ internal PdfAppearance GetListAppearance() { PdfAppearance app = GetBorderAppearance(); app.BeginVariableText(); if (choices == null || choices.Length == 0) { app.EndVariableText(); return(app); } int topChoice = choiceSelection; if (topChoice >= choices.Length) { topChoice = choices.Length - 1; } if (topChoice < 0) { topChoice = 0; } BaseFont ufont = RealFont; float usize = fontSize; if (usize == 0) { usize = 12; } bool borderExtra = borderStyle == PdfBorderDictionary.STYLE_BEVELED || borderStyle == PdfBorderDictionary.STYLE_INSET; float h = box.Height - borderWidth * 2; float offsetX = borderWidth; if (borderExtra) { h -= borderWidth * 2; offsetX *= 2; } float leading = ufont.GetFontDescriptor(BaseFont.BBOXURY, usize) - ufont.GetFontDescriptor(BaseFont.BBOXLLY, usize); int maxFit = (int)(h / leading) + 1; int first = 0; int last = 0; last = topChoice + maxFit / 2 + 1; first = last - maxFit; if (first < 0) { last += first; first = 0; } // first = topChoice; last = first + maxFit; if (last > choices.Length) { last = choices.Length; } topFirst = first; app.SaveState(); app.Rectangle(offsetX, offsetX, box.Width - 2 * offsetX, box.Height - 2 * offsetX); app.Clip(); app.NewPath(); Color fcolor = (textColor == null) ? GrayColor.GRAYBLACK : textColor; app.SetColorFill(new Color(10, 36, 106)); app.Rectangle(offsetX, offsetX + h - (topChoice - first + 1) * leading, box.Width - 2 * offsetX, leading); app.Fill(); float xp = offsetX * 2; float yp = offsetX + h - ufont.GetFontDescriptor(BaseFont.BBOXURY, usize); for (int idx = first; idx < last; ++idx, yp -= leading) { String ptext = choices[idx]; int rtl = CheckRTL(ptext) ? PdfWriter.RUN_DIRECTION_LTR : PdfWriter.RUN_DIRECTION_NO_BIDI; ptext = RemoveCRLF(ptext); Phrase phrase = ComposePhrase(ptext, ufont, (idx == topChoice) ? GrayColor.GRAYWHITE : fcolor, usize); ColumnText.ShowTextAligned(app, Element.ALIGN_LEFT, phrase, xp, yp, 0, rtl, 0); } app.RestoreState(); app.EndVariableText(); return(app); }
/** * Get the <code>PdfAppearance</code> of a text or combo field * @throws IOException on error * @throws DocumentException on error * @return A <code>PdfAppearance</code> */ public PdfAppearance GetAppearance() { PdfAppearance app = GetBorderAppearance(); app.BeginVariableText(); if (text == null || text.Length == 0) { app.EndVariableText(); return(app); } bool borderExtra = borderStyle == PdfBorderDictionary.STYLE_BEVELED || borderStyle == PdfBorderDictionary.STYLE_INSET; float h = box.Height - borderWidth * 2 - extraMarginTop; float bw2 = borderWidth; if (borderExtra) { h -= borderWidth * 2; bw2 *= 2; } float offsetX = Math.Max(bw2, 1); float offX = Math.Min(bw2, offsetX); app.SaveState(); app.Rectangle(offX, offX, box.Width - 2 * offX, box.Height - 2 * offX); app.Clip(); app.NewPath(); String ptext; if ((options & PASSWORD) != 0) { ptext = ObfuscatePassword(text); } else if ((options & MULTILINE) == 0) { ptext = RemoveCRLF(text); } else { ptext = text; //fixed by Kazuya Ujihara (ujihara.jp) } BaseFont ufont = RealFont; Color fcolor = (textColor == null) ? GrayColor.GRAYBLACK : textColor; int rtl = CheckRTL(ptext) ? PdfWriter.RUN_DIRECTION_LTR : PdfWriter.RUN_DIRECTION_NO_BIDI; float usize = fontSize; Phrase phrase = ComposePhrase(ptext, ufont, fcolor, usize); if ((options & MULTILINE) != 0) { float width = box.Width - 4 * offsetX - extraMarginLeft; float factor = ufont.GetFontDescriptor(BaseFont.BBOXURY, 1) - ufont.GetFontDescriptor(BaseFont.BBOXLLY, 1); ColumnText ct = new ColumnText(null); if (usize == 0) { usize = h / factor; if (usize > 4) { if (usize > 12) { usize = 12; } float step = Math.Max((usize - 4) / 10, 0.2f); ct.SetSimpleColumn(0, -h, width, 0); ct.Alignment = alignment; ct.RunDirection = rtl; for (; usize > 4; usize -= step) { ct.YLine = 0; ChangeFontSize(phrase, usize); ct.SetText(phrase); ct.Leading = factor * usize; int status = ct.Go(true); if ((status & ColumnText.NO_MORE_COLUMN) == 0) { break; } } } if (usize < 4) { usize = 4; } } ChangeFontSize(phrase, usize); ct.Canvas = app; float leading = usize * factor; float offsetY = offsetX + h - ufont.GetFontDescriptor(BaseFont.BBOXURY, usize); ct.SetSimpleColumn(extraMarginLeft + 2 * offsetX, -20000, box.Width - 2 * offsetX, offsetY + leading); ct.Leading = leading; ct.Alignment = alignment; ct.RunDirection = rtl; ct.SetText(phrase); ct.Go(); } else { if (usize == 0) { float maxCalculatedSize = h / (ufont.GetFontDescriptor(BaseFont.BBOXURX, 1) - ufont.GetFontDescriptor(BaseFont.BBOXLLY, 1)); ChangeFontSize(phrase, 1); float wd = ColumnText.GetWidth(phrase, rtl, 0); if (wd == 0) { usize = maxCalculatedSize; } else { usize = Math.Min(maxCalculatedSize, (box.Width - extraMarginLeft - 4 * offsetX) / wd); } if (usize < 4) { usize = 4; } } ChangeFontSize(phrase, usize); float offsetY = offX + ((box.Height - 2 * offX) - ufont.GetFontDescriptor(BaseFont.ASCENT, usize)) / 2; if (offsetY < offX) { offsetY = offX; } if (offsetY - offX < -ufont.GetFontDescriptor(BaseFont.DESCENT, usize)) { float ny = -ufont.GetFontDescriptor(BaseFont.DESCENT, usize) + offX; float dy = box.Height - offX - ufont.GetFontDescriptor(BaseFont.ASCENT, usize); offsetY = Math.Min(ny, Math.Max(offsetY, dy)); } if ((options & COMB) != 0 && maxCharacterLength > 0) { int textLen = Math.Min(maxCharacterLength, ptext.Length); int position = 0; if (alignment == Element.ALIGN_RIGHT) { position = maxCharacterLength - textLen; } else if (alignment == Element.ALIGN_CENTER) { position = (maxCharacterLength - textLen) / 2; } float step = (box.Width - extraMarginLeft) / maxCharacterLength; float start = step / 2 + position * step; if (textColor == null) { app.SetGrayFill(0); } else { app.SetColorFill(textColor); } app.BeginText(); foreach (Chunk ck in phrase) { BaseFont bf = ck.Font.BaseFont; app.SetFontAndSize(bf, usize); StringBuilder sb = ck.Append(""); for (int j = 0; j < sb.Length; ++j) { String c = sb.ToString(j, 1); float wd = bf.GetWidthPoint(c, usize); app.SetTextMatrix(extraMarginLeft + start - wd / 2, offsetY - extraMarginTop); app.ShowText(c); start += step; } } app.EndText(); } else { float x; switch (alignment) { case Element.ALIGN_RIGHT: x = extraMarginLeft + box.Width - (2 * offsetX); break; case Element.ALIGN_CENTER: x = extraMarginLeft + (box.Width / 2); break; default: x = extraMarginLeft + (2 * offsetX); break; } ColumnText.ShowTextAligned(app, alignment, phrase, x, offsetY - extraMarginTop, 0, rtl, 0); } } app.RestoreState(); app.EndVariableText(); return(app); }
/** * Gets the button appearance. * @throws IOException on error * @throws DocumentException on error * @return the button appearance */ public PdfAppearance GetAppearance() { PdfAppearance app = GetBorderAppearance(); Rectangle box = new Rectangle(app.BoundingBox); if ((text == null || text.Length == 0) && (layout == LAYOUT_LABEL_ONLY || (image == null && template == null && iconReference == null))) { return(app); } if (layout == LAYOUT_ICON_ONLY && image == null && template == null && iconReference == null) { return(app); } BaseFont ufont = RealFont; bool borderExtra = borderStyle == PdfBorderDictionary.STYLE_BEVELED || borderStyle == PdfBorderDictionary.STYLE_INSET; float h = box.Height - borderWidth * 2; float bw2 = borderWidth; if (borderExtra) { h -= borderWidth * 2; bw2 *= 2; } float offsetX = (borderExtra ? 2 * borderWidth : borderWidth); offsetX = Math.Max(offsetX, 1); float offX = Math.Min(bw2, offsetX); tp = null; float textX = float.NaN; float textY = 0; float fsize = fontSize; float wt = box.Width - 2 * offX - 2; float ht = box.Height - 2 * offX; float adj = (iconFitToBounds ? 0 : offX + 1); int nlayout = layout; if (image == null && template == null && iconReference == null) { nlayout = LAYOUT_LABEL_ONLY; } Rectangle iconBox = null; while (true) { switch (nlayout) { case LAYOUT_LABEL_ONLY: case LAYOUT_LABEL_OVER_ICON: if (text != null && text.Length > 0 && wt > 0 && ht > 0) { fsize = CalculateFontSize(wt, ht); textX = (box.Width - ufont.GetWidthPoint(text, fsize)) / 2; textY = (box.Height - ufont.GetFontDescriptor(BaseFont.ASCENT, fsize)) / 2; } goto case LAYOUT_ICON_ONLY; case LAYOUT_ICON_ONLY: if (nlayout == LAYOUT_LABEL_OVER_ICON || nlayout == LAYOUT_ICON_ONLY) { iconBox = new Rectangle(box.Left + adj, box.Bottom + adj, box.Right - adj, box.Top - adj); } break; case LAYOUT_ICON_TOP_LABEL_BOTTOM: if (text == null || text.Length == 0 || wt <= 0 || ht <= 0) { nlayout = LAYOUT_ICON_ONLY; continue; } float nht = box.Height * 0.35f - offX; if (nht > 0) { fsize = CalculateFontSize(wt, nht); } else { fsize = 4; } textX = (box.Width - ufont.GetWidthPoint(text, fsize)) / 2; textY = offX - ufont.GetFontDescriptor(BaseFont.DESCENT, fsize); iconBox = new Rectangle(box.Left + adj, textY + fsize, box.Right - adj, box.Top - adj); break; case LAYOUT_LABEL_TOP_ICON_BOTTOM: if (text == null || text.Length == 0 || wt <= 0 || ht <= 0) { nlayout = LAYOUT_ICON_ONLY; continue; } nht = box.Height * 0.35f - offX; if (nht > 0) { fsize = CalculateFontSize(wt, nht); } else { fsize = 4; } textX = (box.Width - ufont.GetWidthPoint(text, fsize)) / 2; textY = box.Height - offX - fsize; if (textY < offX) { textY = offX; } iconBox = new Rectangle(box.Left + adj, box.Bottom + adj, box.Right - adj, textY + ufont.GetFontDescriptor(BaseFont.DESCENT, fsize)); break; case LAYOUT_LABEL_LEFT_ICON_RIGHT: if (text == null || text.Length == 0 || wt <= 0 || ht <= 0) { nlayout = LAYOUT_ICON_ONLY; continue; } float nw = box.Width * 0.35f - offX; if (nw > 0) { fsize = CalculateFontSize(wt, nw); } else { fsize = 4; } if (ufont.GetWidthPoint(text, fsize) >= wt) { nlayout = LAYOUT_LABEL_ONLY; fsize = fontSize; continue; } textX = offX + 1; textY = (box.Height - ufont.GetFontDescriptor(BaseFont.ASCENT, fsize)) / 2; iconBox = new Rectangle(textX + ufont.GetWidthPoint(text, fsize), box.Bottom + adj, box.Right - adj, box.Top - adj); break; case LAYOUT_ICON_LEFT_LABEL_RIGHT: if (text == null || text.Length == 0 || wt <= 0 || ht <= 0) { nlayout = LAYOUT_ICON_ONLY; continue; } nw = box.Width * 0.35f - offX; if (nw > 0) { fsize = CalculateFontSize(wt, nw); } else { fsize = 4; } if (ufont.GetWidthPoint(text, fsize) >= wt) { nlayout = LAYOUT_LABEL_ONLY; fsize = fontSize; continue; } textX = box.Width - ufont.GetWidthPoint(text, fsize) - offX - 1; textY = (box.Height - ufont.GetFontDescriptor(BaseFont.ASCENT, fsize)) / 2; iconBox = new Rectangle(box.Left + adj, box.Bottom + adj, textX - 1, box.Top - adj); break; } break; } if (textY < box.Bottom + offX) { textY = box.Bottom + offX; } if (iconBox != null && (iconBox.Width <= 0 || iconBox.Height <= 0)) { iconBox = null; } bool haveIcon = false; float boundingBoxWidth = 0; float boundingBoxHeight = 0; PdfArray matrix = null; if (iconBox != null) { if (image != null) { tp = new PdfTemplate(writer); tp.BoundingBox = new Rectangle(image); writer.AddDirectTemplateSimple(tp, PdfName.FRM); tp.AddImage(image, image.Width, 0, 0, image.Height, 0, 0); haveIcon = true; boundingBoxWidth = tp.BoundingBox.Width; boundingBoxHeight = tp.BoundingBox.Height; } else if (template != null) { tp = new PdfTemplate(writer); tp.BoundingBox = new Rectangle(template.Width, template.Height); writer.AddDirectTemplateSimple(tp, PdfName.FRM); tp.AddTemplate(template, template.BoundingBox.Left, template.BoundingBox.Bottom); haveIcon = true; boundingBoxWidth = tp.BoundingBox.Width; boundingBoxHeight = tp.BoundingBox.Height; } else if (iconReference != null) { PdfDictionary dic = (PdfDictionary)PdfReader.GetPdfObject(iconReference); if (dic != null) { Rectangle r2 = PdfReader.GetNormalizedRectangle(dic.GetAsArray(PdfName.BBOX)); matrix = dic.GetAsArray(PdfName.MATRIX); haveIcon = true; boundingBoxWidth = r2.Width; boundingBoxHeight = r2.Height; } } } if (haveIcon) { float icx = iconBox.Width / boundingBoxWidth; float icy = iconBox.Height / boundingBoxHeight; if (proportionalIcon) { switch (scaleIcon) { case SCALE_ICON_IS_TOO_BIG: icx = Math.Min(icx, icy); icx = Math.Min(icx, 1); break; case SCALE_ICON_IS_TOO_SMALL: icx = Math.Min(icx, icy); icx = Math.Max(icx, 1); break; case SCALE_ICON_NEVER: icx = 1; break; default: icx = Math.Min(icx, icy); break; } icy = icx; } else { switch (scaleIcon) { case SCALE_ICON_IS_TOO_BIG: icx = Math.Min(icx, 1); icy = Math.Min(icy, 1); break; case SCALE_ICON_IS_TOO_SMALL: icx = Math.Max(icx, 1); icy = Math.Max(icy, 1); break; case SCALE_ICON_NEVER: icx = icy = 1; break; default: break; } } float xpos = iconBox.Left + (iconBox.Width - (boundingBoxWidth * icx)) * iconHorizontalAdjustment; float ypos = iconBox.Bottom + (iconBox.Height - (boundingBoxHeight * icy)) * iconVerticalAdjustment; app.SaveState(); app.Rectangle(iconBox.Left, iconBox.Bottom, iconBox.Width, iconBox.Height); app.Clip(); app.NewPath(); if (tp != null) { app.AddTemplate(tp, icx, 0, 0, icy, xpos, ypos); } else { float cox = 0; float coy = 0; if (matrix != null && matrix.Size == 6) { PdfNumber nm = matrix.GetAsNumber(4); if (nm != null) { cox = nm.FloatValue; } nm = matrix.GetAsNumber(5); if (nm != null) { coy = nm.FloatValue; } } app.AddTemplateReference(iconReference, PdfName.FRM, icx, 0, 0, icy, xpos - cox * icx, ypos - coy * icy); } app.RestoreState(); } if (!float.IsNaN(textX)) { app.SaveState(); app.Rectangle(offX, offX, box.Width - 2 * offX, box.Height - 2 * offX); app.Clip(); app.NewPath(); if (textColor == null) { app.ResetGrayFill(); } else { app.SetColorFill(textColor); } app.BeginText(); app.SetFontAndSize(ufont, fsize); app.SetTextMatrix(textX, textY); app.ShowText(text); app.EndText(); app.RestoreState(); } return(app); }
/** * Gets the field appearance. * @param isRadio <CODE>true</CODE> for a radio field and <CODE>false</CODE> * for a check field * @param on <CODE>true</CODE> for the checked state, <CODE>false</CODE> * otherwise * @throws IOException on error * @throws DocumentException on error * @return the appearance */ public PdfAppearance GetAppearance(bool isRadio, bool on) { if (isRadio && checkType == TYPE_CIRCLE) { return(GetAppearanceRadioCircle(on)); } PdfAppearance app = GetBorderAppearance(); if (!on) { return(app); } BaseFont ufont = RealFont; bool borderExtra = borderStyle == PdfBorderDictionary.STYLE_BEVELED || borderStyle == PdfBorderDictionary.STYLE_INSET; float h = box.Height - borderWidth * 2; float bw2 = borderWidth; if (borderExtra) { h -= borderWidth * 2; bw2 *= 2; } float offsetX = (borderExtra ? 2 * borderWidth : borderWidth); offsetX = Math.Max(offsetX, 1); float offX = Math.Min(bw2, offsetX); float wt = box.Width - 2 * offX; float ht = box.Height - 2 * offX; float fsize = fontSize; if (fsize == 0) { float bw = ufont.GetWidthPoint(text, 1); if (bw == 0) { fsize = 12; } else { fsize = wt / bw; } float nfsize = h / (ufont.GetFontDescriptor(BaseFont.ASCENT, 1)); fsize = Math.Min(fsize, nfsize); } app.SaveState(); app.Rectangle(offX, offX, wt, ht); app.Clip(); app.NewPath(); if (textColor == null) { app.ResetGrayFill(); } else { app.SetColorFill(textColor); } app.BeginText(); app.SetFontAndSize(ufont, fsize); app.SetTextMatrix((box.Width - ufont.GetWidthPoint(text, fsize)) / 2, (box.Height - ufont.GetAscentPoint(text, fsize)) / 2); app.ShowText(text); app.EndText(); app.RestoreState(); return(app); }
protected PdfAppearance GetBorderAppearance() { PdfAppearance app = PdfAppearance.CreateAppearance(writer, box.Width, box.Height); switch (rotation) { case 90: app.SetMatrix(0, 1, -1, 0, box.Height, 0); break; case 180: app.SetMatrix(-1, 0, 0, -1, box.Width, box.Height); break; case 270: app.SetMatrix(0, -1, 1, 0, 0, box.Width); break; } app.SaveState(); // background if (backgroundColor != null) { app.SetColorFill(backgroundColor); app.Rectangle(0, 0, box.Width, box.Height); app.Fill(); } // border if (borderStyle == PdfBorderDictionary.STYLE_UNDERLINE) { if (borderWidth != 0 && borderColor != null) { app.SetColorStroke(borderColor); app.SetLineWidth(borderWidth); app.MoveTo(0, borderWidth / 2); app.LineTo(box.Width, borderWidth / 2); app.Stroke(); } } else if (borderStyle == PdfBorderDictionary.STYLE_BEVELED) { if (borderWidth != 0 && borderColor != null) { app.SetColorStroke(borderColor); app.SetLineWidth(borderWidth); app.Rectangle(borderWidth / 2, borderWidth / 2, box.Width - borderWidth, box.Height - borderWidth); app.Stroke(); } // beveled Color actual = backgroundColor; if (actual == null) { actual = Color.WHITE; } app.SetGrayFill(1); DrawTopFrame(app); app.SetColorFill(actual.Darker()); DrawBottomFrame(app); } else if (borderStyle == PdfBorderDictionary.STYLE_INSET) { if (borderWidth != 0 && borderColor != null) { app.SetColorStroke(borderColor); app.SetLineWidth(borderWidth); app.Rectangle(borderWidth / 2, borderWidth / 2, box.Width - borderWidth, box.Height - borderWidth); app.Stroke(); } // inset app.SetGrayFill(0.5f); DrawTopFrame(app); app.SetGrayFill(0.75f); DrawBottomFrame(app); } else { if (borderWidth != 0 && borderColor != null) { if (borderStyle == PdfBorderDictionary.STYLE_DASHED) { app.SetLineDash(3, 0); } app.SetColorStroke(borderColor); app.SetLineWidth(borderWidth); app.Rectangle(borderWidth / 2, borderWidth / 2, box.Width - borderWidth, box.Height - borderWidth); app.Stroke(); if ((options & COMB) != 0 && maxCharacterLength > 1) { float step = box.Width / maxCharacterLength; float yb = borderWidth / 2; float yt = box.Height - borderWidth / 2; for (int k = 1; k < maxCharacterLength; ++k) { float x = step * k; app.MoveTo(x, yb); app.LineTo(x, yt); } app.Stroke(); } } } app.RestoreState(); return(app); }