Advertisement
Guest User

PDF Generation

a guest
Dec 3rd, 2012
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.47 KB | None | 0 0
  1.         private void GenerateDocuments()
  2.         {
  3.             if (_haveTax)
  4.             {
  5.                 ActGenerator ag = new ActGenerator(this);
  6.                 InvoiceGenerator ig = new InvoiceGenerator(this);
  7.                 _act = ag.Generate();
  8.                 _invoice = ig.Generate();
  9.             }
  10.             else
  11.             {
  12.                 NoticeGenerator ng = new NoticeGenerator(this);
  13.                 _notice = ng.Generate();
  14.             }
  15.         }
  16.  
  17.  
  18. using System;
  19. using System.Globalization;
  20. using System.IO;
  21. using System.Linq;
  22. using System.Windows.Forms;
  23. using iTextSharp.text;
  24. using iTextSharp.text.pdf;
  25.  
  26. namespace PASSS
  27. {
  28.     class ActGenerator : IDocumentGenerator
  29.     {
  30.         private readonly DocumentHandler _handler;
  31.  
  32.         // Fonts
  33.         private static BaseFont _baseTahoma;
  34.         private static Font _tahomaBold;
  35.         private static Font _tahoma;
  36.         private static Font _tahoma16Bold;
  37.         private static Font _tahomaSmall;
  38.  
  39.         // Files
  40.         private iTextSharp.text.Document _actDoc;
  41.         private PdfWriter _actWriter;
  42.  
  43.         // Overall sum row
  44.         private PdfPRow _overallSumRow;
  45.  
  46.         public ActGenerator(DocumentHandler handler)
  47.         {
  48.             _handler = handler;
  49.  
  50.             PrepareFonts();
  51.         }
  52.  
  53.         // Prepare fonts
  54.         private static void PrepareFonts()
  55.         {
  56.             _baseTahoma = BaseFont.CreateFont("c:/windows/fonts/tahoma.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  57.             _tahomaBold = new Font(_baseTahoma, 10, Font.BOLD);
  58.             _tahoma = new Font(_baseTahoma, 10, Font.NORMAL);
  59.             _tahoma16Bold = new Font(_baseTahoma, 18, Font.BOLD);
  60.             _tahomaSmall = new Font(_baseTahoma, 8, Font.NORMAL);
  61.         }
  62.  
  63.         public FileInfo Generate()
  64.         {
  65.             DateTime dateOfDoc = _handler.DocData.DocumentDate;
  66.             string actFileName = Settings.Default.PDFPath + "\\" + "Бонус_№_" + _handler.DocData.DocumentNum + "_АКТ_от_" + dateOfDoc.ToString("dd.MM.yyyy") + ".pdf";
  67.  
  68.             try
  69.             {
  70.                 _actDoc = new iTextSharp.text.Document(PageSize.A4, 45, 45, 45, 45);
  71.                 _actWriter = PdfWriter.GetInstance(_actDoc, new FileStream(actFileName, FileMode.Create));
  72.             }
  73.             catch (IOException)
  74.             {
  75.                 MessageBox.Show(Resources.DocumentIsOpenedError, Resources.ErrorHeaderForDialogs, MessageBoxButtons.OK, MessageBoxIcon.Error);
  76.                 return new FileInfo(actFileName);
  77.             }
  78.  
  79.             string header = "Акт  № " + _handler.DocData.DocumentNum + " от " + dateOfDoc.ToString("dd.MM.yyyy");
  80.  
  81.             HeaderFooter hfEvent = new HeaderFooter();
  82.             hfEvent.SetHeader(header);
  83.             _actWriter.PageEvent = hfEvent;
  84.             _actDoc.Open();
  85.  
  86.             // Globus Juridical Address
  87.             AddGlobusJurSection();
  88.  
  89.             // Title
  90.             Paragraph title = new Paragraph();
  91.             title.SpacingBefore = 10;
  92.             title.SpacingAfter = 10;
  93.             title.Font = _tahoma16Bold;
  94.             title.Alignment = Element.ALIGN_CENTER;
  95.             title.Add(header);
  96.             _actDoc.Add(title);
  97.  
  98.             // Debitor and Kreditor numbers
  99.             AddKreDebNumbers();
  100.  
  101.             // Retoure positions table
  102.             AddPositionsTable();
  103.  
  104.             if (_actWriter.GetVerticalPosition(true) < 100)
  105.             {
  106.                 _actDoc.NewPage();
  107.                 AddInvoiceEmptyTableHeader();
  108.                 _actDoc.Add(Chunk.NEWLINE);
  109.             }
  110.  
  111.             // Sum by words and agreement
  112.             AddSumAndAgreement();
  113.  
  114.             // Footer
  115.             AddInvoiceSignatures();
  116.  
  117.             AddGlobusBankDetails();
  118.  
  119.             // Save document
  120.             _actDoc.Close();
  121.  
  122.             return new FileInfo(actFileName);
  123.         }
  124.  
  125.         private void AddGlobusJurSection()
  126.         {
  127.             Paragraph globusJurInfo = new Paragraph();
  128.             globusJurInfo.Leading = 10;
  129.             globusJurInfo.Add(new Phrase("OOO", _tahomaBold));
  130.             globusJurInfo.Add(Chunk.NEWLINE);
  131.             globusJurInfo.Add(new Phrase(_handler.GlobusJurAddress, _tahomaBold));
  132.             _actDoc.Add(globusJurInfo);
  133.         }
  134.  
  135.         private void AddKreDebNumbers()
  136.         {
  137.             Paragraph kredDebInfo = new Paragraph();
  138.             kredDebInfo.Leading = 10;
  139.             kredDebInfo.Add(new Phrase("Заказчик: " + _handler.DocData.Bills.Select(bill => bill.SupplierName).FirstOrDefault(), _tahoma));
  140.             kredDebInfo.Add(Chunk.NEWLINE);
  141.             kredDebInfo.Add(new Phrase("Номер кредитора: " + _handler.DocData.SupplierNum, _tahoma));
  142.             kredDebInfo.Add(Chunk.NEWLINE);
  143.             kredDebInfo.Add(new Phrase("Номер дебитора: " + _handler.DocData.Bills.Select(bill => bill.DebitorNum).FirstOrDefault(), _tahoma));
  144.             _actDoc.Add(kredDebInfo);
  145.         }
  146.  
  147.         private void AddPositionsTable()
  148.         {
  149.             PdfPTable table = new PdfPTable(6);
  150.             table.WidthPercentage = 100;
  151.             table.SpacingBefore = 10;
  152.             table.SpacingAfter = 10;
  153.             float[] colWidths = new[] { 0.3f, 2.1f, 0.5f, 0.5f, 1.0f, 1.0f };
  154.             table.SetWidths(colWidths);
  155.  
  156.             table.AddCell(VariousCells.GetCell("№", _tahomaBold));
  157.             table.AddCell(VariousCells.GetCell("Наименование работы (услуги)", _tahomaBold));
  158.             table.AddCell(VariousCells.GetCell("Ед. изм.", _tahomaBold));
  159.             table.AddCell(VariousCells.GetCell("Количество", _tahomaBold));
  160.             table.AddCell(VariousCells.GetCell("Цена", _tahomaBold));
  161.             table.AddCell(VariousCells.GetCell("Сумма", _tahomaBold));
  162.  
  163.             int rowCounter = 1;
  164.  
  165.             foreach (Bonus bill in _handler.BonusList)
  166.             {
  167.                 table.AddCell(VariousCells.GetCell(rowCounter++.ToString(CultureInfo.InvariantCulture), _tahoma));
  168.                 table.AddCell(VariousCells.GetCell(bill.Description, _tahoma));
  169.                 table.AddCell(VariousCells.GetCell("усл.", _tahoma));
  170.                 table.AddCell(VariousCells.GetCell(bill.Quantity, "{0:#,##0.00}", _tahoma));
  171.                 table.AddCell(VariousCells.GetCell(bill.Price, "{0:#,##0.00}", _tahoma));
  172.                 table.AddCell(VariousCells.GetCell(bill.Sum, "{0:#,##0.00}", _tahomaBold));
  173.             }
  174.  
  175.             PdfPCell sumLabelCell = VariousCells.GetCell("Итого:", Element.ALIGN_RIGHT, Element.ALIGN_MIDDLE, 5, 1, _tahomaBold);
  176.             sumLabelCell.Border = Rectangle.NO_BORDER;
  177.             table.AddCell(sumLabelCell);
  178.  
  179.             PdfPCell sumCell = VariousCells.GetCell(_handler.Sum, "{0:#,##0.00}", _tahomaBold);
  180.             table.AddCell(sumCell);
  181.  
  182.             PdfPCell taxLabelCell = VariousCells.GetCell("Итого НДС:", Element.ALIGN_RIGHT, Element.ALIGN_MIDDLE, 5, 1, _tahomaBold);
  183.             taxLabelCell.Border = Rectangle.NO_BORDER;
  184.             table.AddCell(taxLabelCell);
  185.  
  186.             PdfPCell taxCell = VariousCells.GetCell(_handler.Tax, "{0:#,##0.00}", _tahomaBold);
  187.             table.AddCell(taxCell);
  188.  
  189.             PdfPCell overallSumLabelCell = VariousCells.GetCell("Всего (с учетом НДС):", Element.ALIGN_RIGHT, Element.ALIGN_MIDDLE, 5, 1, _tahomaBold);
  190.             overallSumLabelCell.Border = Rectangle.NO_BORDER;
  191.             table.AddCell(overallSumLabelCell);
  192.  
  193.             PdfPCell overallSumCell = VariousCells.GetCell(_handler.OverallSum, "{0:#,##0.00}", _tahomaBold);
  194.             table.AddCell(overallSumCell);
  195.  
  196.             _overallSumRow = table.GetRow(table.Size - 1);
  197.  
  198.             _actDoc.Add(table);
  199.         }
  200.  
  201.         private void AddInvoiceEmptyTableHeader()
  202.         {
  203.             PdfPTable table = new PdfPTable(6);
  204.             table.WidthPercentage = 100;
  205.             table.SpacingBefore = 10;
  206.             table.SpacingAfter = 10;
  207.             float[] colWidths = new[] { 0.3f, 2.1f, 0.5f, 0.5f, 1.0f, 1.0f };
  208.             table.SetWidths(colWidths);
  209.  
  210.             table.AddCell(VariousCells.GetCell("№", _tahomaBold));
  211.             table.AddCell(VariousCells.GetCell("Наименование работы (услуги)", _tahomaBold));
  212.             table.AddCell(VariousCells.GetCell("Ед. изм.", _tahomaBold));
  213.             table.AddCell(VariousCells.GetCell("Количество", _tahomaBold));
  214.             table.AddCell(VariousCells.GetCell("Цена", _tahomaBold));
  215.             table.AddCell(VariousCells.GetCell("Сумма", _tahomaBold));
  216.  
  217.             table.Rows.Add(_overallSumRow);
  218.  
  219.             _actDoc.Add(table);
  220.         }
  221.  
  222.         private void AddSumAndAgreement()
  223.         {
  224.             Paragraph sumAndAgreement = new Paragraph();
  225.             sumAndAgreement.Leading = 10;
  226.             sumAndAgreement.Add(new Phrase("Всего оказано услуг на сумму: " + _handler.MakeStringSum(_handler.OverallSum) + " ,", _tahomaBold));
  227.             sumAndAgreement.Add(Chunk.NEWLINE);
  228.             sumAndAgreement.Add(new Phrase("в т.ч.: НДС - " + _handler.MakeStringSum(_handler.Tax), _tahomaBold));
  229.             sumAndAgreement.Add(Chunk.NEWLINE);
  230.             sumAndAgreement.Add(new Phrase("Вышеперечисленные услуги выполнены полностью и в срок.", _tahomaBold));
  231.             sumAndAgreement.Add(Chunk.NEWLINE);
  232.             sumAndAgreement.Add(new Phrase("Заказчик претензий по объему, качеству и срокам оказания услуг не имеет.", _tahomaBold));
  233.             _actDoc.Add(sumAndAgreement);
  234.         }
  235.  
  236.         private void AddInvoiceSignatures()
  237.         {
  238.             PdfPTable table = new PdfPTable(3);
  239.             table.SpacingBefore = 20;
  240.             table.WidthPercentage = 100;
  241.             float[] colWidths = new[] { 1.0f, 0.2f, 1.0f };
  242.             table.SetWidths(colWidths);
  243.  
  244.             PdfPCell emptyCell = VariousCells.GetEmptyCell(_tahoma);
  245.             emptyCell.Border = Rectangle.NO_BORDER;
  246.  
  247.             PdfPCell cell = VariousCells.GetCell("Исполнитель:", Element.ALIGN_LEFT, Element.ALIGN_MIDDLE, _tahoma);
  248.             cell.Border = Rectangle.NO_BORDER;
  249.             table.AddCell(cell);
  250.             table.AddCell(emptyCell);
  251.             cell = VariousCells.GetCell("Заказчик:", Element.ALIGN_LEFT, Element.ALIGN_MIDDLE, _tahoma);
  252.             cell.Border = Rectangle.NO_BORDER;
  253.             table.AddCell(cell);
  254.             cell = VariousCells.GetCell("".PadLeft(50) + "подпись", Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, _tahomaSmall);
  255.             cell.Border = Rectangle.NO_BORDER;
  256.             table.AddCell(cell);
  257.             table.AddCell(emptyCell);
  258.             cell = VariousCells.GetCell("".PadLeft(50) + "подпись", Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, _tahomaSmall);
  259.             cell.Border = Rectangle.NO_BORDER;
  260.             table.AddCell(cell);
  261.             cell = VariousCells.GetCell("М.П.", Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, _tahoma);
  262.             cell.Border = Rectangle.NO_BORDER;
  263.             table.AddCell(cell);
  264.             table.AddCell(emptyCell);
  265.             cell = VariousCells.GetCell("М.П.", Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, _tahoma);
  266.             cell.Border = Rectangle.NO_BORDER;
  267.             table.AddCell(cell);
  268.  
  269.             _actDoc.Add(table);
  270.         }
  271.  
  272.         private void AddGlobusBankDetails()
  273.         {
  274.             Paragraph bankDetails = new Paragraph();
  275.             bankDetails.Leading = 10;
  276.             bankDetails.Add(new Phrase("Наши реквизиты:", _tahomaSmall));
  277.             bankDetails.Add(Chunk.NEWLINE);
  278.             bankDetails.Add(new Phrase("ИНН " + _handler.Inn + ", КПП " + _handler.Kpp, _tahomaSmall));
  279.             bankDetails.Add(Chunk.NEWLINE);
  280.             bankDetails.Add(new Phrase("р/с ", _tahomaSmall));
  281.             bankDetails.Add(Chunk.NEWLINE);
  282.             bankDetails.Add(new Phrase("к/с  ", _tahomaSmall));
  283.             _actDoc.Add(bankDetails);
  284.         }
  285.     }
  286.  
  287.     public class HeaderFooter : PdfPageEventHelper
  288.     {
  289.         string _header;
  290.  
  291.         public void SetHeader(String header)
  292.         {
  293.             _header = header;
  294.         }
  295.  
  296.         public override void OnStartPage(PdfWriter writer, iTextSharp.text.Document document)
  297.         {
  298.             BaseFont baseCourBold = BaseFont.CreateFont("c:/windows/fonts/tahoma.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  299.             Font courierBold = new Font(baseCourBold, 8, Font.BOLD);
  300.  
  301.             PdfPTable headerTable = new PdfPTable(2);
  302.             headerTable.WidthPercentage = 65;
  303.             headerTable.DefaultCell.Border = 0;
  304.             headerTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
  305.             float[] headerTableWidths = { 1.0f, 0.4f };
  306.             headerTable.SetWidths(headerTableWidths);
  307.  
  308.             Paragraph fullPar = new Paragraph();
  309.             fullPar.Alignment = Element.ALIGN_CENTER;
  310.  
  311.             if (document.PageNumber == 1)
  312.             {
  313.                 //
  314.             }
  315.             else
  316.             {
  317.                 fullPar.Add(new Phrase(_header, courierBold));
  318.                 headerTable.AddCell(fullPar);
  319.                 headerTable.AddCell(new Phrase("        стр. " + writer.PageNumber, courierBold));
  320.             }
  321.  
  322.             document.Add(headerTable);
  323.             document.Add(Chunk.NEWLINE);
  324.  
  325.             base.OnStartPage(writer, document);
  326.         }
  327.     }
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement