Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void GenerateDocuments()
- {
- if (_haveTax)
- {
- ActGenerator ag = new ActGenerator(this);
- InvoiceGenerator ig = new InvoiceGenerator(this);
- _act = ag.Generate();
- _invoice = ig.Generate();
- }
- else
- {
- NoticeGenerator ng = new NoticeGenerator(this);
- _notice = ng.Generate();
- }
- }
- using System;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Windows.Forms;
- using iTextSharp.text;
- using iTextSharp.text.pdf;
- namespace PASSS
- {
- class ActGenerator : IDocumentGenerator
- {
- private readonly DocumentHandler _handler;
- // Fonts
- private static BaseFont _baseTahoma;
- private static Font _tahomaBold;
- private static Font _tahoma;
- private static Font _tahoma16Bold;
- private static Font _tahomaSmall;
- // Files
- private iTextSharp.text.Document _actDoc;
- private PdfWriter _actWriter;
- // Overall sum row
- private PdfPRow _overallSumRow;
- public ActGenerator(DocumentHandler handler)
- {
- _handler = handler;
- PrepareFonts();
- }
- // Prepare fonts
- private static void PrepareFonts()
- {
- _baseTahoma = BaseFont.CreateFont("c:/windows/fonts/tahoma.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
- _tahomaBold = new Font(_baseTahoma, 10, Font.BOLD);
- _tahoma = new Font(_baseTahoma, 10, Font.NORMAL);
- _tahoma16Bold = new Font(_baseTahoma, 18, Font.BOLD);
- _tahomaSmall = new Font(_baseTahoma, 8, Font.NORMAL);
- }
- public FileInfo Generate()
- {
- DateTime dateOfDoc = _handler.DocData.DocumentDate;
- string actFileName = Settings.Default.PDFPath + "\\" + "Бонус_№_" + _handler.DocData.DocumentNum + "_АКТ_от_" + dateOfDoc.ToString("dd.MM.yyyy") + ".pdf";
- try
- {
- _actDoc = new iTextSharp.text.Document(PageSize.A4, 45, 45, 45, 45);
- _actWriter = PdfWriter.GetInstance(_actDoc, new FileStream(actFileName, FileMode.Create));
- }
- catch (IOException)
- {
- MessageBox.Show(Resources.DocumentIsOpenedError, Resources.ErrorHeaderForDialogs, MessageBoxButtons.OK, MessageBoxIcon.Error);
- return new FileInfo(actFileName);
- }
- string header = "Акт № " + _handler.DocData.DocumentNum + " от " + dateOfDoc.ToString("dd.MM.yyyy");
- HeaderFooter hfEvent = new HeaderFooter();
- hfEvent.SetHeader(header);
- _actWriter.PageEvent = hfEvent;
- _actDoc.Open();
- // Globus Juridical Address
- AddGlobusJurSection();
- // Title
- Paragraph title = new Paragraph();
- title.SpacingBefore = 10;
- title.SpacingAfter = 10;
- title.Font = _tahoma16Bold;
- title.Alignment = Element.ALIGN_CENTER;
- title.Add(header);
- _actDoc.Add(title);
- // Debitor and Kreditor numbers
- AddKreDebNumbers();
- // Retoure positions table
- AddPositionsTable();
- if (_actWriter.GetVerticalPosition(true) < 100)
- {
- _actDoc.NewPage();
- AddInvoiceEmptyTableHeader();
- _actDoc.Add(Chunk.NEWLINE);
- }
- // Sum by words and agreement
- AddSumAndAgreement();
- // Footer
- AddInvoiceSignatures();
- AddGlobusBankDetails();
- // Save document
- _actDoc.Close();
- return new FileInfo(actFileName);
- }
- private void AddGlobusJurSection()
- {
- Paragraph globusJurInfo = new Paragraph();
- globusJurInfo.Leading = 10;
- globusJurInfo.Add(new Phrase("OOO", _tahomaBold));
- globusJurInfo.Add(Chunk.NEWLINE);
- globusJurInfo.Add(new Phrase(_handler.GlobusJurAddress, _tahomaBold));
- _actDoc.Add(globusJurInfo);
- }
- private void AddKreDebNumbers()
- {
- Paragraph kredDebInfo = new Paragraph();
- kredDebInfo.Leading = 10;
- kredDebInfo.Add(new Phrase("Заказчик: " + _handler.DocData.Bills.Select(bill => bill.SupplierName).FirstOrDefault(), _tahoma));
- kredDebInfo.Add(Chunk.NEWLINE);
- kredDebInfo.Add(new Phrase("Номер кредитора: " + _handler.DocData.SupplierNum, _tahoma));
- kredDebInfo.Add(Chunk.NEWLINE);
- kredDebInfo.Add(new Phrase("Номер дебитора: " + _handler.DocData.Bills.Select(bill => bill.DebitorNum).FirstOrDefault(), _tahoma));
- _actDoc.Add(kredDebInfo);
- }
- private void AddPositionsTable()
- {
- PdfPTable table = new PdfPTable(6);
- table.WidthPercentage = 100;
- table.SpacingBefore = 10;
- table.SpacingAfter = 10;
- float[] colWidths = new[] { 0.3f, 2.1f, 0.5f, 0.5f, 1.0f, 1.0f };
- table.SetWidths(colWidths);
- table.AddCell(VariousCells.GetCell("№", _tahomaBold));
- table.AddCell(VariousCells.GetCell("Наименование работы (услуги)", _tahomaBold));
- table.AddCell(VariousCells.GetCell("Ед. изм.", _tahomaBold));
- table.AddCell(VariousCells.GetCell("Количество", _tahomaBold));
- table.AddCell(VariousCells.GetCell("Цена", _tahomaBold));
- table.AddCell(VariousCells.GetCell("Сумма", _tahomaBold));
- int rowCounter = 1;
- foreach (Bonus bill in _handler.BonusList)
- {
- table.AddCell(VariousCells.GetCell(rowCounter++.ToString(CultureInfo.InvariantCulture), _tahoma));
- table.AddCell(VariousCells.GetCell(bill.Description, _tahoma));
- table.AddCell(VariousCells.GetCell("усл.", _tahoma));
- table.AddCell(VariousCells.GetCell(bill.Quantity, "{0:#,##0.00}", _tahoma));
- table.AddCell(VariousCells.GetCell(bill.Price, "{0:#,##0.00}", _tahoma));
- table.AddCell(VariousCells.GetCell(bill.Sum, "{0:#,##0.00}", _tahomaBold));
- }
- PdfPCell sumLabelCell = VariousCells.GetCell("Итого:", Element.ALIGN_RIGHT, Element.ALIGN_MIDDLE, 5, 1, _tahomaBold);
- sumLabelCell.Border = Rectangle.NO_BORDER;
- table.AddCell(sumLabelCell);
- PdfPCell sumCell = VariousCells.GetCell(_handler.Sum, "{0:#,##0.00}", _tahomaBold);
- table.AddCell(sumCell);
- PdfPCell taxLabelCell = VariousCells.GetCell("Итого НДС:", Element.ALIGN_RIGHT, Element.ALIGN_MIDDLE, 5, 1, _tahomaBold);
- taxLabelCell.Border = Rectangle.NO_BORDER;
- table.AddCell(taxLabelCell);
- PdfPCell taxCell = VariousCells.GetCell(_handler.Tax, "{0:#,##0.00}", _tahomaBold);
- table.AddCell(taxCell);
- PdfPCell overallSumLabelCell = VariousCells.GetCell("Всего (с учетом НДС):", Element.ALIGN_RIGHT, Element.ALIGN_MIDDLE, 5, 1, _tahomaBold);
- overallSumLabelCell.Border = Rectangle.NO_BORDER;
- table.AddCell(overallSumLabelCell);
- PdfPCell overallSumCell = VariousCells.GetCell(_handler.OverallSum, "{0:#,##0.00}", _tahomaBold);
- table.AddCell(overallSumCell);
- _overallSumRow = table.GetRow(table.Size - 1);
- _actDoc.Add(table);
- }
- private void AddInvoiceEmptyTableHeader()
- {
- PdfPTable table = new PdfPTable(6);
- table.WidthPercentage = 100;
- table.SpacingBefore = 10;
- table.SpacingAfter = 10;
- float[] colWidths = new[] { 0.3f, 2.1f, 0.5f, 0.5f, 1.0f, 1.0f };
- table.SetWidths(colWidths);
- table.AddCell(VariousCells.GetCell("№", _tahomaBold));
- table.AddCell(VariousCells.GetCell("Наименование работы (услуги)", _tahomaBold));
- table.AddCell(VariousCells.GetCell("Ед. изм.", _tahomaBold));
- table.AddCell(VariousCells.GetCell("Количество", _tahomaBold));
- table.AddCell(VariousCells.GetCell("Цена", _tahomaBold));
- table.AddCell(VariousCells.GetCell("Сумма", _tahomaBold));
- table.Rows.Add(_overallSumRow);
- _actDoc.Add(table);
- }
- private void AddSumAndAgreement()
- {
- Paragraph sumAndAgreement = new Paragraph();
- sumAndAgreement.Leading = 10;
- sumAndAgreement.Add(new Phrase("Всего оказано услуг на сумму: " + _handler.MakeStringSum(_handler.OverallSum) + " ,", _tahomaBold));
- sumAndAgreement.Add(Chunk.NEWLINE);
- sumAndAgreement.Add(new Phrase("в т.ч.: НДС - " + _handler.MakeStringSum(_handler.Tax), _tahomaBold));
- sumAndAgreement.Add(Chunk.NEWLINE);
- sumAndAgreement.Add(new Phrase("Вышеперечисленные услуги выполнены полностью и в срок.", _tahomaBold));
- sumAndAgreement.Add(Chunk.NEWLINE);
- sumAndAgreement.Add(new Phrase("Заказчик претензий по объему, качеству и срокам оказания услуг не имеет.", _tahomaBold));
- _actDoc.Add(sumAndAgreement);
- }
- private void AddInvoiceSignatures()
- {
- PdfPTable table = new PdfPTable(3);
- table.SpacingBefore = 20;
- table.WidthPercentage = 100;
- float[] colWidths = new[] { 1.0f, 0.2f, 1.0f };
- table.SetWidths(colWidths);
- PdfPCell emptyCell = VariousCells.GetEmptyCell(_tahoma);
- emptyCell.Border = Rectangle.NO_BORDER;
- PdfPCell cell = VariousCells.GetCell("Исполнитель:", Element.ALIGN_LEFT, Element.ALIGN_MIDDLE, _tahoma);
- cell.Border = Rectangle.NO_BORDER;
- table.AddCell(cell);
- table.AddCell(emptyCell);
- cell = VariousCells.GetCell("Заказчик:", Element.ALIGN_LEFT, Element.ALIGN_MIDDLE, _tahoma);
- cell.Border = Rectangle.NO_BORDER;
- table.AddCell(cell);
- cell = VariousCells.GetCell("".PadLeft(50) + "подпись", Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, _tahomaSmall);
- cell.Border = Rectangle.NO_BORDER;
- table.AddCell(cell);
- table.AddCell(emptyCell);
- cell = VariousCells.GetCell("".PadLeft(50) + "подпись", Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, _tahomaSmall);
- cell.Border = Rectangle.NO_BORDER;
- table.AddCell(cell);
- cell = VariousCells.GetCell("М.П.", Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, _tahoma);
- cell.Border = Rectangle.NO_BORDER;
- table.AddCell(cell);
- table.AddCell(emptyCell);
- cell = VariousCells.GetCell("М.П.", Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, _tahoma);
- cell.Border = Rectangle.NO_BORDER;
- table.AddCell(cell);
- _actDoc.Add(table);
- }
- private void AddGlobusBankDetails()
- {
- Paragraph bankDetails = new Paragraph();
- bankDetails.Leading = 10;
- bankDetails.Add(new Phrase("Наши реквизиты:", _tahomaSmall));
- bankDetails.Add(Chunk.NEWLINE);
- bankDetails.Add(new Phrase("ИНН " + _handler.Inn + ", КПП " + _handler.Kpp, _tahomaSmall));
- bankDetails.Add(Chunk.NEWLINE);
- bankDetails.Add(new Phrase("р/с ", _tahomaSmall));
- bankDetails.Add(Chunk.NEWLINE);
- bankDetails.Add(new Phrase("к/с ", _tahomaSmall));
- _actDoc.Add(bankDetails);
- }
- }
- public class HeaderFooter : PdfPageEventHelper
- {
- string _header;
- public void SetHeader(String header)
- {
- _header = header;
- }
- public override void OnStartPage(PdfWriter writer, iTextSharp.text.Document document)
- {
- BaseFont baseCourBold = BaseFont.CreateFont("c:/windows/fonts/tahoma.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
- Font courierBold = new Font(baseCourBold, 8, Font.BOLD);
- PdfPTable headerTable = new PdfPTable(2);
- headerTable.WidthPercentage = 65;
- headerTable.DefaultCell.Border = 0;
- headerTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
- float[] headerTableWidths = { 1.0f, 0.4f };
- headerTable.SetWidths(headerTableWidths);
- Paragraph fullPar = new Paragraph();
- fullPar.Alignment = Element.ALIGN_CENTER;
- if (document.PageNumber == 1)
- {
- //
- }
- else
- {
- fullPar.Add(new Phrase(_header, courierBold));
- headerTable.AddCell(fullPar);
- headerTable.AddCell(new Phrase(" стр. " + writer.PageNumber, courierBold));
- }
- document.Add(headerTable);
- document.Add(Chunk.NEWLINE);
- base.OnStartPage(writer, document);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement