Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Globalization;
- using System.Xml;
- using System.Xml.XPath;
- using MigraDoc.DocumentObjectModel;
- using MigraDoc.DocumentObjectModel.Tables;
- using MigraDoc.DocumentObjectModel.Shapes;
- using MigraDoc.Rendering;
- using System.Diagnostics;
- using System.Data;
- using PdfSharp.Drawing;
- using PdfSharp.Pdf;
- /// <summary>
- /// Summary description for InvoiceForm
- /// </summary>
- //This is where I define a "Form" class which I instantiate in my controller.
- //I will post the code to the controller in another bin, to keep things shorter.
- public class PDFform {
- public PDFform() {
- }
- Document document;
- DataTable dt;
- string path;
- TextFrame addressFrame;
- Table table;
- public PDFform(DataTable dtIn, string pathIn) {
- dt = dtIn;
- path = pathIn;
- }
- public Document CreateDocument() {
- // Create a new MigraDoc document
- this.document = new Document();
- this.document.Info.Title = "";
- this.document.Info.Subject = "";
- this.document.Info.Author = "Aftab";
- DefineStyles();
- CreatePage();
- FillContent();
- return this.document;
- }
- void DefineStyles() {
- // Get the predefined style Normal.
- Style style = this.document.Styles["Normal"];
- style.Font.Name = "Times New Roman";
- style = this.document.Styles[StyleNames.Header];
- style.ParagraphFormat.AddTabStop("16cm", TabAlignment.Right);
- style = this.document.Styles[StyleNames.Footer];
- style.ParagraphFormat.AddTabStop("8cm", TabAlignment.Center);
- // Create a new style called Table based on style Normal
- style = this.document.Styles.AddStyle("Table", "Normal");
- style.Font.Name = "Times New Roman";
- style.Font.Size = 9;
- // Create a new style called Reference based on style Normal
- style = this.document.Styles.AddStyle("Reference", "Normal");
- style.ParagraphFormat.SpaceBefore = "5mm";
- style.ParagraphFormat.SpaceAfter = "5mm";
- style.ParagraphFormat.TabStops.AddTabStop("16cm", TabAlignment.Right);
- }
- void CreatePage() {
- // Each MigraDoc document needs at least one section.
- Section section = this.document.AddSection();
- // Put a logo in the header
- Image image = section.AddImage(path);
- image.Top = ShapePosition.Top;
- image.Left = ShapePosition.Left;
- image.WrapFormat.Style = WrapStyle.Through;
- // Create footer
- Paragraph paragraph = section.Footers.Primary.AddParagraph();
- paragraph.AddText("Meridian PDF report.");
- paragraph.Format.Font.Size = 9;
- paragraph.Format.Alignment = ParagraphAlignment.Center;
- // Create the text frame for the address
- this.addressFrame = section.AddTextFrame();
- this.addressFrame.Height = "3.0cm";
- this.addressFrame.Width = "7.0cm";
- this.addressFrame.Left = ShapePosition.Left;
- this.addressFrame.RelativeHorizontal = RelativeHorizontal.Margin;
- this.addressFrame.Top = "5.0cm";
- this.addressFrame.RelativeVertical = RelativeVertical.Page;
- // Put sender in address frame
- paragraph = this.addressFrame.AddParagraph("Meridian, Serbia");
- paragraph.Format.Font.Name = "Times New Roman";
- paragraph.Format.Font.Size = 7;
- paragraph.Format.SpaceAfter = 3;
- // Add the print date field
- paragraph = section.AddParagraph();
- paragraph.Format.SpaceBefore = "6cm";
- paragraph.Style = "Reference";
- paragraph.AddFormattedText("Patients Detail", TextFormat.Bold);
- paragraph.AddTab();
- paragraph.AddText("Date, ");
- paragraph.AddDateField("dd.MM.yyyy");
- // Create the item table
- this.table = section.AddTable();
- this.table.Style = "Table";
- this.table.Borders.Color = TableBorder;
- this.table.Borders.Width = 0.25;
- this.table.Borders.Left.Width = 0.5;
- this.table.Borders.Right.Width = 0.5;
- this.table.Rows.LeftIndent = 0;
- Column column;
- foreach (DataColumn col in dt.Columns) {
- column = this.table.AddColumn(Unit.FromCentimeter(2));
- column.Format.Alignment = ParagraphAlignment.Center;
- }
- Row row = table.AddRow();
- row.HeadingFormat = true;
- row.Format.Alignment = ParagraphAlignment.Center;
- row.Format.Font.Bold = true;
- row.Shading.Color = TableBlue;
- for (int i = 0; i < dt.Columns.Count; i++) {
- row.Cells[i].AddParagraph(dt.Columns[i].ColumnName);
- row.Cells[i].Format.Font.Bold = false;
- row.Cells[i].Format.Alignment = ParagraphAlignment.Left;
- row.Cells[i].VerticalAlignment = VerticalAlignment.Bottom;
- }
- this.table.SetEdge(0, 0, dt.Columns.Count, 1, Edge.Box, BorderStyle.Single, 0.75, Color.Empty);
- }
- void FillContent() {
- Paragraph paragraph = this.addressFrame.AddParagraph();
- paragraph.AddText("Dr. Anwar Aliććšđšžćč");
- paragraph.AddLineBreak();
- paragraph.AddText("Health And Social Services ");
- paragraph.AddLineBreak();
- paragraph.AddText("Karachi");
- Row row1;
- for (int i = 0; i < dt.Rows.Count; i++) {
- row1 = this.table.AddRow();
- row1.TopPadding = 1.5;
- for (int j = 0; j < dt.Columns.Count; j++) {
- row1.Cells[j].Shading.Color = TableGray;
- row1.Cells[j].VerticalAlignment = VerticalAlignment.Center;
- row1.Cells[j].Format.Alignment = ParagraphAlignment.Left;
- row1.Cells[j].Format.FirstLineIndent = 1;
- row1.Cells[j].AddParagraph(dt.Rows[i][j].ToString());
- this.table.SetEdge(0, this.table.Rows.Count - 2, dt.Columns.Count, 1, Edge.Box, BorderStyle.Single, 0.75);
- }
- }
- paragraph = this.document.LastSection.AddParagraph();
- paragraph.Format.SpaceBefore = "1cm";
- paragraph.Format.Borders.Width = 0.75;
- paragraph.Format.Borders.Distance = 3;
- paragraph.Format.Borders.Color = TableBorder;
- paragraph.Format.Shading.Color = TableGray;
- paragraph.AddText("Note: For any complain please contact us in 24 hours from the issuance of bill");
- }
- #if true
- readonly static Color TableBorder = new Color(81, 125, 192);
- readonly static Color TableBlue = new Color(235, 240, 249);
- readonly static Color TableGray = new Color(242, 242, 242);
- #else
- // CMYK colors
- readonly static Color tableBorder = Color.FromCmyk(100, 50, 0, 30);
- readonly static Color tableBlue = Color.FromCmyk(0, 80, 50, 30);
- readonly static Color tableGray = Color.FromCmyk(30, 0, 0, 0, 100);
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement