private void generateReport() { try { String equipmentName = ""; document = new Document(PageSize.LETTER); String FilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\\Resources\\" + inspectionType.Text + "_" + DateTime.Today.ToString("yyyy-MM-dd") +".pdf"; PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(FilePath, FileMode.OpenOrCreate)); document.Open(); document.AddTitle("Report"); document.AddSubject("Equipment Report"); document.AddKeywords("Csharp, PDF, iText"); document.AddAuthor(""); document.AddCreator(""); iTextSharp.text.Image pdfLogo = iTextSharp.text.Image.GetInstance(AppDomain.CurrentDomain.BaseDirectory + "\\Resources\\" + "logo.JPG"); pdfLogo.Alignment = iTextSharp.text.Image.ALIGN_RIGHT; pdfLogo.ScaleAbsolute(150, 85); document.Add(pdfLogo); Paragraph preface = new Paragraph("Fire-Alert" + "\n" + "Report of " + inspectionType.Text + "\n", TimesTitle); preface.Alignment = Element.ALIGN_CENTER; document.Add(preface); document.Add(new Paragraph(" ")); #region Inspection table PdfPTable inspectionTable = new PdfPTable(1); if (inspectionType.Text.Contains("Extinguisher")) equipmentName = "Extinguisher"; else if (inspectionType.Text.Contains("Hose")) equipmentName = "FireHoseCabinet"; else if (inspectionType.Text.Contains("Light")) equipmentName = "EmergencyLight"; // Load XML inspection file from resources string url = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\\Resources\\inspection.xml"; XmlDocument doc = new System.Xml.XmlDocument(); doc.Load(url); XmlElement docElement = doc.DocumentElement; // loop through all childNodes XmlNode start = docElement.FirstChild; // franchisee foreach (XmlNode c1 in start) // contracts { foreach (XmlNode c2 in c1.ChildNodes) // addresses { // Skip if not matching contract ID if (Convert.ToInt32(c2.Attributes["id"].InnerText) == Convert.ToInt32(addressBox.SelectedValue)) { Console.WriteLine(Convert.ToInt32(c1.Attributes["id"].InnerText)); Console.WriteLine(Convert.ToInt32(addressBox.SelectedValue)); #region Address info table PdfPTable addrTable = new PdfPTable(4); addrTable.HorizontalAlignment = Element.ALIGN_LEFT; addrTable.TotalWidth = 530f; addrTable.LockedWidth = true; float[] addrWidths = new float[] { 50f, 100f, 50f, 100f }; addrTable.SetWidths(addrWidths); XmlAttributeCollection billTo = c1.ParentNode.Attributes; XmlAttributeCollection location = c2.Attributes; string[] billToAddr = billTo["address"].InnerText.Split(','); addCell(addrTable, "Bill To:", 1, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, billTo["name"].InnerText, 1, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); addCell(addrTable, "Location:", 1, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, location["contact"].InnerText, 1, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); addCell(addrTable, " ", 1, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, billToAddr[0], 1, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); addCell(addrTable, " ", 1, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, location["address"].InnerText, 1, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); addCell(addrTable, " ", 1, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, billToAddr[2] + "," + billToAddr[1], 1, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); addCell(addrTable, " ", 1, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, location["city"].InnerText, 1, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); addCell(addrTable, " ", 1, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, billToAddr[3], 1, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); addCell(addrTable, " ", 1, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, location["postalCode"].InnerText, 1, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); addCell(addrTable, "Tel:", 2, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, new Franchisee().getAddress(Convert.ToInt32(start.Attributes["id"].InnerText))[0], 2, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); String clientInfo = new Client().get(new ClientContract().getClient(addressBox.SelectedValue.ToString())); String[] client = new String[9]; client = clientInfo.Split(','); if (client.Length < 6) client = new String[6] {"", "", "", "", "", "No client found" }; addCell(addrTable, "Contact:", 1, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, client[5], 1, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); addCell(addrTable, "Tel:", 1, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, client[3], 1, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); addCell(addrTable, " ", 4, 4, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); string technicianID = c2.Attributes["InspectorID"].InnerText; string technicianName = new Users().getName(Convert.ToInt32(technicianID)); if (technicianName == null || technicianName == "") technicianName = "Technician Not Found"; addCell(addrTable, "Technician: ", 2, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, technicianName, 2, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); addCell(addrTable, "Date: ", 2, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, DateTime.Now.ToString("MMMM d, yyyy"), 2, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); for (int i = 0; i < addrTable.Rows.Count; i++) for (int j = 0; j < addrTable.Rows[i].GetCells().Length; j++) { if (addrTable.Rows[i].GetCells()[j] == null) continue; if (j % 2 != 0) // Add bottom border to info cells addrTable.Rows[i].GetCells()[j].Border = iTextSharp.text.Rectangle.BOTTOM_BORDER; else // Remove bottom border from titles addrTable.Rows[i].GetCells()[j].Border = iTextSharp.text.Rectangle.NO_BORDER; } #endregion document.Add(addrTable); foreach (XmlNode c3 in c2.ChildNodes) // floors { foreach (XmlNode c4 in c3.ChildNodes) //rooms { bool isFirstEquipment = true; int itemNum = 1; foreach (XmlNode c5 in c4.ChildNodes) // equipment { if (c5.Name.Equals(equipmentName)) { #region Set up header if (isFirstEquipment) // Set up table header based on first piece of equipment { isFirstEquipment = false; inspectionTable = new PdfPTable(c5.Attributes.Count + c5.ChildNodes.Count + 1); inspectionTable.HorizontalAlignment = 0; inspectionTable.TotalWidth = 530f; inspectionTable.LockedWidth = true; float []iWidths = new float[c5.Attributes.Count + c5.ChildNodes.Count + 1]; iWidths[0] = 20f; for (int i = 1; i < iWidths.Length; i++) { if (i < c5.Attributes.Count + 1) iWidths[i] = 35f; else iWidths[i] = 20f; } inspectionTable.SetWidths(iWidths); createHeader(inspectionTable, c5); } #endregion addEquipmentRow(inspectionTable, c5, itemNum); itemNum++; } } } } } } } document.Add(new Paragraph(" ")); document.Add(inspectionTable); #endregion document.Close(); } catch (Exception ex) { MessageBox.Show("Could not display the document because " + ex.ToString()); } }
private void generateStatement() { try { //start creating the PDF //Create a Catalog Dictionary CatalogDict catalogDict = new CatalogDict(); //Create a Page Tree Dictionary PageTreeDict pageTreeDict = new PageTreeDict(); //Create a Font Dictionary - Only the standard fonts Time, Helvetica and courier etc can be created by this method. //See Adobe doco for more info on other fonts FontDict TimesRoman = new FontDict(); FontDict TimesItalic = new FontDict(); FontDict TimesBold = new FontDict(); FontDict Courier = new FontDict(); //Create the info Dictionary InfoDict infoDict = new InfoDict(); Invoice invoice = new Invoice(); //Create the font called Times Roman TimesRoman.CreateFontDict("T1", "Times-Roman"); //Create the font called Times Italic TimesItalic.CreateFontDict("T2", "Times-Italic"); //Create the font called Times Bold TimesBold.CreateFontDict("T3", "Times-Bold"); //Create the font called Courier Courier.CreateFontDict("T4", "Times-Roman"); //Set the info Dictionary. xxx will be the invoice number infoDict.SetInfo("Statement of " + cbClients.Text, "System Generated", "Fire-Alert"); //Create a utility object Utility pdfUtility = new Utility(); String FilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\\Resources\\Statement_" + cbClients.SelectedValue + ".pdf"; //Open a file specifying the file name as the output pdf file //String FilePath = @"C:\Users\Hassan\Desktop\Preview.pdf"; FileStream file = new FileStream(FilePath, FileMode.Create); int size = 0; file.Write(pdfUtility.GetHeader("1.5", out size), 0, size); file.Close(); //Finished the first step //Create a Page Dictionary , this represents a visible page PageDict page = new PageDict(); ContentDict content = new ContentDict(); //The page size object will hold all the page size information //also holds the dictionary objects for font, images etc. //A4 595,842 //Letter 612,792 PageSize pSize = new PageSize(612, 792); //A4 paper portrait in 1/72" measurements pSize.SetMargins(10, 10, 10, 10); //create the page main details page.CreatePage(pageTreeDict.objectNum, pSize); //add a page pageTreeDict.AddPage(page.objectNum); //add the fonts to this page page.AddResource(TimesRoman, content.objectNum); page.AddResource(TimesItalic, content.objectNum); page.AddResource(TimesBold, content.objectNum); page.AddResource(Courier, content.objectNum); //Create a Text And Table Object that presents the text elements in the page TextAndTables textAndtable = new TextAndTables(pSize); //create the reference to an image and the data that represents it String ImagePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\\Resources\\logo.jpg"; //file path to image source ImageDict I1 = new ImageDict(); //new image dictionary object I1.CreateImageDict("I1", ImagePath); //create the object which describes the image page.AddImageResource(I1.PDFImageName, I1, content.objectNum); //create a reference where the PDF can identify which object //describes the image when we want to draw it on the page /* * draw the image to page (add the instruction to the content stream which says draw the image called I1 starting * at X = 269, Y = 20 and with an ACTUAL image size on the page of w = 144 and h = 100) */ PageImages pi = new PageImages(); content.SetStream(pi.ShowImage("I1", 450, 690, 155, 85)); //tell the PDF we want to draw an image called 'I1', where and what size String clientInfo = new Client().get(cbClients.SelectedValue.ToString()); String[] client = new String[9]; client = clientInfo.Split(','); String franchiseeInfo = new Franchisee().get(new ClientContract().getFranchiseeOfClient(cbClients.SelectedValue.ToString())); String[] franchisee = new String[6]; franchisee = franchiseeInfo.Split(','); if (franchisee.Length < 6) { franchisee = new String[6] { "", "", "", "", "", "" } } ; String userInfo = new Users().get(userid.ToString()); String[] user = new String[4]; user = userInfo.Split(','); //Add text to the page textAndtable.AddText(260, 50, "STATEMENT", 20, "T3", Align.LeftAlign); textAndtable.AddText(50, 80, franchisee[5], 16, "T3", Align.LeftAlign); textAndtable.AddText(50, 90, franchisee[0], 10, "T3", Align.LeftAlign); textAndtable.AddText(50, 100, franchisee[2] + ", " + franchisee[3] + ", " + franchisee[4] + " " + franchisee[1], 10, "T3", Align.LeftAlign); textAndtable.AddText(50, 110, user[2], 10, "T3", Align.LeftAlign); // textAndtable.AddText(50, 60, "Total: " + txtTotal.Text, 10, "T3", Align.LeftAlign); textAndtable.AddText(50, 190, client[0], 10, "T3", Align.LeftAlign); textAndtable.AddText(50, 205, client[5], 10, "T4", Align.LeftAlign); textAndtable.AddText(50, 215, client[1], 10, "T4", Align.LeftAlign); textAndtable.AddText(50, 225, client[6] + ", " + client[7] + " " + client[2], 10, "T4", Align.LeftAlign); textAndtable.AddText(50, 235, client[8], 10, "T4", Align.LeftAlign); textAndtable.AddText(430, 150, "PLEASE RETURN THIS PORTION WITH", 8, "T4", Align.LeftAlign); textAndtable.AddText(430, 160, "YOUR PAYMENT", 8, "T4", Align.LeftAlign); textAndtable.AddText(400, 175, client[0], 10, "T3", Align.LeftAlign); textAndtable.AddText(430, 210, "IF PAYING BY INVOICE CHECK", 8, "T4", Align.LeftAlign); textAndtable.AddText(430, 220, "INDIVIDUAL INVOICES PAID", 8, "T4", Align.LeftAlign); textAndtable.AddText(435, 240, "AMOUNT REMITTED __________", 8, "T4", Align.LeftAlign); Align[] alignC = new Align[1]; alignC[0] = Align.CenterAlign; //Specify the color for the cell and the line ColorSpec cellColor = new ColorSpec(255, 255, 255); ColorSpec lineColor = new ColorSpec(1, 1, 1); //Fill in the parameters for the table TableParams table = new TableParams(1, 100); table.yPos = 700; table.xPos = 50; table.rowHeight = 15; //Set the parameters of this table textAndtable.SetParams(table, cellColor, Align.RightAlign, 3); textAndtable.AddRow(false, 10, "T3", alignC, false, "Statement Date"); textAndtable.AddRow(false, 10, "T4", alignC, false, DateTime.Today.ToShortDateString()); //After drawing table and text add them to the page content.SetStream(textAndtable.EndTable(lineColor, true)); table.yPos = 630; table.xPos = 0; //Set the parameters of this table textAndtable.SetParams(table, cellColor, Align.CenterAlign, 3); textAndtable.AddRow(false, 10, "T3", alignC, false, "Statement Date"); textAndtable.AddRow(false, 10, "T4", alignC, false, DateTime.Today.ToShortDateString()); //After drawing table and text add them to the page content.SetStream(textAndtable.EndTable(lineColor, true)); textAndtable.AddText(50, 275, "Page: 1", 10, "T4", Align.LeftAlign); Align[] alignC1 = new Align[5]; alignC1[0] = Align.CenterAlign; alignC1[1] = Align.CenterAlign; alignC1[2] = Align.CenterAlign; alignC1[3] = Align.CenterAlign; alignC1[4] = Align.CenterAlign; Align[] alignC2 = new Align[5]; alignC2[0] = Align.LeftAlign; alignC2[1] = Align.LeftAlign; alignC2[2] = Align.RightAlign; alignC2[3] = Align.RightAlign; alignC2[4] = Align.CenterAlign; //Fill in the parameters for the table TableParams table2 = new TableParams(5, 150, 90, 90, 90, 40); table2.yPos = 510; table2.xPos = 37; table2.rowHeight = 15; //Set the parameters of this table textAndtable.SetParams(table2, cellColor, Align.LeftAlign, 3); // MessageBox.Show("\u221A"); textAndtable.AddRow(false, 10, "T3", alignC1, false, "Statement Date", "Invoice No.", "Balance", "Total Due", ""); String[] rows = new Invoice().getOutstandingInvoices(cbClients.SelectedValue.ToString()); double total = 0; double interestTotal = 0; double interest = 0; double below30 = 0; double between3160 = 0; double over60 = 0; DateTime interestDate = DateTime.Today.Date.AddDays(-30); for (int i = 0; i < rows.Length; i++) { String[] cells = new String[3]; cells = rows[i].Split(','); total += Convert.ToDouble(cells[2]); if (Convert.ToDateTime(cells[0]) < interestDate) { interestTotal += Convert.ToDouble(cells[2]); } if (Convert.ToDateTime(cells[0]) > interestDate) { below30 += Convert.ToDouble(cells[2]); } if (Convert.ToDateTime(cells[0]) < DateTime.Today.Date.AddDays(-30) && Convert.ToDateTime(cells[0]) >= DateTime.Today.Date.AddDays(-60)) { between3160 += Convert.ToDouble(cells[2]); } if (Convert.ToDateTime(cells[0]) < DateTime.Today.Date.AddDays(-60)) { over60 += Convert.ToDouble(cells[2]); } Console.WriteLine(cells[0] + " " + cells[1]); textAndtable.AddRow(false, 10, "T4", alignC2, false, cells[0], cells[1], "$" + cells[2], "$" + total.ToString(), ""); } interest = interestTotal * 0.02; total += interest; below30 += interest; textAndtable.AddRow(false, 10, "T4", alignC2, false, "2% interest on balance over 30 days", "", "$" + (interest).ToString(), "$" + total.ToString(), ""); //After drawing table and text add them to the page content.SetStream(textAndtable.EndTable(lineColor, true)); Align[] alignC3 = new Align[5]; alignC3[0] = Align.LeftAlign; alignC3[1] = Align.RightAlign; alignC3[2] = Align.RightAlign; alignC3[3] = Align.RightAlign; alignC3[4] = Align.CenterAlign; //Fill in the parameters for the table 120,100,100,100,40 = 460 TableParams table3 = new TableParams(5, 100, 90, 90, 90, 90); table3.yPos = 100; table3.xPos = 37; table3.rowHeight = 15; //Set the parameters of this table textAndtable.SetParams(table3, cellColor, Align.LeftAlign, 3); // MessageBox.Show("\u221A"); textAndtable.AddRow(false, 10, "T3", alignC3, false, "Age", "Current ", "31-60 ", "Over 60 ", "Total"); textAndtable.AddRow(false, 10, "T3", alignC3, false, "Amount", below30.ToString(), between3160.ToString(), over60.ToString(), total.ToString()); content.SetStream(textAndtable.EndTable(lineColor, true)); content.SetStream(textAndtable.EndText()); //All done - send the information to the PDF file size = 0; file = new FileStream(FilePath, FileMode.Append); file.Write(page.GetPageDict(file.Length, out size), 0, size); file.Write(content.GetContentDict(file.Length, out size), 0, size); file.Close(); file = new FileStream(FilePath, FileMode.Append); file.Write(catalogDict.GetCatalogDict(pageTreeDict.objectNum, file.Length, out size), 0, size); file.Write(pageTreeDict.GetPageTree(file.Length, out size), 0, size); file.Write(TimesRoman.GetFontDict(file.Length, out size), 0, size); file.Write(TimesItalic.GetFontDict(file.Length, out size), 0, size); file.Write(TimesBold.GetFontDict(file.Length, out size), 0, size); file.Write(Courier.GetFontDict(file.Length, out size), 0, size); //write image dict file.Write(I1.GetImageDict(file.Length, out size), 0, size); file.Write(infoDict.GetInfoDict(file.Length, out size), 0, size); file.Write(pdfUtility.CreateXrefTable(file.Length, out size), 0, size); file.Write(pdfUtility.GetTrailer(catalogDict.objectNum, infoDict.objectNum, out size), 0, size); file.Close(); } catch (Exception ex) { MessageBox.Show("Could not display the document because " + ex.ToString()); } }
private void generateReport() { try { String equipmentName = ""; document = new Document(PageSize.LETTER); String FilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\\Resources\\" + inspectionType.Text + "_" + DateTime.Today.ToString("yyyy-MM-dd") + ".pdf"; PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(FilePath, FileMode.OpenOrCreate)); document.Open(); document.AddTitle("Report"); document.AddSubject("Equipment Report"); document.AddKeywords("Csharp, PDF, iText"); document.AddAuthor(""); document.AddCreator(""); iTextSharp.text.Image pdfLogo = iTextSharp.text.Image.GetInstance(AppDomain.CurrentDomain.BaseDirectory + "\\Resources\\" + "logo.JPG"); pdfLogo.Alignment = iTextSharp.text.Image.ALIGN_RIGHT; pdfLogo.ScaleAbsolute(150, 85); document.Add(pdfLogo); Paragraph preface = new Paragraph("Fire-Alert" + "\n" + "Report of " + inspectionType.Text + "\n", TimesTitle); preface.Alignment = Element.ALIGN_CENTER; document.Add(preface); document.Add(new Paragraph(" ")); #region Inspection table PdfPTable inspectionTable = new PdfPTable(1); if (inspectionType.Text.Contains("Extinguisher")) { equipmentName = "Extinguisher"; } else if (inspectionType.Text.Contains("Hose")) { equipmentName = "FireHoseCabinet"; } else if (inspectionType.Text.Contains("Light")) { equipmentName = "EmergencyLight"; } // Load XML inspection file from resources string url = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\\Resources\\inspection.xml"; XmlDocument doc = new System.Xml.XmlDocument(); doc.Load(url); XmlElement docElement = doc.DocumentElement; // loop through all childNodes XmlNode start = docElement.FirstChild; // franchisee foreach (XmlNode c1 in start) // contracts { foreach (XmlNode c2 in c1.ChildNodes) // addresses { // Skip if not matching contract ID if (Convert.ToInt32(c2.Attributes["id"].InnerText) == Convert.ToInt32(addressBox.SelectedValue)) { Console.WriteLine(Convert.ToInt32(c1.Attributes["id"].InnerText)); Console.WriteLine(Convert.ToInt32(addressBox.SelectedValue)); #region Address info table PdfPTable addrTable = new PdfPTable(4); addrTable.HorizontalAlignment = Element.ALIGN_LEFT; addrTable.TotalWidth = 530f; addrTable.LockedWidth = true; float[] addrWidths = new float[] { 50f, 100f, 50f, 100f }; addrTable.SetWidths(addrWidths); XmlAttributeCollection billTo = c1.ParentNode.Attributes; XmlAttributeCollection location = c2.Attributes; string[] billToAddr = billTo["address"].InnerText.Split(','); addCell(addrTable, "Bill To:", 1, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, billTo["name"].InnerText, 1, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); addCell(addrTable, "Location:", 1, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, location["contact"].InnerText, 1, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); addCell(addrTable, " ", 1, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, billToAddr[0], 1, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); addCell(addrTable, " ", 1, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, location["address"].InnerText, 1, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); addCell(addrTable, " ", 1, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, billToAddr[2] + "," + billToAddr[1], 1, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); addCell(addrTable, " ", 1, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, location["city"].InnerText, 1, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); addCell(addrTable, " ", 1, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, billToAddr[3], 1, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); addCell(addrTable, " ", 1, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, location["postalCode"].InnerText, 1, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); addCell(addrTable, "Tel:", 2, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, new Franchisee().getAddress(Convert.ToInt32(start.Attributes["id"].InnerText))[0], 2, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); String clientInfo = new Client().get(new ClientContract().getClient(addressBox.SelectedValue.ToString())); String[] client = new String[9]; client = clientInfo.Split(','); if (client.Length < 6) { client = new String[6] { "", "", "", "", "", "No client found" } } ; addCell(addrTable, "Contact:", 1, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, client[5], 1, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); addCell(addrTable, "Tel:", 1, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, client[3], 1, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); addCell(addrTable, " ", 4, 4, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); string technicianID = c2.Attributes["InspectorID"].InnerText; string technicianName = new Users().getName(Convert.ToInt32(technicianID)); if (technicianName == null || technicianName == "") { technicianName = "Technician Not Found"; } addCell(addrTable, "Technician: ", 2, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, technicianName, 2, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); addCell(addrTable, "Date: ", 2, 1, 0, BaseColor.WHITE, Times, PdfPCell.ALIGN_RIGHT); addCell(addrTable, DateTime.Now.ToString("MMMM d, yyyy"), 2, 1, 0, BaseColor.WHITE, TimesRegular, PdfPCell.ALIGN_LEFT); for (int i = 0; i < addrTable.Rows.Count; i++) { for (int j = 0; j < addrTable.Rows[i].GetCells().Length; j++) { if (addrTable.Rows[i].GetCells()[j] == null) { continue; } if (j % 2 != 0) // Add bottom border to info cells { addrTable.Rows[i].GetCells()[j].Border = iTextSharp.text.Rectangle.BOTTOM_BORDER; } else // Remove bottom border from titles { addrTable.Rows[i].GetCells()[j].Border = iTextSharp.text.Rectangle.NO_BORDER; } } } #endregion document.Add(addrTable); foreach (XmlNode c3 in c2.ChildNodes) // floors { foreach (XmlNode c4 in c3.ChildNodes) //rooms { bool isFirstEquipment = true; int itemNum = 1; foreach (XmlNode c5 in c4.ChildNodes) // equipment { if (c5.Name.Equals(equipmentName)) { #region Set up header if (isFirstEquipment) // Set up table header based on first piece of equipment { isFirstEquipment = false; inspectionTable = new PdfPTable(c5.Attributes.Count + c5.ChildNodes.Count + 1); inspectionTable.HorizontalAlignment = 0; inspectionTable.TotalWidth = 530f; inspectionTable.LockedWidth = true; float [] iWidths = new float[c5.Attributes.Count + c5.ChildNodes.Count + 1]; iWidths[0] = 20f; for (int i = 1; i < iWidths.Length; i++) { if (i < c5.Attributes.Count + 1) { iWidths[i] = 35f; } else { iWidths[i] = 20f; } } inspectionTable.SetWidths(iWidths); createHeader(inspectionTable, c5); } #endregion addEquipmentRow(inspectionTable, c5, itemNum); itemNum++; } } } } } } } document.Add(new Paragraph(" ")); document.Add(inspectionTable); #endregion document.Close(); } catch (Exception ex) { MessageBox.Show("Could not display the document because " + ex.ToString()); } }
private void generateStatement() { try { //start creating the PDF //Create a Catalog Dictionary CatalogDict catalogDict = new CatalogDict(); //Create a Page Tree Dictionary PageTreeDict pageTreeDict = new PageTreeDict(); //Create a Font Dictionary - Only the standard fonts Time, Helvetica and courier etc can be created by this method. //See Adobe doco for more info on other fonts FontDict TimesRoman = new FontDict(); FontDict TimesItalic = new FontDict(); FontDict TimesBold = new FontDict(); FontDict Courier = new FontDict(); //Create the info Dictionary InfoDict infoDict = new InfoDict(); Invoice invoice = new Invoice(); //Create the font called Times Roman TimesRoman.CreateFontDict("T1", "Times-Roman"); //Create the font called Times Italic TimesItalic.CreateFontDict("T2", "Times-Italic"); //Create the font called Times Bold TimesBold.CreateFontDict("T3", "Times-Bold"); //Create the font called Courier Courier.CreateFontDict("T4", "Times-Roman"); //Set the info Dictionary. xxx will be the invoice number infoDict.SetInfo("Statement of " + cbClients.Text, "System Generated", "Fire-Alert"); //Create a utility object Utility pdfUtility = new Utility(); String FilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\\Resources\\Statement_"+cbClients.SelectedValue+".pdf"; //Open a file specifying the file name as the output pdf file //String FilePath = @"C:\Users\Hassan\Desktop\Preview.pdf"; FileStream file = new FileStream(FilePath, FileMode.Create); int size = 0; file.Write(pdfUtility.GetHeader("1.5", out size), 0, size); file.Close(); //Finished the first step //Create a Page Dictionary , this represents a visible page PageDict page = new PageDict(); ContentDict content = new ContentDict(); //The page size object will hold all the page size information //also holds the dictionary objects for font, images etc. //A4 595,842 //Letter 612,792 PageSize pSize = new PageSize(612, 792); //A4 paper portrait in 1/72" measurements pSize.SetMargins(10, 10, 10, 10); //create the page main details page.CreatePage(pageTreeDict.objectNum, pSize); //add a page pageTreeDict.AddPage(page.objectNum); //add the fonts to this page page.AddResource(TimesRoman, content.objectNum); page.AddResource(TimesItalic, content.objectNum); page.AddResource(TimesBold, content.objectNum); page.AddResource(Courier, content.objectNum); //Create a Text And Table Object that presents the text elements in the page TextAndTables textAndtable = new TextAndTables(pSize); //create the reference to an image and the data that represents it String ImagePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\\Resources\\logo.jpg"; //file path to image source ImageDict I1 = new ImageDict(); //new image dictionary object I1.CreateImageDict("I1", ImagePath); //create the object which describes the image page.AddImageResource(I1.PDFImageName, I1, content.objectNum); //create a reference where the PDF can identify which object //describes the image when we want to draw it on the page /* * draw the image to page (add the instruction to the content stream which says draw the image called I1 starting * at X = 269, Y = 20 and with an ACTUAL image size on the page of w = 144 and h = 100) */ PageImages pi = new PageImages(); content.SetStream(pi.ShowImage("I1", 450, 690, 155, 85)); //tell the PDF we want to draw an image called 'I1', where and what size String clientInfo = new Client().get(cbClients.SelectedValue.ToString()); String[] client = new String[9]; client = clientInfo.Split(','); String franchiseeInfo = new Franchisee().get(new ClientContract().getFranchiseeOfClient(cbClients.SelectedValue.ToString())); String[] franchisee = new String[6]; franchisee = franchiseeInfo.Split(','); if (franchisee.Length < 6) franchisee = new String[6] { "", "", "", "", "", "" }; String userInfo = new Users().get(userid.ToString()); String[] user = new String[4]; user = userInfo.Split(','); //Add text to the page textAndtable.AddText(260, 50, "STATEMENT", 20, "T3", Align.LeftAlign); textAndtable.AddText(50, 80, franchisee[5], 16, "T3", Align.LeftAlign); textAndtable.AddText(50, 90, franchisee[0], 10, "T3", Align.LeftAlign); textAndtable.AddText(50, 100, franchisee[2] + ", " + franchisee[3] + ", " + franchisee[4] + " " + franchisee[1], 10, "T3", Align.LeftAlign); textAndtable.AddText(50, 110, user[2], 10, "T3", Align.LeftAlign); // textAndtable.AddText(50, 60, "Total: " + txtTotal.Text, 10, "T3", Align.LeftAlign); textAndtable.AddText(50, 190, client[0], 10, "T3", Align.LeftAlign); textAndtable.AddText(50, 205, client[5], 10, "T4", Align.LeftAlign); textAndtable.AddText(50, 215, client[1], 10, "T4", Align.LeftAlign); textAndtable.AddText(50, 225, client[6] + ", " + client[7] + " " + client[2], 10, "T4", Align.LeftAlign); textAndtable.AddText(50, 235, client[8], 10, "T4", Align.LeftAlign); textAndtable.AddText(430, 150, "PLEASE RETURN THIS PORTION WITH", 8, "T4", Align.LeftAlign); textAndtable.AddText(430, 160, "YOUR PAYMENT", 8, "T4", Align.LeftAlign); textAndtable.AddText(400, 175, client[0], 10, "T3", Align.LeftAlign); textAndtable.AddText(430, 210, "IF PAYING BY INVOICE CHECK", 8, "T4", Align.LeftAlign); textAndtable.AddText(430, 220, "INDIVIDUAL INVOICES PAID", 8, "T4", Align.LeftAlign); textAndtable.AddText(435, 240, "AMOUNT REMITTED __________", 8, "T4", Align.LeftAlign); Align[] alignC = new Align[1]; alignC[0] = Align.CenterAlign; //Specify the color for the cell and the line ColorSpec cellColor = new ColorSpec(255, 255, 255); ColorSpec lineColor = new ColorSpec(1, 1, 1); //Fill in the parameters for the table TableParams table = new TableParams(1, 100); table.yPos = 700; table.xPos = 50; table.rowHeight = 15; //Set the parameters of this table textAndtable.SetParams(table, cellColor, Align.RightAlign, 3); textAndtable.AddRow(false, 10, "T3", alignC, false, "Statement Date"); textAndtable.AddRow(false, 10, "T4", alignC, false, DateTime.Today.ToShortDateString()); //After drawing table and text add them to the page content.SetStream(textAndtable.EndTable(lineColor, true)); table.yPos = 630; table.xPos = 0; //Set the parameters of this table textAndtable.SetParams(table, cellColor, Align.CenterAlign, 3); textAndtable.AddRow(false, 10, "T3", alignC, false, "Statement Date"); textAndtable.AddRow(false, 10, "T4", alignC, false, DateTime.Today.ToShortDateString()); //After drawing table and text add them to the page content.SetStream(textAndtable.EndTable(lineColor, true)); textAndtable.AddText(50, 275, "Page: 1", 10, "T4", Align.LeftAlign); Align[] alignC1 = new Align[5]; alignC1[0] = Align.CenterAlign; alignC1[1] = Align.CenterAlign; alignC1[2] = Align.CenterAlign; alignC1[3] = Align.CenterAlign; alignC1[4] = Align.CenterAlign; Align[] alignC2 = new Align[5]; alignC2[0] = Align.LeftAlign; alignC2[1] = Align.LeftAlign; alignC2[2] = Align.RightAlign; alignC2[3] = Align.RightAlign; alignC2[4] = Align.CenterAlign; //Fill in the parameters for the table TableParams table2 = new TableParams(5, 150,90,90,90,40); table2.yPos = 510; table2.xPos = 37; table2.rowHeight = 15; //Set the parameters of this table textAndtable.SetParams(table2, cellColor, Align.LeftAlign, 3); // MessageBox.Show("\u221A"); textAndtable.AddRow(false, 10, "T3", alignC1, false, "Statement Date", "Invoice No.", "Balance", "Total Due", ""); String[] rows = new Invoice().getOutstandingInvoices(cbClients.SelectedValue.ToString()); double total = 0; double interestTotal = 0; double interest = 0; double below30 = 0; double between3160 = 0; double over60 = 0; DateTime interestDate = DateTime.Today.Date.AddDays(-30); for (int i = 0; i < rows.Length; i++) { String[] cells = new String[3]; cells = rows[i].Split(','); total += Convert.ToDouble(cells[2]); if (Convert.ToDateTime(cells[0]) < interestDate) interestTotal += Convert.ToDouble(cells[2]); if (Convert.ToDateTime(cells[0]) > interestDate) below30 += Convert.ToDouble(cells[2]); if (Convert.ToDateTime(cells[0]) < DateTime.Today.Date.AddDays(-30) && Convert.ToDateTime(cells[0]) >= DateTime.Today.Date.AddDays(-60)) between3160 += Convert.ToDouble(cells[2]); if (Convert.ToDateTime(cells[0]) < DateTime.Today.Date.AddDays(-60)) over60 += Convert.ToDouble(cells[2]); Console.WriteLine(cells[0] + " " + cells[1]); textAndtable.AddRow(false, 10, "T4", alignC2, false, cells[0], cells[1], "$" + cells[2], "$" + total.ToString(), ""); } interest = interestTotal * 0.02; total += interest; below30 += interest; textAndtable.AddRow(false, 10, "T4", alignC2, false, "2% interest on balance over 30 days", "", "$" + (interest).ToString(), "$" + total.ToString(), ""); //After drawing table and text add them to the page content.SetStream(textAndtable.EndTable(lineColor, true)); Align[] alignC3 = new Align[5]; alignC3[0] = Align.LeftAlign; alignC3[1] = Align.RightAlign; alignC3[2] = Align.RightAlign; alignC3[3] = Align.RightAlign; alignC3[4] = Align.CenterAlign; //Fill in the parameters for the table 120,100,100,100,40 = 460 TableParams table3 = new TableParams(5, 100, 90, 90, 90, 90); table3.yPos = 100; table3.xPos = 37; table3.rowHeight = 15; //Set the parameters of this table textAndtable.SetParams(table3, cellColor, Align.LeftAlign, 3); // MessageBox.Show("\u221A"); textAndtable.AddRow(false, 10, "T3", alignC3, false, "Age", "Current ", "31-60 ", "Over 60 ", "Total"); textAndtable.AddRow(false, 10, "T3", alignC3, false, "Amount", below30.ToString(), between3160.ToString(), over60.ToString(), total.ToString()); content.SetStream(textAndtable.EndTable(lineColor, true)); content.SetStream(textAndtable.EndText()); //All done - send the information to the PDF file size = 0; file = new FileStream(FilePath, FileMode.Append); file.Write(page.GetPageDict(file.Length, out size), 0, size); file.Write(content.GetContentDict(file.Length, out size), 0, size); file.Close(); file = new FileStream(FilePath, FileMode.Append); file.Write(catalogDict.GetCatalogDict(pageTreeDict.objectNum, file.Length, out size), 0, size); file.Write(pageTreeDict.GetPageTree(file.Length, out size), 0, size); file.Write(TimesRoman.GetFontDict(file.Length, out size), 0, size); file.Write(TimesItalic.GetFontDict(file.Length, out size), 0, size); file.Write(TimesBold.GetFontDict(file.Length, out size), 0, size); file.Write(Courier.GetFontDict(file.Length, out size), 0, size); //write image dict file.Write(I1.GetImageDict(file.Length, out size), 0, size); file.Write(infoDict.GetInfoDict(file.Length, out size), 0, size); file.Write(pdfUtility.CreateXrefTable(file.Length, out size), 0, size); file.Write(pdfUtility.GetTrailer(catalogDict.objectNum, infoDict.objectNum, out size), 0, size); file.Close(); } catch (Exception ex) { MessageBox.Show("Could not display the document because " + ex.ToString()); } }