/** * Creates a Cell with these attributes. * @param rowAttributes * @return a cell based on these attributes. * @throws BadElementException */ public Cell CreateCell(SimpleCell rowAttributes) { Cell cell = new Cell(); cell.CloneNonPositionParameters(rowAttributes); cell.SoftCloneNonPositionParameters(this); cell.Colspan = colspan; cell.HorizontalAlignment = horizontalAlignment; cell.VerticalAlignment = verticalAlignment; cell.UseAscender = useAscender; cell.UseBorderPadding = useBorderPadding; cell.UseDescender = useDescender; foreach (IElement element in content) { cell.AddElement(element); } return(cell); }
/// <summary> /// Renderizza un elemento immagine in una cella iTextSharp.text. /// </summary> /// <param name="ImgElement"> /// Elemento immagine. NOTA: il percorso dev'essere ASSOLUTO, ad esempio C:\img\img.gif /// </param> /// <returns> /// iTextSharp.text.Cell con i contenuti opportuni /// </returns> private static iTextSharp.text.Cell RenderElement(DocTemplateVers.Domain.DTO.ServiceExport.DTO_ElementImage ImgElement, float MaxWidth) { iTextSharp.text.Cell cell = new iTextSharp.text.Cell(); cell.Border = 0; RtfHelper.SetAlignment(ref cell, ImgElement.Alignment); //cell.Width = MaxWidth; iTextSharp.text.Image img; try { img = iTextSharp.text.Image.GetInstance(ImgElement.Path); if (ImgElement.Width > 0 && ImgElement.Height > 0) { img.ScaleAbsolute(ImgElement.Width, ImgElement.Height); } else if (ImgElement.Width > 0) { img.ScaleAbsoluteWidth(ImgElement.Width); } else if (ImgElement.Height > 0) { img.ScaleAbsoluteHeight(ImgElement.Height); } if (img.Width > MaxWidth) { img.ScaleAbsoluteWidth(MaxWidth); } cell.AddElement(img); } catch (Exception ex) { //cell = null; } return(cell); //return new iTextSharp.text.Cell(new iTextSharp.text.Paragraph("NOT IMPLEMENT!")); }
/** * Creates a Cell with these attributes. * @param rowAttributes * @return a cell based on these attributes. * @throws BadElementException */ public Cell CreateCell(SimpleCell rowAttributes) { Cell cell = new Cell(); cell.CloneNonPositionParameters(rowAttributes); cell.SoftCloneNonPositionParameters(this); cell.Colspan = colspan; cell.HorizontalAlignment = horizontalAlignment; cell.VerticalAlignment = verticalAlignment; cell.UseAscender = useAscender; cell.UseBorderPadding = useBorderPadding; cell.UseDescender = useDescender; foreach (IElement element in content) { cell.AddElement(element); } return cell; }
protected void GeneratePDF() { // Refresh the summary grid else there will be nothing to generate (no postback) this.PopulateGrid(); // Create and initialize a new document object iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.LEGAL, 24, 24, 24, 24); document.PageSize.Rotate(); System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); try { // Create an instance of the writer object iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, memoryStream); // Add some meta information to the document Label lblPageTitle = (Label)(this.Page.Master.FindControl("lblDefaultMasterPageTitle")); document.AddAuthor(lblPageTitle.Text); document.AddSubject(this.lblReportTitle.Text); // Open the document document.Open(); // Create a table to match our current summary grid iTextSharp.text.Table table = null; if (this.DateRangeType == DateRangeTypes.UserSpecified) table = new iTextSharp.text.Table(6); else if (this.DateRangeType == DateRangeTypes.Daily) table = new iTextSharp.text.Table(7); else table = new iTextSharp.text.Table(8); table.TableFitsPage = true; // Apply spacing/padding/borders/column widths to the table table.Padding = 2; table.Spacing = 0; table.DefaultCellBorderWidth = 1; if (this.DateRangeType == DateRangeTypes.UserSpecified) { float[] headerwidths1 = { 30, 30, 30, 30, 30, 30 }; table.Widths = headerwidths1; } else if (this.DateRangeType == DateRangeTypes.Daily) { float[] headerwidths2 = { 40, 30, 30, 30, 30, 30, 30 }; table.Widths = headerwidths2; } else { float[] headerwidths3 = { 40, 30, 30, 30, 30, 30, 30, 35 }; table.Widths = headerwidths3; } table.Width = 100; // Add report title spanning all columns iTextSharp.text.Font titleFont = new iTextSharp.text.Font(Font.GetFamilyIndex("Tahoma"), 6, Font.BOLD); titleFont.Color = new iTextSharp.text.Color(System.Drawing.Color.Firebrick); iTextSharp.text.Cell titleCell = new iTextSharp.text.Cell(); titleCell.SetHorizontalAlignment("Left"); titleCell.SetVerticalAlignment("Top"); titleCell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.Color.White); titleCell.BorderWidth = 0; if (this.DateRangeType == DateRangeTypes.UserSpecified) titleCell.Colspan = 6; else if (this.DateRangeType == DateRangeTypes.Daily) titleCell.Colspan = 7; else titleCell.Colspan = 8; titleCell.AddElement(new iTextSharp.text.Phrase(this.lblReportTitle.Text, titleFont)); table.AddCell(titleCell); // Add table headers for (int i = 0; i < this.grdRaveForm.Columns.Count; i++) { iTextSharp.text.Font headerCellFont = new iTextSharp.text.Font(Font.GetFamilyIndex("Tahoma"), 8, Font.NORMAL + Font.UNDERLINE); headerCellFont.Color = new iTextSharp.text.Color(System.Drawing.Color.White); iTextSharp.text.Cell headerCell = new iTextSharp.text.Cell(); headerCell.SetHorizontalAlignment("Left"); headerCell.SetVerticalAlignment("Top"); headerCell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.Color.SteelBlue); headerCell.BorderColor = new iTextSharp.text.Color(System.Drawing.Color.White); headerCell.AddElement(new iTextSharp.text.Phrase(this.grdRaveForm.Columns[i].HeaderText, headerCellFont)); table.AddCell(headerCell); } table.EndHeaders(); // Add data to the table int j = 0; int k = 0; string phrase = ""; foreach (System.Data.DataRow row in this._pdfDataTable.Rows) { j++; // Increment the row counter k = 0; // Reset the column counter for the new row foreach (System.Data.DataColumn col in this._pdfDataTable.Columns) { k++; // Increment the column counter iTextSharp.text.Font cellFont = new iTextSharp.text.Font(Font.GetFamilyIndex("Tahoma"), 6, Font.NORMAL); if (j % 2 == 0) { cellFont.Color = new iTextSharp.text.Color(System.Drawing.Color.DarkRed); } else { cellFont.Color = new iTextSharp.text.Color(System.Drawing.Color.Black); } iTextSharp.text.Cell cell = new iTextSharp.text.Cell(); cell.SetHorizontalAlignment("Left"); cell.SetVerticalAlignment("Top"); cell.BorderColor = new iTextSharp.text.Color(System.Drawing.Color.White); if (j % 2 == 0) { cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.Color.LightGray); } else { cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.Color.White); } // Generate formatted phrase for cell. switch (col.ColumnName) { case "SummaryDate": case "MaxDate": phrase = String.Format("{0,10:MM/dd/yyyy}", row[col]); break; case "AvgSecs": case "StdDevSecs": phrase = String.Format("{0:F2}", row[col]); break; case "FormIDCount": phrase = String.Format("{0:G}", row[col]); break; default: phrase = row[col].ToString(); break; } cell.AddElement(new iTextSharp.text.Phrase(phrase, cellFont)); table.AddCell(cell); } } // Add the table to the document document.Add(table); // Close the document document.Close(); // Show the document Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=" + Enum.GetName(typeof(DateRangeTypes), this.DateRangeType) + "Performance" + ((DropDownList)this.Page.Master.FindControl("ddlReport")).SelectedItem.Text.Replace(" ", String.Empty) + ".pdf"); Response.ContentType = "application/pdf"; Response.BinaryWrite(memoryStream.ToArray()); Response.End(); } catch (Exception xcptn) { Response.Write(xcptn.Message); } }
public static iTS.Cell HtmlToCell(String Html) { iTS.Cell cell = new iTextSharp.text.Cell(); Boolean Error = false; // iTextSharp.text.html.simpleparser.StyleSheet styles = //new iTextSharp.text.html.simpleparser.StyleSheet(); // aaa // styles.LoadTagStyle("ul", "indent", "10"); // styles.LoadTagStyle("ol", "indent", "10"); Html = lm.Comol.Core.DomainModel.Helpers.Export.RtfHelper.HtmlCheckRtf(Html); System.Collections.ArrayList AL_Content = new System.Collections.ArrayList(); try { AL_Content = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList( new System.IO.StringReader(Html), lm.Comol.Core.DomainModel.Helpers.Export.RtfHelper.GetStyles()); } catch { Error = true; } if (Error) { Error = false; Html = lm.Comol.Core.DomainModel.Helpers.Export.RtfHelper.RemoveHtmlTag(Html); try { AL_Content = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList( new System.IO.StringReader(Html), lm.Comol.Core.DomainModel.Helpers.Export.RtfHelper.GetStyles()); } catch { Error = true; } } if (Error) { cell.AddElement(new iTS.Paragraph(Html)); } else { for (int j = 0; j < AL_Content.Count; j++) { try { iTS.IElement element = CheckconvertedElement((iTS.IElement)AL_Content[j]); if (element != null) { cell.AddElement(element); } } catch (Exception ex) { } } } return(cell); }
/// <summary> /// 输出到PDF /// </summary> /// <param name="list"></param> /// <param name="sysFont"></param> /// <param name="multiRow"></param> /// <param name="export">算高度的情况最后输出时使用</param> /// <returns></returns> public Table WriteFields(List<List<PdfDesc>> list, System.Drawing.Font sysFont, int multiRow, bool export) { #region Variable Definition Cell cell = null; int maxColumnCount = -1; int maxRowCount = -1; LineSeparator lineSeparator = null; int tempCount = 0; int previousFieldCells = 0; Table tb = null; #endregion //try //{ Font pdfFont = this.GetPdfFont(sysFont); //Hashtable allStartIndex = new Hashtable(); Dictionary<int, int> allStartIndex = new Dictionary<int, int>(); if (export) { foreach (List<PdfDesc> row in list) { if (!allStartIndex.ContainsKey(row[0].FieldNum)) { allStartIndex.Add(row[0].FieldNum, list.IndexOf(row)); } } } else { allStartIndex.Add(0, 0); } List<int> startIndex = new List<int>(); foreach (int index in allStartIndex.Values) { startIndex.Add(index); } for (int l = 0; l < startIndex.Count; l++) { //計算最大Column和最大Row maxColumnCount = 0; if (startIndex.Count == 1) { maxRowCount = list.Count; } else if (l != startIndex.Count - 1) { maxRowCount = startIndex[l + 1] - startIndex[l]; } else { maxRowCount = list.Count - startIndex[l]; } for (int s = startIndex[l]; s < list.Count; s++) //foreach (List<PdfDesc> row in list) { if (startIndex.Count != 1) { if (l != startIndex.Count - 1 && s == startIndex[l + 1]) { break; } } List<PdfDesc> row = list[s]; foreach (PdfDesc pdfDesc in row) { tempCount += pdfDesc.Cells; } if (tempCount > maxColumnCount) { maxColumnCount = tempCount; } tempCount = 0; } tb = new Table(maxColumnCount, maxRowCount); #region 計算欄位寬度 if (multiRow == 1) { int[] widths = new int[maxColumnCount]; previousFieldCells = 0; List<PdfDesc> firstRow = list[startIndex[l]]; for (int i = 0; i < firstRow.Count; i++) { int widthPercent = Convert.ToInt32(Math.Truncate((UnitConversion.GetPdfLetterWidth(firstRow[i].Width, sysFont) / Convert.ToDouble((this.pdfDoc.PageSize.Width - this.pdfDoc.LeftMargin - this.pdfDoc.RightMargin))) * 100)); //算出百分比 if (i == 0) { widths[i] = widthPercent; if (firstRow[i].Cells > 1) { for (int j = 0; j < firstRow[i].Cells - 1; j++) { widths[i + j + 1] = widthPercent; } } } else { widths[previousFieldCells] = widthPercent; if (firstRow[i].Cells > 1) { for (int j = 0; j < firstRow[i].Cells - 1; j++) { widths[previousFieldCells + j + 1] = widthPercent; } } } previousFieldCells += firstRow[i].Cells; } tb.SetWidths(widths); previousFieldCells = 0; } #endregion if (!this.report.Format.ColumnGridLine) { tb.Border = Rectangle.NO_BORDER; } tb.Cellpadding = PdfSizeConfig.Cellpadding; //tb.Width = ((this.pdfDoc.PageSize.Width - this.pdfDoc.LeftMargin - this.pdfDoc.RightMargin) / this.pdfDoc.PageSize.Width) * 100; //此處為百分比 tb.Width = 100; tb.Alignment = Element.ALIGN_LEFT; for (int j = startIndex[l]; j < list.Count; j++) { if (startIndex.Count != 1) { if (l != startIndex.Count - 1 && j == startIndex[l + 1]) { break; } } List<PdfDesc> row = list[j]; previousFieldCells = 0; for (int i = 0; i < row.Count; i++) { PdfDesc pdfDesc = row[i]; switch (pdfDesc.GroupGap) { case DataSourceItem.GroupGapType.None: cell = new Cell(new Chunk(pdfDesc.Value, pdfFont)); cell.Colspan = pdfDesc.Cells; cell.HorizontalAlignment = this.GetPdfHAlignByStr(pdfDesc.HAlign); break; case DataSourceItem.GroupGapType.EmptyRow: if (i == 0) { cell = new Cell(new Chunk(String.Empty, pdfFont)); cell.Colspan = maxColumnCount; } break; case DataSourceItem.GroupGapType.SingleLine: if (i == 0) { cell = new Cell(); lineSeparator = new LineSeparator(); lineSeparator.LineWidth = cell.Width; lineSeparator.Offset = PdfSizeConfig.LineSeparatorOffsetU; cell.AddElement(lineSeparator); cell.Colspan = tb.Columns; } break; case DataSourceItem.GroupGapType.DoubleLine: if (i == 0) { cell = new Cell(); lineSeparator = new LineSeparator(); lineSeparator.LineWidth = cell.Width; lineSeparator.Offset = PdfSizeConfig.LineSeparatorOffsetU; cell.AddElement(lineSeparator); lineSeparator = new LineSeparator(); lineSeparator.LineWidth = cell.Width; lineSeparator.Offset = PdfSizeConfig.LineSeparatorOffsetD; cell.AddElement(lineSeparator); cell.Colspan = tb.Columns; } break; } cell.BorderWidthLeft = pdfDesc.LeftLine == true ? PdfSizeConfig.BorderWidth : PdfSizeConfig.BorderWidthZero; cell.BorderWidthRight = pdfDesc.RightLine == true ? PdfSizeConfig.BorderWidth : PdfSizeConfig.BorderWidthZero; cell.BorderWidthTop = pdfDesc.TopLine == true ? PdfSizeConfig.BorderWidth : PdfSizeConfig.BorderWidthZero; cell.BorderWidthBottom = pdfDesc.BottomLine == true ? PdfSizeConfig.BorderWidth : PdfSizeConfig.BorderWidthZero; if (j == list.Count - 1) { cell.BorderWidthBottom = report.Format.RowGridLine == true ? PdfSizeConfig.BorderWidth : PdfSizeConfig.BorderWidthZero; } cell.UseAscender = true; //此屬性設置為True的時候VerticalAlignment才會起作用 cell.VerticalAlignment = Cell.ALIGN_MIDDLE; switch (pdfDesc.GroupGap) { case DataSourceItem.GroupGapType.None: if (i == 0) { tb.AddCell(cell, j, i); } else { tb.AddCell(cell, j, previousFieldCells); } break; case DataSourceItem.GroupGapType.EmptyRow: case DataSourceItem.GroupGapType.SingleLine: case DataSourceItem.GroupGapType.DoubleLine: if (i == 0) { tb.AddCell(cell, j, i); } break; } previousFieldCells += pdfDesc.Cells; } } if (!ExportByHeight || export) { this.pdfDoc.Add(tb); } } //} //catch (Exception ex) //{ // log.WriteExceptionInfo(ex); // throw ex; //} return tb; }
private void RenderSignature(iTS.Document doc) { IList <iTS.Cell> cellsLeft = new List <iTS.Cell>(); IList <iTS.Cell> cellsCenter = new List <iTS.Cell>(); IList <iTS.Cell> cellsRight = new List <iTS.Cell>(); int cellLeft = 0; int cellCenter = 0; int cellRight = 0; foreach (TemplVers_Export.DTO_Signature sgn in _template.Signatures) { iTS.Cell cell = new iTS.Cell(); if (sgn.IsHTML) { cell = HtmlToCell(sgn.Text); // iTS.html.simpleparser.StyleSheet styles = //new iTS.html.simpleparser.StyleSheet(); // styles.LoadTagStyle("ul", "indent", "10"); // styles.LoadTagStyle("ol", "indent", "10"); //System.Collections.ArrayList AL_Content = iTS.html.simpleparser.HTMLWorker.ParseToList(new StringReader(sgn.Text), lm.Comol.Core.DomainModel.Helpers.Export.RtfHelper.GetStyles()); //for (int j = 0; j < AL_Content.Count; j++) //{ // cell.AddElement(CheckconvertedElement((iTS.IElement)AL_Content[j])); //} } else { cell.AddElement(new iTS.Paragraph(sgn.Text)); } if (sgn.HasImage) { iTS.Image img; try { img = iTS.Image.GetInstance(sgn.Path); if (sgn.Width > 0 && sgn.Height > 0) { img.ScaleAbsolute(sgn.Width, sgn.Height); } else if (sgn.Width > 0) { img.ScaleAbsoluteWidth(sgn.Width); } else if (sgn.Height > 0) { img.ScaleAbsoluteHeight(sgn.Height); } //if (img.Width > MaxWidth) // img.ScaleAbsoluteWidth(MaxWidth); cell.AddElement(img); } catch (Exception ex) { string Exception = ex.ToString(); //cell = null; } } cell.Border = 0; switch (sgn.Position) { case DomainModel.DocTemplateVers.SignaturePosition.left: // SignaturePosition.left: cellsLeft.Add(cell); cellLeft++; break; case DomainModel.DocTemplateVers.SignaturePosition.center: // SignaturePosition.center: cellsCenter.Add(cell); cellCenter++; break; default: cellsRight.Add(cell); cellRight++; break; } } iTS.Table signTable = new iTS.Table(3); iTS.Cell VoidCell = new iTS.Cell(" "); VoidCell.Border = 0; int MaxRow = (cellLeft > cellRight) ? cellLeft : cellRight; MaxRow = (MaxRow > cellCenter) ? MaxRow : cellCenter; if (MaxRow > 0) { for (int r = 0; r < MaxRow; r++) { if (r < cellLeft) { signTable.AddCell(cellsLeft[r]); } else { signTable.AddCell(VoidCell); } if (r < cellCenter) { signTable.AddCell(cellsCenter[r]); } else { signTable.AddCell(VoidCell); } if (r < cellRight) { signTable.AddCell(cellsRight[r]); } else { signTable.AddCell(VoidCell); } } try { doc.Add(signTable); } catch { } } }