Created
February 23, 2019 13:09
-
-
Save molaie/595e87434cb0520986d62c5703a719e3 to your computer and use it in GitHub Desktop.
Pdf Report Test Case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using iTextSharp.text; | |
using PdfRpt.Core.Contracts; | |
using PdfRpt.Core.Helper; | |
using PdfRpt.FluentInterface; | |
using System; | |
using System.Text; | |
namespace PdfReportSamples.HtmlCellTemplate { | |
public class HtmlCellTemplatePdfReport { | |
public IPdfReportData CreatePdfReport() { | |
var rand1 = new Random(); | |
return new PdfReport().DocumentPreferences(doc => { | |
doc.RunDirection(PdfRunDirection.RightToLeft); | |
doc.Orientation(PageOrientation.Landscape); | |
doc.PageSize(PdfPageSize.A4); | |
doc.DocumentMetadata(new DocumentMetadata { Author = "Vahid", Application = "PdfRpt", Keywords = "Test", Subject = "Test Rpt", Title = "Test" }); | |
doc.Compression(new CompressionSettings { | |
CompressionLevel = CompressionLevel.BestCompression, | |
EnableCompression = true | |
}); | |
}) | |
.DefaultFonts(fonts => { | |
fonts.Path(System.IO.Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"), "fonts\\arial.ttf"), | |
System.IO.Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"), "fonts\\verdana.ttf")); | |
fonts.Size(9); | |
fonts.Color(System.Drawing.Color.Black); | |
}) | |
.PagesFooter(footer => { | |
footer.DefaultFooter(DateTime.Now.ToString("MM/dd/yyyy")); | |
}) | |
.PagesHeader(header => { | |
header.CacheHeader(cache: true); | |
}) | |
.MainTableTemplate(template => { | |
template.BasicTemplate(BasicTemplate.SnowyPineTemplate); | |
}) | |
.MainTablePreferences(table => { | |
table.ShowHeaderRow(false); | |
table.ColumnsWidthsType(TableColumnWidthType.Relative); | |
}) | |
.MainTableDataSource(dataSource => { | |
var table = new System.Data.DataTable("SalaryList"); | |
table.Columns.Add("User", typeof(string)); | |
var rnd = new Random(); | |
for (int i = 0; i < 100; i++) { | |
var photo = System.IO.Path.Combine(AppPath.ApplicationPath, "Images\\" + rnd.Next(1, 5).ToString("00") + ".png"); | |
table.Rows.Add("کاربر " + i); | |
} | |
dataSource.DataTable(table); | |
}) | |
.MainTableEvents(events => { | |
events.DataSourceIsEmpty(message: "There is no data available to display."); | |
events.CellCreated(args => { | |
//Nothing | |
}); | |
}) | |
.MainTableColumns(columns => { | |
columns.AddColumn(column => { | |
column.PropertyName("User"); | |
column.CellsHorizontalAlignment(HorizontalAlignment.Center); | |
column.IsVisible(true); | |
column.Order(1); | |
column.Width(3); | |
column.HeaderCell("User"); | |
column.CalculatedField(list => { | |
var user = list.GetSafeStringValueOf("User"); | |
var isOdd = rand1.Next(10000) % 2 == 0; | |
if (isOdd) { | |
return @"<table style='width: 100%; font-size:9pt; border: 0;'> | |
<tr><td align='center' style='width: 100%; margin: 10px; font-size:9pt; border-bottom: 1px solid red;'>" + user + @"</td></tr> | |
</table>"; | |
} else { | |
return @"<table style='width: 100%; font-size:9pt; border: 0;'> | |
<tr><td colspan='6' align='center' style='width: 100%; margin: 10px; font-size:9pt; border-bottom: 1px solid red;'>" + user + @"</td></tr> | |
<tr> | |
<td>یییییییی</td> | |
<td>فوق دیپلم</td> | |
<td>ببببببببب</td> | |
<td>ببببببببب</td> | |
<td>files/toranj/Survey/08330483291.png</td> | |
<td>files/toranj/Survey/08330483292.png</td> | |
</tr> | |
<tr> | |
<td>بببببببببب</td> | |
<td>دیپلم</td> | |
<td>لللللللللل</td> | |
<td>لللللللللللل</td> | |
<td>files/toranj/Survey/02230906173.png</td> | |
<td>files/toranj/Survey/15054805622.png</td> | |
</tr> | |
</table>"; | |
} | |
}); | |
column.ColumnItemsTemplate(template => { | |
template.XHtml(); | |
}); | |
}); | |
}) | |
.Export(export => { | |
export.ToXml(); | |
export.ToExcel(); | |
}) | |
.Generate(data => data.AsPdfFile(string.Format("{0}\\Pdf\\HtmlCellTemplateSample-{1}.pdf", | |
AppPath.ApplicationPath, Guid.NewGuid().ToString("N")))); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment