public GetRectangle ( float top, float bottom ) : |
||
top | float | the top position |
bottom | float | the bottom position |
return |
public void AddPageNumber(string str, string displaytext) { byte[] bytes = File.ReadAllBytes(str); iTextSharp.text.Font blackFont = FontFactory.GetFont("Arial", 6, iTextSharp.text.Font.NORMAL, BaseColor.BLACK); using (var stream = new MemoryStream()) { stream.Flush(); System.Windows.Forms.Application.DoEvents(); PdfReader reader = new PdfReader(bytes); PdfReader.unethicalreading = true; Paragraph p = new Paragraph(); Document doc = new Document(); using (PdfStamper stamper = new PdfStamper(reader, stream)) { System.Windows.Forms.Application.DoEvents(); //PdfContentByte canvas = stamper.GetOverContent(1); //iTextSharp.text.Rectangle size = reader.GetPageSizeWithRotation(1); int pages = reader.NumberOfPages; for (int i = 1; i <= pages; i++) { iTextSharp.text.Rectangle mediabox = reader.GetPageSize(i); float x, y; x = mediabox.Height; y = mediabox.Width; iTextSharp.text.Rectangle cropBox = reader.GetCropBox(i); cropBox.GetRectangle(y - 1000, x - 50); iTextSharp.text.Rectangle bottomRight = new iTextSharp.text.Rectangle(cropBox.GetRight(199), cropBox.Bottom, cropBox.Right, cropBox.GetBottom(18)); //TextField tf = new TextField(stamper.Writer, new iTextSharp.text.Rectangle(0, 0, 300, 100), displaytext); ////Change the orientation of the text //tf.Rotation = 0; EmptyTextBoxSimple(stamper, i, bottomRight, BaseColor.WHITE); ColumnText columnText = GenerateTextBox(stamper, i, bottomRight); columnText.AddText(new Phrase(displaytext.ToString(), blackFont)); columnText.Go(); } } bytes = stream.ToArray(); //stream.Flush(); } System.Threading.Thread.Sleep(200); File.WriteAllBytes(str, bytes); }
/** * Adds a new table to * @param table Table to add. Rendered rows will be deleted after processing. * @param onlyFirstPage Render only the first full page * @throws DocumentException */ private void AddPdfTable(Table t) { // before every table, we flush all lines FlushLines(); PdfTable table = new PdfTable(t, IndentLeft, IndentRight, IndentTop - currentHeight); RenderingContext ctx = new RenderingContext(); ctx.pagetop = IndentTop; ctx.oldHeight = currentHeight; ctx.cellGraphics = new PdfContentByte(writer); ctx.rowspanMap = new Hashtable(); ctx.table = table; // initialisation of parameters PdfCell cell; // drawing the table ArrayList headercells = table.HeaderCells; ArrayList cells = table.Cells; ArrayList rows = ExtractRows(cells, ctx); bool isContinue = false; while (cells.Count != 0) { // initialisation of some extra parameters; ctx.lostTableBottom = 0; // loop over the cells bool cellsShown = false; // draw the cells (line by line) ListIterator iterator = new ListIterator(rows); bool atLeastOneFits = false; while (iterator.HasNext()) { ArrayList row = (ArrayList) iterator.Next(); AnalyzeRow(rows, ctx); RenderCells(ctx, row, table.HasToFitPageCells() & atLeastOneFits); if (!MayBeRemoved(row)) { break; } ConsumeRowspan(row, ctx); iterator.Remove(); atLeastOneFits = true; } // compose cells array list for subsequent code cells.Clear(); Hashtable opt = new Hashtable(); foreach (ArrayList row in rows) { foreach (PdfCell cellp in row) { if (!opt.ContainsKey(cellp)) { cells.Add(cellp); opt[cellp] = null; } } } // we paint the graphics of the table after looping through all the cells Rectangle tablerec = new Rectangle(table); tablerec.Border = table.Border; tablerec.BorderWidth = table.BorderWidth; tablerec.BorderColor = table.BorderColor; tablerec.BackgroundColor = table.BackgroundColor; PdfContentByte under = writer.DirectContentUnder; under.Rectangle(tablerec.GetRectangle(Top, IndentBottom)); under.Add(ctx.cellGraphics); // bugfix by Gerald Fehringer: now again add the border for the table // since it might have been covered by cell backgrounds tablerec.BackgroundColor = null; tablerec = tablerec.GetRectangle(Top, IndentBottom); tablerec.Border = table.Border; under.Rectangle(tablerec); // end bugfix ctx.cellGraphics = new PdfContentByte(null); // if the table continues on the next page if (rows.Count != 0) { isContinue = true; graphics.SetLineWidth(table.BorderWidth); if (cellsShown && (table.Border & Rectangle.BOTTOM_BORDER) == Rectangle.BOTTOM_BORDER) { // Draw the bottom line // the color is set to the color of the element Color tColor = table.BorderColor; if (tColor != null) { graphics.SetColorStroke(tColor); } graphics.MoveTo(table.Left, Math.Max(table.Bottom, IndentBottom)); graphics.LineTo(table.Right, Math.Max(table.Bottom, IndentBottom)); graphics.Stroke(); if (tColor != null) { graphics.ResetRGBColorStroke(); } } // old page pageEmpty = false; float difference = ctx.lostTableBottom; // new page NewPage(); // G.F.: if something added in page event i.e. currentHeight > 0 float heightCorrection = 0; bool somethingAdded = false; if (currentHeight > 0) { heightCorrection = 6; currentHeight += heightCorrection; somethingAdded = true; NewLine(); FlushLines(); indentation.indentTop = currentHeight - leading; currentHeight = 0; } else { FlushLines(); } // this part repeats the table headers (if any) int size = headercells.Count; if (size > 0) { // this is the top of the headersection cell = (PdfCell) headercells[0]; float oldTop = cell.GetTop(0); // loop over all the cells of the table header for (int ii = 0; ii < size; ii++) { cell = (PdfCell) headercells[ii]; // calculation of the new cellpositions cell.Top = IndentTop - oldTop + cell.GetTop(0); cell.Bottom = IndentTop - oldTop + cell.GetBottom(0); ctx.pagetop = cell.Bottom; // we paint the borders of the cell ctx.cellGraphics.Rectangle(cell.Rectangle(IndentTop, IndentBottom)); // we write the text of the cell ArrayList images = cell.GetImages(IndentTop, IndentBottom); foreach (Image image in images) { cellsShown = true; graphics.AddImage(image); } lines = cell.GetLines(IndentTop, IndentBottom); float cellTop = cell.GetTop(IndentTop); text.MoveText(0, cellTop-heightCorrection); float cellDisplacement = FlushLines() - cellTop+heightCorrection; text.MoveText(0, cellDisplacement); } currentHeight = IndentTop - ctx.pagetop + table.Cellspacing; text.MoveText(0, ctx.pagetop - IndentTop - currentHeight); } else { if (somethingAdded) { ctx.pagetop = IndentTop; text.MoveText(0, -table.Cellspacing); } } ctx.oldHeight = currentHeight - heightCorrection; // calculating the new positions of the table and the cells size = Math.Min(cells.Count, table.Columns); int i = 0; while (i < size) { cell = (PdfCell) cells[i]; if (cell.GetTop(-table.Cellspacing) > ctx.lostTableBottom) { float newBottom = ctx.pagetop - difference + cell.Bottom; float neededHeight = cell.RemainingHeight; if (newBottom > ctx.pagetop - neededHeight) { difference += newBottom - (ctx.pagetop - neededHeight); } } i++; } size = cells.Count; table.Top = IndentTop; table.Bottom = ctx.pagetop - difference + table.GetBottom(table.Cellspacing); for (i = 0; i < size; i++) { cell = (PdfCell) cells[i]; float newBottom = ctx.pagetop - difference + cell.Bottom; float newTop = ctx.pagetop - difference + cell.GetTop(-table.Cellspacing); if (newTop > IndentTop - currentHeight) { newTop = IndentTop - currentHeight; } cell.Top = newTop ; cell.Bottom = newBottom ; } } } float tableHeight = table.Top - table.Bottom; // bugfix by Adauto Martins when have more than two tables and more than one page // If continuation of table in other page (bug report #1460051) if (isContinue) { currentHeight = tableHeight; text.MoveText(0, -(tableHeight - (ctx.oldHeight * 2))); } else { currentHeight = ctx.oldHeight + tableHeight; text.MoveText(0, -tableHeight); } pageEmpty = false; }