استفاده از Data Annotations جهت تعریف خواص ستونها در PdfReport
نویسنده: وحید نصیری
تاریخ: ۱۳۹۱/۰۷/۲۰ ۱۰:۴۲
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
using System.ComponentModel;
namespace PdfReportSamples.Models
{
public enum JobTitle
{
[Description("Grunt")]
Grunt,
[Description("Programmer")]
Programmer,
[Description("Analyst Programmer")]
AnalystProgrammer,
[Description("Project Manager")]
ProjectManager,
[Description("Chief Information Officer")]
ChiefInformationOfficer,
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using PdfReportSamples.Models;
using PdfRpt.Aggregates.Numbers;
using PdfRpt.ColumnsItemsTemplates;
using PdfRpt.Core.Contracts;
using PdfRpt.Core.Helper;
using PdfRpt.DataAnnotations;
namespace PdfReportSamples.DataAnnotations
{
public class Person
{
[IsVisible(false)]
public int Id { get; set; }
[DisplayName("User name")]
//Note: If you don't specify the ColumnItemsTemplate, a new TextBlockField() will be used automatically.
[ColumnItemsTemplate(typeof(TextBlockField))]
public string Name { get; set; }
[DisplayName("Job title")]
public JobTitle JobTitle { set; get; }
[DisplayName("Date of birth")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime DateOfBirth { get; set; }
[DisplayName("Date of death")]
[DisplayFormat(NullDisplayText = "-", DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime? DateOfDeath { get; set; }
[DisplayFormat(DataFormatString = "{0:n0}")]
[CustomAggregateFunction(typeof(Sum))]
public int Salary { get; set; }
[IsCalculatedField(true)]
[DisplayName("Calculated Field")]
[DisplayFormat(DataFormatString = "{0:n0}")]
[AggregateFunction(AggregateFunction.Sum)]
public string CalculatedField { get; set; }
[CalculatedFieldFormula("CalculatedField")]
public static Func<IList<CellData>, object> CalculatedFieldFormula =
list =>
{
if (list == null) return string.Empty;
var salary = (int)list.GetValueOf<Person>(x => x.Salary);
return salary * 0.8;
};//Note: It's a static field, not a property.
}
}
using System;
using System.Collections.Generic;
using PdfReportSamples.Models;
namespace PdfReportSamples.DataAnnotations
{
public static class PersonnelDataSource
{
public static IList<Person> CreatePersonnelList()
{
return new List<Person>
{
new Person
{
Id = 1,
Name = "Edward",
DateOfBirth = new DateTime(1900, 1, 1),
DateOfDeath = new DateTime(1990, 10, 15),
JobTitle = JobTitle.ChiefInformationOfficer,
Salary = 5000
},
new Person
{
Id = 2,
Name = "Margaret",
DateOfBirth = new DateTime(1950, 2, 9),
DateOfDeath = null,
JobTitle = JobTitle.AnalystProgrammer,
Salary = 4000
},
new Person
{
Id = 3,
Name = "Grant",
DateOfBirth = new DateTime(1975, 6, 13),
DateOfDeath = null,
JobTitle = JobTitle.Programmer,
Salary = 3500
}
};
}
}
}
using System;
using PdfRpt.Core.Contracts;
using PdfRpt.FluentInterface;
namespace PdfReportSamples.DataAnnotations
{
public class DataAnnotationsPdfReport
{
public IPdfReportData CreatePdfReport()
{
return new PdfReport().DocumentPreferences(doc =>
{
doc.RunDirection(PdfRunDirection.LeftToRight);
doc.Orientation(PageOrientation.Portrait);
doc.PageSize(PdfPageSize.A4);
doc.DocumentMetadata(new DocumentMetadata { Author = "Vahid", Application = "PdfRpt", Keywords = "Test", Subject = "Test Rpt", Title = "Test" });
})
.DefaultFonts(fonts =>
{
fonts.Path(Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\tahoma.ttf",
Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\verdana.ttf");
})
.PagesFooter(footer =>
{
footer.DefaultFooter(printDate: DateTime.Now.ToString("MM/dd/yyyy"));
})
.PagesHeader(header =>
{
header.DefaultHeader(defaultHeader =>
{
defaultHeader.ImagePath(AppPath.ApplicationPath + "\\Images\\01.png");
defaultHeader.Message("new rpt.");
defaultHeader.RunDirection(PdfRunDirection.LeftToRight);
});
})
.MainTableTemplate(template =>
{
template.BasicTemplate(BasicTemplate.ClassicTemplate);
})
.MainTablePreferences(table =>
{
table.ColumnsWidthsType(TableColumnWidthType.FitToContent);
})
.MainTableDataSource(dataSource =>
{
dataSource.StronglyTypedList(PersonnelDataSource.CreatePersonnelList());
})
.MainTableEvents(events =>
{
events.DataSourceIsEmpty(message: "There is no data available to display.");
})
.MainTableSummarySettings(summary =>
{
summary.OverallSummarySettings("Total");
summary.PageSummarySettings("Page Summary");
summary.PreviousPageSummarySettings("Pervious Page Summary");
})
.MainTableAdHocColumnsConventions(adHocColumns =>
{
adHocColumns.ShowRowNumberColumn(true);
adHocColumns.RowNumberColumnCaption("#");
})
.Export(export =>
{
export.ToExcel();
export.ToXml();
})
.Generate(data => data.AsPdfFile(AppPath.ApplicationPath + "\\Pdf\\DataAnnotationsSampleRpt.pdf"));
}
}
}