-
Notifications
You must be signed in to change notification settings - Fork 2
/
CustomLabelReportBuilder.cs
102 lines (100 loc) · 5.68 KB
/
CustomLabelReportBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.Wizards;
using DevExpress.XtraPrinting;
using System.Drawing.Printing;
using DevExpress.XtraReports.Wizards.Labels;
using System.Linq;
using System.Drawing;
using DevExpress.XtraPrinting.Native;
namespace dxWinFormsSample {
public class CustomLabelReportBuilder {
public CustomLabelReportBuilder() { }
public XtraReport GenerateLabelReport(float width, float height, float vPitch, float hPitch, System.Drawing.GraphicsUnit measurementUnit, float bottomMargin, float topMargin, float leftMargin, float rightMargin, int paperKindID) {
CustomLabelReportModel model = new CustomLabelReportModel();
model.LabelWidth = width;
model.LabelHeight = height;
model.VerticalPitch = vPitch;
model.HorizontalPitch = hPitch;
model.MeasurementUnit = measurementUnit;
model.BottomMargin = bottomMargin;
model.LeftMargin = leftMargin;
model.TopMargin = topMargin;
model.RightMargin = rightMargin;
model.PaperKindID = paperKindID;
return BuildLabelReport(model);
}
public XtraReport GenerateLabelReport(CustomLabelReportModel model) {
return BuildLabelReport(model);
}
private XtraReport BuildLabelReport(CustomLabelReportModel model) {
var report = new XtraReport();
PaperKindList paperKindList = InitPaperKindList(model, report);
report.ReportUnit = model.MeasurementUnit == System.Drawing.GraphicsUnit.Millimeter ? ReportUnit.TenthsOfAMillimeter : ReportUnit.HundredthsOfAnInch;
report.PaperKind = paperKindList.PaperKind;
report.Landscape = paperKindList.Landscape;
report.RollPaper = false;
float labelDpi = GraphicsDpi.UnitToDpi(model.MeasurementUnit);
int top = (int)XRConvert.Convert(model.TopMargin, labelDpi, report.Dpi);
int left = (int)XRConvert.Convert(model.LeftMargin, labelDpi, report.Dpi);
int right = (int)XRConvert.Convert(model.RightMargin, labelDpi, report.Dpi);
int bottom = (int)XRConvert.Convert(model.BottomMargin, labelDpi, report.Dpi);
report.Margins = new Margins(left, right, top, bottom);
float labelWidth = XRConvert.Convert(model.LabelWidth, labelDpi, report.Dpi);
float labelHeight = XRConvert.Convert(model.LabelHeight, labelDpi, report.Dpi);
DetailBand detail = new DetailBand();
report.Bands.Add(detail);
XRPanel panel = new XRPanel();
panel.CanGrow = false;
detail.Controls.Add(panel);
panel.WidthF = labelWidth;
panel.HeightF = labelHeight;
panel.Borders = BorderSide.All;
detail.HeightF = XRConvert.Convert(model.VerticalPitch, labelDpi, report.Dpi);
var labelInfo = DevExpress.XtraReports.Wizards.Builder.ReportInfoFactory.CreateLabelInfo(GetDefaultLabelReportMode(model));
report.ReportPrintOptions.DetailCountOnEmptyDataSource = labelInfo.CalculateLabelCount(paperKindList.Size, paperKindList.UnitDpi);
if (labelInfo.MoreOneColumnOnPage(paperKindList.Size, paperKindList.UnitDpi)) {
detail.MultiColumn.ColumnWidth = XRConvert.Convert(labelInfo.LabelWidth, labelDpi, report.Dpi);
detail.MultiColumn.ColumnSpacing = XRConvert.Convert(labelInfo.HPitch - labelInfo.LabelWidth, labelDpi, report.Dpi);
detail.MultiColumn.Layout = ColumnLayout.AcrossThenDown;
}
return report;
}
private ReportModel GetDefaultLabelReportMode(CustomLabelReportModel model) {
XtraReportModel reportModel = new XtraReportModel();
reportModel.ReportType = ReportType.Label;
var customLblInfo = new CustomLabelInformation();
customLblInfo.Height = model.LabelHeight;
customLblInfo.Width = model.LabelWidth;
customLblInfo.TopMargin = model.TopMargin;
customLblInfo.BottomMargin = model.BottomMargin;
customLblInfo.LeftMargin = model.LeftMargin;
customLblInfo.RightMargin = model.RightMargin;
customLblInfo.VerticalPitch = model.VerticalPitch;
customLblInfo.HorizontalPitch = model.HorizontalPitch;
customLblInfo.PaperKindDataId = model.PaperKindID;
customLblInfo.Unit = model.MeasurementUnit;
reportModel.CustomLabelInformation = customLblInfo;
return reportModel;
}
private static PaperKindList InitPaperKindList(CustomLabelReportModel model, XtraReport report) {
var paperKindList = new PaperKindList(report.Dpi);
var labelRepository = new LabelProductRepositoryFactory().Create();
var paperKindItems = labelRepository
.GetSortedPaperKinds()
.Select(ConvertToPaperKindItem);
foreach (var item in paperKindItems) {
paperKindList.Add(item);
}
paperKindList.CurrentID = model.PaperKindID;
return paperKindList;
}
static PaperKindItem ConvertToPaperKindItem(PaperKindData paperKindData) {
float dpi = DevExpress.XtraPrinting.GraphicsDpi.UnitToDpi(paperKindData.Unit);
var paperKind = (PaperKind)paperKindData.EnumId;
SizeF size = paperKind == PaperKind.Custom
? new SizeF(paperKindData.Width, paperKindData.Height)
: PageSizeInfo.GetPageSizeF(paperKind, dpi, PageSizeInfo.DefaultSize);
return new PaperKindItem(paperKindData.Name, size, paperKindData.Id, paperKind, dpi, paperKindData.IsRollPaper);
}
}
}