نحوه ایجاد یک گزارش فاکتور فروش توسط PdfReport
نویسنده: وحید نصیری
تاریخ: ۱۳۹۱/۰۷/۲۳ ۲۱:۵۹
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
using System;
using System.Collections.Generic;
using iTextSharp.text;
using iTextSharp.text.pdf;
using PdfReportSamples.Models;
using PdfReportSamples.Templates;
using PdfRpt.Core.Contracts;
using PdfRpt.Core.Helper;
using PdfRpt.FluentInterface;
namespace PdfReportSamples.Tax
{
public class TaxPdfReport
{
public IPdfReportData CreatePdfReport()
{
return new PdfReport().DocumentPreferences(doc =>
{
doc.RunDirection(PdfRunDirection.RightToLeft);
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(AppPath.ApplicationPath + "\\fonts\\irsans.ttf",
Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\verdana.ttf");
})
.PagesFooter(footer =>
{
footer.DefaultFooter(DateTime.Now.ToString("MM/dd/yyyy"));
})
.PagesHeader(header =>
{
header.DefaultHeader(defaultHeader =>
{
defaultHeader.ImagePath(AppPath.ApplicationPath + "\\Images\\01.png");
defaultHeader.Message("گزارش جدید ما");
});
})
.MainTableTemplate(template =>
{
template.CustomTemplate(new TransparentTemplate());
})
.MainTablePreferences(table =>
{
table.ColumnsWidthsType(TableColumnWidthType.Relative);
})
.MainTableDataSource(dataSource =>
{
var listOfRows = new List<User>();
for (int i = 0; i < 7; i++)
{
listOfRows.Add(new User { Id = i, LastName = "نام خانوادگی " + i, Name = "نام " + i, Balance = i + 1000 });
}
dataSource.StronglyTypedList<User>(listOfRows);
})
.MainTableSummarySettings(summarySettings =>
{
summarySettings.OverallSummarySettings("جمع کل");
summarySettings.PreviousPageSummarySettings("نقل از صفحه قبل");
summarySettings.PageSummarySettings("جمع صفحه");
})
.MainTableColumns(columns =>
{
columns.AddColumn(column =>
{
column.PropertyName("rowNo");
column.IsRowNumber(true);
column.CellsHorizontalAlignment(HorizontalAlignment.Center);
column.IsVisible(true);
column.Order(0);
column.Width(1);
column.HeaderCell("ردیف");
});
columns.AddColumn(column =>
{
column.PropertyName<User>(x => x.Id);
column.CellsHorizontalAlignment(HorizontalAlignment.Center);
column.IsVisible(true);
column.Order(1);
column.Width(2);
column.HeaderCell("شماره");
});
columns.AddColumn(column =>
{
column.PropertyName<User>(x => x.Name);
column.CellsHorizontalAlignment(HorizontalAlignment.Center);
column.IsVisible(true);
column.Order(2);
column.Width(3);
column.HeaderCell("نام");
});
columns.AddColumn(column =>
{
column.PropertyName<User>(x => x.LastName);
column.CellsHorizontalAlignment(HorizontalAlignment.Center);
column.IsVisible(true);
column.Order(3);
column.Width(3);
column.HeaderCell("نام خانوادگی");
});
columns.AddColumn(column =>
{
column.PropertyName<User>(x => x.Balance);
column.CellsHorizontalAlignment(HorizontalAlignment.Center);
column.IsVisible(true);
column.Order(4);
column.Width(2);
column.HeaderCell("موجودی");
column.ColumnItemsTemplate(template =>
{
template.TextBlock();
template.DisplayFormatFormula(obj => obj == null ? string.Empty : string.Format("{0:n0}", obj));
});
column.AggregateFunction(aggregateFunction =>
{
aggregateFunction.NumericAggregateFunction(AggregateFunction.Sum);
aggregateFunction.DisplayFormatFormula(obj => obj == null ? string.Empty : string.Format("{0:n0}", obj));
});
});
})
.MainTableEvents(events =>
{
events.DataSourceIsEmpty(message: "There is no data available to display.");
events.MainTableAdded(args =>
{
var balanceData = args.LastOverallAggregateValueOf<User>(u => u.Balance);
var balance = double.Parse(balanceData, System.Globalization.NumberStyles.AllowThousands);
var others = Math.Round(balance * 1.8 / 100);
var tax = Math.Round(balance * 2.2 / 100);
var total = balance + tax + others;
var taxTable = new PdfPTable(args.Table); // Create a clone of the MainTable's structure
taxTable.AddSimpleRow(
null /* null = empty cell */, null, null,
(data, cellProperties) =>
{
data.Value = "مالیات";
cellProperties.PdfFont = args.PdfFont;
cellProperties.HorizontalAlignment = HorizontalAlignment.Left;
},
(data, cellProperties) =>
{
data.Value = string.Format("{0:n0}", tax);
cellProperties.PdfFont = args.PdfFont;
cellProperties.BorderColor = BaseColor.LIGHT_GRAY;
cellProperties.ShowBorder = true;
});
taxTable.AddSimpleRow(
null, null, null,
(data, cellProperties) =>
{
data.Value = "عوارض";
cellProperties.PdfFont = args.PdfFont;
cellProperties.HorizontalAlignment = HorizontalAlignment.Left;
},
(data, cellProperties) =>
{
data.Value = string.Format("{0:n0}", others);
cellProperties.PdfFont = args.PdfFont;
cellProperties.BorderColor = BaseColor.LIGHT_GRAY;
cellProperties.ShowBorder = true;
});
taxTable.AddSimpleRow(
null, null, null,
(data, cellProperties) =>
{
data.Value = "جمع کل";
cellProperties.PdfFont = args.PdfFont;
cellProperties.HorizontalAlignment = HorizontalAlignment.Left;
},
(data, cellProperties) =>
{
data.Value = string.Format("{0:n0}", total);
cellProperties.PdfFont = args.PdfFont;
cellProperties.BorderColor = BaseColor.LIGHT_GRAY;
cellProperties.ShowBorder = true;
});
taxTable.AddSimpleRow(
null, null, null,
(data, cellProperties) =>
{
data.Value = "قابل پرداخت";
cellProperties.PdfFont = args.PdfFont;
cellProperties.HorizontalAlignment = HorizontalAlignment.Left;
},
(data, cellProperties) =>
{
data.Value = total.NumberToText(Language.Persian) + " ریال";
cellProperties.PdfFont = args.PdfFont;
cellProperties.BorderColor = BaseColor.LIGHT_GRAY;
cellProperties.ShowBorder = true;
cellProperties.PdfFontStyle = DocumentFontStyle.Bold;
});
args.PdfDoc.Add(taxTable);
});
})
.Export(export =>
{
export.ToExcel();
})
.Generate(data => data.AsPdfFile(AppPath.ApplicationPath + "\\Pdf\\TaxReportSample.pdf"));
}
}
}
var balanceData = args.LastOverallAggregateValueOf<User>(u => u.Balance); var balance = double.Parse(balanceData, System.Globalization.NumberStyles.AllowThousands);
var others = Math.Round(balance * 1.8 / 100); var tax = Math.Round(balance * 2.2 / 100); var total = balance + tax + others;
var taxTable = new PdfPTable(args.Table);
taxTable.AddSimpleRow(
null /* null = empty cell */, null, null,
(data, cellProperties) =>
{
data.Value = "مالیات";
cellProperties.PdfFont = args.PdfFont;
cellProperties.HorizontalAlignment = HorizontalAlignment.Left;
},
(data, cellProperties) =>
{
data.Value = string.Format("{0:n0}", tax);
cellProperties.PdfFont = args.PdfFont;
cellProperties.BorderColor = BaseColor.LIGHT_GRAY;
cellProperties.ShowBorder = true;
});
args.PdfDoc.Add(taxTable);
total.NumberToText(Language.Persian)
.Export(export =>
{
export.ToExcel();
}) MainTableEvents(events =>
{
events.MainTableAdded(args =>
{
var grid = new PdfGrid(1) { WidthPercentage = 90 };
grid.AddSimpleRow((d, p) =>
{
p.RunDirection = PdfRunDirection.RightToLeft;
p.HorizontalAlignment = HorizontalAlignment.Justified;
p.ShowBorder = true;
d.Value = form.Ideas; // I want to convert HTML to PDF
p.PdfFont = args.PdfFont;
p.CellPadding = 5f;
}); .MainTableEvents(events =>
{
events.MainTableAdded(args =>
{
var grid = new PdfGrid(1) { WidthPercentage = 90 };
grid.AddSimpleRow((d, p) =>
{
d.CellTemplate = new XHtmlField(); //Using iTextSharp's HTML to PDF capabilities.
p.RunDirection = PdfRunDirection.RightToLeft;
p.HorizontalAlignment = HorizontalAlignment.Justified;
p.ShowBorder = true;
d.Value = form.Ideas;
p.PdfFont = args.PdfFont;
p.CellPadding = 5f;
}); // فونت جدول اصلی
.DefaultFonts(fonts =>
{
fonts.Path(
Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\bnazanin.ttf",
Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\tahoma.ttf");
fonts.Size(10);
}) events.MainTableAdded(args =>
{
// جدول دوم
var grid = new PdfGrid(1) { WidthPercentage = 90 };
grid.AddSimpleRow((d, p) =>
{
// تعریف فونت جدول دوم
var font = new GenericFontProvider(
Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\IRNazanin.ttf",
Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\tahoma.ttf")
{
Color = BaseColor.LIGHT_GRAY,
Size = 8
};
d.CellTemplate = new XHtmlField();
p.RunDirection = PdfRunDirection.RightToLeft;
p.HorizontalAlignment = HorizontalAlignment.Justified;
p.ShowBorder = true;
d.Value = form.Ideas;
p.PdfFont = font;
p.CellPadding = 5f;
});