Пример #1
0
        private static PdfPageTemplateElement CreateFooterTemplate(PdfDocument doc, PdfMargins margins)
        {
            SizeF pageSize = doc.PageSettings.Size;

            PdfPageTemplateElement footerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Bottom + margins.Top);

            footerSpace.Foreground = false;

            float x = margins.Left;
            float y = 0;

            DrawLine(footerSpace.Graphics, x, y, GetRightPage(), y);

            y += 5;
            PdfStringFormat format     = new PdfStringFormat(PdfTextAlignment.Left);
            string          footerText = "This document was generated using computer software\nand is deemed to be as accurate as possible.";

            footerSpace.Graphics.DrawString(footerText, pageFont, PdfBrushes.Gray, x, y, format);

            PdfPageNumberField number         = new PdfPageNumberField();
            PdfPageCountField  count          = new PdfPageCountField();
            PdfCompositeField  compositeField = new PdfCompositeField(pageFont, PdfBrushes.Gray, "Page {0} of {1}", number, count);

            compositeField.StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top);
            SizeF size = pageFont.MeasureString(compositeField.Text);

            compositeField.Bounds = new RectangleF(GetRightPage() - margins.Right - size.Width, y, size.Width, size.Height);
            compositeField.Draw(footerSpace.Graphics);

            return(footerSpace);
        }
Пример #2
0
        private PdfPageTemplateElement CreateFooterTemplate(PdfDocument doc, PdfMargins margins, SizeF pageSize)
        {
            //create a PdfPageTemplateElement object which works as footer space
            PdfPageTemplateElement footerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Bottom);

            footerSpace.Foreground = false;

            //declare two float variables
            float x = margins.Left;
            float y = 0;

            //draw line in footer space
            PdfPen pen = new PdfPen(PdfBrushes.Gray, 1);

            footerSpace.Graphics.DrawLine(pen, x, y, pageSize.Width - x, y);

            //draw text in footer space
            y = y + 5;
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 10f), true);
            //draw dynamic field in footer space
            PdfPageNumberField number         = new PdfPageNumberField();
            PdfPageCountField  count          = new PdfPageCountField();
            PdfCompositeField  compositeField = new PdfCompositeField(font, PdfBrushes.Black, "Page {0} of {1}", number, count);

            compositeField.StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Top);
            SizeF size = font.MeasureString(compositeField.Text);

            compositeField.Bounds = new RectangleF(x, y, size.Width, size.Height);
            compositeField.Draw(footerSpace.Graphics);

            //return footerSpace
            return(footerSpace);
        }
Пример #3
0
        /// <summary>
        /// Draws footer to the document.
        /// </summary>
        private PdfPageTemplateElement AddFooter(float width, string footerText)
        {
            RectangleF rect = new RectangleF(0, 0, width, 50);
            //Create a new instance of PdfPageTemplateElement class.
            PdfPageTemplateElement footer = new PdfPageTemplateElement(rect);
            PdfGraphics            g      = footer.Graphics;

            // Draw footer text.
            PdfSolidBrush brush = new PdfSolidBrush(Color.Gray);
            PdfFont       font  = new PdfTrueTypeFont(new Font("Helvetica", 6, FontStyle.Bold), true);
            float         x     = (width / 2) - (font.MeasureString(footerText).Width) / 2;

            g.DrawString(footerText, font, brush, new RectangleF(x, g.ClientSize.Height - 10, font.MeasureString(footerText).Width, font.Height));

            //Create page number field
            PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush);

            //Create page count field
            PdfPageCountField count = new PdfPageCountField(font, brush);

            PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count);

            compositeField.Bounds = footer.Bounds;
            compositeField.Draw(g, new PointF(470, 40));

            return(footer);
        }
Пример #4
0
        //create the pdf footer
        void pdfConvertor_DrawPDFFooter(object sender, PDFHeaderFooterEventArgs e)
        {
            PdfPageTemplateElement footer = e.HeaderFooterTemplate;

            PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 8);

            PdfSolidBrush brush = new PdfSolidBrush(Color.Gray);

            PdfPen pen = new PdfPen(Color.DarkBlue, 3f);

            font = new PdfStandardFont(PdfFontFamily.Helvetica, 6, PdfFontStyle.Bold);
            PdfStringFormat format = new PdfStringFormat();

            format.Alignment     = PdfTextAlignment.Center;
            format.LineAlignment = PdfVerticalAlignment.Middle;
            footer.Graphics.DrawString("@Copyright 2008", font, brush, new RectangleF(0, footer.Height - 40, footer.Width, footer.Height), format);

            format               = new PdfStringFormat();
            format.Alignment     = PdfTextAlignment.Right;
            format.LineAlignment = PdfVerticalAlignment.Bottom;

            //Create page number field
            PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush);

            //Create page count field
            PdfPageCountField count = new PdfPageCountField(font, brush);

            PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count);

            compositeField.Bounds = footer.Bounds;
            compositeField.Draw(footer.Graphics, new PointF(470, footer.Height - 10));
        }
Пример #5
0
        void GridPdfExportExtension_DrawPdfFooter(object sender, PdfHeaderFooterEventArgs e)
        {
            PdfPageTemplateElement footer = e.HeaderFooterTemplate;
            PdfSolidBrush          brush  = new PdfSolidBrush(new PdfColor(System.Drawing.Color.Gray));
            PdfFont         font          = new PdfStandardFont(PdfFontFamily.Helvetica, 6, PdfFontStyle.Bold);
            PdfStringFormat format        = new PdfStringFormat();

            format.Alignment     = PdfTextAlignment.Center;
            format.LineAlignment = PdfVerticalAlignment.Middle;
            footer.Graphics.DrawString("@Copyright 2020", font, brush, new System.Drawing.RectangleF(0, footer.Height - 40, footer.Width, footer.Height), format);

            format               = new PdfStringFormat();
            format.Alignment     = PdfTextAlignment.Right;
            format.LineAlignment = PdfVerticalAlignment.Bottom;

            //Create page number field.
            PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush);

            //Create page count field.
            PdfPageCountField count = new PdfPageCountField(font, brush);

            PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count);

            compositeField.Bounds = footer.Bounds;
            compositeField.Draw(footer.Graphics, new System.Drawing.PointF(470, footer.Height - 10));
            //header.Graphics.DrawImage(PdfImage.FromFile(@"pack:/application:,,,/syncfusion.gridcontroldemos.wpf;component/Assets/GridControl/Footer.png"), 0, 0, header.Width, header.Height);
        }
Пример #6
0
        private void AddFooterToPage(PdfDocument doc, string pageNum, ref PdfPageBase page1, float pageWidth, string fontName)
        {
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            PdfMargins       margin   = new PdfMargins();

            margin.Top    = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = margin.Top;
            margin.Left   = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right  = margin.Left;

            var pageSize = PdfPageSize.A4;

            PdfPageTemplateElement bottomSpace
                = new PdfPageTemplateElement(pageSize.Width, margin.Bottom - 10);

            bottomSpace.Foreground = true;
            doc.Template.Bottom    = bottomSpace;

            PdfTrueTypeFont pageNormalFont = new PdfTrueTypeFont(new Font(fontName, 6f, FontStyle.Regular));
            PdfPen          linePen1       = new PdfPen(Color.Black, 0.5f);
            //draw footer label
            float y = pageNormalFont.Height + 1;

            bottomSpace.Graphics.DrawLine(linePen1, 0, y, pageSize.Width - 1, y);
            y = y + 1;


            PdfPageNumberField pageNumber      = new PdfPageNumberField();
            PdfPageCountField  pageCount       = new PdfPageCountField();
            PdfCompositeField  pageNumberLabel = new PdfCompositeField();

            pageNumberLabel.AutomaticFields
                = new PdfAutomaticField[] { pageNumber, pageCount };
            pageNumberLabel.Brush        = PdfBrushes.Black;
            pageNumberLabel.Font         = pageNormalFont;
            pageNumberLabel.StringFormat = new PdfStringFormat(PdfTextAlignment.Right);
            pageNumberLabel.Text         = "Page {0}";
            pageNumberLabel.Draw(bottomSpace.Graphics, pageSize.Width - MARGIN_FROM_RIGHT, y);

            //footerY = footerY + 6;
            var    brush2  = new PdfSolidBrush(Color.Black);
            var    font2   = new PdfTrueTypeFont(new Font(fontName, 6f, FontStyle.Regular));
            var    format2 = new PdfStringFormat(PdfTextAlignment.Left);
            string text    = "Proof Of Delivery - Generated by Linfox Australia Pty Ltd.";

            bottomSpace.Graphics.DrawString(text, font2, brush2, 0, y, format2);
            text = "Page " + pageNumberLabel.Text;
            //bottomSpace.Graphics.DrawString(text, font2, brush2, pageWidth - 30, y, format2);

            string tz = "";

            text = "Generated On: " + DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss ") + tz;
            bottomSpace.Graphics.DrawString(text, font2, brush2, 0, y + 10, format2);
        }
Пример #7
0
        private void createFooter()
        {
            //footer
            PdfPageTemplateElement footer = new PdfPageTemplateElement(new RectangleF(new PointF(0, _document.PageSettings.Height - heightFooter), new SizeF(_document.PageSettings.Width, heightFooter)));

            footer.Graphics.DrawString(System.DateTime.Now.ToString(), smallFont, brushBlack, new PointF(marginLeft, 15));

            //Creates page number field.
            PdfPageNumberField pageNumber = new PdfPageNumberField(smallFont, brushBlack);
            //Creates page count field.
            PdfPageCountField count = new PdfPageCountField(smallFont, brushBlack);
            //Adds the fields in composite fields.
            PdfCompositeField compositeField = new PdfCompositeField(smallFont, brushBlack, "Pagina {0} di {1}", pageNumber, count);

            compositeField.Bounds = footer.Bounds;
            //Draws the composite field in footer.
            compositeField.Draw(footer.Graphics, new PointF(_document.PageSettings.Width - marginRight * 2, 15));

            _document.Template.Bottom = footer;
        }
        private void AddFooter(PdfDocument doc, string footerText)
        {
            RectangleF rect      = new RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 50);
            PdfColor   blueColor = new PdfColor(System.Drawing.Color.FromArgb(255, 0, 0, 255));
            PdfColor   GrayColor = new PdfColor(System.Drawing.Color.FromArgb(255, 128, 128, 128));
            //Create a page template
            PdfPageTemplateElement footer = new PdfPageTemplateElement(rect);
            PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 8);

            PdfSolidBrush brush = new PdfSolidBrush(GrayColor);

            PdfPen pen = new PdfPen(blueColor, 3f);

            font = new PdfStandardFont(PdfFontFamily.Helvetica, 6, PdfFontStyle.Bold);
            PdfStringFormat format = new PdfStringFormat();

            format.Alignment     = PdfTextAlignment.Center;
            format.LineAlignment = PdfVerticalAlignment.Middle;
            footer.Graphics.DrawString(footerText, font, brush, new RectangleF(0, 18, footer.Width, footer.Height), format);

            format               = new PdfStringFormat();
            format.Alignment     = PdfTextAlignment.Right;
            format.LineAlignment = PdfVerticalAlignment.Bottom;

            //Create page number field
            PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush);

            //Create page count field
            PdfPageCountField count = new PdfPageCountField(font, brush);

            PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count);

            compositeField.Bounds = footer.Bounds;
            compositeField.Draw(footer.Graphics, new PointF(470, 40));

            //Add the footer template at the bottom
            doc.Template.Bottom = footer;
        }
        private async void GeneratePDF_Click(object sender, RoutedEventArgs e)
        {
            // Create a new document class object.
            PdfDocument doc        = new PdfDocument();
            PdfColor    blackColor = new PdfColor(System.Drawing.Color.FromArgb(255, 0, 0, 0));

            // Create few sections with one page in each.
            for (int i = 0; i < 4; ++i)
            {
                section = doc.Sections.Add();

                //Create page label
                PdfPageLabel label = new PdfPageLabel();

                label.Prefix      = "Sec" + i + "-";
                section.PageLabel = label;
                page = section.Pages.Add();
                section.Pages[0].Graphics.SetTransparency(0.35f);
                section.PageSettings.Transition.PageDuration = 1;
                section.PageSettings.Transition.Duration     = 1;
                section.PageSettings.Transition.Style        = PdfTransitionStyle.Box;
            }

            //Set page size
            doc.PageSettings.Size = PdfPageSize.A6;

            //Set viewer prefernce.
            doc.ViewerPreferences.HideToolbar = true;
            doc.ViewerPreferences.PageMode    = PdfPageMode.FullScreen;

            //Set page orientation
            doc.PageSettings.Orientation = PdfPageOrientation.Landscape;

            //Create a brush
            PdfSolidBrush brush = new PdfSolidBrush(blackColor);

            brush.Color = new PdfColor(System.Drawing.Color.FromArgb(255, 144, 238, 144));

            //Create a Rectangle
            PdfRectangle rect = new PdfRectangle(0, 0, 1000f, 1000f);

            rect.Brush = brush;
            PdfPen pen = new PdfPen(blackColor);

            pen.Width = 6f;

            //Get the first page in first section
            page = doc.Sections[0].Pages[0];

            //Draw the rectangle
            rect.Draw(page.Graphics);

            //Draw a line
            page.Graphics.DrawLine(pen, 0, 100, 300, 100);

            // Add margins.
            doc.PageSettings.SetMargins(0f);

            //Get the first page in second section
            page = doc.Sections[1].Pages[0];
            doc.Sections[1].PageSettings.Rotate = PdfPageRotateAngle.RotateAngle90;
            rect.Draw(page.Graphics);

            page.Graphics.DrawLine(pen, 0, 100, 300, 100);

            // Change the angle f the section. This should rotate the previous page.
            doc.Sections[2].PageSettings.Rotate = PdfPageRotateAngle.RotateAngle180;
            page = doc.Sections[2].Pages[0];
            rect.Draw(page.Graphics);
            page.Graphics.DrawLine(pen, 0, 100, 300, 100);

            section = doc.Sections[3];
            section.PageSettings.Orientation = PdfPageOrientation.Portrait;
            page = section.Pages[0];
            rect.Draw(page.Graphics);
            page.Graphics.DrawLine(pen, 0, 100, 300, 100);

            //Set the font
            PdfFont       font       = new PdfStandardFont(PdfFontFamily.Helvetica, 16f);
            PdfSolidBrush fieldBrush = new PdfSolidBrush(blackColor);

            //Create page number field
            PdfPageNumberField pageNumber = new PdfPageNumberField(font, fieldBrush);

            //Create page count field
            PdfPageCountField count = new PdfPageCountField(font, fieldBrush);

            //Draw page template
            PdfPageTemplateElement templateElement = new PdfPageTemplateElement(400, 400);

            templateElement.Graphics.DrawString("Page :\tof", font, PdfBrushes.Black, new PointF(260, 200));

            //Draw current page number
            pageNumber.Draw(templateElement.Graphics, new PointF(306, 200));

            //Draw number of pages
            count.Draw(templateElement.Graphics, new PointF(345, 200));
            doc.Template.Stamps.Add(templateElement);
            templateElement.Background = true;

            // Save and close the document.
            MemoryStream stream = new MemoryStream();
            await doc.SaveAsync(stream);

            doc.Close(true);
            Save(stream, "PageSettings.pdf");
        }
Пример #10
0
        public ActionResult PageSettings(string InsideBrowser)
        {
            // Create a new document class object.
            PdfDocument doc = new PdfDocument();

            PdfSection section;

            PdfPage pdfPage;

            // Create few sections with one page in each.
            for (int i = 0; i < 4; ++i)
            {
                section = doc.Sections.Add();
                //Create page label
                PdfPageLabel label = new PdfPageLabel();

                label.Prefix      = "Sec" + i + "-";
                section.PageLabel = label;
                pdfPage           = section.Pages.Add();
                section.Pages[0].Graphics.SetTransparency(0.35f);
                section.PageSettings.Transition.PageDuration = 1;
                section.PageSettings.Transition.Duration     = 1;
                section.PageSettings.Transition.Style        = PdfTransitionStyle.Box;
            }

            //Set page size
            doc.PageSettings.Size = PdfPageSize.A6;

            //Set viewer prefernce.
            doc.ViewerPreferences.HideToolbar = true;
            doc.ViewerPreferences.PageMode    = PdfPageMode.FullScreen;

            //Set page orientation
            doc.PageSettings.Orientation = PdfPageOrientation.Landscape;

            //Create a brush
            PdfSolidBrush brush = new PdfSolidBrush(Color.Black);

            brush.Color = new PdfColor(Syncfusion.Drawing.Color.LightGreen);

            //Create a Rectangle
            PdfRectangle rect = new PdfRectangle(0, 0, 1000f, 1000f);

            rect.Brush = brush;
            PdfPen pen = new PdfPen(Syncfusion.Drawing.Color.Black);

            pen.Width = 6f;

            //Get the first page in first section
            pdfPage = doc.Sections[0].Pages[0];

            //Draw the rectangle
            rect.Draw(pdfPage.Graphics);

            //Draw a line
            pdfPage.Graphics.DrawLine(pen, 0, 100, 300, 100);

            // Add margins.
            doc.PageSettings.SetMargins(0f);

            //Get the first page in second section
            pdfPage = doc.Sections[1].Pages[0];
            doc.Sections[1].PageSettings.Rotate = PdfPageRotateAngle.RotateAngle90;
            rect.Draw(pdfPage.Graphics);

            pdfPage.Graphics.DrawLine(pen, 0, 100, 300, 100);

            // Change the angle f the section. This should rotate the previous page.
            doc.Sections[2].PageSettings.Rotate = PdfPageRotateAngle.RotateAngle180;
            pdfPage = doc.Sections[2].Pages[0];
            rect.Draw(pdfPage.Graphics);
            pdfPage.Graphics.DrawLine(pen, 0, 100, 300, 100);

            section = doc.Sections[3];
            section.PageSettings.Orientation = PdfPageOrientation.Portrait;
            pdfPage = section.Pages[0];
            rect.Draw(pdfPage.Graphics);
            pdfPage.Graphics.DrawLine(pen, 0, 100, 300, 100);

            //Set the font
            PdfFont       font       = new PdfStandardFont(PdfFontFamily.Helvetica, 16f);
            PdfSolidBrush fieldBrush = new PdfSolidBrush(Color.Black);

            //Create page number field
            PdfPageNumberField pageNumber = new PdfPageNumberField(font, fieldBrush);

            //Create page count field
            PdfPageCountField count = new PdfPageCountField(font, fieldBrush);

            //Draw page template
            PdfPageTemplateElement templateElement = new PdfPageTemplateElement(400, 400);

            templateElement.Graphics.DrawString("Page :\tof", font, PdfBrushes.Black, new PointF(260, 200));

            //Draw current page number
            pageNumber.Draw(templateElement.Graphics, new PointF(306, 200));

            //Draw number of pages
            count.Draw(templateElement.Graphics, new PointF(345, 200));
            doc.Template.Stamps.Add(templateElement);
            templateElement.Background = true;

            //Save the PDF to the MemoryStream
            MemoryStream ms = new MemoryStream();

            doc.Save(ms);

            //If the position is not set to '0' then the PDF will be empty.
            ms.Position = 0;

            //Close the PDF document.
            doc.Close(true);

            //Download the PDF document in the browser.
            FileStreamResult fileStreamResult = new FileStreamResult(ms, "application/pdf");

            fileStreamResult.FileDownloadName = "PageSettings.pdf";
            return(fileStreamResult);
        }
        private ActionResult ExportPdfLINEPayRemittance(List <LINEPayRemittance> LINEPayRemittance, DateTime RemittanceDate)
        {
            // Load the PDF Template
            Stream pdfStream = System.IO.File.OpenRead(_hostingEnvironment.WebRootPath + @"\assets\templates\LINEPayRemittance.pdf");

            PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, (float)6.5);
            PdfFont fontRemittanceDate = new PdfStandardFont(PdfFontFamily.Helvetica, (float)8);

            // Load a PDF document.
            PdfLoadedDocument loadedDocument = new PdfLoadedDocument(pdfStream);

            //Create a new PDF document.
            PdfDocument pdfDocument = new PdfDocument();

            pdfDocument.ImportPage(loadedDocument, 0);

            PdfPage pdfPage = pdfDocument.Pages[0];

            //Create a new PdfGrid.
            PdfGrid pdfGrid = new PdfGrid();

            //Add three columns.
            pdfGrid.Columns.Add(9);

            //Add header.
            pdfGrid.Headers.Add(1);

            PdfGridRow pdfGridHeader = pdfGrid.Headers[0];

            string[] headerStr = { "ERP ID", "Branch ID", "Report Date", "Branch Name", "TUC DCSP", "TUC", "TUP", "TUD", "Captured" };

            int[] columnsWidth = { 40, 40, 50, 0, 50, 50, 50, 50, 30 };
            pdfGridHeader.Style.Font            = font;
            pdfGridHeader.Style.BackgroundBrush = PdfBrushes.LightGray;
            pdfGridHeader.Height = (float)11;

            for (int i = 0; i < headerStr.Count(); i++)
            {
                if (!i.Equals(3))
                {
                    pdfGrid.Columns[i].Width = columnsWidth[i];
                }

                pdfGridHeader.Cells[i].StringFormat.Alignment = PdfTextAlignment.Center;

                pdfGridHeader.Cells[i].Value                      = headerStr[i];
                pdfGridHeader.Cells[i].Style.CellPadding          = new PdfPaddings((float)1.5, (float)1.5, (float)1.5, (float)1.5);
                pdfGridHeader.Cells[i].Style.Borders.Left.Width   = (float)0.5;
                pdfGridHeader.Cells[i].Style.Borders.Right.Width  = (float)0.5;
                pdfGridHeader.Cells[i].Style.Borders.Top.Width    = (float)0.5;
                pdfGridHeader.Cells[i].Style.Borders.Bottom.Width = (float)0.5;
            }

            pdfGridHeader.Cells[4].StringFormat.Alignment = PdfTextAlignment.Right;
            pdfGridHeader.Cells[5].StringFormat.Alignment = PdfTextAlignment.Right;
            pdfGridHeader.Cells[6].StringFormat.Alignment = PdfTextAlignment.Right;

            PdfGridRow pdfGridRow;

            LINEPayRemittance.ForEach(line => {
                //Add rows.
                pdfGridRow            = pdfGrid.Rows.Add();
                pdfGridRow.Style.Font = font;

                pdfGridRow.Height = (float)11;

                pdfGridRow.Cells[0].Value = line.ERP_ID;
                pdfGridRow.Cells[1].Value = line.BranchID;
                pdfGridRow.Cells[2].Value = line.ReportDate.ToString("dd/MM/yyyy", enUS);
                pdfGridRow.Cells[3].Value = line.BranchName;
                pdfGridRow.Cells[4].Value = line.branch_type.Equals("DCSP-SHOP") ? line.TUC.ToString("#,0.00"):"-";
                pdfGridRow.Cells[5].Value = !line.branch_type.Equals("DCSP-SHOP") ? line.TUC.ToString("#,0.00") : "-";
                pdfGridRow.Cells[6].Value = line.TUP.ToString("#,0.00");
                pdfGridRow.Cells[7].Value = line.TUD.ToString("#,0.00");
                pdfGridRow.Cells[8].Value = line.TUDVerifyDate == null ? line.Captured : "Yes";

                pdfGridRow.Cells[0].StringFormat.Alignment = PdfTextAlignment.Center;
                pdfGridRow.Cells[2].StringFormat.Alignment = PdfTextAlignment.Center;
                pdfGridRow.Cells[4].StringFormat.Alignment = PdfTextAlignment.Right;
                pdfGridRow.Cells[5].StringFormat.Alignment = PdfTextAlignment.Right;
                pdfGridRow.Cells[6].StringFormat.Alignment = PdfTextAlignment.Right;
                pdfGridRow.Cells[7].StringFormat.Alignment = PdfTextAlignment.Right;
                pdfGridRow.Cells[8].StringFormat.Alignment = PdfTextAlignment.Center;

                for (int i = 0; i < pdfGridRow.Cells.Count; i++)
                {
                    pdfGridRow.Cells[i].Style.Borders.Left.Width   = (float)0.5;
                    pdfGridRow.Cells[i].Style.Borders.Right.Width  = (float)0.5;
                    pdfGridRow.Cells[i].Style.Borders.Top.Width    = (float)0.5;
                    pdfGridRow.Cells[i].Style.Borders.Bottom.Width = (float)0.5;

                    pdfGridRow.Cells[i].Style.CellPadding = new PdfPaddings((float)1.5, (float)1.5, (float)1.5, (float)1.5);
                }
            });

            //Add rows Sum
            pdfGridRow = pdfGrid.Rows.Add();
            pdfGridRow.Cells[4].Value             = LINEPayRemittance.Where(line => line.branch_type.Equals("DCSP-SHOP")).Sum(line => line.TUC).ToString("#,0.00");
            pdfGridRow.Cells[5].Value             = LINEPayRemittance.Where(line => !line.branch_type.Equals("DCSP-SHOP")).Sum(line => line.TUC).ToString("#,0.00");
            pdfGridRow.Cells[6].Value             = LINEPayRemittance.Sum(line => line.TUP).ToString("#,0.00");
            pdfGridRow.Cells[7].Value             = LINEPayRemittance.Sum(line => line.TUD).ToString("#,0.00");
            pdfGridRow.Cells[4].Style.CellPadding = new PdfPaddings((float)1.5, (float)1.5, (float)1.5, (float)1.5);
            pdfGridRow.Cells[5].Style.CellPadding = new PdfPaddings((float)1.5, (float)1.5, (float)1.5, (float)1.5);
            pdfGridRow.Cells[6].Style.CellPadding = new PdfPaddings((float)1.5, (float)1.5, (float)1.5, (float)1.5);
            pdfGridRow.Cells[7].Style.CellPadding = new PdfPaddings((float)1.5, (float)1.5, (float)1.5, (float)1.5);

            for (int i = 0; i < pdfGridRow.Cells.Count; i++)
            {
                pdfGridRow.Cells[i].Style.Borders.Left.Color   = PdfColor.Empty;
                pdfGridRow.Cells[i].Style.Borders.Right.Color  = PdfColor.Empty;
                pdfGridRow.Cells[i].Style.Borders.Bottom.Color = PdfColor.Empty;
                pdfGridRow.Cells[i].Style.Borders.Left.Width   = (float)0.5;
                pdfGridRow.Cells[i].Style.Borders.Right.Width  = (float)0.5;
                pdfGridRow.Cells[i].Style.Borders.Top.Width    = (float)0.5;
                pdfGridRow.Cells[i].Style.Borders.Bottom.Width = (float)0.5;
            }

            pdfGridRow.Cells[4].StringFormat.Alignment = PdfTextAlignment.Right;
            pdfGridRow.Cells[5].StringFormat.Alignment = PdfTextAlignment.Right;
            pdfGridRow.Cells[6].StringFormat.Alignment = PdfTextAlignment.Right;
            pdfGridRow.Cells[7].StringFormat.Alignment = PdfTextAlignment.Right;

            pdfGridRow.Height     = (float)11;
            pdfGridRow.Style.Font = font;

            for (int i = 4; i <= 7; i++)
            {
                pdfGridRow.Cells[i].Style.Borders.Top.Color    = new PdfColor(Color.Black);
                pdfGridRow.Cells[i].Style.Borders.Left.Color   = new PdfColor(Color.Black);
                pdfGridRow.Cells[i].Style.Borders.Right.Color  = new PdfColor(Color.Black);
                pdfGridRow.Cells[i].Style.Borders.Bottom.Color = new PdfColor(Color.Black);
            }

            //Add rows Total
            pdfGridRow = pdfGrid.Rows.Add();
            pdfGridRow.Cells[3].Value = "Total";
            pdfGridRow.Cells[4].Value = (
                LINEPayRemittance.Sum(line => line.TUC)
                +
                LINEPayRemittance.Sum(line => line.TUP)
                +
                LINEPayRemittance.Sum(line => line.TUD)).ToString("#,0.00");
            pdfGridRow.Cells[4].ColumnSpan        = 4;
            pdfGridRow.Cells[3].Style.CellPadding = new PdfPaddings((float)1.5, (float)1.5, (float)1.5, (float)1.5);
            pdfGridRow.Cells[4].Style.CellPadding = new PdfPaddings((float)1.5, (float)1.5, (float)1.5, (float)1.5);

            pdfGridRow.Cells[3].StringFormat.Alignment = PdfTextAlignment.Right;
            pdfGridRow.Cells[4].StringFormat.Alignment = PdfTextAlignment.Center;

            pdfGridRow.Height     = (float)11;
            pdfGridRow.Style.Font = font;

            for (int i = 0; i < pdfGridRow.Cells.Count; i++)
            {
                pdfGridRow.Cells[i].Style.Borders.Top.Color    = PdfColor.Empty;
                pdfGridRow.Cells[i].Style.Borders.Left.Color   = PdfColor.Empty;
                pdfGridRow.Cells[i].Style.Borders.Right.Color  = PdfColor.Empty;
                pdfGridRow.Cells[i].Style.Borders.Bottom.Color = PdfColor.Empty;
            }

            pdfGridRow.Cells[4].Style.Borders.Top.Color    = new PdfColor(Color.Black);
            pdfGridRow.Cells[4].Style.Borders.Left.Color   = new PdfColor(Color.Black);
            pdfGridRow.Cells[4].Style.Borders.Right.Color  = new PdfColor(Color.Black);
            pdfGridRow.Cells[4].Style.Borders.Bottom.Color = new PdfColor(Color.Black);
            pdfGridRow.Cells[4].Style.Borders.Left.Width   = (float)0.5;
            pdfGridRow.Cells[4].Style.Borders.Right.Width  = (float)0.5;
            pdfGridRow.Cells[4].Style.Borders.Top.Width    = (float)0.5;
            pdfGridRow.Cells[4].Style.Borders.Bottom.Width = (float)0.5;


            //Create PDF graphics for the page.
            PdfGraphics graphics = pdfPage.Graphics;

            //Draw the text.
            graphics.DrawString(RemittanceDate.ToString("dd MMM yyyy", enUS), fontRemittanceDate, PdfBrushes.Black, new PointF((float)458, (float)57.5));

            //Draw the PdfGrid.
            //pdfGrid.Draw(pdfPage, (float)20, (float)90, (float)555.28,);
            //pdfGrid.Draw(pdfPage, new RectangleF(20, 90, pdfDocument.Pages[0].GetClientSize().Width - 40, pdfDocument.Pages[0].GetClientSize().Height - 140));

            //Set properties to paginate the table.
            PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat();

            layoutFormat.Break          = PdfLayoutBreakType.FitElement;
            layoutFormat.Layout         = PdfLayoutType.Paginate;
            layoutFormat.PaginateBounds = new RectangleF(20, 20, pdfDocument.Pages[0].GetClientSize().Width - 40, pdfDocument.Pages[0].GetClientSize().Height - 50);

            //Draw PdfLightTable.
            pdfGrid.Draw(pdfPage, 20f, 90f, (pdfDocument.Pages[0].GetClientSize().Width - 40), layoutFormat);

            //Create a Page template that can be used as footer.
            RectangleF             bounds = new RectangleF(0, 0, pdfDocument.Pages[0].GetClientSize().Width, 50);
            PdfPageTemplateElement footer = new PdfPageTemplateElement(bounds);
            PdfBrush brush = new PdfSolidBrush(Color.Black);

            //Create page number field.
            PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush);

            //Create page count field.
            PdfPageCountField count = new PdfPageCountField(font, brush);

            //Add the fields in composite fields.
            PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count);

            string            printDate          = DateTime.Now.ToString("dd MMM yyyy HH:mm:ss", enUS);
            PdfCompositeField compositePrintDate = new PdfCompositeField(font, brush, string.Format("Printed Date : {0}", printDate));

            compositeField.Bounds = footer.Bounds;

            //Draw the composite field in footer.
            compositeField.Draw(footer.Graphics, new PointF(pdfDocument.Pages[0].GetClientSize().Width - 50, 30));
            compositePrintDate.Draw(footer.Graphics, new PointF(20, 30));

            //Add the footer template at the bottom.
            pdfDocument.Template.Bottom = footer;



            MemoryStream ms = new MemoryStream();

            pdfDocument.Save(ms);
            ms.Position = 0;

            //Close the document
            pdfDocument.Close(true);

            //Save the document.
            return(File(ms, "Application/pdf"));
        }
Пример #12
0
        private FileStreamResult ExportPDF(List <EOD> edo)
        {
            // Load the PDF Template
            Stream pdfStream = System.IO.File.OpenRead(_hostingEnvironment.WebRootPath + @"\assets\templates\CloseShop2.pdf");

            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, (float)8);
            //PdfFont fontText = new PdfStandardFont(PdfFontFamily.Helvetica, (float)12);
            PdfFont fontTextTHSarabunNew = new PdfTrueTypeFont(System.IO.File.OpenRead(_hostingEnvironment.WebRootPath + $@"\assets\fonts\THSarabunNew\THSarabunNew Bold.ttf"), 15);
            PdfFont fontTextCalibri      = new PdfTrueTypeFont(System.IO.File.OpenRead(_hostingEnvironment.WebRootPath + $@"\assets\fonts\calibri\Calibri.ttf"), 13);
            PdfFont fontTextCalibriBold  = new PdfTrueTypeFont(System.IO.File.OpenRead(_hostingEnvironment.WebRootPath + $@"\assets\fonts\calibri\Calibri.ttf"), 13, PdfFontStyle.Bold);

            // Load a PDF document.
            PdfLoadedDocument loadedDocument = new PdfLoadedDocument(pdfStream);

            //Create a new PDF document.
            PdfDocument pdfDocument = new PdfDocument();

            int     numPage = 1;
            PdfPage pdfPage;

            //Set the format for string.
            PdfStringFormat formatAlignRight  = new PdfStringFormat(PdfTextAlignment.Right);
            PdfStringFormat formatAlignCenter = new PdfStringFormat(PdfTextAlignment.Center);

            edo.ForEach(e => {
                pdfDocument.ImportPage(loadedDocument, 0);

                pdfPage = pdfDocument.Pages[numPage - 1];

                // Create PDF graphics for the page
                PdfGraphics graphics = pdfPage.Graphics;

                float xPosition = 105;
                float yPosition = (float)85.5;//106.5;
                float gap       = (float)20.5;

                #region Header Left
                // Branch Name
                graphics.DrawString(e.BranchName.Replace("KERRY EXPRESS", "Kerry Express"), fontTextTHSarabunNew, PdfBrushes.Black, new PointF(xPosition, yPosition));


                // Date
                yPosition += gap;
                graphics.DrawString(e.Report_Date.ToString("dd-MMMM-yyyy", _cultureTHInfo), fontTextTHSarabunNew, PdfBrushes.Black, new PointF(xPosition, yPosition));


                // Branch ID
                yPosition += gap + (float)1;
                graphics.DrawString(e.BranchID, fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition));


                // Cost Center
                yPosition += gap + (float)1;
                graphics.DrawString(e.ERPID, fontTextTHSarabunNew, PdfBrushes.Black, new PointF(xPosition, yPosition));
                #endregion


                #region Header Right
                // Branch Type
                xPosition = (float)464;
                yPosition = (float)107;
                graphics.DrawString(e.BranchType.Split('-')[0], fontTextCalibri, PdfBrushes.Black, new PointF(xPosition - (float)14.5, yPosition), formatAlignCenter);

                // Total Transfer
                xPosition = (float)555.31;
                yPosition = (float)107;
                graphics.DrawString(e.TotalTransfer.ToString("N"), fontTextCalibriBold, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Shipment
                yPosition += gap;
                graphics.DrawString(e.TotalShipments.ToString("N0"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Boxes
                yPosition += gap;
                graphics.DrawString(e.TotalBoxes.ToString("N0"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);
                #endregion

                #region Detail Service
                xPosition = (float)281.585;
                yPosition = (float)219;
                gap       = (float)19;

                // Transport Service
                graphics.DrawString(e.TransportService.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // AM Service
                yPosition += gap - 1;
                graphics.DrawString(e.AMService.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // PUP Service
                yPosition += gap - 2;
                graphics.DrawString(e.PUPService.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // SAT Service
                yPosition += gap - 1;
                graphics.DrawString(e.SATService.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // RAS Service
                yPosition += gap - 1;
                graphics.DrawString(e.RASService.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // COD Service
                yPosition += gap - 1;
                graphics.DrawString(e.CODService.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // INSUR Service
                yPosition += gap - 1;
                graphics.DrawString(e.INSURService.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Package Service
                yPosition += gap - 2;
                graphics.DrawString(e.PACKAGEService.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Sale Package Service
                yPosition += gap - 1;
                graphics.DrawString(e.SALEService.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);


                // Discount
                yPosition += gap + 1;
                graphics.DrawString(e.Discount.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Line Top-up Service
                yPosition += gap - 1;
                graphics.DrawString(e.LNTUPService.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                //rabbitTopUp
                yPosition += gap + 2;
                graphics.DrawString(e.rabbitTopUp.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                //mPayService
                yPosition += gap - 1;
                graphics.DrawString(e.mPayService.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Total Shipments
                yPosition += gap;
                graphics.DrawString(e.Shipment.ToString("N0"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Total Boxes
                yPosition += gap + 2;
                graphics.DrawString(e.Boxes.ToString("N0"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Drop-off Boxes
                yPosition += gap + 2;
                graphics.DrawString(e.DropOffBoxes.ToString("N0"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Total Detail Service
                yPosition += (float)34.5;
                graphics.DrawString(e.TotalDetailService.ToString("N"), fontTextCalibriBold, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);
                #endregion

                // Total Freight Revenue
                yPosition += (float)26;
                graphics.DrawString(e.TotalFreightRevenue.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                #region Detail Pay
                xPosition = (float)555.31;
                yPosition = (float)219.5;
                gap       = (float)19;

                // Cash
                graphics.DrawString(e.Cash.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Rabbit
                yPosition += gap + 2;
                graphics.DrawString(e.Rabbit.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Credit Card BBL
                yPosition += gap + 2;
                graphics.DrawString(e.CreditBBL.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Credit Card SCB
                yPosition += gap + 2;
                graphics.DrawString(e.CreditSCB.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Credit QR Payment
                yPosition += gap + 2;
                graphics.DrawString(e.QRPay.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // LinePay
                yPosition += gap + 2;
                graphics.DrawString(e.LinePay.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Total Detail Pay
                yPosition += (float)32;
                graphics.DrawString(e.TotalDetailPay.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);
                #endregion

                #region Detail Surcharge
                yPosition = (float)439.5;
                gap       = (float)19;

                // Transportation
                graphics.DrawString(e.Transportation.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // VAS Surcharge
                yPosition += gap + 2;
                graphics.DrawString(e.VASSurcharge.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Discount
                yPosition += gap + 2;
                graphics.DrawString(e.Discount.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Vat
                yPosition += gap + 2;
                graphics.DrawString(e.VAT.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Total Detail Surcharge
                yPosition += (float)29;
                graphics.DrawString(e.TotalDetailSurcharge.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);
                #endregion

                #region BSD Surcharge
                xPosition = (float)281.585;
                yPosition = (float)632.5;
                gap       = (float)19;

                // CITY
                graphics.DrawString(e.BSDCity.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // CITYN
                yPosition += gap + 2;
                graphics.DrawString(e.BSDCityn.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // CITYS
                yPosition += gap + 2;
                graphics.DrawString(e.BSDCitys.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Grab
                yPosition += gap + 2;
                graphics.DrawString(e.BSDGrab.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Discount
                yPosition += gap + 2;
                graphics.DrawString(e.BSDDiscount.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Total
                yPosition += gap + (float)12.5;
                graphics.DrawString(e.BSDTotalDetailService.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Total Consignment
                yPosition += gap + 12;
                graphics.DrawString(e.BSDConsignment.ToString("N0"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);
                #endregion

                #region BSD Surcharge
                xPosition = (float)555.31;
                yPosition = (float)632.5;
                gap       = (float)19;

                // Cash
                graphics.DrawString(e.BSDCash.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Line Pay
                yPosition += gap + 2;
                graphics.DrawString(e.BSDLinePay.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Total Payment
                yPosition += gap + 2;
                graphics.DrawString(e.BSDTotalPayment.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Line Topup
                yPosition += gap + 2;
                graphics.DrawString(e.BSDLineTopUp.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Dummy
                yPosition += gap + 2;
                graphics.DrawString("", fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Total Payment Cash
                yPosition += gap + (float)12.5;
                graphics.DrawString(e.BSDTotalPaymentCash.ToString("N"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Total Payment Boxes
                yPosition += gap + 12;
                graphics.DrawString(e.BSDBoxes.ToString("N0"), fontTextCalibri, PdfBrushes.Black, new PointF(xPosition, yPosition), formatAlignRight);

                // Lasted Update
                string closedDate = e.LastedUpdate.HasValue
                ? e.LastedUpdate.Value.ToString("dd MMM yyyy HH:mm:ss", _cultureENInfo)
                : "N/A";

                yPosition += gap + (float)7.8;
                xPosition  = pdfDocument.Pages[0].GetClientSize().Width - 24;
                graphics.DrawString($"Closed Date/Time : {closedDate}", font, PdfBrushes.Red, new PointF(xPosition, yPosition), formatAlignRight);
                #endregion


                if (!(e.TotalDetailService.Equals(e.TotalDetailPay) && e.TotalDetailSurcharge.Equals(e.TotalDetailPay)))
                {
                    //watermark text.
                    PdfFont fontTextTHSarabunNewBold = new PdfTrueTypeFont(System.IO.File.OpenRead(_hostingEnvironment.WebRootPath + $@"\assets\fonts\THSarabunNew\THSarabunNew Bold.ttf"), 48);
                    PdfGraphicsState state           = graphics.Save();
                    graphics.SetTransparency(0.50f);
                    graphics.RotateTransform(-40);
                    graphics.DrawString("ข้อมูลไม่ถูกต้อง โปรดติดต่อผู้ดูแลระบบ", fontTextTHSarabunNewBold, PdfPens.Red, PdfBrushes.Red, new PointF(-300, 460));
                }

                numPage++;
            });

            //Set properties to paginate the table.
            PdfGridLayoutFormat layoutFormat = new PdfGridLayoutFormat();
            layoutFormat.Break          = PdfLayoutBreakType.FitElement;
            layoutFormat.Layout         = PdfLayoutType.Paginate;
            layoutFormat.PaginateBounds = new RectangleF(20, 20, pdfDocument.Pages[0].GetClientSize().Width - 40, pdfDocument.Pages[0].GetClientSize().Height - 50);

            //Create a Page template that can be used as footer.
            RectangleF             bounds = new RectangleF(0, 0, pdfDocument.Pages[0].GetClientSize().Width, 50);
            PdfPageTemplateElement footer = new PdfPageTemplateElement(bounds);
            PdfBrush brush = new PdfSolidBrush(Color.Black);

            //Create page number field.
            PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush);

            //Create page count field.
            PdfPageCountField count = new PdfPageCountField(font, brush);

            //Add the fields in composite fields.
            PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count);

            string            printDate          = DateTime.Now.ToString("dd MMM yyyy HH:mm:ss", _cultureENInfo);
            PdfCompositeField compositePrintDate = new PdfCompositeField(font, brush, string.Format("Printed from PDC/CloseShop      Printed Date/Time : {0}", printDate));

            compositeField.Bounds = footer.Bounds;

            //Draw the composite field in footer.
            compositeField.Draw(footer.Graphics, new PointF(pdfDocument.Pages[0].GetClientSize().Width - (float)63.5, 30));
            compositePrintDate.Draw(footer.Graphics, new PointF((float)24, 30));

            //Add the footer template at the bottom.
            pdfDocument.Template.Bottom = footer;

            MemoryStream ms = new MemoryStream();
            pdfDocument.Save(ms);
            ms.Position = 0;

            //Close the document
            pdfDocument.Close(true);

            // Close file
            pdfStream.Dispose();

            //Save the document.
            return(File(ms, "Application/pdf"));
        }
Пример #13
0
        private void SetDocumentTemplate(PdfDocument doc, SizeF pageSize, PdfMargins margin)
        {
            PdfPageTemplateElement leftSpace
                = new PdfPageTemplateElement(margin.Left, pageSize.Height);

            doc.Template.Left = leftSpace;

            PdfPageTemplateElement topSpace
                = new PdfPageTemplateElement(pageSize.Width, margin.Top);

            topSpace.Foreground = true;
            doc.Template.Top    = topSpace;

            //draw header label
            PdfTrueTypeFont font   = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic));
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
            String          label  = "Demo of Spire.Pdf";
            SizeF           size   = font.MeasureString(label, format);
            float           y      = topSpace.Height - font.Height - 1;
            PdfPen          pen    = new PdfPen(Color.Black, 0.75f);

            topSpace.Graphics.SetTransparency(0.5f);
            topSpace.Graphics.DrawLine(pen, margin.Left, y, pageSize.Width - margin.Right, y);
            y = y - 1 - size.Height;
            topSpace.Graphics.DrawString(label, font, PdfBrushes.Black, pageSize.Width - margin.Right, y, format);

            PdfPageTemplateElement rightSpace
                = new PdfPageTemplateElement(margin.Right, pageSize.Height);

            doc.Template.Right = rightSpace;

            PdfPageTemplateElement bottomSpace
                = new PdfPageTemplateElement(pageSize.Width, margin.Bottom);

            bottomSpace.Foreground = true;
            doc.Template.Bottom    = bottomSpace;

            //draw footer label
            y = font.Height + 1;
            bottomSpace.Graphics.SetTransparency(0.5f);
            bottomSpace.Graphics.DrawLine(pen, margin.Left, y, pageSize.Width - margin.Right, y);
            y = y + 1;
            PdfPageNumberField pageNumber      = new PdfPageNumberField();
            PdfPageCountField  pageCount       = new PdfPageCountField();
            PdfCompositeField  pageNumberLabel = new PdfCompositeField();

            pageNumberLabel.AutomaticFields
                = new PdfAutomaticField[] { pageNumber, pageCount };
            pageNumberLabel.Brush        = PdfBrushes.Black;
            pageNumberLabel.Font         = font;
            pageNumberLabel.StringFormat = format;
            pageNumberLabel.Text         = "page {0} of {1}";
            pageNumberLabel.Draw(bottomSpace.Graphics, pageSize.Width - margin.Right, y);

            PdfImage headerImage
                = PdfImage.FromFile(@"..\..\..\..\..\..\..\Data\Header.png");
            PointF pageLeftTop            = new PointF(-margin.Left, -margin.Top);
            PdfPageTemplateElement header = new PdfPageTemplateElement(pageLeftTop, headerImage.PhysicalDimension);

            header.Foreground = false;
            header.Graphics.SetTransparency(0.5f);
            header.Graphics.DrawImage(headerImage, 0, 0);
            doc.Template.Stamps.Add(header);

            PdfImage footerImage
                = PdfImage.FromFile(@"..\..\..\..\..\..\..\Data\Footer.png");

            y = pageSize.Height - footerImage.PhysicalDimension.Height;
            PointF footerLocation         = new PointF(-margin.Left, y);
            PdfPageTemplateElement footer = new PdfPageTemplateElement(footerLocation, footerImage.PhysicalDimension);

            footer.Foreground = false;
            footer.Graphics.SetTransparency(0.5f);
            footer.Graphics.DrawImage(footerImage, 0, 0);
            doc.Template.Stamps.Add(footer);
        }
Пример #14
0
        void DrawAutomaticField(String fieldName, PdfPageBase page, RectangleF bounds)
        {
            PdfTrueTypeFont font  = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic));
            PdfBrush        brush = PdfBrushes.OrangeRed;
            PdfStringFormat format
                = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);

            if ("DateTimeField" == fieldName)
            {
                PdfDateTimeField field = new PdfDateTimeField();
                field.Font             = font;
                field.Brush            = brush;
                field.StringFormat     = format;
                field.Bounds           = bounds;
                field.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                field.Draw(page.Canvas);
            }

            if ("CreationDateField" == fieldName)
            {
                PdfCreationDateField field = new PdfCreationDateField();
                field.Font             = font;
                field.Brush            = brush;
                field.StringFormat     = format;
                field.Bounds           = bounds;
                field.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                field.Draw(page.Canvas);
            }

            if ("DocumentAuthorField" == fieldName)
            {
                PdfDocumentAuthorField field = new PdfDocumentAuthorField();
                field.Font         = font;
                field.Brush        = brush;
                field.StringFormat = format;
                field.Bounds       = bounds;
                field.Draw(page.Canvas);
            }


            if ("SectionNumberField" == fieldName)
            {
                PdfSectionNumberField field = new PdfSectionNumberField();
                field.Font         = font;
                field.Brush        = brush;
                field.StringFormat = format;
                field.Bounds       = bounds;
                field.Draw(page.Canvas);
            }

            if ("SectionPageNumberField" == fieldName)
            {
                PdfSectionPageNumberField field = new PdfSectionPageNumberField();
                field.Font         = font;
                field.Brush        = brush;
                field.StringFormat = format;
                field.Bounds       = bounds;
                field.Draw(page.Canvas);
            }

            if ("SectionPageCountField" == fieldName)
            {
                PdfSectionPageCountField field = new PdfSectionPageCountField();
                field.Font         = font;
                field.Brush        = brush;
                field.StringFormat = format;
                field.Bounds       = bounds;
                field.Draw(page.Canvas);
            }

            if ("PageNumberField" == fieldName)
            {
                PdfPageNumberField field = new PdfPageNumberField();
                field.Font         = font;
                field.Brush        = brush;
                field.StringFormat = format;
                field.Bounds       = bounds;
                field.Draw(page.Canvas);
            }

            if ("PageCountField" == fieldName)
            {
                PdfPageCountField field = new PdfPageCountField();
                field.Font         = font;
                field.Brush        = brush;
                field.StringFormat = format;
                field.Bounds       = bounds;
                field.Draw(page.Canvas);
            }

            if ("DestinationPageNumberField" == fieldName)
            {
                PdfDestinationPageNumberField field = new PdfDestinationPageNumberField();
                field.Font         = font;
                field.Brush        = brush;
                field.StringFormat = format;
                field.Bounds       = bounds;
                field.Page         = page as PdfNewPage;
                field.Draw(page.Canvas);
            }

            if ("CompositeField" == fieldName)
            {
                PdfSectionPageNumberField field1 = new PdfSectionPageNumberField();
                field1.NumberStyle = PdfNumberStyle.LowerRoman;
                PdfSectionPageCountField field2 = new PdfSectionPageCountField();
                PdfCompositeField        fields = new PdfCompositeField();
                fields.Font            = font;
                fields.Brush           = brush;
                fields.StringFormat    = format;
                fields.Bounds          = bounds;
                fields.AutomaticFields = new PdfAutomaticField[] { field1, field2 };
                fields.Text            = "section page {0} of {1}";
                fields.Draw(page.Canvas);
            }
        }
Пример #15
0
        private void GeneratePDF_Click(object sender, RoutedEventArgs e)
        {
            //Create a new PDF document.
            PdfDocument document = new PdfDocument();
            //Add a page to the document.
            PdfPage page = document.Pages.Add();
            //Create bounds to draw a image.
            RectangleF imageRect = new RectangleF(50, 50, 425, 642);
            //Get the image file stream from assembly.
            Stream imageStream = typeof(EBook).GetTypeInfo().Assembly.GetManifestResourceStream("syncfusion.pdfdemos.winui.Assets.Pdf_Succinctly_img_1.jpg");
            //Create new image from stream.
            PdfBitmap image = new PdfBitmap(imageStream);

            //Draw image to PDF page.
            page.Graphics.DrawImage(image, imageRect);

            //Add a page to the document.
            PdfPage titlePage = document.Pages.Add();
            //Create font, string format and text bounds.
            PdfStandardFont times30Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 30);
            RectangleF      textRect    = new RectangleF(0, 60, titlePage.GetClientSize().Width, titlePage.GetClientSize().Height);
            PdfStringFormat format      = new PdfStringFormat(PdfTextAlignment.Center);

            //Draw a text to PDF page.
            titlePage.Graphics.DrawString("PDF Succinctly", times30Font, PdfBrushes.Black, textRect, format);

            //Create bounds to draw a image.
            imageRect = new RectangleF(40, 110, 435, 5);
            //Get the template PDF file stream from assembly.
            imageStream = typeof(EBook).GetTypeInfo().Assembly.GetManifestResourceStream("syncfusion.pdfdemos.winui.Assets.Pdf_Succinctly_img_5.jpg");
            //Create new image from stream.
            image = new PdfBitmap(imageStream);
            //Draw image to PDF page.
            titlePage.Graphics.DrawImage(image, imageRect);

            //Create font, text bounds to draw a string to PDF page.
            PdfFont helvetica16 = new PdfStandardFont(PdfFontFamily.Helvetica, 16, PdfFontStyle.Bold);

            textRect = new RectangleF(0, 130, titlePage.GetClientSize().Width, titlePage.GetClientSize().Height);
            titlePage.Graphics.DrawString("By\nRyan Hodson", helvetica16, PdfBrushes.Black, textRect, format);

            //Create font, text bounds to draw a string to PDF page.
            PdfFont helvetica20 = new PdfStandardFont(PdfFontFamily.Helvetica, 20);

            textRect = new RectangleF(0, 220, titlePage.GetClientSize().Width, titlePage.GetClientSize().Height);
            titlePage.Graphics.DrawString("Foreword by Daniel Jebaraj", helvetica20, PdfBrushes.Black, textRect, format);

            //Add new section to PDF document.
            PdfSection section = document.Sections.Add();
            //Add a new page to section.
            PdfPage contentPage = section.Pages.Add();

            //Add a new text content to PDF page.
            AddParagraph(contentPage, "Table of Contents", new RectangleF(20, 60, 495, contentPage.GetClientSize().Height), true, true);

            //Create a header template and draw a text.
            PdfPageTemplateElement headerElement = new PdfPageTemplateElement(new Rectangle(0, 0, 515, 50), contentPage);

            //Set transparency to header graphics.
            headerElement.Graphics.SetTransparency(0.6F);
            //Create alignment and string format for header content.
            PdfTextAlignment     textAlingment     = PdfTextAlignment.Right;
            PdfVerticalAlignment verticalAlignment = PdfVerticalAlignment.Middle;

            format = new PdfStringFormat(textAlingment, verticalAlignment);
            //Draw text to header template.
            headerElement.Graphics.DrawString("PDF Succinctly", new PdfStandardFont(PdfFontFamily.Helvetica, 10), PdfBrushes.Black, new RectangleF(0, 0, 515, 50), format);
            //Draw line to header template.
            headerElement.Graphics.DrawLine(PdfPens.Gray, new PointF(0, 49), new PointF(515, 49));
            //Set the top page template.
            section.Template.Top = headerElement;

            //Create a footer template and draw a text.
            PdfPageTemplateElement footerElement = new PdfPageTemplateElement(new RectangleF(0, 0, 515, 50), contentPage);

            //Set transparency to header graphics.
            footerElement.Graphics.SetTransparency(0.6F);
            //Create new font to draw the content in footer template.
            PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 7);
            //Create page number field.
            PdfPageNumberField pageNumber = new PdfPageNumberField(font, PdfBrushes.Black);
            //Create page count field.
            PdfPageCountField count = new PdfPageCountField(font, PdfBrushes.Black);
            //Add the fields in composite fields.
            PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.Black, "Page {0} of {1}", pageNumber, count);

            compositeField.Bounds = footerElement.Bounds;
            //Draw the composite field in footer.
            compositeField.Draw(footerElement.Graphics, new PointF(450, 35));
            //Set the bottom page template.
            section.Template.Bottom = footerElement;
            //Add a new PDF page to PDF document.
            page = document.Pages.Add();
            SizeF pageSize = page.GetClientSize();

            //Add a new text content to PDF page and get layout result.
            PdfLayoutResult result = AddParagraph(page, "Introduction", new RectangleF(20, 25, 495, pageSize.Height), true, true);

            //Add to table of content and get layout result.
            PdfLayoutResult tableContent = AddTableOfContents(contentPage, "Introduction", new RectangleF(20, 110, 470, result.Page.GetClientSize().Height), true, 4, 20, result.Bounds.Top, result.Page);
            //Create color for bookmark.
            PdfColor bookmarkColor = new PdfColor(0, 0, 0);

            //Add bookmark to PDF page.
            AddBookmark(page, "Introduction", result.Bounds.Location, document, null, bookmarkColor);

            //Add a new text content to PDF page and get layout result.
            result = AddParagraph(result.Page, "Adobe Systems Incorporated\'s Portable Document Format (PDF) is the de facto standard for the accurate, reliable, and platform-independent representation of a paged document. It\'s the only universally accepted file format that allows pixel-perfect layouts. In addition, PDF supports user interaction and collaborative workflows that are not possible with printed documents.\n\nPDF documents have been in widespread use for years, and dozens of free and commercial PDF readers, editors, and libraries are readily available. However, despite this popularity, it\'s still difficult to find a succinct guide to the native PDF format. Understanding the internal workings of a PDF makes it possible to dynamically generate PDF documents. For example, a web server can extract information from a database, use it to customize an invoice, and serve it to the customer on the fly.",
                                  new RectangleF(20, result.Bounds.Bottom + 20, 495, pageSize.Height), false, false);

            //Add a new text content to PDF page and get layout result.
            result = AddParagraph(result.Page, "The PDF Standard", new RectangleF(20, result.Bounds.Bottom + 25, 495, pageSize.Height), true, false);

            //Add to table of content and get layout result.
            tableContent = AddTableOfContents(tableContent.Page, "The PDF Standard", new RectangleF(20, tableContent.Bounds.Bottom, 470, result.Page.GetClientSize().Height),
                                              false, 4, 20, result.Bounds.Top, result.Page);

            //Add bookmark to PDF page.
            AddBookmark(result.Page, "The PDF Standard", result.Bounds.Location, document, null, bookmarkColor);

            //Add a new text content to PDF page and get layout result.
            result = AddParagraph(result.Page, "The PDF format is an open standard maintained by the International Organization for Standardization. The official specification is defined in ISO 32000-1:2008, but Adobe also provides a free, comprehensive guide called PDF Reference, Sixth Edition, version 1.7.",
                                  new RectangleF(20, result.Bounds.Bottom + 20, 495, pageSize.Height), false);

            //Add a new text content to PDF page and get layout result.
            result = AddParagraph(result.Page, "Chapter 1 Conceptual Overview", new RectangleF(20, result.Bounds.Bottom + 25, 495, pageSize.Height), true, true);

            //Add to table of content and get layout result.
            tableContent = AddTableOfContents(tableContent.Page, "Chapter 1 Conceptual Overview", new RectangleF(20, tableContent.Bounds.Bottom, 470, result.Page.GetClientSize().Height),
                                              true, 4, 20, result.Bounds.Top, result.Page);

            //Add bookmark to PDF page.
            PdfBookmark standardBookmark = AddBookmark(result.Page, "Chapter 1 Conceptual Overview", result.Bounds.Location, document, null, bookmarkColor);

            //Add a new text content to PDF page and get layout result.
            result = AddParagraph(result.Page, "We\'ll begin with a conceptual overview of a simple PDF document. This chapter is designed to be a brief orientation before diving in and creating a real document from scratch.\nA PDF file can be divided into four parts: a header, body, cross-reference table, and trailer. The header marks the file as a PDF, the body defines the visible document, the cross-reference table lists the location of everything in the file, and the trailer provides instructions for how to start reading the file.",
                                  new RectangleF(20, result.Bounds.Bottom + 20, 495, pageSize.Height), false);

            //Add a new PDF page to PDF document.
            PdfPage page2 = document.Pages.Add();

            //Get the image file stream from assembly.
            Stream imageStream1 = typeof(EBook).GetTypeInfo().Assembly.GetManifestResourceStream("syncfusion.pdfdemos.winui.Assets.Pdf_Succinctly_img_2.jpg");
            //Create PDF bitmap image from stream.
            PdfBitmap bitmap = new PdfBitmap(imageStream1);

            //Draw the image to PDF page.
            page2.Graphics.DrawImage(bitmap, new RectangleF(10, 0, 495, 600));

            //Add a new text content to PDF page and get layout result.
            result = AddParagraph(page2, "Every PDF file must have these four components.", new RectangleF(20, 620, 495, page2.GetClientSize().Height), false);

            //Add a new text content to PDF page and get layout result.
            result = AddParagraph(document.Pages.Add(), "Header", new RectangleF(20, 15, 495, pageSize.Height), true);

            //Add to table of content and get layout result.
            tableContent = AddTableOfContents(tableContent.Page, "Header", new RectangleF(20, tableContent.Bounds.Bottom, 470, result.Page.GetClientSize().Height),
                                              false, 6, 20, result.Bounds.Top, result.Page);

            //Add bookmark to PDF page.
            AddBookmark(result.Page, "Header", result.Bounds.Location, null, standardBookmark, bookmarkColor);

            //Add a new text content to PDF page and get layout result.
            result = AddParagraph(result.Page, "The header is simply a PDF version number and an arbitrary sequence of binary data. The binary data prevents na�ve applications from processing the PDF as a text file. This would result in a corrupted file, since a PDF typically consists of both plain text and binary data (e.g., a binary font file can be directly embedded in a PDF).",
                                  new RectangleF(20, result.Bounds.Bottom + 20, 495, pageSize.Height), false);

            //Add a new text content to PDF page and get layout result.
            result = AddParagraph(result.Page, "Body", new RectangleF(20, result.Bounds.Bottom + 25, 495, pageSize.Height), true);

            //Add to table of content and get layout result.
            tableContent = AddTableOfContents(tableContent.Page, "Body", new RectangleF(20, tableContent.Bounds.Bottom, 470, result.Page.GetClientSize().Height),
                                              false, 6, 20, result.Bounds.Top, result.Page);

            //Add bookmark to PDF page.
            AddBookmark(result.Page, "Body", result.Bounds.Location, null, standardBookmark, bookmarkColor);

            //Add a new text content to PDF page and get layout result.
            result = AddParagraph(result.Page, "The body of a PDF contains the entire visible document. The minimum elements required in a valid PDF body are:\n\n1. A page tree \n2. Pages \n3. Resources \n4. Content \n5. The catalog \n\nThe page tree serves as the root of the document. In the simplest case, it is just a list of the pages in the document. Each page is defined as an independent entity with metadata (e.g., page dimensions) and a reference to its resources and content, which are defined separately. Together, the page tree and page objects create the \"paper\" that composes the document.\n\nResources are objects that are required to render a page. For example, a single font is typically used across several pages, so storing the font information in an external resource is much more efficient. A content object defines the text and graphics that actually show up on the page. Together, content objects and resources define the appearance of an individual page.\nFinally, the document\'s catalog tells applications where to start reading the document. Often, this is just a pointer to the root page tree.",
                                  new RectangleF(20, result.Bounds.Bottom + 20, 495, pageSize.Height), false);

            //Add a new PDF page to PDF document.
            PdfPage page3 = document.Pages.Add();
            //Get the image file stream from assembly.
            Stream imageStream2 = typeof(EBook).GetTypeInfo().Assembly.GetManifestResourceStream("syncfusion.pdfdemos.winui.Assets.Pdf_Succinctly_img_3.jpg");
            //Create PDF bitmap image from stream.
            PdfBitmap bitmap2 = new PdfBitmap(imageStream2);

            //Draw the image to PDF page.
            page3.Graphics.DrawImage(bitmap2, new RectangleF(20, 0, 300, 400));

            //Add a new text content to PDF page and get layout result.
            result = AddParagraph(page3, "Cross-Reference Table", new RectangleF(20, 425, 495, pageSize.Height), true);

            //Add to table of content and get layout result.
            tableContent = AddTableOfContents(tableContent.Page, "Cross-Reference Table", new RectangleF(20, tableContent.Bounds.Bottom, 470, result.Page.GetClientSize().Height),
                                              false, 7, 20, result.Bounds.Top, result.Page);

            //Add bookmark to PDF page.
            AddBookmark(result.Page, "Cross-Reference Table", result.Bounds.Location, null, standardBookmark, bookmarkColor);

            //Add a new text content to PDF page and get layout result.
            result = AddParagraph(result.Page, "After the header and the body comes the cross-reference table. It records the byte location of each object in the body of the file. This enables random-access of the document, so when rendering a page, only the objects required for that page are read from the file. This makes PDFs much faster than their PostScript predecessors, which had to read in the entire file before processing it.",
                                  new RectangleF(20, result.Bounds.Bottom + 20, 495, pageSize.Height), false);

            //Add a new text content to PDF page and get layout result.
            result = AddParagraph(result.Page, "Trailer", new RectangleF(20, result.Bounds.Bottom + 25, 495, pageSize.Height), true);

            //Add to table of content and get layout result.
            tableContent = AddTableOfContents(tableContent.Page, "Trailer", new RectangleF(20, tableContent.Bounds.Bottom, 470, result.Page.GetClientSize().Height),
                                              false, 7, 20, result.Bounds.Top, result.Page);

            //Add bookmark to PDF page.
            AddBookmark(result.Page, "Trailer", result.Bounds.Location, null, standardBookmark, bookmarkColor);

            //Add a new text content to PDF page and get layout result.
            result = AddParagraph(result.Page, "Finally, we come to the last component of a PDF document. The trailer tells applications how to start reading the file. At minimum, it contains three things:\n\n\n1. A reference to the catalog which links to the root of the document.\n2. The location of the cross-reference table.\n3. The size of the cross-reference table.\n\nSince a trailer is all you need to begin processing a document, PDFs are typically read back-to-front: first, the end of the file is found, and then you read backwards until you arrive at the beginning of the trailer. After that, you should have all the information you need to load any page in the PDF.",
                                  new RectangleF(20, result.Bounds.Bottom + 20, 495, pageSize.Height), false);

            //Add a new text content to PDF page and get layout result.
            result = AddParagraph(result.Page, "Summary", new RectangleF(20, result.Bounds.Bottom + 25, 495, pageSize.Height), true);

            //Add to table of content and get layout result.
            tableContent = AddTableOfContents(tableContent.Page, "Summary", new RectangleF(20, tableContent.Bounds.Bottom, 470, result.Page.GetClientSize().Height),
                                              false, 8, 20, result.Bounds.Top, result.Page);

            //Add bookmark to PDF page.
            AddBookmark(result.Page, "Summary", result.Bounds.Location, null, standardBookmark, bookmarkColor);

            //Add a new text content to PDF page and get layout result.
            result = AddParagraph(result.Page, "To conclude our overview, a PDF document has a header, a body, a cross-reference table, and a trailer. The trailer serves as the entryway to the entire document, giving you access to any object via the cross-reference table, and pointing you toward the root of the document. The relationship between these elements is shown in the following figure.",
                                  new RectangleF(20, result.Bounds.Bottom + 20, 495, pageSize.Height), false);

            //Get the image file stream from assembly.
            Stream imageStream3 = typeof(EBook).GetTypeInfo().Assembly.GetManifestResourceStream("syncfusion.pdfdemos.winui.Assets.Pdf_Succinctly_img_4.jpg");
            //Create PDF bitmap image from stream.
            PdfBitmap bitmap3 = new PdfBitmap(imageStream3);

            //Draw the image to PDF page.
            result.Page.Graphics.DrawImage(bitmap3, new RectangleF(20, result.Bounds.Bottom + 20, 495, 400));

            //Creating the stream object.
            using (MemoryStream stream = new MemoryStream())
            {
                //Save the document into stream.
                document.Save(stream);
                document.Close();

                stream.Position = 0;

                //Save the output stream as a file using file picker.
                PdfUtil.Save("E-Book.pdf", stream);
            }
        }
Пример #16
0
        private void SetDocumentTemplate(PdfDocument doc, SizeF pageSize, PdfMargins margin)
        {
            PdfPageTemplateElement leftSpace
                = new PdfPageTemplateElement(margin.Left, pageSize.Height);
            doc.Template.Left = leftSpace;

            PdfPageTemplateElement topSpace
                = new PdfPageTemplateElement(pageSize.Width, margin.Top);
            topSpace.Foreground = true;
            doc.Template.Top = topSpace;

            //draw header label
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic));
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
            String label = "Demo of Spire.Pdf";
            SizeF size = font.MeasureString(label, format);
            float y = topSpace.Height - font.Height - 1;
            PdfPen pen = new PdfPen(Color.Black, 0.75f);
            topSpace.Graphics.SetTransparency(0.5f);
            topSpace.Graphics.DrawLine(pen, margin.Left, y, pageSize.Width - margin.Right, y);
            y = y - 1 - size.Height;
            topSpace.Graphics.DrawString(label, font, PdfBrushes.Black, pageSize.Width - margin.Right, y, format);

            PdfPageTemplateElement rightSpace
                = new PdfPageTemplateElement(margin.Right, pageSize.Height);
            doc.Template.Right = rightSpace;

            PdfPageTemplateElement bottomSpace
                = new PdfPageTemplateElement(pageSize.Width, margin.Bottom);
            bottomSpace.Foreground = true;
            doc.Template.Bottom = bottomSpace;

            //draw footer label
            y = font.Height + 1;
            bottomSpace.Graphics.SetTransparency(0.5f);
            bottomSpace.Graphics.DrawLine(pen, margin.Left, y, pageSize.Width - margin.Right, y);
            y = y + 1;
            PdfPageNumberField pageNumber = new PdfPageNumberField();
            PdfPageCountField pageCount = new PdfPageCountField();
            PdfCompositeField pageNumberLabel = new PdfCompositeField();
            pageNumberLabel.AutomaticFields
                = new PdfAutomaticField[] { pageNumber, pageCount };
            pageNumberLabel.Brush = PdfBrushes.Black;
            pageNumberLabel.Font = font;
            pageNumberLabel.StringFormat = format;
            pageNumberLabel.Text = "page {0} of {1}";
            pageNumberLabel.Draw(bottomSpace.Graphics, pageSize.Width - margin.Right, y);

            PdfImage headerImage
                = PdfImage.FromFile(@"..\..\..\..\..\..\Data\Header.png");
            PointF pageLeftTop = new PointF(-margin.Left, -margin.Top);
            PdfPageTemplateElement header = new PdfPageTemplateElement(pageLeftTop, headerImage.PhysicalDimension);
            header.Foreground = false;
            header.Graphics.SetTransparency(0.5f);
            header.Graphics.DrawImage(headerImage, 0, 0);
            doc.Template.Stamps.Add(header);

            PdfImage footerImage
                = PdfImage.FromFile(@"..\..\..\..\..\..\Data\Footer.png");
            y = pageSize.Height - footerImage.PhysicalDimension.Height;
            PointF footerLocation = new PointF(-margin.Left, y);
            PdfPageTemplateElement footer = new PdfPageTemplateElement(footerLocation, footerImage.PhysicalDimension);
            footer.Foreground = false;
            footer.Graphics.SetTransparency(0.5f);
            footer.Graphics.DrawImage(footerImage, 0, 0);
            doc.Template.Stamps.Add(footer);
        }
Пример #17
0
        public ActionResult CreateDocument(string id, string vrsta, string znak)
        {
            PdfFont font    = new PdfStandardFont(PdfFontFamily.Helvetica, 10);
            PdfFont bigFont = new PdfStandardFont(PdfFontFamily.Helvetica, 20);

            PdfBrush    brush = new PdfSolidBrush(Syncfusion.Drawing.Color.Black);
            PdfDocument doc   = new PdfDocument();
            //Add a page to the document.
            PdfPage page = doc.Pages.Add();
            //Create PDF graphics for the page
            PdfGraphics graphics = page.Graphics;
            PdfGrid     pdfGrid  = new PdfGrid();

            //Load the image as stream.
            FileStream imageStream = new FileStream("logo.png", FileMode.Open, FileAccess.Read);
            PdfBitmap  image       = new PdfBitmap(imageStream);

            graphics.DrawImage(image, 200, 200);

            //Draw the image
            Syncfusion.Drawing.RectangleF bounds    = new Syncfusion.Drawing.RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 50);
            PdfPageTemplateElement        header    = new PdfPageTemplateElement(bounds);
            PdfCompositeField             compField = new PdfCompositeField(bigFont, brush, "OZO - Izvjestaj o natjecajima");

            compField.Draw(header.Graphics, new Syncfusion.Drawing.PointF(120, 0));

            //Add the header at the top.

            doc.Template.Top = header;
            //Save the PDF document to stream
            //Create a Page template that can be used as footer.


            PdfPageTemplateElement footer = new PdfPageTemplateElement(bounds);



            //Create page number field.

            PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush);

            //Create page count field.

            PdfPageCountField count = new PdfPageCountField(font, brush);

            //Add the fields in composite fields.

            PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Stranica {0} od {1}", pageNumber, count);

            compositeField.Bounds = footer.Bounds;

            //Draw the composite field in footer.

            compositeField.Draw(footer.Graphics, new Syncfusion.Drawing.PointF(450, 40));

            //Add the footer template at the bottom.

            doc.Template.Bottom = footer;

            MemoryStream stream = new MemoryStream();
            //Add values to list
            List <object> data = new List <object>();

            switch (vrsta)
            {
            case "Naziv":

                var natječaji = _context.Natječaj
                                .AsNoTracking()
                                .Where(n => n.Naziv.Equals(id))
                                .OrderBy(o => o.Id)
                                .ToList();
                ispisi(natječaji, data);

                break;

            case "Cijena":
                int cijena = int.Parse(id);

                if (znak == "manje")
                {
                    natječaji = _context.Natječaj
                                .AsNoTracking()
                                .Where(n => n.Cijena < cijena)
                                .OrderBy(o => o.Id)
                                .ToList();
                    ispisi(natječaji, data);
                }
                else if (znak == "vise")
                {
                    natječaji = _context.Natječaj
                                .AsNoTracking()
                                .Where(n => n.Cijena > cijena)
                                .OrderBy(o => o.Id)
                                .ToList();
                    ispisi(natječaji, data);
                }
                else
                {
                    natječaji = _context.Natječaj
                                .AsNoTracking()
                                .Where(n => n.Cijena.Equals(cijena))
                                .OrderBy(o => o.Id)
                                .ToList();
                    ispisi(natječaji, data);
                }
                break;

            case "Poslodavac":
                natječaji = _context.Natječaj
                            .AsNoTracking()
                            .Where(n => n.Poslodavac.Equals(id))
                            .OrderBy(o => o.Id)
                            .ToList();
                ispisi(natječaji, data);

                break;

            case "Status":
                bool stanje = bool.Parse(id);
                natječaji = _context.Natječaj
                            .AsNoTracking()
                            .Where(n => n.Status.Equals(stanje))
                            .OrderBy(o => o.Id)
                            .ToList();
                ispisi(natječaji, data);

                break;

            case "Lokacija":
                natječaji = _context.Natječaj
                            .AsNoTracking()
                            .Where(n => n.Lokacija.Equals(id))
                            .OrderBy(o => o.Id)
                            .ToList();
                ispisi(natječaji, data);

                break;

            default:
                Console.WriteLine("Niste unijeli string");
                break;
            }



            //Add list to IEnumerable
            IEnumerable <object> dataTable = data;

            //Assign data source.
            pdfGrid.DataSource = dataTable;
            pdfGrid.Draw(page, new Syncfusion.Drawing.PointF(50, 50));

            doc.Save(stream);
            //If the position is not set to '0' then the PDF will be empty.
            stream.Position = 0;
            //Close the document.
            doc.Close(true);
            //Defining the ContentType for pdf file.
            string contentType = "application/pdf";
            //Define the file name.
            string fileName = "Natječaji-Reports.pdf";

            return(File(stream, contentType, fileName));
        }
Пример #18
0
        private void button1_Click(object sender, System.EventArgs e)
        {
            // Create a new document class object.
            PdfDocument doc = new PdfDocument();
          
            // Create few sections with one page in each.
            for (int i = 0; i < 4; ++i)
            {
                section = doc.Sections.Add();
               
                //Create page label
                PdfPageLabel label = new PdfPageLabel();

                label.Prefix = "Sec" + i + "-";
                section.PageLabel = label;
                page = section.Pages.Add();
                section.Pages[0].Graphics.SetTransparency(0.35f);
                section.PageSettings.Transition.PageDuration = 1;
                section.PageSettings.Transition.Duration = 1;
                section.PageSettings.Transition.Style = PdfTransitionStyle.Box;
             }

            //Set page size
            doc.PageSettings.Size = PdfPageSize.A6;

            //Set viewer prefernce.
            doc.ViewerPreferences.HideToolbar = true;
            doc.ViewerPreferences.PageMode = PdfPageMode.FullScreen;

            //Set page orientation
            doc.PageSettings.Orientation = PdfPageOrientation.Landscape;
           
            //Create a brush
            PdfSolidBrush brush = new PdfSolidBrush(Color.Black);
            brush.Color = new PdfColor(System.Drawing.Color.LightGreen);
           
            //Create a Rectangle
            PdfRectangle rect = new PdfRectangle(0, 0, 1000f, 1000f);
            rect.Brush = brush;
            PdfPen pen = new PdfPen(System.Drawing.Color.Black);
            pen.Width = 6f;

            //Get the first page in first section
            page = doc.Sections[0].Pages[0];

            //Draw the rectangle
            rect.Draw(page.Graphics);
            
            //Draw a line
            page.Graphics.DrawLine(pen, 0, 100, 300, 100);

            // Add margins.
            doc.PageSettings.SetMargins(0f);
            
            //Get the first page in second section
            page = doc.Sections[1].Pages[0];
            doc.Sections[1].PageSettings.Rotate = PdfPageRotateAngle.RotateAngle90;
            rect.Draw(page.Graphics);

            page.Graphics.DrawLine(pen, 0, 100, 300, 100);

            // Change the angle f the section. This should rotate the previous page.
            doc.Sections[2].PageSettings.Rotate = PdfPageRotateAngle.RotateAngle180;
            page = doc.Sections[2].Pages[0];
            rect.Draw(page.Graphics);
            page.Graphics.DrawLine(pen, 0, 100, 300, 100);

            section = doc.Sections[3];
            section.PageSettings.Orientation = PdfPageOrientation.Portrait;
            page = section.Pages[0];
            rect.Draw(page.Graphics);
            page.Graphics.DrawLine(pen, 0, 100, 300, 100);

            //Set the font
            PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16f);
            PdfSolidBrush fieldBrush = new PdfSolidBrush(Color.Black);
            
            //Create page number field
            PdfPageNumberField pageNumber = new PdfPageNumberField(font, fieldBrush);
            
            //Create page count field
            PdfPageCountField count = new PdfPageCountField(font, fieldBrush);
            
            //Draw page template
            PdfPageTemplateElement templateElement = new PdfPageTemplateElement(400, 400);
            templateElement.Graphics.DrawString("Page :\tof", font, PdfBrushes.Black, new PointF(260, 200));
            
            //Draw current page number
            pageNumber.Draw(templateElement.Graphics, new PointF(306, 200));
            
            //Draw number of pages
            count.Draw(templateElement.Graphics, new PointF(345, 200));
            doc.Template.Stamps.Add(templateElement);
            templateElement.Background = true;


            //Save the PDF
            doc.Save("Sample.pdf");

            //Message box confirmation to view the created PDF document.
            if (MessageBox.Show("Do you want to view the PDF file?", "PDF File Created",
                MessageBoxButtons.YesNo, MessageBoxIcon.Information)
                == DialogResult.Yes)
            {
                //Launching the PDF file using the default Application.[Acrobat Reader]
#if NETCORE
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.StartInfo = new System.Diagnostics.ProcessStartInfo("Sample.pdf")
                {
                    UseShellExecute = true
                };
                process.Start();
#else
                System.Diagnostics.Process.Start("Sample.pdf");
#endif
                this.Close();
            }
            else
            {
                // Exit
                this.Close();
            }
        }
Пример #19
0
        public ActionResult CreateDocumentWithoutFilter()
        {
            PdfFont font    = new PdfStandardFont(PdfFontFamily.Helvetica, 10);
            PdfFont bigFont = new PdfStandardFont(PdfFontFamily.Helvetica, 20);

            PdfBrush    brush = new PdfSolidBrush(Syncfusion.Drawing.Color.Black);
            PdfDocument doc   = new PdfDocument();
            //Add a page to the document.
            PdfPage page = doc.Pages.Add();
            //Create PDF graphics for the page
            PdfGraphics graphics = page.Graphics;
            PdfGrid     pdfGrid  = new PdfGrid();

            //Load the image as stream.
            FileStream imageStream = new FileStream("logo.png", FileMode.Open, FileAccess.Read);
            PdfBitmap  image       = new PdfBitmap(imageStream);

            graphics.DrawImage(image, 200, 200);

            //Draw the image
            Syncfusion.Drawing.RectangleF bounds    = new Syncfusion.Drawing.RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 50);
            PdfPageTemplateElement        header    = new PdfPageTemplateElement(bounds);
            PdfCompositeField             compField = new PdfCompositeField(bigFont, brush, "OZO - Izvjestaj o referentnim tipovima");

            compField.Draw(header.Graphics, new Syncfusion.Drawing.PointF(120, 0));

            //Add the header at the top.

            doc.Template.Top = header;
            //Save the PDF document to stream
            //Create a Page template that can be used as footer.


            PdfPageTemplateElement footer = new PdfPageTemplateElement(bounds);



            //Create page number field.

            PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush);

            //Create page count field.

            PdfPageCountField count = new PdfPageCountField(font, brush);

            //Add the fields in composite fields.

            PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Stranica {0} od {1}", pageNumber, count);

            compositeField.Bounds = footer.Bounds;

            //Draw the composite field in footer.

            compositeField.Draw(footer.Graphics, new Syncfusion.Drawing.PointF(450, 40));

            //Add the footer template at the bottom.

            doc.Template.Bottom = footer;

            MemoryStream  stream = new MemoryStream();
            List <object> data   = new List <object>();

            var oprema = _context.ReferentniTip
                         .AsNoTracking()
                         .OrderBy(o => o.Id)
                         .ToList();

            foreach (var komad in oprema)
            {
                var row = new
                {
                    Naziv = komad.Naziv,
                };
                data.Add(row);
            }



            //Add list to IEnumerable
            IEnumerable <object> dataTable = data;

            //Assign data source.
            pdfGrid.DataSource = dataTable;
            pdfGrid.Draw(page, new Syncfusion.Drawing.PointF(50, 50));
            doc.Save(stream);
            //If the position is not set to '0' then the PDF will be empty.
            stream.Position = 0;
            //Close the document.
            doc.Close(true);
            //Defining the ContentType for pdf file.
            string contentType = "application/pdf";
            //Define the file name.
            string fileName = "Referentni tipovi-Reports.pdf";

            return(File(stream, contentType, fileName));
        }
Пример #20
0
        void DrawAutomaticField(String fieldName, PdfPageBase page, RectangleF bounds)
        {
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic));
            PdfBrush brush = PdfBrushes.OrangeRed;
            PdfStringFormat format
                = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);

            if ("DateTimeField" == fieldName)
            {
                PdfDateTimeField field = new PdfDateTimeField();
                field.Font = font;
                field.Brush = brush;
                field.StringFormat = format;
                field.Bounds = bounds;
                field.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                field.Draw(page.Canvas);
            }

            if ("CreationDateField" == fieldName)
            {
                PdfCreationDateField field = new PdfCreationDateField();
                field.Font = font;
                field.Brush = brush;
                field.StringFormat = format;
                field.Bounds = bounds;
                field.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                field.Draw(page.Canvas);
            }

            if ("DocumentAuthorField" == fieldName)
            {
                PdfDocumentAuthorField field = new PdfDocumentAuthorField();
                field.Font = font;
                field.Brush = brush;
                field.StringFormat = format;
                field.Bounds = bounds;
                field.Draw(page.Canvas);
            }


            if ("SectionNumberField" == fieldName)
            {
                PdfSectionNumberField field = new PdfSectionNumberField();
                field.Font = font;
                field.Brush = brush;
                field.StringFormat = format;
                field.Bounds = bounds;
                field.Draw(page.Canvas);
            }

            if ("SectionPageNumberField" == fieldName)
            {
                PdfSectionPageNumberField field = new PdfSectionPageNumberField();
                field.Font = font;
                field.Brush = brush;
                field.StringFormat = format;
                field.Bounds = bounds;
                field.Draw(page.Canvas);
            }

            if ("SectionPageCountField" == fieldName)
            {
                PdfSectionPageCountField field = new PdfSectionPageCountField();
                field.Font = font;
                field.Brush = brush;
                field.StringFormat = format;
                field.Bounds = bounds;
                field.Draw(page.Canvas);
            }

            if ("PageNumberField" == fieldName)
            {
                PdfPageNumberField field = new PdfPageNumberField();
                field.Font = font;
                field.Brush = brush;
                field.StringFormat = format;
                field.Bounds = bounds;
                field.Draw(page.Canvas);
            }

            if ("PageCountField" == fieldName)
            {
                PdfPageCountField field = new PdfPageCountField();
                field.Font = font;
                field.Brush = brush;
                field.StringFormat = format;
                field.Bounds = bounds;
                field.Draw(page.Canvas);
            }

            if ("DestinationPageNumberField" == fieldName)
            {
                PdfDestinationPageNumberField field = new PdfDestinationPageNumberField();
                field.Font = font;
                field.Brush = brush;
                field.StringFormat = format;
                field.Bounds = bounds;
                field.Page = page as PdfNewPage;
                field.Draw(page.Canvas);
            }

            if ("CompositeField" == fieldName)
            {
                PdfSectionPageNumberField field1 = new PdfSectionPageNumberField();
                field1.NumberStyle = PdfNumberStyle.LowerRoman;
                PdfSectionPageCountField field2 = new PdfSectionPageCountField();
                PdfCompositeField fields = new PdfCompositeField();
                fields.Font = font;
                fields.Brush = brush;
                fields.StringFormat = format;
                fields.Bounds = bounds;
                fields.AutomaticFields = new PdfAutomaticField[] { field1, field2 };
                fields.Text = "section page {0} of {1}";
                fields.Draw(page.Canvas);
            }
        }