Untitled
unknown
csharp
2 years ago
6.2 kB
20
Indexable
public string PDFTableFromDGV(System.Data.DataTable dt, string filename,string DocumentTitle,string ImagePath) {
string Message = "";
try
{
//Defining Filename and path
string webpath = _webHostEnvironment.WebRootPath;
filename = filename + "-" + DateTime.Now.ToString("yyyy'-'MM'-'dd-t") + ".pdf";
var dest = Path.Combine(webpath, filename);
//Working with fonts
var font = Path.Combine(webpath, "IBMPlexSansHebrew-Regular.ttf");
PdfFont hebrewFont = CreateFont(font, PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED);
/* PdfFont hebrewFont = CreateFont(StandardFonts.TIMES_ROMAN);*/
/*PdfFont hebrewFont = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);*/
/*PdfFont font = PdfFontFactory.CreateFont(PdfFontFactory.EmbeddingStrategy.FORCE_EMBEDDED);*/
//Create a base font object making sure to specify IDENTITY-H
/*BaseFont bf = BaseFont.CreateFont(ARIALUNI_TFF, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);*/
//Intialize Pdf Writer, Document and closes as well
using (PdfWriter writer = new PdfWriter(dest))
{
using (PdfDocument pdf = new PdfDocument(writer))
{
//Clear any existing pdf contents
if (pdf.GetNumberOfPdfObjects() > 0)
{
Message = "cleared";
}
// Getting Rows & Columns Counts
int dgvrowcount = dt.Rows.Count;
int dgvcolumncount = dt.Columns.Count;
//Create Table and define it's column.
//float[] cloumnwidth = new float[dt.Columns.Count];
//float[] cloumnwidth = { 3, 3, 3, 12, 7, 6, 6, 6, 6, 6, 6, 6, 2, 2, 2, 12, 12 };
float[] cloumnwidth = { 10,10,30,30,10,10 };
/*for (int counter = 0; counter < dt.Columns.Count; counter++)
{
float strWidth = (float)dt.Columns[counter].ColumnName.ToString().Length;
cloumnwidth[counter] = strWidth;
}*/
Table table = new Table(cloumnwidth, true);
//table.SetWidth(iText.Layout.Properties.UnitValue.CreatePercentValue(100));
table.SetAutoLayout();
//Create and Define document.
var document = new Document(pdf, PageSize.A4);
//Add Image in Pdf Report.
ImageData image = ImageDataFactory.Create(ImagePath);
Image Image = new Image(image);
//Set height 25% of the page and Width 125% of page in float
Image.SetMarginBottom(20F).SetHorizontalAlignment(HorizontalAlignment.CENTER).SetHeight(55F).SetWidth(263F);
document.Add(Image);
//Add Title in Pdf Report.
Paragraph DocTitle = (new Paragraph(DocumentTitle + " - " + DateTime.Now.ToString("yyyy'-'MM'-'dd")).SetTextAlignment(TextAlignment.CENTER).SetFontSize(15F).SetUnderline().SetMarginBottom(10F));
DocTitle.GetAccessibilityProperties().SetRole(StandardRoles.H1);
document.Add(DocTitle);
// Print The Datatable Header To Table Header
for (int i = 0; i < dgvcolumncount; i++)
{
iText.Layout.Element.Cell headerCells = new iText.Layout.Element.Cell()
.SetBackgroundColor(iText.Kernel.Colors.ColorConstants.LIGHT_GRAY)
.SetTextAlignment(iText.Layout.Properties.TextAlignment.LEFT)
.SetHeight(25F);
//headerCells.SetNextRenderer(new RoundedCornersCellRenderer(headerCells));
var gteCell = headerCells.Add(new Paragraph(dt.Columns[i].ColumnName));
table.AddHeaderCell(gteCell);
}
//Print The DataTable Cells To Table Cells
for (int i = 0; i < dgvrowcount; i++)
{
for (int c = 0; c < dgvcolumncount; c++)
{
//Create new Cells and personalise it.
Cell cells = new Cell().SetBackgroundColor(iText.Kernel.Colors.ColorConstants.WHITE);
Cell gteCell = new Cell();
//Conditions to Add data accordingly.
//Condition 1 : check for hebrew characters and change it's font.
if (Regex.IsMatch(dt.Rows[i][dt.Columns[c].ColumnName].ToString(), @"^[\p{IsHebrew} ]+$"))
{
gteCell = cells.SetFont(hebrewFont).Add(new Paragraph(new string(dt.Rows[i][dt.Columns[c].ColumnName].ToString().Reverse().ToArray()))).SetTextAlignment(TextAlignment.RIGHT);
table.AddCell(gteCell);
}
//Condition 2 : check for decimal and convert to currency.
else if ((dt.Columns[c].DataType == typeof(decimal)))
{
string currecny = String.Format("{0:C}", Decimal.Parse((dt.Rows[i][dt.Columns[c].ColumnName].ToString())));
gteCell = cells.Add(new Paragraph(currecny.ToString()).SetTextAlignment(TextAlignment.RIGHT));
table.AddCell(gteCell);
}
//Condition 3 : Rest text.
else
{
gteCell = cells.Add(new Paragraph(dt.Rows[i][dt.Columns[c].ColumnName].ToString()).SetTextAlignment(TextAlignment.LEFT));
table.AddCell(gteCell);
}
};
};
table.IsComplete();
//table.Complete();
//Add Document to the table.
document.Add(table);
}
}
Message = "Success";
}
catch (Exception ex)
{
Message = ex.Message;
}
return Message;
}Editor is loading...
Leave a Comment