コード例 #1
0
ファイル: reports.aspx.cs プロジェクト: halonDesigns/AIS_Time
        public List<byte[]> WriteToPdfForProjectWeekly(string sourceFile, CSList<TimeProjectHours> projects, DateTime dt, TimeProjects projectDetails, WeeklyReportResult[] results)
        {
            var pages = new List<byte[]>();

            int totalItemCount = 0;
            int currentLoopCount = 0;
            int totalHours = 0;
            decimal totalCharge = 0;

            int pageCount = 0;
            while (totalItemCount < projects.Count)
            {
                pageCount += 1;
                var reader = new PdfReader(sourceFile);

                using (var memoryStream = new MemoryStream())
                {
                    // PDFStamper is the class we use from iTextSharp to alter an existing PDF.
                    var pdfStamper = new PdfStamper(reader, memoryStream);

                    Rectangle pageSize = reader.GetPageSizeWithRotation(1);

                    PdfContentByte pdfPageContents = pdfStamper.GetOverContent(1);
                    pdfPageContents.BeginText(); // Start working with text.

                    BaseFont baseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, Encoding.ASCII.EncodingName, false);
                    pdfPageContents.SetFontAndSize(baseFont, 10); // 10 point font
                    pdfPageContents.SetRGBColorFill(0, 0, 0);

                    //      //customer
                    //TimeCustomers customer = TimeCustomers.ReadFirst("TimeCustomerID = @TimeCustomerID", "@TimeCustomerID", projectDetails.TimeCustomerID);
                    //pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, customer.CustomerName, 210, (pageSize.Height - 150), 0);

                    //project name
                    pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, projectDetails.ProjectName, 210, (pageSize.Height - 150), 0);

                    //Project Number
                    pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, projectDetails.ProjectNumber, 525, (pageSize.Height - 150), 0);

                    //Date of report, shows week span
                    pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, String.Format("{0:MMMM dd, yyyy}", dt), 210, (pageSize.Height - 172), 0);
                    pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, String.Format("{0:MMMM dd, yyyy}", dt.AddDays(6)), 525, (pageSize.Height - 172), 0);

                    int yPos = 221;
                    int localLoopCount = 0;
                    foreach (var timeProjectHourse in projects)
                    {
                        if (localLoopCount > currentLoopCount || currentLoopCount == 0)
                        {
                            TimeResources resource = TimeResources.ReadFirst("TimeResourceID = @TimeResourceID", "@TimeResourceID", timeProjectHourse.TimeResourceID);
                            TimeAISCodes classCode = TimeAISCodes.ReadFirst("TimeAISCodeID = @TimeAISCodeID", "@TimeAISCodeID", resource.TimeAISCodeID);
                            TimeDepartments deptCode = TimeDepartments.ReadFirst("TimeDepartmentID = @TimeDepartmentID", "@TimeDepartmentID", timeProjectHourse.TimeDepartmentID);
                            TimeProjects project = TimeProjects.ReadFirst("TimeProjectID = @TimeProjectID", "@TimeProjectID", timeProjectHourse.TimeProjectID);
                            //TimeEmployees employee = TimeEmployees.ReadFirst("TimeEmployeeID = @TimeEmployeeID", "@TimeEmployeeID", timeProjectHourse.TimeEmployeeID);

                            //show the date worked
                            pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, String.Format("{0:MMM d}", timeProjectHourse.DateOfWork), 82, (pageSize.Height - yPos), 0);
                            //show the employee id
                            pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "AIS-0" + timeProjectHourse.TimeEmployeeID, 121, (pageSize.Height - yPos), 0);
                            //show the code
                            pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, classCode.AISCode, 176, (pageSize.Height - yPos), 0);
                            //show the function
                            pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, deptCode.DepartmentName, 205, (pageSize.Height - yPos), 0);
                            //show the hours worked
                            pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, timeProjectHourse.HoursOfWork.ToString(), 640, (pageSize.Height - yPos), 0);
                            //show the hourly rate
                            pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, String.Format("{0:C}", resource.HourlyRate), 662, (pageSize.Height - yPos), 0);

                            decimal totalForDay = timeProjectHourse.HoursOfWork * resource.HourlyRate;

                            //show the total dollars for that day
                            pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, String.Format("{0:C}", totalForDay), 705, (pageSize.Height - yPos), 0);

                            const int NUM_CHARS_ALLOWED = 65;
                            int numLines = 0;
                            if (timeProjectHourse.Description.Length <= NUM_CHARS_ALLOWED)
                                numLines = 1;
                            else if (timeProjectHourse.Description.Length > NUM_CHARS_ALLOWED && timeProjectHourse.Description.Length <= (NUM_CHARS_ALLOWED * 2))
                                numLines = 2;
                            else if (timeProjectHourse.Description.Length > (NUM_CHARS_ALLOWED * 2) && timeProjectHourse.Description.Length <= (NUM_CHARS_ALLOWED * 3))
                                numLines = 3;
                            else if (timeProjectHourse.Description.Length > (NUM_CHARS_ALLOWED * 3) && timeProjectHourse.Description.Length <= (NUM_CHARS_ALLOWED * 4))
                                numLines = 4;

                            int partCount = numLines;
                            string input = timeProjectHourse.Description;
                            var descResults = new string[partCount];
                            int rem = timeProjectHourse.Description.Length % NUM_CHARS_ALLOWED;
                            for (var i = 0; i < partCount; i++)
                            {
                                if (i == partCount - 1)
                                    descResults[i] = input.Substring(NUM_CHARS_ALLOWED * i, rem);
                                else
                                    descResults[i] = input.Substring(NUM_CHARS_ALLOWED * i, NUM_CHARS_ALLOWED);
                            }

                            for (var l = 0; l < numLines; l++)
                            {
                                pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, descResults[l], 325, (pageSize.Height - yPos), 0);
                                yPos += 10;
                            }

                            //increment the total hours
                            totalHours += timeProjectHourse.HoursOfWork;

                            //increment the total charge-out
                            totalCharge += totalForDay;

                            var botPos = (int)(pageSize.Height - yPos);
                            pdfPageContents.SetLineWidth((float).5);
                            pdfPageContents.MoveTo(68, botPos);
                            pdfPageContents.LineTo(pageSize.Width - 38, botPos);
                            pdfPageContents.Stroke();

                            yPos += 10;

                            totalItemCount++;

                            //check to see if we are at the bottom of the page
                            if (yPos > 480) //540
                            {
                                break;
                            }
                        }
                        localLoopCount++;
                    }

                    currentLoopCount = localLoopCount;

                    if (totalItemCount == projects.Count)
                    {
                        //Total
                        pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, "Totals", 620, (pageSize.Height - yPos), 0);
                        pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, totalHours.ToString(), 640, (pageSize.Height - yPos), 0);
                        pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, String.Format("{0:C}", totalCharge), 705, (pageSize.Height - yPos), 0);

                        pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Page " + pageCount, 705, (pageSize.Height - (yPos + 20)), 0);
                    }
                    else
                    {
                        pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Page " + pageCount, 705, (pageSize.Height - (yPos + 20)), 0);
                    }

                    #region LEGEND SECTION
                    //horizontal
                    pdfPageContents.MoveTo(68, 40);
                    pdfPageContents.LineTo(pageSize.Width - 325, 40);
                    pdfPageContents.Stroke();

                    pdfPageContents.SetLineWidth((float).5);
                    pdfPageContents.MoveTo(68, 55);
                    pdfPageContents.LineTo(pageSize.Width - 325, 55);
                    pdfPageContents.Stroke();

                    pdfPageContents.MoveTo(68, 70);
                    pdfPageContents.LineTo(pageSize.Width - 325, 70);
                    pdfPageContents.Stroke();

                    pdfPageContents.MoveTo(68, 85);
                    pdfPageContents.LineTo(pageSize.Width - 325, 85);
                    pdfPageContents.Stroke();

                    pdfPageContents.MoveTo(68, 100);
                    pdfPageContents.LineTo(pageSize.Width - 325, 100);
                    pdfPageContents.Stroke();
                    //horizontal

                    //vertical
                    pdfPageContents.MoveTo(68, 40);
                    pdfPageContents.LineTo(68, 100);
                    pdfPageContents.Stroke();

                    pdfPageContents.MoveTo(191, 40);
                    pdfPageContents.LineTo(191, 100);
                    pdfPageContents.Stroke();

                    pdfPageContents.MoveTo(204, 40);
                    pdfPageContents.LineTo(204, 100);
                    pdfPageContents.Stroke();

                    pdfPageContents.MoveTo(304, 40);
                    pdfPageContents.LineTo(304, 100);
                    pdfPageContents.Stroke();

                    pdfPageContents.MoveTo(318, 40);
                    pdfPageContents.LineTo(318, 100);
                    pdfPageContents.Stroke();

                    pdfPageContents.MoveTo(445, 40);
                    pdfPageContents.LineTo(445, 100);
                    pdfPageContents.Stroke();

                    pdfPageContents.MoveTo(pageSize.Width - 325, 40);
                    pdfPageContents.LineTo(pageSize.Width - 325, 100);
                    pdfPageContents.Stroke();
                    //end vertical

                    pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, " CODE LEGEND", 70, 105, 0);
                    pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, " Assistant Project Engineer   B   Software Developer       D   Advanced Specialist Engineer   F+   ", 78, 90, 0);
                    pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, " General Management           C   Specialist Engineer        D   Report Writing                           R   ", 78, 75, 0);
                    pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, " Mathematician                     D   User Experience (GUI)  D   Technician                                  T4   ", 78, 60, 0);
                    pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "                                                                                                 Technical Meeting                     TM", 78, 45, 0);
                    #endregion

                    pdfPageContents.EndText(); // Done working with text
                    pdfStamper.FormFlattening = true; // enable this if you want the PDF flattened.
                    pdfStamper.Close(); // Always close the stamper or you'll have a 0 byte stream.

                    pages.Add(memoryStream.ToArray());
                }
            }

            return pages;
        }
コード例 #2
0
ファイル: reports.aspx.cs プロジェクト: halonDesigns/AIS_Time
        private void CreateProjectWeeklyPDFReport(CSList<TimeProjectHours> projects, DateTime dt, TimeProjects projectDetails, WeeklyReportResult[] results)
        {
            try
            {
                string templatePdfPath = Server.MapPath("PDFimages");
                string oldFile = templatePdfPath + "\\WeeklyProjectTemplate.pdf";

                List<byte[]> pages = WriteToPdfForProjectWeekly(oldFile, projects, dt, projectDetails, results);

                //if (b == null) return;
                //HttpResponse response = HttpContext.Current.Response;
                //response.Clear();
                //response.ContentType = "application/pdf";
                //response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}.pdf", "WeeklyProjectTimeCard"));
                //response.BinaryWrite(b);
                //response.Flush();
                //response.End();

                if (pages == null) return;
                using (var output = new MemoryStream())
                {
                    var document = new Document();
                    var writer = new PdfCopy(document, output);
                    document.Open();
                    for (int index = 0; index < pages.Count; index++)
                    {
                        var file = pages[index];
                        var reader = new PdfReader(file);
                        int n = reader.NumberOfPages;
                        for (int p = 1; p <= n; p++)
                        {
                            PdfImportedPage page = writer.GetImportedPage(reader, p);
                            writer.AddPage(page);
                        }
                    }
                    document.Close();
                    Response.ContentType = "application/pdf";
                    Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}.pdf", "WeeklyProjectTimeCard"));
                    Response.BinaryWrite(output.ToArray());
                    Response.Flush();
                    Response.End();
                }
            }
            catch (Exception ex)
            {
                lblErrorWeeklyReport.Text = "Error: " + ex.Message;
            }
        }