Advertisement
Guest User

Bjcombs1989 question

a guest
Sep 21st, 2017
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 21.59 KB | None | 0 0
  1. public class PDF
  2.     {
  3.         public string GenerateDocument(DataRow client, List<string> properties, List<Covers> covers, List<KeyValuePair<Forms, int>> forms)
  4.         {
  5.             SQLDataSetTableAdapters.CL_Cover_PropertiesTableAdapter db_covers = new SQLDataSetTableAdapters.CL_Cover_PropertiesTableAdapter();
  6.  
  7.             #region Address block
  8.             // First add the date and address block to the list of properties
  9.             List<DocumentProperty> document_properties = new List<DocumentProperty>();
  10.             document_properties.AddRange(DocumentTemplates.AddressBlock(client, 1, true));
  11.             #endregion
  12.  
  13.             #region Covers
  14.             // Then add all the covers
  15.             foreach (Covers cover in covers)
  16.             {
  17.                 DataTable cover_properties = db_covers.GetLetterDetails((int)cover);
  18.  
  19.                 foreach (DataRow row in cover_properties.Rows)
  20.                 {
  21.                     DocumentProperty dp = ImposeProperty(client, row["property"].ToString());
  22.  
  23.                     if (dp != null)
  24.                         document_properties.Add(dp);
  25.                 }//end foreach property for the cover
  26.             }//end foreach cover
  27.             #endregion
  28.  
  29.             #region Properties
  30.             // Then add all the properties
  31.             foreach (string property in properties)
  32.             {
  33.                 DocumentProperty dp = ImposeProperty(client, property);
  34.  
  35.                 if (dp != null)
  36.                     document_properties.Add(dp);
  37.             }//end foreach cover
  38.             #endregion
  39.  
  40.             #region Enclosures Section
  41.             // Then add the forms
  42.             StringBuilder enclosures = new StringBuilder("\r\n");
  43.  
  44.             foreach (var kvpForms in forms)
  45.             {
  46.                 string volume = "";
  47.  
  48.                 if (kvpForms.Value > 1) volume = " (" + kvpForms.Value + " copies)";
  49.  
  50.                 switch (kvpForms.Key)
  51.                 {
  52.                     case Forms.AFR:
  53.                         enclosures.AppendLine("Adult Function Report" + volume);
  54.                         break;
  55.                     case Forms.AFR3rdParty:
  56.                         enclosures.AppendLine("3rd Party Adult Function Report" + volume);
  57.                         break;
  58.                     case Forms.AR:
  59.                         enclosures.AppendLine("SSA Form^ Appointment of Representative" + volume);
  60.                         break;
  61.                     case Forms.DependantInfo:
  62.                         enclosures.AppendLine("Dependant Information Sheet" + volume);
  63.                         break;
  64.                     case Forms.DIB_Application:
  65.                         enclosures.AppendLine("SSA Form^ DIB (Title 2) Benefits Application" + volume);
  66.                         break;
  67.                     case Forms.DL_Form:
  68.                         enclosures.AppendLine("Medical Update Log" + volume);
  69.                         break;
  70.                     case Forms.DR:
  71.                         enclosures.AppendLine("SSA Form^ Appeal Disability Report" + volume);
  72.                         break;
  73.                     case Forms.DR_3368:
  74.                         enclosures.AppendLine("SSA Form^ Initial Application Disability Report" + volume);
  75.                         break;
  76.                     case Forms.FA:
  77.                         enclosures.AppendLine("Fee Agreement" + volume);
  78.                         break;
  79.                     case Forms.GA_Letter:
  80.                         enclosures.AppendLine("General Assistance Letter" + volume);
  81.                         break;
  82.                     case Forms.HIPPA:
  83.                         enclosures.AppendLine("HIPPA Complaint Medical Release" + volume);
  84.                         break;
  85.                     case Forms.HITECH:
  86.                         enclosures.AppendLine("Medical Record Patient Request / Release" + volume);
  87.                         break;
  88.                     case Forms.MRFC:
  89.                         enclosures.AppendLine("2-Page Mental Impairments Questionnaire" + volume);
  90.                         break;
  91.                     case Forms.ODAR_Questionnaires:
  92.                         enclosures.AppendLine("SSA Forms^ Medications, Work History, Recent Medical Treatment" + volume);
  93.                         break;
  94.                     case Forms.PRFC:
  95.                         enclosures.AppendLine("1-Page Physical Impairments Questionnaire" + volume);
  96.                         break;
  97.                     case Forms.PRFC_Long:
  98.                         enclosures.AppendLine("3-Page Physical Impairments Questionnaire" + volume);
  99.                         break;
  100.                     case Forms.SR:
  101.                         enclosures.AppendLine("SSA Form^ Authorization to Disclose Information" + volume);
  102.                         break;
  103.                     case Forms.SSI_Application:
  104.                         enclosures.AppendLine("SSA Form^ SSI (Title 16) Benefits Application" + volume);
  105.                         break;
  106.                     case Forms.WAR:
  107.                         enclosures.AppendLine("SSA Form^ Work Activity Report" + volume);
  108.                         break;
  109.                     case Forms.WHR:
  110.                         enclosures.AppendLine("SSA Form^ Work History Report" + volume);
  111.                         break;
  112.                     default:
  113.                         throw new ApplicationException("Unrecognized forms");
  114.                 }//end switch
  115.             }//end foreac
  116.  
  117.             #endregion
  118.  
  119.             #region Signature Area
  120.             document_properties.AddRange(DocumentTemplates.SignatureLine(1, enclosures.ToString()));
  121.  
  122.             #endregion
  123.  
  124.             #region Generate main letter
  125.             string destination = Path.GetTempFileName().Replace(".tmp", ".pdf");
  126.             FileStream fs = new FileStream(destination, FileMode.Create, FileAccess.Write, FileShare.None);
  127.             Document doc = new Document(new Rectangle(PageSize.LETTER), 58, 58, 100, 50);
  128.             PdfWriter writer = PdfWriter.GetInstance(doc, fs);
  129.             doc.Open();
  130.  
  131.             PdfReader reader = new PdfReader(Path.Combine(@"J:\documents\", "letterhead.pdf"));
  132.             writer.DirectContent.AddTemplate(writer.GetImportedPage(reader, 1), 0, 0);
  133.  
  134.             foreach (DocumentProperty dp in document_properties)
  135.                 AddParagraph(dp, doc);
  136.  
  137.             #endregion
  138.  
  139.             #region Build Forms
  140.  
  141.             if (writer.PageNumber % 2 == 1)
  142.             {
  143.                 doc.NewPage();
  144.                 AddParagraph(new DocumentProperty(" ", 2, Font.NORMAL, 0, 0, 12, 0f), doc);
  145.             }
  146.  
  147.             int next_page = writer.PageNumber + 1;
  148.  
  149.             Dictionary<string, DocumentTemplate> form_details = new Dictionary<string, DocumentTemplate>();
  150.  
  151.             foreach (var kvpForms in forms)
  152.             {
  153.                 for (int x = 1; x <= kvpForms.Value; x++)
  154.                 {
  155.                     switch (kvpForms.Key)
  156.                     {
  157.                         case Forms.AR:
  158.                             form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.AR(client, next_page));
  159.                             break;
  160.                         case Forms.DependantInfo:
  161.                             form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.DependantInfo(client, next_page));
  162.                             break;
  163.                         case Forms.FA:
  164.                             form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.FA(client, next_page));
  165.                             break;
  166.                         case Forms.HIPPA:
  167.                             form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.Hippa(client, next_page));
  168.                             break;
  169.                         case Forms.HITECH:
  170.                             form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.Hitech(client, next_page));
  171.                             break;
  172.                         case Forms.SR:
  173.                             form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.SR(client, next_page));
  174.                             break;
  175.                         case Forms.DL_Form:
  176.                             form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.DL(client, next_page));
  177.                             break;
  178.                         case Forms.GA_Letter:
  179.                             form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.GA(client, next_page));
  180.                             break;
  181.                         case Forms.MRFC:
  182.                             form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.MRFC(client, next_page));
  183.                             break;
  184.                         case Forms.PRFC:
  185.                             form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.PRFC_Long(client, next_page));
  186.                             break;
  187.                         case Forms.PRFC_Long:
  188.                             form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.PRFC_Long(client, next_page));
  189.                             break;
  190.                         case Forms.AFR:
  191.                             form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.AFR3373(client, next_page));
  192.                             break;
  193.                         case Forms.AFR3rdParty:
  194.                             form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.AFR3rdParty3380(client, next_page));
  195.                             break;
  196.                         case Forms.DR:
  197.                             form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.DR3441(client, next_page));
  198.                             break;
  199.                         case Forms.DR_3368:
  200.                             form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.DR3368(client, next_page));
  201.                             break;
  202.                         case Forms.DIB_Application:
  203.                             form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.ApplicationDIB(client, next_page));
  204.                             break;
  205.                         case Forms.ODAR_Questionnaires:
  206.                             break;
  207.                         case Forms.SSI_Application:
  208.                             form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.ApplicationSSI(client, next_page));
  209.                             break;
  210.                         case Forms.WAR:
  211.                             break;
  212.                         case Forms.WHR:
  213.                             break;
  214.                         default:
  215.                             throw new ApplicationException("Unrecognized forms");
  216.                     }//end switch
  217.  
  218.                     next_page += form_details[kvpForms.Key.ToString() + x].TotalPages;
  219.                 } // for loop
  220.             }//end foreac
  221.  
  222.             #endregion
  223.  
  224.             #region Add forms
  225.             int total_page_count = next_page - 1;
  226.             next_page = 2;
  227.  
  228.             foreach (KeyValuePair<string, DocumentTemplate> kvpFormTemplate in form_details)
  229.             {
  230.                 foreach (string template in kvpFormTemplate.Value.Templates)
  231.                 {
  232.                     reader = new PdfReader(Path.Combine(@"J:\documents\", template));
  233.  
  234.                     //Step 6: Loop through each page of the document
  235.                     for (int page = 1; page <= reader.NumberOfPages; page++)
  236.                     {
  237.  
  238.                        
  239.                         next_page++;
  240.                         total_page_count++;
  241.                         doc.NewPage();
  242.  
  243.                         //Step 6.1: Add the page
  244.                         writer.DirectContent.AddTemplate(writer.GetImportedPage(reader, page), 0, 0);
  245.                         //Step 6.2: Get the properties for this page
  246.                         DocumentProperty[] pagesProperties = kvpFormTemplate.Value.Properties.Where(p => (p.P == next_page)).ToArray();
  247.                         //Step 6.3: Add the property to this page
  248.                         foreach (DocumentProperty property in pagesProperties)
  249.                         {
  250.                             if (property.Type == DocumentProperty.DocumentPropertyType.Image)
  251.                                 AddImage(property, writer);
  252.                             else if (property.Type == DocumentProperty.DocumentPropertyType.Text)
  253.                                 AddText(property, writer);
  254.                             else if (property.Type == DocumentProperty.DocumentPropertyType.Paragraph)
  255.                                 AddParagraph(property, doc);
  256.                             else if (property.Type == DocumentProperty.DocumentPropertyType.Barcode)
  257.                                 AddBarcode(property, writer);
  258.                         } // end foreach property
  259.                     } // end for each page number
  260.                 } // end for each template
  261.             }// end foreach form to add
  262.  
  263.             // All done, we need to count the pages and the barcode to page one
  264.  
  265.             doc.PageCount = 1;
  266.  
  267.             AddText(new DocumentProperty("TEST", 1, 50, 50, 16), writer);
  268.  
  269.             Console.WriteLine("Page count = " + total_page_count);
  270.  
  271.             #endregion
  272.  
  273.             doc.Close();
  274.            
  275.             return destination;
  276.         }
  277.         public string GenerateDocument(DocumentTemplate template, bool narrow = false, string client_folder = "")
  278.         {
  279.             return GenerateDocument(template.Templates, narrow, client_folder, template.Properties);
  280.         }
  281.         public string GenerateDocument(string[] templates, bool narrow = false, string client_folder = "", params DocumentProperty[] properties)
  282.         {
  283.             string destination = Path.GetTempFileName().Replace(".tmp", ".pdf");
  284.             //Step 1: Create a System.IO.FileStream object:
  285.             FileStream fs = new FileStream(destination, FileMode.Create, FileAccess.Write, FileShare.None);
  286.             //Step 2: Create a iTextSharp.text.Document object:
  287.             Document doc = new Document(new Rectangle(PageSize.LETTER), 58, 58, narrow ? 50 : 100, 50);
  288.             //Step 3: Create a iTextSharp.text.pdf.PdfWriter object. It helps to write the Document to the Specified FileStream:
  289.             PdfWriter writer = PdfWriter.GetInstance(doc, fs);
  290.             //Step 4: Openning the Document:
  291.             doc.Open();
  292.             //Step 5: Add Base Content
  293.             int totalPageCount = 0;
  294.  
  295.             foreach (string template in templates)
  296.             {
  297.                 PdfReader reader;
  298.  
  299.                 if (template.Contains("Please select the "))
  300.                 {
  301.                     System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
  302.                     ofd.Title = template;
  303.                     ofd.CheckFileExists = true;
  304.                     ofd.Multiselect = false;
  305.                     ofd.InitialDirectory = client_folder;
  306.  
  307.                     while (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
  308.                     {
  309.                         System.Windows.Forms.MessageBox.Show("Please choose the file", "Document Generator");
  310.                     };
  311.  
  312.                     reader = new PdfReader(ofd.FileName);
  313.                 }
  314.                 else
  315.                     reader = new PdfReader(Path.Combine(@"J:\documents\", template));
  316.  
  317.                 //Step 6: Loop through each page of the document
  318.                 for (int page = 1; page <= reader.NumberOfPages; page++)
  319.                 {
  320.                     totalPageCount++;
  321.  
  322.                     if (totalPageCount > 1)
  323.                     {
  324.                         doc.NewPage();
  325.                     }
  326.  
  327.                     //Step 6.1: Add the page
  328.                     writer.DirectContent.AddTemplate(writer.GetImportedPage(reader, page), 0, 0);
  329.                     //Step 6.2: Get the properties for this page
  330.                     DocumentProperty[] pagesProperties = properties.Where(p => (p.P == totalPageCount)).ToArray();
  331.                     //Step 6.3: Add the property to this page
  332.                     foreach (DocumentProperty property in pagesProperties)
  333.                     {
  334.                         if (property.Type == DocumentProperty.DocumentPropertyType.Image)
  335.                             AddImage(property, writer);
  336.                         else if (property.Type == DocumentProperty.DocumentPropertyType.Text)
  337.                             AddText(property, writer);
  338.                         else if (property.Type == DocumentProperty.DocumentPropertyType.Paragraph)
  339.                             AddParagraph(property, doc);
  340.                         else if (property.Type == DocumentProperty.DocumentPropertyType.Barcode)
  341.                             AddBarcode(property, writer);
  342.                     } // end foreach property
  343.                 } // end for each page number
  344.             } // end for each template
  345.  
  346.             //Step 7: Closing the Document:
  347.  
  348.             doc.Close();
  349.  
  350.             return destination;
  351.         }
  352.  
  353.         private void AddBarcode(DocumentProperty property, PdfWriter writer)
  354.         {
  355.  
  356.             Barcode39 code39 = new Barcode39();
  357.             code39.CodeType = iTextSharp.text.pdf.Barcode.CODE128;
  358.             code39.ChecksumText = true;
  359.             code39.GenerateChecksum = true;
  360.             code39.StartStopText = false;
  361.             code39.Code = property.Value;
  362.  
  363.             // Create a blank image
  364.             System.Drawing.Bitmap bmpimg = new System.Drawing.Bitmap((int)property.W, (int)property.H); // provide width and height based on the barcode image to be generated. harcoded for sample purpose
  365.  
  366.             System.Drawing.Graphics bmpgraphics = System.Drawing.Graphics.FromImage(bmpimg);
  367.             bmpgraphics.Clear(System.Drawing.Color.White); // Provide this, else the background will be black by default
  368.  
  369.             // generate the code128 barcode
  370.             bmpgraphics.DrawImage(code39.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White), new System.Drawing.Point(0, 0));
  371.  
  372.             //generate the text below the barcode image. If you want the placement to be dynamic, calculate the point based on size of the image
  373.             // bmpgraphics.DrawString(<< Barcode value >>, new System.Drawing.Font("Arial", 8, FontStyle.Regular), SystemBrushes.WindowText, new Point(15, 24));
  374.  
  375.             // Save the output stream as gif. You can also save it to external file
  376.             string temp = Path.GetTempFileName().Replace("tmp", "gif");
  377.             bmpimg.Save(temp, System.Drawing.Imaging.ImageFormat.Gif);
  378.  
  379.             PdfPTable pdfFooter; PdfPCell pdfCell1;
  380.             iTextSharp.text.Image signature = iTextSharp.text.Image.GetInstance(temp);
  381.             //Image signature = Image.GetInstance(@"J:\signatures\black.png"); signature.ScaleAbsolute(property.W, property.H);
  382.             pdfFooter = new PdfPTable(1); pdfCell1 = new PdfPCell(signature); pdfCell1.Border = 0; pdfFooter.TotalWidth = 450; pdfFooter.AddCell(pdfCell1);
  383.             pdfFooter.WriteSelectedRows(0, -1, property.X, property.Y, writer.DirectContent);
  384.         }
  385.  
  386.         private void AddImage(DocumentProperty property, PdfWriter writer)
  387.         {
  388.             PdfPTable pdfFooter; PdfPCell pdfCell1;
  389.             Image signature = Image.GetInstance(property.Value); signature.ScaleToFit(property.W, property.H);
  390.             //Image signature = Image.GetInstance(@"J:\signatures\black.png"); signature.ScaleAbsolute(property.W, property.H);
  391.             pdfFooter = new PdfPTable(1); pdfCell1 = new PdfPCell(signature); pdfCell1.Border = 0; pdfFooter.TotalWidth = 450; pdfFooter.AddCell(pdfCell1);
  392.             pdfFooter.WriteSelectedRows(0, -1, property.X, property.Y, writer.DirectContent);
  393.         }
  394.  
  395.         private void AddText(DocumentProperty property, PdfWriter writer)
  396.         {
  397.             PdfPTable pdfFooter; PdfPCell pdfCell1;
  398.             pdfFooter = new PdfPTable(1); pdfCell1 = new PdfPCell(new Phrase(property.Value, new Font(Font.FontFamily.TIMES_ROMAN, (int)property.H))); pdfCell1.Border = 0; pdfFooter.TotalWidth = 450; pdfFooter.AddCell(pdfCell1);
  399.             pdfFooter.WriteSelectedRows(0, -1, property.X, property.Y, writer.DirectContent);
  400.         }
  401.  
  402.         private void AddParagraph(DocumentProperty property, Document doc)
  403.         {
  404.             for (int x = 0; x < (int)property.Y; x++)
  405.             {
  406.                 doc.Add(new AlignedParagraph(" ", (int)property.H, property.Alignment, true));
  407.             }
  408.  
  409.             doc.Add(new AlignedParagraph(property.Value, (int)property.H, property.Alignment, property.X, true, property.I));
  410.  
  411.             for (int x = 0; x < (int)property.W; x++)
  412.             {
  413.                 doc.Add(new AlignedParagraph(" ", (int)property.H, property.Alignment, true));
  414.             }
  415.         }
  416.  
  417.  
  418.  
  419.         private DocumentProperty ImposeProperty(DataRow client, string text)
  420.         {
  421.             if (client["cm"].ToString() != "")
  422.             {
  423.                 text = text.Replace("{CM}", client["cm"].ToString().Trim().Split('_')[1]);
  424.                 text = text.Replace("{CM:Phone}", client["cmphone"].ToString().Trim());
  425.             }
  426.  
  427.             if (text.Contains("[IFCM==\"\"]") && client["cm"].ToString().Trim() != "")
  428.                 return null;
  429.  
  430.             if (text.Contains("[IFCM!=\"\"]") && client["cm"].ToString().Trim() == "")
  431.                 return null;
  432.  
  433.             text = text.Replace("[IFCM!=\"\"]", "");
  434.             text = text.Replace("[IFCM==\"\"]", "");
  435.  
  436.             if (text.Contains("•")) return new DocumentProperty(text, 1, Font.NORMAL, 0, 0, 12, 30);
  437.  
  438.             return new DocumentProperty(text, 1, Font.NORMAL, 1, 0, 12);
  439.         }
  440.     } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement