//public FileResult DownloadFile()
        //{
        //    string filePath = _hostingEnvironment.ContentRootPath;
        //    string filename = "Conversion.pdf";
        //    IFileProvider provider = new PhysicalFileProvider(filePath);
        //    IFileInfo fileInfo = provider.GetFileInfo(filename);
        //    var readStream = fileInfo.CreateReadStream();
        //    var mimeType = "application/pdf";
        //    return File(readStream, mimeType, filename);
        //}

        public string CreatePDF(PdfPTable table)
        {
            string name     = CreateName();
            var    document = new iTextSharp.text.Document();

            iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(name, FileMode.Create));
            document.Open();
            document.Add(table);
            document.Close();
            return(name);
        }
Beispiel #2
0
        public bool CreateReport(DataTable rows)
        {
            bool isSuccessful = false;

            if (rows == null)
            {
                reportObject.ReportError      = ReportConstants.NODATA;
                reportObject.ReportErrorLevel = (int)LogLevel.INFO;
                return(false);
            }

            iTextSharp.text.Document document = new iTextSharp.text.Document(_pageSize);

            try
            {
                //set up RunReport event overrides & create doc
                string reportFileName = Path.GetFullPath(reportObject.ReportTempFileFullName);
                if (!Directory.Exists(Path.GetDirectoryName(reportFileName)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(reportFileName));
                }
                PdfWriter    writer   = PdfWriter.GetInstance(document, new FileStream(reportFileName, FileMode.Create));
                MyPageEvents eventMgr = new MyPageEvents
                {
                    footer     = _footer,
                    disclaimer = _disclaimer
                };
                writer.PageEvent = eventMgr;

                PdfPTable table = new PdfPTable(headers.Count);
                Image     gif   = Image.GetInstance(Common.Properties.Resources.logo, BaseColor.WHITE);
                gif.ScalePercent(35);

                _reportFont = FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL);

                document.AddTitle(reportObject.ReportTitle);

                document.SetPageSize(_pageSize);
                document.SetMargins(-50, -50, 10, 45);

                PrintReportHeader(table, gif);

                // Print Headings
                columnWidths = new float[headers.Count];
                for (int i = 0; i < headers.Count; i++)
                {
                    columnWidths[i] = headers[i].width;
                }

                table.SetWidthPercentage(columnWidths, _pageSize);


                foreach (heading h in headers)
                {
                    int      alignment = (h.header_align >= 0) ? h.header_align : h.align;
                    PdfPCell cell      = new PdfPCell(new Paragraph(h.text, _reportFont))
                    {
                        Border = Rectangle.BOTTOM_BORDER,
                        HorizontalAlignment = alignment,
                        VerticalAlignment   = Element.ALIGN_TOP,
                    };
                    table.AddCell(cell);
                }

                table.HeaderRows = table.Rows.Count;



                int    group_count     = 0; // only used if report has a groupByTitle & groupBy Fields
                string lastGroup       = "";
                string nxtGroup        = "";
                bool   breakGroup      = false;
                int    nrGroupByFields = 0;


                // Print Report Details
                for (int rowIter = 0; rowIter < rows.Rows.Count; rowIter++)
                {
                    DataRow row = rows.Rows[rowIter];

                    for (int i = 0; i < headers.Count; i++)
                    {
                        heading h = headers[i];

                        int alignment = (h.field_align >= 0) ? h.field_align : h.align;

                        if (!rows.Columns.Contains(h.fieldName))
                        {
                            reportObject.ReportError = "Application Field (" + h.fieldName +
                                                       ") not in the returned data.";
                            reportObject.ReportErrorLevel = (int)LogLevel.FATAL;
                            throw new Exception("FieldConfig");
                        }

                        string value = "";

                        if (h.Translator != null)
                        {
                            value = h.Translator(row[h.fieldName]);
                        }
                        else
                        {
                            value = String.Format(h.format, row[h.fieldName]);
                        }

                        PdfPCell cell = new PdfPCell(new Phrase(value, _reportFont));

                        if (h.formatICN)
                        {
                            cell = this.GetFormattedICNCell(value);
                        }

                        cell.HorizontalAlignment = alignment;
                        cell.Border = Rectangle.NO_BORDER;

                        table.AddCell(cell);


                        if (h.groupBy)
                        {
                            group_count++;

                            if (lastGroup.Length == 0)
                            {
                                nrGroupByFields++;
                                lastGroup = row[h.fieldName].ToString();
                            }

                            if (rowIter + 1 < rows.Rows.Count &&
                                !lastGroup.Equals(rows.Rows[rowIter + 1][h.fieldName].ToString()))
                            {
                                breakGroup = true;
                                nxtGroup   = rows.Rows[rowIter + 1][h.fieldName].ToString();
                            }
                        }


                        if (h.showTotal)
                        {
                            decimal ttl      = 0;
                            bool    didParse = decimal.TryParse(row[h.fieldName].ToString(), out ttl);
                            if (didParse)
                            {
                                h.total      += ttl;
                                h.groupTotal += ttl;

                                headers[i] = h;

                                if (rowIter == 0)
                                {
                                    nrTotalFields++;
                                }
                            }
                        }
                    }


                    if (breakGroup)
                    {
                        printGroupFooter(table, lastGroup, group_count);
                        group_count = 0;

                        lastGroup  = nxtGroup;
                        breakGroup = false;
                    }
                }

                if (!string.IsNullOrEmpty(GroupByFooterTitle) && nrGroupByFields > 0)
                {
                    printGroupFooter(table, lastGroup, group_count);
                }


                if (!String.IsNullOrEmpty(_totalLabel))
                {
                    PrintReportFooter(table);
                }

                table.AddCell(new PdfPCell {
                    Colspan = headers.Count, Border = Rectangle.NO_BORDER
                });
                new RunReport().ReportLines(table, true, "", true, _reportFont); // End of Report message

                document.Open();
                document.Add(table);
                document.Close();


                isSuccessful = true;
            }
            catch (DocumentException de)
            {
                reportObject.ReportError      = de.Message;
                reportObject.ReportErrorLevel = (int)LogLevel.ERROR;
            }
            catch (IOException ioe)
            {
                reportObject.ReportError      = ioe.Message;
                reportObject.ReportErrorLevel = (int)LogLevel.ERROR;
            }
            catch (Exception e)
            {
                reportObject.ReportError      = e.Message;
                reportObject.ReportErrorLevel = (int)LogLevel.ERROR;
            }

            return(isSuccessful);
        }