Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.11 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Xml;
  4. using System.Xml.XPath;
  5. using MigraDoc.DocumentObjectModel;
  6. using MigraDoc.DocumentObjectModel.Tables;
  7. using MigraDoc.DocumentObjectModel.Shapes;
  8. using MigraDoc.Rendering;
  9. using System.Diagnostics;
  10. using System.Data;
  11. using PdfSharp.Drawing;
  12. using PdfSharp.Pdf;
  13. /// <summary>
  14. /// Summary description for InvoiceForm
  15. /// </summary>
  16.  
  17.  
  18.  
  19. //This is where I define a "Form" class which I instantiate in my controller.
  20. //I will post the code to the controller in another bin, to keep things shorter.
  21.  
  22.  
  23.  
  24. public class PDFform {
  25.     public PDFform() {
  26.  
  27.     }
  28.     Document document;
  29.     DataTable dt;
  30.     string path;
  31.     TextFrame addressFrame;
  32.     Table table;
  33.  
  34.     public PDFform(DataTable dtIn, string pathIn) {
  35.         dt = dtIn;
  36.         path = pathIn;
  37.     }
  38.  
  39.     public Document CreateDocument() {
  40.         // Create a new MigraDoc document
  41.         this.document = new Document();
  42.         this.document.Info.Title = "";
  43.         this.document.Info.Subject = "";
  44.         this.document.Info.Author = "Aftab";
  45.        
  46.  
  47.         DefineStyles();
  48.  
  49.         CreatePage();
  50.  
  51.         FillContent();
  52.  
  53.         return this.document;
  54.     }
  55.  
  56.     void DefineStyles() {
  57.         // Get the predefined style Normal.
  58.         Style style = this.document.Styles["Normal"];
  59.         style.Font.Name = "Times New Roman";
  60.  
  61.         style = this.document.Styles[StyleNames.Header];
  62.         style.ParagraphFormat.AddTabStop("16cm", TabAlignment.Right);
  63.  
  64.         style = this.document.Styles[StyleNames.Footer];
  65.         style.ParagraphFormat.AddTabStop("8cm", TabAlignment.Center);
  66.  
  67.         // Create a new style called Table based on style Normal
  68.         style = this.document.Styles.AddStyle("Table", "Normal");
  69.         style.Font.Name = "Times New Roman";
  70.         style.Font.Size = 9;
  71.  
  72.         // Create a new style called Reference based on style Normal
  73.         style = this.document.Styles.AddStyle("Reference", "Normal");
  74.         style.ParagraphFormat.SpaceBefore = "5mm";
  75.         style.ParagraphFormat.SpaceAfter = "5mm";
  76.         style.ParagraphFormat.TabStops.AddTabStop("16cm", TabAlignment.Right);
  77.     }
  78.  
  79.     void CreatePage() {
  80.         // Each MigraDoc document needs at least one section.
  81.         Section section = this.document.AddSection();
  82.  
  83.         // Put a logo in the header
  84.         Image image = section.AddImage(path);
  85.  
  86.  
  87.         image.Top = ShapePosition.Top;
  88.         image.Left = ShapePosition.Left;
  89.         image.WrapFormat.Style = WrapStyle.Through;
  90.  
  91.         // Create footer
  92.         Paragraph paragraph = section.Footers.Primary.AddParagraph();
  93.         paragraph.AddText("Meridian PDF report.");
  94.         paragraph.Format.Font.Size = 9;
  95.         paragraph.Format.Alignment = ParagraphAlignment.Center;
  96.  
  97.         // Create the text frame for the address
  98.         this.addressFrame = section.AddTextFrame();
  99.         this.addressFrame.Height = "3.0cm";
  100.         this.addressFrame.Width = "7.0cm";
  101.         this.addressFrame.Left = ShapePosition.Left;
  102.         this.addressFrame.RelativeHorizontal = RelativeHorizontal.Margin;
  103.         this.addressFrame.Top = "5.0cm";
  104.         this.addressFrame.RelativeVertical = RelativeVertical.Page;
  105.  
  106.         // Put sender in address frame
  107.         paragraph = this.addressFrame.AddParagraph("Meridian, Serbia");
  108.         paragraph.Format.Font.Name = "Times New Roman";
  109.         paragraph.Format.Font.Size = 7;
  110.         paragraph.Format.SpaceAfter = 3;
  111.  
  112.         // Add the print date field
  113.         paragraph = section.AddParagraph();
  114.         paragraph.Format.SpaceBefore = "6cm";
  115.         paragraph.Style = "Reference";
  116.         paragraph.AddFormattedText("Patients Detail", TextFormat.Bold);
  117.         paragraph.AddTab();
  118.         paragraph.AddText("Date, ");
  119.         paragraph.AddDateField("dd.MM.yyyy");
  120.  
  121.         // Create the item table
  122.         this.table = section.AddTable();
  123.         this.table.Style = "Table";
  124.         this.table.Borders.Color = TableBorder;
  125.         this.table.Borders.Width = 0.25;
  126.         this.table.Borders.Left.Width = 0.5;
  127.         this.table.Borders.Right.Width = 0.5;
  128.         this.table.Rows.LeftIndent = 0;
  129.  
  130.  
  131.         Column column;
  132.         foreach (DataColumn col in dt.Columns) {
  133.  
  134.             column = this.table.AddColumn(Unit.FromCentimeter(2));
  135.             column.Format.Alignment = ParagraphAlignment.Center;
  136.  
  137.  
  138.         }
  139.         Row row = table.AddRow();
  140.         row.HeadingFormat = true;
  141.         row.Format.Alignment = ParagraphAlignment.Center;
  142.         row.Format.Font.Bold = true;
  143.         row.Shading.Color = TableBlue;
  144.  
  145.  
  146.         for (int i = 0; i < dt.Columns.Count; i++) {
  147.  
  148.             row.Cells[i].AddParagraph(dt.Columns[i].ColumnName);
  149.             row.Cells[i].Format.Font.Bold = false;
  150.             row.Cells[i].Format.Alignment = ParagraphAlignment.Left;
  151.             row.Cells[i].VerticalAlignment = VerticalAlignment.Bottom;
  152.  
  153.  
  154.         }
  155.  
  156.  
  157.         this.table.SetEdge(0, 0, dt.Columns.Count, 1, Edge.Box, BorderStyle.Single, 0.75, Color.Empty);
  158.  
  159.  
  160.     }
  161.  
  162.     void FillContent() {
  163.  
  164.         Paragraph paragraph = this.addressFrame.AddParagraph();
  165.         paragraph.AddText("Dr. Anwar Aliććšđšžćč");
  166.         paragraph.AddLineBreak();
  167.         paragraph.AddText("Health And Social Services ");
  168.         paragraph.AddLineBreak();
  169.         paragraph.AddText("Karachi");
  170.  
  171.         Row row1;
  172.         for (int i = 0; i < dt.Rows.Count; i++) {
  173.             row1 = this.table.AddRow();
  174.  
  175.  
  176.             row1.TopPadding = 1.5;
  177.  
  178.  
  179.             for (int j = 0; j < dt.Columns.Count; j++) {
  180.  
  181.                 row1.Cells[j].Shading.Color = TableGray;
  182.                 row1.Cells[j].VerticalAlignment = VerticalAlignment.Center;
  183.  
  184.                 row1.Cells[j].Format.Alignment = ParagraphAlignment.Left;
  185.                 row1.Cells[j].Format.FirstLineIndent = 1;
  186.  
  187.  
  188.  
  189.                 row1.Cells[j].AddParagraph(dt.Rows[i][j].ToString());
  190.  
  191.  
  192.                 this.table.SetEdge(0, this.table.Rows.Count - 2, dt.Columns.Count, 1, Edge.Box, BorderStyle.Single, 0.75);
  193.             }
  194.         }
  195.  
  196.         paragraph = this.document.LastSection.AddParagraph();
  197.         paragraph.Format.SpaceBefore = "1cm";
  198.         paragraph.Format.Borders.Width = 0.75;
  199.         paragraph.Format.Borders.Distance = 3;
  200.         paragraph.Format.Borders.Color = TableBorder;
  201.         paragraph.Format.Shading.Color = TableGray;
  202.  
  203.         paragraph.AddText("Note: For any complain please contact us in 24 hours from the issuance of bill");
  204.  
  205.  
  206.     }
  207. #if true
  208.  
  209.     readonly static Color TableBorder = new Color(81, 125, 192);
  210.     readonly static Color TableBlue = new Color(235, 240, 249);
  211.     readonly static Color TableGray = new Color(242, 242, 242);
  212. #else
  213.     // CMYK colors
  214.     readonly static Color tableBorder = Color.FromCmyk(100, 50, 0, 30);
  215.     readonly static Color tableBlue = Color.FromCmyk(0, 80, 50, 30);
  216.     readonly static Color tableGray = Color.FromCmyk(30, 0, 0, 0, 100);
  217. #endif
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement