/** * Writes the border and background of one cell in the row. * * @param xPos The x-coordinate where the table starts on the canvas * @param yPos The y-coordinate where the table starts on the canvas * @param currentMaxHeight The height of the cell to be drawn. * @param cell * @param canvases * @since 2.1.6 extra parameter currentMaxHeight */ public void WriteBorderAndBackground(float xPos, float yPos, float currentMaxHeight, PdfPCell cell, PdfContentByte[] canvases) { Color background = cell.BackgroundColor; if (background != null || cell.HasBorders()) { // Add xPos resp. yPos to the cell's coordinates for absolute coordinates float right = cell.Right + xPos; float top = cell.Top + yPos; float left = cell.Left + xPos; float bottom = top - currentMaxHeight; if (background != null) { PdfContentByte backgr = canvases[PdfPTable.BACKGROUNDCANVAS]; backgr.SetColorFill(background); backgr.Rectangle(left, bottom, right - left, top - bottom); backgr.Fill(); } if (cell.HasBorders()) { Rectangle newRect = new Rectangle(left, bottom, right, top); // Clone non-position parameters except for the background color newRect.CloneNonPositionParameters(cell); newRect.BackgroundColor = null; // Write the borders on the line canvas PdfContentByte lineCanvas = canvases[PdfPTable.LINECANVAS]; lineCanvas.Rectangle(newRect); } } }
public PdfArray RotateAnnotations(PdfWriter writer, Rectangle pageSize) { PdfArray array = new PdfArray(); int rotation = pageSize.Rotation % 360; int currentPage = writer.CurrentPageNumber; for (int k = 0; k < annotations.Count; ++k) { PdfAnnotation dic = (PdfAnnotation)annotations[k]; int page = dic.PlaceInPage; if (page > currentPage) { delayedAnnotations.Add(dic); continue; } if (dic.IsForm()) { if (!dic.IsUsed()) { Hashtable templates = dic.Templates; if (templates != null) acroForm.AddFieldTemplates(templates); } PdfFormField field = (PdfFormField)dic; if (field.Parent == null) acroForm.AddDocumentField(field.IndirectReference); } if (dic.IsAnnotation()) { array.Add(dic.IndirectReference); if (!dic.IsUsed()) { PdfRectangle rect = (PdfRectangle)dic.Get(PdfName.RECT); if (rect != null) { switch (rotation) { case 90: dic.Put(PdfName.RECT, new PdfRectangle( pageSize.Top - rect.Bottom, rect.Left, pageSize.Top - rect.Top, rect.Right)); break; case 180: dic.Put(PdfName.RECT, new PdfRectangle( pageSize.Right - rect.Left, pageSize.Top - rect.Bottom, pageSize.Right - rect.Right, pageSize.Top - rect.Top)); break; case 270: dic.Put(PdfName.RECT, new PdfRectangle( rect.Bottom, pageSize.Right - rect.Left, rect.Top, pageSize.Right - rect.Right)); break; } } } } if (!dic.IsUsed()) { dic.SetUsed(); writer.AddToBody(dic, dic.IndirectReference); } } return array; }
/** * Creates a new instance of PushbuttonField * @param writer the document <CODE>PdfWriter</CODE> * @param box the field location and dimensions * @param fieldName the field name. If <CODE>null</CODE> only the widget keys * will be included in the field allowing it to be used as a kid field. */ public PushbuttonField(PdfWriter writer, Rectangle box, String fieldName) : base(writer, box, fieldName) { }
/** * Sets the size of the page * * @param rect A Rectangle representing the page * @return <code>false</code> */ public override bool SetPageSize(Rectangle rect) { rtfDoc.GetDocumentHeader().GetPageSetting().SetPageSize(rect); return true; }
/** * @see com.lowagie.text.pdf.PdfPTableEvent#tableLayout(com.lowagie.text.pdf.PdfPTable, float[][], float[], int, int, com.lowagie.text.pdf.PdfContentByte[]) */ public void TableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) { float[] width = widths[0]; Rectangle rect = new Rectangle(width[0], heights[heights.Length - 1], width[width.Length - 1], heights[0]); rect.CloneNonPositionParameters(this); int bd = rect.Border; rect.Border = Rectangle.NO_BORDER; canvases[PdfPTable.BACKGROUNDCANVAS].Rectangle(rect); rect.Border = bd; rect.BackgroundColor = null; canvases[PdfPTable.LINECANVAS].Rectangle(rect); }
/** * @see com.lowagie.text.pdf.PdfPCellEvent#cellLayout(com.lowagie.text.pdf.PdfPCell, com.lowagie.text.Rectangle, com.lowagie.text.pdf.PdfContentByte[]) */ public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { float sp_left = spacing_left; if (float.IsNaN(sp_left)) sp_left = 0f; float sp_right = spacing_right; if (float.IsNaN(sp_right)) sp_right = 0f; float sp_top = spacing_top; if (float.IsNaN(sp_top)) sp_top = 0f; float sp_bottom = spacing_bottom; if (float.IsNaN(sp_bottom)) sp_bottom = 0f; Rectangle rect = new Rectangle(position.GetLeft(sp_left), position.GetBottom(sp_bottom), position.GetRight(sp_right), position.GetTop(sp_top)); rect.CloneNonPositionParameters(this); canvases[PdfPTable.BACKGROUNDCANVAS].Rectangle(rect); rect.BackgroundColor = null; canvases[PdfPTable.LINECANVAS].Rectangle(rect); }
/// <summary> /// Constructs a new Document-object. /// </summary> /// <param name="pageSize">the pageSize</param> /// <param name="marginLeft">the margin on the left</param> /// <param name="marginRight">the margin on the right</param> /// <param name="marginTop">the margin on the top</param> /// <param name="marginBottom">the margin on the bottom</param> public Document(Rectangle pageSize, float marginLeft, float marginRight, float marginTop, float marginBottom) { this.pageSize = pageSize; this.marginLeft = marginLeft; this.marginRight = marginRight; this.marginTop = marginTop; this.marginBottom = marginBottom; }
/** * Sets the signature to be visible. An empty signature field with the same name must already exist. * @param fieldName the existing empty signature field name */ public void SetVisibleSignature(String fieldName) { AcroFields af = writer.AcroFields; AcroFields.Item item = af.GetFieldItem(fieldName); if (item == null) throw new ArgumentException("The field " + fieldName + " does not exist."); PdfDictionary merged = item.GetMerged(0); if (!PdfName.SIG.Equals(PdfReader.GetPdfObject(merged.Get(PdfName.FT)))) throw new ArgumentException("The field " + fieldName + " is not a signature field."); this.fieldName = fieldName; PdfArray r = merged.GetAsArray(PdfName.RECT); float llx = r.GetAsNumber(0).FloatValue; float lly = r.GetAsNumber(1).FloatValue; float urx = r.GetAsNumber(2).FloatValue; float ury = r.GetAsNumber(3).FloatValue; pageRect = new Rectangle(llx, lly, urx, ury); pageRect.Normalize(); page = item.GetPage(0); int rotation = writer.reader.GetPageRotation(page); Rectangle pageSize = writer.reader.GetPageSizeWithRotation(page); switch (rotation) { case 90: pageRect = new Rectangle( pageRect.Bottom, pageSize.Top - pageRect.Left, pageRect.Top, pageSize.Top - pageRect.Right); break; case 180: pageRect = new Rectangle( pageSize.Right - pageRect.Left, pageSize.Top - pageRect.Bottom, pageSize.Right - pageRect.Right, pageSize.Top - pageRect.Top); break; case 270: pageRect = new Rectangle( pageSize.Right - pageRect.Bottom, pageRect.Left, pageSize.Right - pageRect.Top, pageRect.Right); break; } if (rotation != 0) pageRect.Normalize(); rect = new Rectangle(this.pageRect.Width, this.pageRect.Height); }
/** * Fits the text to some rectangle adjusting the font size as needed. * @param font the font to use * @param text the text * @param rect the rectangle where the text must fit * @param maxFontSize the maximum font size * @param runDirection the run direction * @return the calculated font size that makes the text fit */ public static float FitText(Font font, String text, Rectangle rect, float maxFontSize, int runDirection) { ColumnText ct = null; int status = 0; if (maxFontSize <= 0) { int cr = 0; int lf = 0; char[] t = text.ToCharArray(); for (int k = 0; k < t.Length; ++k) { if (t[k] == '\n') ++lf; else if (t[k] == '\r') ++cr; } int minLines = Math.Max(cr, lf) + 1; maxFontSize = Math.Abs(rect.Height) / minLines - 0.001f; } font.Size = maxFontSize; Phrase ph = new Phrase(text, font); ct = new ColumnText(null); ct.SetSimpleColumn(ph, rect.Left, rect.Bottom, rect.Right, rect.Top, maxFontSize, Element.ALIGN_LEFT); ct.RunDirection = runDirection; status = ct.Go(true); if ((status & ColumnText.NO_MORE_TEXT) != 0) return maxFontSize; float precision = 0.1f; float min = 0; float max = maxFontSize; float size = maxFontSize; for (int k = 0; k < 50; ++k) { //just in case it doesn't converge size = (min + max) / 2; ct = new ColumnText(null); font.Size = size; ct.SetSimpleColumn(new Phrase(text, font), rect.Left, rect.Bottom, rect.Right, rect.Top, size, Element.ALIGN_LEFT); ct.RunDirection = runDirection; status = ct.Go(true); if ((status & ColumnText.NO_MORE_TEXT) != 0) { if (max - min < size * precision) return size; min = size; } else max = size; } return size; }
/** * @see com.lowagie.text.pdf.PdfPCellEvent#cellLayout(com.lowagie.text.pdf.PdfPCell, com.lowagie.text.Rectangle, com.lowagie.text.pdf.PdfContentByte[]) */ public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { foreach (IPdfPCellEvent eventa in events) { eventa.CellLayout(cell, position, canvases); } }
/** * Sets the signature to be visible. It creates a new visible signature field. * @param pageRect the position and dimension of the field in the page * @param page the page to place the field. The fist page is 1 * @param fieldName the field name or <CODE>null</CODE> to generate automatically a new field name */ public void SetVisibleSignature(Rectangle pageRect, int page, String fieldName) { if (fieldName != null) { if (fieldName.IndexOf('.') >= 0) throw new ArgumentException("Field names cannot contain a dot."); AcroFields af = writer.AcroFields; AcroFields.Item item = af.GetFieldItem(fieldName); if (item != null) throw new ArgumentException("The field " + fieldName + " already exists."); this.fieldName = fieldName; } if (page < 1 || page > writer.reader.NumberOfPages) throw new ArgumentException("Invalid page number: " + page); this.pageRect = new Rectangle(pageRect); this.pageRect.Normalize(); rect = new Rectangle(this.pageRect.Width, this.pageRect.Height); this.page = page; newField = true; }
/** * Called when a <CODE>Chunk</CODE> with a generic tag is written. * <P> * It is usefull to pinpoint the <CODE>Chunk</CODE> location to generate * bookmarks, for example. * * @param writer * the <CODE>PdfWriter</CODE> for this document * @param document * the document * @param rect * the <CODE>Rectangle</CODE> containing the <CODE>Chunk * </CODE> * @param text * the text of the tag */ public virtual void OnGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) { foreach (IPdfPageEvent eventa in events) { eventa.OnGenericTag(writer, document, rect, text); } }
/// <summary> /// Sets the pagesize. /// </summary> /// <param name="pageSize">the new pagesize</param> /// <returns>a boolean</returns> public virtual bool SetPageSize(Rectangle pageSize) { this.pageSize = pageSize; return true; }
/** * Writes a number of cells (not necessarily all cells). * * @param colStart The first column to be written. * Remember that the column index starts with 0. * @param colEnd The last column to be written. * Remember that the column index starts with 0. * If -1, all the columns to the end are written. * @param xPos The x-coordinate where the table starts on the canvas * @param yPos The y-coordinate where the table starts on the canvas */ public void WriteCells(int colStart, int colEnd, float xPos, float yPos, PdfContentByte[] canvases) { if (!calculated) CalculateHeights(); if (colEnd < 0) colEnd = cells.Length; else colEnd = Math.Min(colEnd, cells.Length); if (colStart < 0) colStart = 0; if (colStart >= colEnd) return; int newStart; for (newStart = colStart; newStart >= 0; --newStart) { if (cells[newStart] != null) break; if (newStart > 0) xPos -= widths[newStart - 1]; } if (newStart < 0) newStart = 0; if (cells[newStart] != null) xPos -= cells[newStart].Left; for (int k = newStart; k < colEnd; ++k) { PdfPCell cell = cells[k]; if (cell == null) continue; float currentMaxHeight = maxHeight + extraHeights[k]; WriteBorderAndBackground(xPos, yPos, currentMaxHeight, cell, canvases); Image img = cell.Image; float tly = cell.Top + yPos - cell.EffectivePaddingTop; if (cell.Height <= currentMaxHeight) { switch (cell.VerticalAlignment) { case Element.ALIGN_BOTTOM: tly = cell.Top + yPos - currentMaxHeight + cell.Height - cell.EffectivePaddingTop; break; case Element.ALIGN_MIDDLE: tly = cell.Top + yPos + (cell.Height - currentMaxHeight) / 2 - cell.EffectivePaddingTop; break; default: break; } } if (img != null) { if (cell.Rotation != 0) { img = Image.GetInstance(img); img.Rotation = img.GetImageRotation() + (float)(cell.Rotation * Math.PI / 180.0); } bool vf = false; if (cell.Height > currentMaxHeight) { img.ScalePercent(100); float scale = (currentMaxHeight - cell.EffectivePaddingTop - cell .EffectivePaddingBottom) / img.ScaledHeight; img.ScalePercent(scale * 100); vf = true; } float left = cell.Left + xPos + cell.EffectivePaddingLeft; if (vf) { switch (cell.HorizontalAlignment) { case Element.ALIGN_CENTER: left = xPos + (cell.Left + cell.EffectivePaddingLeft + cell.Right - cell.EffectivePaddingRight - img .ScaledWidth) / 2; break; case Element.ALIGN_RIGHT: left = xPos + cell.Right - cell.EffectivePaddingRight - img.ScaledWidth; break; default: break; } tly = cell.Top + yPos - cell.EffectivePaddingTop; } img.SetAbsolutePosition(left, tly - img.ScaledHeight); canvases[PdfPTable.TEXTCANVAS].AddImage(img); } else { // rotation sponsored by Connection GmbH if (cell.Rotation == 90 || cell.Rotation == 270) { float netWidth = currentMaxHeight - cell.EffectivePaddingTop - cell.EffectivePaddingBottom; float netHeight = cell.Width - cell.EffectivePaddingLeft - cell.EffectivePaddingRight; ColumnText ct = ColumnText.Duplicate(cell.Column); ct.Canvases = canvases; ct.SetSimpleColumn(0, 0, netWidth + 0.001f, -netHeight); ct.Go(true); float calcHeight = -ct.YLine; if (netWidth <= 0 || netHeight <= 0) calcHeight = 0; if (calcHeight > 0) { if (cell.UseDescender) calcHeight -= ct.Descender; ct = ColumnText.Duplicate(cell.Column); ct.Canvases = canvases; ct.SetSimpleColumn(-0.003f, -0.001f, netWidth + 0.003f, calcHeight); float pivotX; float pivotY; if (cell.Rotation == 90) { pivotY = cell.Top + yPos - currentMaxHeight + cell.EffectivePaddingBottom; switch (cell.VerticalAlignment) { case Element.ALIGN_BOTTOM: pivotX = cell.Left + xPos + cell.Width - cell.EffectivePaddingRight; break; case Element.ALIGN_MIDDLE: pivotX = cell.Left + xPos + (cell.Width + cell.EffectivePaddingLeft - cell.EffectivePaddingRight + calcHeight) / 2; break; default: //top pivotX = cell.Left + xPos + cell.EffectivePaddingLeft + calcHeight; break; } SaveAndRotateCanvases(canvases, 0,1,-1,0,pivotX,pivotY); } else { pivotY = cell.Top + yPos - cell.EffectivePaddingTop; switch (cell.VerticalAlignment) { case Element.ALIGN_BOTTOM: pivotX = cell.Left + xPos + cell.EffectivePaddingLeft; break; case Element.ALIGN_MIDDLE: pivotX = cell.Left + xPos + (cell.Width + cell.EffectivePaddingLeft - cell.EffectivePaddingRight - calcHeight) / 2; break; default: //top pivotX = cell.Left + xPos + cell.Width - cell.EffectivePaddingRight - calcHeight; break; } SaveAndRotateCanvases(canvases, 0,-1,1,0,pivotX,pivotY); } try { ct.Go(); } finally { RestoreCanvases(canvases); } } } else { float fixedHeight = cell.FixedHeight; float rightLimit = cell.Right + xPos - cell.EffectivePaddingRight; float leftLimit = cell.Left + xPos + cell.EffectivePaddingLeft; if (cell.NoWrap) { switch (cell.HorizontalAlignment) { case Element.ALIGN_CENTER: rightLimit += 10000; leftLimit -= 10000; break; case Element.ALIGN_RIGHT: if (cell.Rotation == 180) { rightLimit += RIGHT_LIMIT; } else { leftLimit -= RIGHT_LIMIT; } break; default: if (cell.Rotation == 180) { leftLimit -= RIGHT_LIMIT; } else { rightLimit += RIGHT_LIMIT; } break; } } ColumnText ct = ColumnText.Duplicate(cell.Column); ct.Canvases = canvases; float bry = tly - (currentMaxHeight - cell.EffectivePaddingTop - cell.EffectivePaddingBottom); if (fixedHeight > 0) { if (cell.Height > currentMaxHeight) { tly = cell.Top + yPos - cell.EffectivePaddingTop; bry = cell.Top + yPos - currentMaxHeight + cell.EffectivePaddingBottom; } } if ((tly > bry || ct.ZeroHeightElement()) && leftLimit < rightLimit) { ct.SetSimpleColumn(leftLimit, bry - 0.001f, rightLimit, tly); if (cell.Rotation == 180) { float shx = leftLimit + rightLimit; float shy = yPos + yPos - currentMaxHeight + cell.EffectivePaddingBottom - cell.EffectivePaddingTop; SaveAndRotateCanvases(canvases, -1,0,0,-1,shx,shy); } try { ct.Go(); } finally { if (cell.Rotation == 180) { RestoreCanvases(canvases); } } } } } IPdfPCellEvent evt = cell.CellEvent; if (evt != null) { Rectangle rect = new Rectangle(cell.Left + xPos, cell.Top + yPos - currentMaxHeight, cell.Right + xPos, cell.Top + yPos); evt.CellLayout(cell, rect, canvases); } } }
/** * Copies all of the parameters from a <CODE>Rectangle</CODE> object * except the position. * * @param rect * <CODE>Rectangle</CODE> to copy from */ public virtual void SoftCloneNonPositionParameters(Rectangle rect) { if (rect.rotation != 0) this.rotation = rect.rotation; if (rect.border != UNDEFINED) this.border = rect.border; if (rect.borderWidth != UNDEFINED) this.borderWidth = rect.borderWidth; if (rect.borderColor != null) this.borderColor = rect.borderColor; if (rect.backgroundColor != null) this.backgroundColor = rect.backgroundColor; if (rect.borderColorLeft != null) this.borderColorLeft = rect.borderColorLeft; if (rect.borderColorRight != null) this.borderColorRight = rect.borderColorRight; if (rect.borderColorTop != null) this.borderColorTop = rect.borderColorTop; if (rect.borderColorBottom != null) this.borderColorBottom = rect.borderColorBottom; if (rect.borderWidthLeft != UNDEFINED) this.borderWidthLeft = rect.borderWidthLeft; if (rect.borderWidthRight != UNDEFINED) this.borderWidthRight = rect.borderWidthRight; if (rect.borderWidthTop != UNDEFINED) this.borderWidthTop = rect.borderWidthTop; if (rect.borderWidthBottom != UNDEFINED) this.borderWidthBottom = rect.borderWidthBottom; if (useVariableBorders) this.useVariableBorders = rect.useVariableBorders; }
/** * Gets the main appearance layer. * <p> * Consult <A HREF="http://partners.adobe.com/asn/developer/pdfs/tn/PPKAppearances.pdf">PPKAppearances.pdf</A> * for further details. * @return the main appearance layer * @throws DocumentException on error * @throws IOException on error */ public PdfTemplate GetAppearance() { if (IsInvisible()) { PdfTemplate t = new PdfTemplate(writer); t.BoundingBox = new Rectangle(0, 0); writer.AddDirectTemplateSimple(t, null); return t; } if (app[0] == null) { PdfTemplate t = app[0] = new PdfTemplate(writer); t.BoundingBox = new Rectangle(100, 100); writer.AddDirectTemplateSimple(t, new PdfName("n0")); t.SetLiteral("% DSBlank\n"); } if (app[1] == null && !acro6Layers) { PdfTemplate t = app[1] = new PdfTemplate(writer); t.BoundingBox = new Rectangle(100, 100); writer.AddDirectTemplateSimple(t, new PdfName("n1")); t.SetLiteral(questionMark); } if (app[2] == null) { String text; if (layer2Text == null) { StringBuilder buf = new StringBuilder(); buf.Append("Digitally signed by ").Append(PdfPKCS7.GetSubjectFields((X509Certificate)certChain[0]).GetField("CN")).Append('\n'); buf.Append("Date: ").Append(signDate.ToString("yyyy.MM.dd HH:mm:ss zzz")); if (reason != null) buf.Append('\n').Append("Reason: ").Append(reason); if (location != null) buf.Append('\n').Append("Location: ").Append(location); text = buf.ToString(); } else text = layer2Text; PdfTemplate t = app[2] = new PdfTemplate(writer); t.BoundingBox = rect; writer.AddDirectTemplateSimple(t, new PdfName("n2")); if (image != null) { if (imageScale == 0) { t.AddImage(image, rect.Width, 0, 0, rect.Height, 0, 0); } else { float usableScale = imageScale; if (imageScale < 0) usableScale = Math.Min(rect.Width / image.Width, rect.Height / image.Height); float w = image.Width * usableScale; float h = image.Height * usableScale; float x = (rect.Width - w) / 2; float y = (rect.Height - h) / 2; t.AddImage(image, w, 0, 0, h, x, y); } } Font font; if (layer2Font == null) font = new Font(); else font = new Font(layer2Font); float size = font.Size; Rectangle dataRect = null; Rectangle signatureRect = null; if (Render == SignatureRender.NameAndDescription || (Render == SignatureRender.GraphicAndDescription && this.SignatureGraphic != null)) { // origin is the bottom-left signatureRect = new Rectangle( MARGIN, MARGIN, rect.Width / 2 - MARGIN, rect.Height - MARGIN); dataRect = new Rectangle( rect.Width / 2 + MARGIN / 2, MARGIN, rect.Width - MARGIN / 2, rect.Height - MARGIN); if (rect.Height > rect.Width) { signatureRect = new Rectangle( MARGIN, rect.Height / 2, rect.Width - MARGIN, rect.Height); dataRect = new Rectangle( MARGIN, MARGIN, rect.Width - MARGIN, rect.Height / 2 - MARGIN); } } else { dataRect = new Rectangle( MARGIN, MARGIN, rect.Width - MARGIN, rect.Height * (1 - TOP_SECTION) - MARGIN); } if (Render == SignatureRender.NameAndDescription) { string signedBy = Legacy.Text.Pdf.PdfPKCS7.GetSubjectFields(this.certChain[0]).GetField("CN"); Rectangle sr2 = new Rectangle(signatureRect.Width - MARGIN, signatureRect.Height - MARGIN ); float signedSize = FitText(font, signedBy, sr2, -1, runDirection); ColumnText ct2 = new ColumnText(t); ct2.RunDirection = runDirection; ct2.SetSimpleColumn(new Phrase(signedBy, font), signatureRect.Left, signatureRect.Bottom, signatureRect.Right, signatureRect.Top, signedSize, Element.ALIGN_LEFT); ct2.Go(); } else if (Render == SignatureRender.GraphicAndDescription) { ColumnText ct2 = new ColumnText(t); ct2.RunDirection = runDirection; ct2.SetSimpleColumn(signatureRect.Left, signatureRect.Bottom, signatureRect.Right, signatureRect.Top, 0, Element.ALIGN_RIGHT); Image im = Image.GetInstance(SignatureGraphic); im.ScaleToFit(signatureRect.Width, signatureRect.Height); Paragraph p = new Paragraph(); // must calculate the point to draw from to make image appear in middle of column float x = 0; // experimentation found this magic number to counteract Adobe's signature graphic, which // offsets the y co-ordinate by 15 units float y = -im.ScaledHeight + 15; x = x + (signatureRect.Width - im.ScaledWidth) / 2; y = y - (signatureRect.Height - im.ScaledHeight) / 2; p.Add(new Chunk(im, x + (signatureRect.Width - im.ScaledWidth) / 2, y, false)); ct2.AddElement(p); ct2.Go(); } if (size <= 0) { Rectangle sr = new Rectangle(dataRect.Width, dataRect.Height); size = FitText(font, text, sr, 12, runDirection); } ColumnText ct = new ColumnText(t); ct.RunDirection = runDirection; ct.SetSimpleColumn(new Phrase(text, font), dataRect.Left, dataRect.Bottom, dataRect.Right, dataRect.Top, size, Element.ALIGN_LEFT); ct.Go(); } if (app[3] == null && !acro6Layers) { PdfTemplate t = app[3] = new PdfTemplate(writer); t.BoundingBox = new Rectangle(100, 100); writer.AddDirectTemplateSimple(t, new PdfName("n3")); t.SetLiteral("% DSBlank\n"); } if (app[4] == null && !acro6Layers) { PdfTemplate t = app[4] = new PdfTemplate(writer); t.BoundingBox = new Rectangle(0, rect.Height * (1 - TOP_SECTION), rect.Right, rect.Top); writer.AddDirectTemplateSimple(t, new PdfName("n4")); Font font; if (layer2Font == null) font = new Font(); else font = new Font(layer2Font); float size = font.Size; String text = "Signature Not Verified"; if (layer4Text != null) text = layer4Text; Rectangle sr = new Rectangle(rect.Width - 2 * MARGIN, rect.Height * TOP_SECTION - 2 * MARGIN); size = FitText(font, text, sr, 15, runDirection); ColumnText ct = new ColumnText(t); ct.RunDirection = runDirection; ct.SetSimpleColumn(new Phrase(text, font), MARGIN, 0, rect.Width - MARGIN, rect.Height - MARGIN, size, Element.ALIGN_LEFT); ct.Go(); } int rotation = writer.reader.GetPageRotation(page); Rectangle rotated = new Rectangle(rect); int n = rotation; while (n > 0) { rotated = rotated.Rotate(); n -= 90; } if (frm == null) { frm = new PdfTemplate(writer); frm.BoundingBox = rotated; writer.AddDirectTemplateSimple(frm, new PdfName("FRM")); float scale = Math.Min(rect.Width, rect.Height) * 0.9f; float x = (rect.Width - scale) / 2; float y = (rect.Height - scale) / 2; scale /= 100; if (rotation == 90) frm.ConcatCTM(0, 1, -1, 0, rect.Height, 0); else if (rotation == 180) frm.ConcatCTM(-1, 0, 0, -1, rect.Width, rect.Height); else if (rotation == 270) frm.ConcatCTM(0, -1, 1, 0, 0, rect.Width); frm.AddTemplate(app[0], 0, 0); if (!acro6Layers) frm.AddTemplate(app[1], scale, 0, 0, scale, x, y); frm.AddTemplate(app[2], 0, 0); if (!acro6Layers) { frm.AddTemplate(app[3], scale, 0, 0, scale, x, y); frm.AddTemplate(app[4], 0, 0); } } PdfTemplate napp = new PdfTemplate(writer); napp.BoundingBox = rotated; writer.AddDirectTemplateSimple(napp, null); napp.AddTemplate(frm, 0, 0); return napp; }
/** * Adds a blank page. * @param rect The page dimension * @param rotation The rotation angle in degrees * @since 2.1.5 */ public void AddPage(Rectangle rect, int rotation) { PdfRectangle mediabox = new PdfRectangle(rect, rotation); PageResources resources = new PageResources(); PdfPage page = new PdfPage(mediabox, new Hashtable(), resources.Resources, 0); page.Put(PdfName.TABS, Tabs); root.AddPage(page); ++currentPageNumber; }
/** * @see com.lowagie.text.pdf.PdfPCellEvent#cellLayout(com.lowagie.text.pdf.PdfPCell, com.lowagie.text.Rectangle, com.lowagie.text.pdf.PdfContentByte[]) */ public void CellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] canvases) { if (cellField == null || (fieldWriter == null && parent == null)) throw new ArgumentException("You have used the wrong constructor for this FieldPositioningEvents class."); cellField.Put(PdfName.RECT, new PdfRectangle(rect.GetLeft(padding), rect.GetBottom(padding), rect.GetRight(padding), rect.GetTop(padding))); if (parent == null) fieldWriter.AddAnnotation(cellField); else parent.AddKid(cellField); }
/// <summary> /// Constructs a new Document-object. /// </summary> /// <param name="pageSize">the pageSize</param> public Document(Rectangle pageSize) : this(pageSize, 36, 36, 36, 36) { }
/** * @see com.lowagie.text.pdf.PdfPageEvent#onGenericTag(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document, com.lowagie.text.Rectangle, java.lang.String) */ public override void OnGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) { rect.Bottom = rect.Bottom - 3; PdfFormField field = (PdfFormField) genericChunkFields[text]; if (field == null) { TextField tf = new TextField(writer, new Rectangle(rect.GetLeft(padding), rect.GetBottom(padding), rect.GetRight(padding), rect.GetTop(padding)), text); tf.FontSize = 14; field = tf.GetTextField(); } else { field.Put(PdfName.RECT, new PdfRectangle(rect.GetLeft(padding), rect.GetBottom(padding), rect.GetRight(padding), rect.GetTop(padding))); } if (parent == null) writer.AddAnnotation(field); else parent.AddKid(field); }
/// <summary> /// Sets the pagesize. /// </summary> /// <param name="pageSize">the new pagesize</param> /// <returns>a bool</returns> public virtual bool SetPageSize(Rectangle pageSize) { this.pageSize = pageSize; foreach (IDocListener listener in listeners) { listener.SetPageSize(pageSize); } return true; }
/// <summary> /// Constructs a Rectangle-object. /// </summary> /// <param name="rect">another Rectangle</param> public Rectangle(Rectangle rect) : this(rect.llx, rect.lly, rect.urx, rect.ury) { CloneNonPositionParameters(rect); }
/** Sets the percentage width of the table from the absolute column width. * @param columnWidth the absolute width of each column * @param pageSize the page size * @throws DocumentException */ public void SetWidthPercentage(float[] columnWidth, Rectangle pageSize) { if (columnWidth.Length != NumberOfColumns) throw new ArgumentException("Wrong number of columns."); float totalWidth = 0; for (int k = 0; k < columnWidth.Length; ++k) totalWidth += columnWidth[k]; widthPercentage = totalWidth / (pageSize.Right - pageSize.Left) * 100f; SetWidths(columnWidth); }
/** * Copies all of the parameters from a <CODE>Rectangle</CODE> object * except the position. * * @param rect * <CODE>Rectangle</CODE> to copy from */ public virtual void CloneNonPositionParameters(Rectangle rect) { this.rotation = rect.rotation; this.border = rect.border; this.borderWidth = rect.borderWidth; this.borderColor = rect.borderColor; this.backgroundColor = rect.backgroundColor; this.borderColorLeft = rect.borderColorLeft; this.borderColorRight = rect.borderColorRight; this.borderColorTop = rect.borderColorTop; this.borderColorBottom = rect.borderColorBottom; this.borderWidthLeft = rect.borderWidthLeft; this.borderWidthRight = rect.borderWidthRight; this.borderWidthTop = rect.borderWidthTop; this.borderWidthBottom = rect.borderWidthBottom; this.useVariableBorders = rect.useVariableBorders; }
public static PdfAnnotation ConvertAnnotation(PdfWriter writer, Annotation annot, Rectangle defaultRect) { switch (annot.AnnotationType) { case Annotation.URL_NET: return new PdfAnnotation(writer, annot.GetLlx(), annot.GetLly(), annot.GetUrx(), annot.GetUry(), new PdfAction((Uri) annot.Attributes[Annotation.URL])); case Annotation.URL_AS_STRING: return new PdfAnnotation(writer, annot.GetLlx(), annot.GetLly(), annot.GetUrx(), annot.GetUry(), new PdfAction((String) annot.Attributes[Annotation.FILE])); case Annotation.FILE_DEST: return new PdfAnnotation(writer, annot.GetLlx(), annot.GetLly(), annot.GetUrx(), annot.GetUry(), new PdfAction((String) annot.Attributes[Annotation.FILE], (String) annot.Attributes[Annotation.DESTINATION])); case Annotation.SCREEN: bool[] sparams = (bool[])annot.Attributes[Annotation.PARAMETERS]; String fname = (String) annot.Attributes[Annotation.FILE]; String mimetype = (String) annot.Attributes[Annotation.MIMETYPE]; PdfFileSpecification fs; if (sparams[0]) fs = PdfFileSpecification.FileEmbedded(writer, fname, fname, null); else fs = PdfFileSpecification.FileExtern(writer, fname); PdfAnnotation ann = PdfAnnotation.CreateScreen(writer, new Rectangle(annot.GetLlx(), annot.GetLly(), annot.GetUrx(), annot.GetUry()), fname, fs, mimetype, sparams[1]); return ann; case Annotation.FILE_PAGE: return new PdfAnnotation(writer, annot.GetLlx(), annot.GetLly(), annot.GetUrx(), annot.GetUry(), new PdfAction((String) annot.Attributes[Annotation.FILE], (int)annot.Attributes[Annotation.PAGE])); case Annotation.NAMED_DEST: return new PdfAnnotation(writer, annot.GetLlx(), annot.GetLly(), annot.GetUrx(), annot.GetUry(), new PdfAction((int) annot.Attributes[Annotation.NAMED])); case Annotation.LAUNCH: return new PdfAnnotation(writer, annot.GetLlx(), annot.GetLly(), annot.GetUrx(), annot.GetUry(), new PdfAction((String) annot.Attributes[Annotation.APPLICATION],(String) annot.Attributes[Annotation.PARAMETERS],(String) annot.Attributes[Annotation.OPERATION],(String) annot.Attributes[Annotation.DEFAULTDIR])); default: return new PdfAnnotation(writer, defaultRect.Left, defaultRect.Bottom, defaultRect.Right, defaultRect.Top, new PdfString(annot.Title, PdfObject.TEXT_UNICODE), new PdfString(annot.Content, PdfObject.TEXT_UNICODE)); } }
/// <summary> /// Gets a Rectangle that is altered to fit on the page. /// </summary> /// <param name="top">the top position</param> /// <param name="bottom">the bottom position</param> /// <returns>a Rectangle</returns> public Rectangle GetRectangle(float top, float bottom) { Rectangle tmp = new Rectangle(this); if (this.Top > top) { tmp.Top = top; tmp.Border = border - (border & TOP_BORDER); } if (Bottom < bottom) { tmp.Bottom = bottom; tmp.Border = border - (border & BOTTOM_BORDER); } return tmp; }
/** * Called when a <CODE>Chunk</CODE> with a generic tag is written. * <P> * It is usefull to pinpoint the <CODE>Chunk</CODE> location to generate * bookmarks, for example. * * @param writer the <CODE>PdfWriter</CODE> for this document * @param document the document * @param rect the <CODE>Rectangle</CODE> containing the <CODE>Chunk</CODE> * @param text the text of the tag */ public virtual void OnGenericTag(PdfWriter writer,Document document,Rectangle rect,string text) { }
/// <summary> /// Swaps the values of urx and ury and of lly and llx in order to rotate the rectangle. /// </summary> /// <returns>a Rectangle</returns> public Rectangle Rotate() { Rectangle rect = new Rectangle(lly, llx, ury, urx); rect.rotation = rotation + 90; rect.rotation %= 360; return rect; }
/** * 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; }
public PdfRectangle(Rectangle rectangle) : this(rectangle.Left, rectangle.Bottom, rectangle.Right, rectangle.Top, 0) { }