Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PDF
- {
- public string GenerateDocument(DataRow client, List<string> properties, List<Covers> covers, List<KeyValuePair<Forms, int>> forms)
- {
- SQLDataSetTableAdapters.CL_Cover_PropertiesTableAdapter db_covers = new SQLDataSetTableAdapters.CL_Cover_PropertiesTableAdapter();
- #region Address block
- // First add the date and address block to the list of properties
- List<DocumentProperty> document_properties = new List<DocumentProperty>();
- document_properties.AddRange(DocumentTemplates.AddressBlock(client, 1, true));
- #endregion
- #region Covers
- // Then add all the covers
- foreach (Covers cover in covers)
- {
- DataTable cover_properties = db_covers.GetLetterDetails((int)cover);
- foreach (DataRow row in cover_properties.Rows)
- {
- DocumentProperty dp = ImposeProperty(client, row["property"].ToString());
- if (dp != null)
- document_properties.Add(dp);
- }//end foreach property for the cover
- }//end foreach cover
- #endregion
- #region Properties
- // Then add all the properties
- foreach (string property in properties)
- {
- DocumentProperty dp = ImposeProperty(client, property);
- if (dp != null)
- document_properties.Add(dp);
- }//end foreach cover
- #endregion
- #region Enclosures Section
- // Then add the forms
- StringBuilder enclosures = new StringBuilder("\r\n");
- foreach (var kvpForms in forms)
- {
- string volume = "";
- if (kvpForms.Value > 1) volume = " (" + kvpForms.Value + " copies)";
- switch (kvpForms.Key)
- {
- case Forms.AFR:
- enclosures.AppendLine("Adult Function Report" + volume);
- break;
- case Forms.AFR3rdParty:
- enclosures.AppendLine("3rd Party Adult Function Report" + volume);
- break;
- case Forms.AR:
- enclosures.AppendLine("SSA Form^ Appointment of Representative" + volume);
- break;
- case Forms.DependantInfo:
- enclosures.AppendLine("Dependant Information Sheet" + volume);
- break;
- case Forms.DIB_Application:
- enclosures.AppendLine("SSA Form^ DIB (Title 2) Benefits Application" + volume);
- break;
- case Forms.DL_Form:
- enclosures.AppendLine("Medical Update Log" + volume);
- break;
- case Forms.DR:
- enclosures.AppendLine("SSA Form^ Appeal Disability Report" + volume);
- break;
- case Forms.DR_3368:
- enclosures.AppendLine("SSA Form^ Initial Application Disability Report" + volume);
- break;
- case Forms.FA:
- enclosures.AppendLine("Fee Agreement" + volume);
- break;
- case Forms.GA_Letter:
- enclosures.AppendLine("General Assistance Letter" + volume);
- break;
- case Forms.HIPPA:
- enclosures.AppendLine("HIPPA Complaint Medical Release" + volume);
- break;
- case Forms.HITECH:
- enclosures.AppendLine("Medical Record Patient Request / Release" + volume);
- break;
- case Forms.MRFC:
- enclosures.AppendLine("2-Page Mental Impairments Questionnaire" + volume);
- break;
- case Forms.ODAR_Questionnaires:
- enclosures.AppendLine("SSA Forms^ Medications, Work History, Recent Medical Treatment" + volume);
- break;
- case Forms.PRFC:
- enclosures.AppendLine("1-Page Physical Impairments Questionnaire" + volume);
- break;
- case Forms.PRFC_Long:
- enclosures.AppendLine("3-Page Physical Impairments Questionnaire" + volume);
- break;
- case Forms.SR:
- enclosures.AppendLine("SSA Form^ Authorization to Disclose Information" + volume);
- break;
- case Forms.SSI_Application:
- enclosures.AppendLine("SSA Form^ SSI (Title 16) Benefits Application" + volume);
- break;
- case Forms.WAR:
- enclosures.AppendLine("SSA Form^ Work Activity Report" + volume);
- break;
- case Forms.WHR:
- enclosures.AppendLine("SSA Form^ Work History Report" + volume);
- break;
- default:
- throw new ApplicationException("Unrecognized forms");
- }//end switch
- }//end foreac
- #endregion
- #region Signature Area
- document_properties.AddRange(DocumentTemplates.SignatureLine(1, enclosures.ToString()));
- #endregion
- #region Generate main letter
- string destination = Path.GetTempFileName().Replace(".tmp", ".pdf");
- FileStream fs = new FileStream(destination, FileMode.Create, FileAccess.Write, FileShare.None);
- Document doc = new Document(new Rectangle(PageSize.LETTER), 58, 58, 100, 50);
- PdfWriter writer = PdfWriter.GetInstance(doc, fs);
- doc.Open();
- PdfReader reader = new PdfReader(Path.Combine(@"J:\documents\", "letterhead.pdf"));
- writer.DirectContent.AddTemplate(writer.GetImportedPage(reader, 1), 0, 0);
- foreach (DocumentProperty dp in document_properties)
- AddParagraph(dp, doc);
- #endregion
- #region Build Forms
- if (writer.PageNumber % 2 == 1)
- {
- doc.NewPage();
- AddParagraph(new DocumentProperty(" ", 2, Font.NORMAL, 0, 0, 12, 0f), doc);
- }
- int next_page = writer.PageNumber + 1;
- Dictionary<string, DocumentTemplate> form_details = new Dictionary<string, DocumentTemplate>();
- foreach (var kvpForms in forms)
- {
- for (int x = 1; x <= kvpForms.Value; x++)
- {
- switch (kvpForms.Key)
- {
- case Forms.AR:
- form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.AR(client, next_page));
- break;
- case Forms.DependantInfo:
- form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.DependantInfo(client, next_page));
- break;
- case Forms.FA:
- form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.FA(client, next_page));
- break;
- case Forms.HIPPA:
- form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.Hippa(client, next_page));
- break;
- case Forms.HITECH:
- form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.Hitech(client, next_page));
- break;
- case Forms.SR:
- form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.SR(client, next_page));
- break;
- case Forms.DL_Form:
- form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.DL(client, next_page));
- break;
- case Forms.GA_Letter:
- form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.GA(client, next_page));
- break;
- case Forms.MRFC:
- form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.MRFC(client, next_page));
- break;
- case Forms.PRFC:
- form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.PRFC_Long(client, next_page));
- break;
- case Forms.PRFC_Long:
- form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.PRFC_Long(client, next_page));
- break;
- case Forms.AFR:
- form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.AFR3373(client, next_page));
- break;
- case Forms.AFR3rdParty:
- form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.AFR3rdParty3380(client, next_page));
- break;
- case Forms.DR:
- form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.DR3441(client, next_page));
- break;
- case Forms.DR_3368:
- form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.DR3368(client, next_page));
- break;
- case Forms.DIB_Application:
- form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.ApplicationDIB(client, next_page));
- break;
- case Forms.ODAR_Questionnaires:
- break;
- case Forms.SSI_Application:
- form_details.Add(kvpForms.Key.ToString() + x, DocumentTemplates.ApplicationSSI(client, next_page));
- break;
- case Forms.WAR:
- break;
- case Forms.WHR:
- break;
- default:
- throw new ApplicationException("Unrecognized forms");
- }//end switch
- next_page += form_details[kvpForms.Key.ToString() + x].TotalPages;
- } // for loop
- }//end foreac
- #endregion
- #region Add forms
- int total_page_count = next_page - 1;
- next_page = 2;
- foreach (KeyValuePair<string, DocumentTemplate> kvpFormTemplate in form_details)
- {
- foreach (string template in kvpFormTemplate.Value.Templates)
- {
- reader = new PdfReader(Path.Combine(@"J:\documents\", template));
- //Step 6: Loop through each page of the document
- for (int page = 1; page <= reader.NumberOfPages; page++)
- {
- next_page++;
- total_page_count++;
- doc.NewPage();
- //Step 6.1: Add the page
- writer.DirectContent.AddTemplate(writer.GetImportedPage(reader, page), 0, 0);
- //Step 6.2: Get the properties for this page
- DocumentProperty[] pagesProperties = kvpFormTemplate.Value.Properties.Where(p => (p.P == next_page)).ToArray();
- //Step 6.3: Add the property to this page
- foreach (DocumentProperty property in pagesProperties)
- {
- if (property.Type == DocumentProperty.DocumentPropertyType.Image)
- AddImage(property, writer);
- else if (property.Type == DocumentProperty.DocumentPropertyType.Text)
- AddText(property, writer);
- else if (property.Type == DocumentProperty.DocumentPropertyType.Paragraph)
- AddParagraph(property, doc);
- else if (property.Type == DocumentProperty.DocumentPropertyType.Barcode)
- AddBarcode(property, writer);
- } // end foreach property
- } // end for each page number
- } // end for each template
- }// end foreach form to add
- // All done, we need to count the pages and the barcode to page one
- doc.PageCount = 1;
- AddText(new DocumentProperty("TEST", 1, 50, 50, 16), writer);
- Console.WriteLine("Page count = " + total_page_count);
- #endregion
- doc.Close();
- return destination;
- }
- public string GenerateDocument(DocumentTemplate template, bool narrow = false, string client_folder = "")
- {
- return GenerateDocument(template.Templates, narrow, client_folder, template.Properties);
- }
- public string GenerateDocument(string[] templates, bool narrow = false, string client_folder = "", params DocumentProperty[] properties)
- {
- string destination = Path.GetTempFileName().Replace(".tmp", ".pdf");
- //Step 1: Create a System.IO.FileStream object:
- FileStream fs = new FileStream(destination, FileMode.Create, FileAccess.Write, FileShare.None);
- //Step 2: Create a iTextSharp.text.Document object:
- Document doc = new Document(new Rectangle(PageSize.LETTER), 58, 58, narrow ? 50 : 100, 50);
- //Step 3: Create a iTextSharp.text.pdf.PdfWriter object. It helps to write the Document to the Specified FileStream:
- PdfWriter writer = PdfWriter.GetInstance(doc, fs);
- //Step 4: Openning the Document:
- doc.Open();
- //Step 5: Add Base Content
- int totalPageCount = 0;
- foreach (string template in templates)
- {
- PdfReader reader;
- if (template.Contains("Please select the "))
- {
- System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
- ofd.Title = template;
- ofd.CheckFileExists = true;
- ofd.Multiselect = false;
- ofd.InitialDirectory = client_folder;
- while (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
- {
- System.Windows.Forms.MessageBox.Show("Please choose the file", "Document Generator");
- };
- reader = new PdfReader(ofd.FileName);
- }
- else
- reader = new PdfReader(Path.Combine(@"J:\documents\", template));
- //Step 6: Loop through each page of the document
- for (int page = 1; page <= reader.NumberOfPages; page++)
- {
- totalPageCount++;
- if (totalPageCount > 1)
- {
- doc.NewPage();
- }
- //Step 6.1: Add the page
- writer.DirectContent.AddTemplate(writer.GetImportedPage(reader, page), 0, 0);
- //Step 6.2: Get the properties for this page
- DocumentProperty[] pagesProperties = properties.Where(p => (p.P == totalPageCount)).ToArray();
- //Step 6.3: Add the property to this page
- foreach (DocumentProperty property in pagesProperties)
- {
- if (property.Type == DocumentProperty.DocumentPropertyType.Image)
- AddImage(property, writer);
- else if (property.Type == DocumentProperty.DocumentPropertyType.Text)
- AddText(property, writer);
- else if (property.Type == DocumentProperty.DocumentPropertyType.Paragraph)
- AddParagraph(property, doc);
- else if (property.Type == DocumentProperty.DocumentPropertyType.Barcode)
- AddBarcode(property, writer);
- } // end foreach property
- } // end for each page number
- } // end for each template
- //Step 7: Closing the Document:
- doc.Close();
- return destination;
- }
- private void AddBarcode(DocumentProperty property, PdfWriter writer)
- {
- Barcode39 code39 = new Barcode39();
- code39.CodeType = iTextSharp.text.pdf.Barcode.CODE128;
- code39.ChecksumText = true;
- code39.GenerateChecksum = true;
- code39.StartStopText = false;
- code39.Code = property.Value;
- // Create a blank image
- 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
- System.Drawing.Graphics bmpgraphics = System.Drawing.Graphics.FromImage(bmpimg);
- bmpgraphics.Clear(System.Drawing.Color.White); // Provide this, else the background will be black by default
- // generate the code128 barcode
- bmpgraphics.DrawImage(code39.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White), new System.Drawing.Point(0, 0));
- //generate the text below the barcode image. If you want the placement to be dynamic, calculate the point based on size of the image
- // bmpgraphics.DrawString(<< Barcode value >>, new System.Drawing.Font("Arial", 8, FontStyle.Regular), SystemBrushes.WindowText, new Point(15, 24));
- // Save the output stream as gif. You can also save it to external file
- string temp = Path.GetTempFileName().Replace("tmp", "gif");
- bmpimg.Save(temp, System.Drawing.Imaging.ImageFormat.Gif);
- PdfPTable pdfFooter; PdfPCell pdfCell1;
- iTextSharp.text.Image signature = iTextSharp.text.Image.GetInstance(temp);
- //Image signature = Image.GetInstance(@"J:\signatures\black.png"); signature.ScaleAbsolute(property.W, property.H);
- pdfFooter = new PdfPTable(1); pdfCell1 = new PdfPCell(signature); pdfCell1.Border = 0; pdfFooter.TotalWidth = 450; pdfFooter.AddCell(pdfCell1);
- pdfFooter.WriteSelectedRows(0, -1, property.X, property.Y, writer.DirectContent);
- }
- private void AddImage(DocumentProperty property, PdfWriter writer)
- {
- PdfPTable pdfFooter; PdfPCell pdfCell1;
- Image signature = Image.GetInstance(property.Value); signature.ScaleToFit(property.W, property.H);
- //Image signature = Image.GetInstance(@"J:\signatures\black.png"); signature.ScaleAbsolute(property.W, property.H);
- pdfFooter = new PdfPTable(1); pdfCell1 = new PdfPCell(signature); pdfCell1.Border = 0; pdfFooter.TotalWidth = 450; pdfFooter.AddCell(pdfCell1);
- pdfFooter.WriteSelectedRows(0, -1, property.X, property.Y, writer.DirectContent);
- }
- private void AddText(DocumentProperty property, PdfWriter writer)
- {
- PdfPTable pdfFooter; PdfPCell pdfCell1;
- 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);
- pdfFooter.WriteSelectedRows(0, -1, property.X, property.Y, writer.DirectContent);
- }
- private void AddParagraph(DocumentProperty property, Document doc)
- {
- for (int x = 0; x < (int)property.Y; x++)
- {
- doc.Add(new AlignedParagraph(" ", (int)property.H, property.Alignment, true));
- }
- doc.Add(new AlignedParagraph(property.Value, (int)property.H, property.Alignment, property.X, true, property.I));
- for (int x = 0; x < (int)property.W; x++)
- {
- doc.Add(new AlignedParagraph(" ", (int)property.H, property.Alignment, true));
- }
- }
- private DocumentProperty ImposeProperty(DataRow client, string text)
- {
- if (client["cm"].ToString() != "")
- {
- text = text.Replace("{CM}", client["cm"].ToString().Trim().Split('_')[1]);
- text = text.Replace("{CM:Phone}", client["cmphone"].ToString().Trim());
- }
- if (text.Contains("[IFCM==\"\"]") && client["cm"].ToString().Trim() != "")
- return null;
- if (text.Contains("[IFCM!=\"\"]") && client["cm"].ToString().Trim() == "")
- return null;
- text = text.Replace("[IFCM!=\"\"]", "");
- text = text.Replace("[IFCM==\"\"]", "");
- if (text.Contains("•")) return new DocumentProperty(text, 1, Font.NORMAL, 0, 0, 12, 30);
- return new DocumentProperty(text, 1, Font.NORMAL, 1, 0, 12);
- }
- } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement