سفارشی سازی Header و Footer در PdfReport
نویسنده: وحید نصیری
تاریخ: ۱۳۹۱/۰۷/۱۷ ۱۷:۵۹
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
namespace PdfReportSamples.Models
{
public class Task
{
public int Id { set; get; }
public string Name { set; get; }
public int PercentCompleted { set; get; }
public bool IsActive { set; get; }
public User Assignee { set; get; }
}
}
using System;
namespace PdfReportSamples.Models
{
public class User
{
public int Id { set; get; }
public string Name { set; get; }
public string LastName { set; get; }
public long Balance { set; get; }
public DateTime RegisterDate { set; get; }
}
}
column.PropertyName<Task>(x => x.Assignee.Name)
using System;
using System.Collections.Generic;
using System.Drawing;
using PdfReportSamples.Models;
using PdfRpt.Core.Contracts;
using PdfRpt.FluentInterface;
namespace PdfReportSamples.CustomHeaderFooter
{
public class CustomHeaderFooterPdfReport
{
readonly CustomHeader _customHeader = new CustomHeader();
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.CustomFooter(new CustomFooter(footer.PdfFont, PdfRunDirection.LeftToRight));
})
.PagesHeader(header =>
{
header.CustomHeader(_customHeader);
})
.MainTableTemplate(template =>
{
template.BasicTemplate(BasicTemplate.SilverTemplate);
})
.MainTablePreferences(table =>
{
table.ColumnsWidthsType(TableColumnWidthType.Relative);
table.MultipleColumnsPerPage(new MultipleColumnsPerPage
{
ColumnsGap = 22,
ColumnsPerPage = 2,
ColumnsWidth = 250,
IsRightToLeft = false,
TopMargin = 7
});
})
.MainTableDataSource(dataSource =>
{
var rows = new List<Task>();
var rnd = new Random();
for (int i = 1; i < 210; i++)
{
rows.Add(new Task
{
Assignee = new User
{
Id = i,
Name = "user-" + i
},
IsActive = rnd.Next(0, 2) == 1 ? true : false,
Name = "task-" + i
});
}
dataSource.StronglyTypedList(rows);
})
.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<Task>(x => x.Name);
column.CellsHorizontalAlignment(HorizontalAlignment.Center);
column.IsVisible(true);
column.Order(1);
column.Width(3);
column.HeaderCell("Task Name");
});
columns.AddColumn(column =>
{
column.PropertyName<Task>(x => x.Assignee.Name); // nested property support
column.CellsHorizontalAlignment(HorizontalAlignment.Center);
column.IsVisible(true);
column.Order(2);
column.Width(3);
column.HeaderCell("Assignee");
});
columns.AddColumn(column =>
{
column.PropertyName<Task>(x => x.IsActive);
column.CellsHorizontalAlignment(HorizontalAlignment.Center);
column.IsVisible(true);
column.Order(3);
column.Width(2);
column.HeaderCell("Active");
column.ColumnItemsTemplate(template =>
{
template.Checkmark(checkmarkFillColor: Color.Green, crossSignFillColor: Color.DarkRed);
});
});
})
.MainTableEvents(events =>
{
events.DataSourceIsEmpty(message: "There is no data available to display.");
})
.Export(export =>
{
export.ToExcel();
})
.Generate(data => data.AsPdfFile(AppPath.ApplicationPath + "\\Pdf\\CustomHeaderFooterPdfReportSample.pdf"));
}
}
}
using System.Collections.Generic;
using iTextSharp.text;
using iTextSharp.text.pdf;
using PdfRpt.Core.Contracts;
using PdfRpt.Core.Helper;
namespace PdfReportSamples.CustomHeaderFooter
{
public class CustomHeader : IPageHeader
{
public PdfPTable RenderingGroupHeader(Document pdfDoc, PdfWriter pdfWriter, IList<CellData> rowdata, IList<SummaryCellData> summaryData)
{
return null;
}
Image _image;
public PdfPTable RenderingReportHeader(Document pdfDoc, PdfWriter pdfWriter, IList<SummaryCellData> summaryData)
{
if (_image == null) //cache is empty
{
var templatePath = AppPath.ApplicationPath + "\\data\\PdfHeaderTemplate.pdf";
_image = PdfImageHelper.GetITextSharpImageFromPdfTemplate(pdfWriter, templatePath);
}
var table = new PdfPTable(1);
var cell = new PdfPCell(_image, true) { Border = 0 };
table.AddCell(cell);
return table;
}
}
}
using System.Collections.Generic;
using iTextSharp.text;
using iTextSharp.text.pdf;
using PdfRpt.Core.Contracts;
namespace PdfReportSamples.CustomHeaderFooter
{
public class CustomFooter : IPageFooter
{
PdfContentByte _pdfContentByte;
readonly IPdfFont _pdfRptFont;
readonly Font _font;
readonly PdfRunDirection _direction;
PdfTemplate _template;
public CustomFooter(IPdfFont pdfRptFont, PdfRunDirection direction)
{
_direction = direction;
_pdfRptFont = pdfRptFont;
_font = _pdfRptFont.Fonts[0];
}
public void ClosingDocument(PdfWriter writer, Document document, IList<SummaryCellData> columnCellsSummaryData)
{
_template.BeginText();
_template.SetFontAndSize(_pdfRptFont.Fonts[0].BaseFont, 8);
_template.SetTextMatrix(0, 0);
_template.ShowText((writer.PageNumber - 1).ToString());
_template.EndText();
}
public void PageFinished(PdfWriter writer, Document document, IList<SummaryCellData> columnCellsSummaryData)
{
var pageSize = document.PageSize;
var text = "Page " + writer.PageNumber + " / ";
var textLen = _font.BaseFont.GetWidthPoint(text, _font.Size);
var center = (pageSize.Left + pageSize.Right) / 2;
var align = _direction == PdfRunDirection.RightToLeft ? Element.ALIGN_RIGHT : Element.ALIGN_LEFT;
ColumnText.ShowTextAligned(
canvas: _pdfContentByte,
alignment: align,
phrase: new Phrase(text, _font),
x: center,
y: pageSize.GetBottom(25),
rotation: 0,
runDirection: (int)_direction,
arabicOptions: 0);
var x = _direction == PdfRunDirection.RightToLeft ? center - textLen : center + textLen;
_pdfContentByte.AddTemplate(_template, x, pageSize.GetBottom(25));
}
public void DocumentOpened(PdfWriter writer, IList<SummaryCellData> columnCellsSummaryData)
{
_pdfContentByte = writer.DirectContent;
_template = _pdfContentByte.CreateTemplate(50, 50);
}
}
}
using System.Collections.Generic;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace PdfRpt.Core.Contracts
{
public interface IPageHeader
{
PdfPTable RenderingGroupHeader(Document pdfDoc, PdfWriter pdfWriter, IList<CellData> newGroupInfo, IList<SummaryCellData> summaryData);
PdfPTable RenderingReportHeader(Document pdfDoc, PdfWriter pdfWriter, IList<SummaryCellData> summaryData);
}
}
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Collections.Generic;
namespace PdfRpt.Core.Contracts
{
public interface IPageFooter
{
void DocumentOpened(PdfWriter writer, IList<SummaryCellData> columnCellsSummaryData);
void PageFinished(PdfWriter writer, Document document, IList<SummaryCellData> columnCellsSummaryData);
void ClosingDocument(PdfWriter writer, Document document, IList<SummaryCellData> columnCellsSummaryData);
}
}
readonly CustomHeader _customHeader = new CustomHeader();
//...
.PagesFooter(footer =>
{
footer.CustomFooter(new CustomFooter(footer.PdfFont, PdfRunDirection.LeftToRight));
})
.PagesHeader(header =>
{
header.CustomHeader(_customHeader);
})
public static iTextSharp.text.Image GetITextSharpImageFromAcroForm(
this PdfWriter pdfWriter,
string pdfTemplateFilePath,
IList<CellData> data,
Action<IList<CellData>, AcroFields, PdfStamper> onFillAcroForm,
IList<iTextSharp.text.Font> fonts,
int pageNumber = 1).MainTableTemplate(template =>
{
template.CustomTemplate(new TransparentTemplate());
})