private static async Task CreatePDFAsync()
        {
            PdfDocument document = new PdfDocument();
            //Add a page to the document.
            PdfPage page = document.Pages.Add();
            //Create PDF graphics for the page.
            PdfGraphics graphics = page.Graphics;
            //Set the standard font.
            PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);

            //Draw the text.
            graphics.DrawString("This is a grid", font, PdfBrushes.Black, new PointF(10, 10));

            var pdfBitmap1 = await GetPdfBitmap("watch");

            graphics.DrawImage(pdfBitmap1, 10, 60, 120, 120);

            //Creates the PdfGrid
            PdfGrid pdfGrid = new PdfGrid();

            //Adds the columns
            pdfGrid.Columns.Add();
            pdfGrid.Columns.Add();

            pdfGrid.Columns[0].Width = 50;

            //Adding grid cell style
            PdfGridCellStyle rowStyle = new PdfGridCellStyle();
            //Creating Border
            PdfBorders border = new PdfBorders();

            border.All = PdfPens.Blue;
            //setting border to the style
            rowStyle.Borders = border;

            //Adds the row and value to the cell
            for (int i = 0; i < 10; i++)
            {
                PdfGridRow pdfGridRow = pdfGrid.Rows.Add();

                pdfGrid.Rows[i].Height = 80;

                pdfGridRow.Cells[1].Value = "asdf asdf";

                //pdfGridRow.Cells[0].Style = rowStyle;
                pdfGridRow.Cells[1].Style = rowStyle;

                //Applies the image
                pdfGrid.Rows[i].Cells[0].Style.BackgroundImage = pdfBitmap1;
                pdfGrid.Rows[i].Cells[0].Value = "";
            }

            //Draws the Grid
            pdfGrid.Draw(page, new PointF(10, 200));

            await SaveFile(document);
        }
Beispiel #2
0
        public async Task <IActionResult> OnPostAsync(Invoice model)
        //public IActionResult OnPost(Invoice model)
        {
            // ComponentInfo.SetLicense("FREE-LICENSE-KEY");

            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Invoice.Add(Invoice);
            await _context.SaveChangesAsync();

            /*  SaveOptions options = GetSaveOptions(model.SelectedFormat);
             * DocumentModel document = this.Process(model);
             *
             *
             * return File(GetBytes(document, options), options.ContentType, "Invoice." + model.SelectedFormat.ToLowerInvariant());
             * // return RedirectToPage("./Index"); */



            // string webRootPath = _hostingEnvironment.WebRootPath;
            string fileName = @"Invoice.pdf";
            //    string URL = string.Format("{0}://{1}/{2}", Request.Scheme, Request.Host, fileName);
            //    FileInfo file = new FileInfo(Path.Combine(fileName));
            var memoryStream = new MemoryStream();


            //create a PDF document
            PdfDocument doc = new PdfDocument();

            doc.PageSettings.Size = PdfPageSize.A4;

            //reset the default margins to 0
            doc.PageSettings.Margins = new PdfMargins(0);

            //create a PdfMargins object, the parameters indicate the page margins you want to set
            PdfMargins margins = new PdfMargins(0, 40, 0, 40);

            //create a header template with content and apply it to page template
            doc.Template.Top    = CreateHeaderTemplate(doc, margins);
            doc.Template.Bottom = CreateFooterTemplate(doc, margins);

            //apply blank templates to other parts of page template
            //  doc.Template.Bottom = new PdfPageTemplateElement(doc.PageSettings.Size.Width, margins.Bottom);
            doc.Template.Left  = new PdfPageTemplateElement(margins.Left, doc.PageSettings.Size.Height);
            doc.Template.Right = new PdfPageTemplateElement(margins.Right, doc.PageSettings.Size.Height);


            PdfPageBase     page  = doc.Pages.Add();
            PdfTrueTypeFont font  = new PdfTrueTypeFont(new Font("Arial", 18f, FontStyle.Bold));
            PdfSolidBrush   brush = new PdfSolidBrush(Color.Gray);

            page.Canvas.DrawString(String.Format("#{0}", Invoice.ID), font, brush, new PointF(520, 0));


            PdfTrueTypeFont font2  = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Bold));
            PdfTrueTypeFont font3  = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold));
            PdfSolidBrush   brush2 = new PdfSolidBrush(Color.Black);
            PdfSolidBrush   brush3 = new PdfSolidBrush(Color.White);

            page.Canvas.DrawString("Date:\nValid Until: ", font2, brush, new PointF(420, 25));

            page.Canvas.DrawString(String.Format("{0}\n{1}", DateTime.Now.ToString("MMM dd, yyyy"), Invoice.Date.ToString("MMM dd, yyyy")), font2, brush2, new PointF(490, 25));

            page.Canvas.DrawString("Bill To:", font3, brush, new PointF(70, 55));
            page.Canvas.DrawString(String.Format("{0}\n{1}\n{2}, {3}", Invoice.Name, Invoice.Project, Invoice.City, Invoice.Country), font3, brush2, new PointF(70, 70));



            /*    String[] data
             * = {
             * "Item;Quantity;Rate;Amount",
             * String.Format("{0};{1};{2};{3}",Invoice.Item1,Invoice.Quantity1,Invoice.Rate1,Invoice.Amount1)
             * };
             *  String[][] dataSource
             *      = new String[data.Length][];
             *  for (int i = 0; i < data.Length; i++)
             *  {
             *      dataSource[i] = data[i].Split(';');
             *  }
             *
             *  PdfTable table = new PdfTable();
             *  table.Style.CellPadding = 3;
             *  table.Style.BorderPen = new PdfPen(brush3, 0.75f);
             *  table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
             *  table.Style.HeaderSource = PdfHeaderSource.Rows;
             *  table.Style.HeaderRowCount = 1;
             *  table.Style.ShowHeader = true;
             *  table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.Black;
             *  table.DataSource = dataSource;
             *  foreach (PdfColumn column in table.Columns)
             *  {
             *      column.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
             *  }
             *  table.Draw(page, new PointF(100, 135));
             */



            PdfGrid grid = new PdfGrid();

            grid.Columns.Add(5);
            float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);

            for (int j = 0; j < grid.Columns.Count; j++)
            {
                grid.Columns[j].Width = width * 0.20f;
            }

            PdfGridRow row0   = grid.Rows.Add();
            PdfGridRow row1   = grid.Rows.Add();
            PdfGridRow row2   = grid.Rows.Add();
            PdfGridRow row3   = grid.Rows.Add();
            PdfGridRow row4   = grid.Rows.Add();
            PdfGridRow row5   = grid.Rows.Add();
            PdfGridRow row6   = grid.Rows.Add();
            PdfGridRow row7   = grid.Rows.Add();
            float      height = 20.0f;

            for (int i = 0; i < grid.Rows.Count; i++)
            {
                grid.Rows[i].Height = height;
            }


            //SET CAP ~56 LETTERS


            row0.Style.Font      = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold), true);
            row0.Style.TextBrush = new PdfSolidBrush(Color.White);
            row1.Style.Font      = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold), true);


            row0.Cells[0].Value                 = "\tItem";
            row0.Cells[0].StringFormat          = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
            row0.Cells[0].Style.BackgroundBrush = PdfBrushes.Black;
            // row0.Cells[0].RowSpan = 2;
            row0.Cells[0].ColumnSpan = 2;
            //  row0.Cells[0].Style.BackgroundBrush = PdfBrushes.Black;

            row1.Cells[0].Value        = String.Format(" {0}", Invoice.Item1);
            row1.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
            row1.Cells[0].ColumnSpan   = 2;

            row2.Cells[0].Value        = String.Format(" {0}", Invoice.Item2);
            row2.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
            row2.Cells[0].ColumnSpan   = 2;
            row3.Cells[0].Value        = String.Format(" {0}", Invoice.Item3);
            row3.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
            row3.Cells[0].ColumnSpan   = 2;
            row4.Cells[0].Value        = String.Format(" {0}", Invoice.Item4);
            row4.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
            row4.Cells[0].ColumnSpan   = 2;
            row5.Cells[0].Value        = String.Format(" {0}", Invoice.Item5);
            row5.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
            row5.Cells[0].ColumnSpan   = 2;
            row6.Cells[0].Value        = String.Format(" {0}", Invoice.Item6);
            row6.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
            row6.Cells[0].ColumnSpan   = 2;
            row7.Cells[0].Value        = String.Format(" {0}", Invoice.Item7);
            row7.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
            row7.Cells[0].ColumnSpan   = 2;


            row0.Cells[2].Value                 = "Quantity";
            row0.Cells[2].StringFormat          = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row0.Cells[2].Style.BackgroundBrush = PdfBrushes.Black;

            row1.Cells[2].Value        = String.Format("{0}", Invoice.Quantity1);
            row1.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row2.Cells[2].Value        = String.Format("{0}", Invoice.Quantity2);
            row2.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row3.Cells[2].Value        = String.Format("{0}", Invoice.Quantity3);
            row3.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row4.Cells[2].Value        = String.Format("{0}", Invoice.Quantity4);
            row4.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row5.Cells[2].Value        = String.Format("{0}", Invoice.Quantity5);
            row5.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row6.Cells[2].Value        = String.Format("{0}", Invoice.Quantity6);
            row6.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row7.Cells[2].Value        = String.Format("{0}", Invoice.Quantity7);
            row7.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

            row0.Cells[3].Value                 = "Rate [AED]";
            row0.Cells[3].StringFormat          = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row0.Cells[3].Style.BackgroundBrush = PdfBrushes.Black;
            //   row0.Cells[1].ColumnSpan = 3;

            row1.Cells[3].Value        = String.Format("{0}", Invoice.Rate1);
            row1.Cells[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row2.Cells[3].Value        = String.Format("{0}", Invoice.Rate2);
            row2.Cells[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row3.Cells[3].Value        = String.Format("{0}", Invoice.Rate3);
            row3.Cells[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row4.Cells[3].Value        = String.Format("{0}", Invoice.Rate4);
            row4.Cells[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row5.Cells[3].Value        = String.Format("{0}", Invoice.Rate5);
            row5.Cells[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row6.Cells[3].Value        = String.Format("{0}", Invoice.Rate6);
            row6.Cells[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row7.Cells[3].Value        = String.Format("{0}", Invoice.Rate7);
            row7.Cells[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

            row0.Cells[4].Value = "Amount [AED]";
            //   row0.Cells[4].Style.Font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold | FontStyle.Italic), true);
            row0.Cells[4].StringFormat          = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row0.Cells[4].Style.BackgroundBrush = PdfBrushes.Black;

            row1.Cells[4].Value        = String.Format("{0}", Invoice.Amount1);
            row1.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row2.Cells[4].Value        = String.Format("{0}", Invoice.Amount2);
            row2.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row3.Cells[4].Value        = String.Format("{0}", Invoice.Amount3);
            row3.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row4.Cells[4].Value        = String.Format("{0}", Invoice.Amount4);
            row4.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row5.Cells[4].Value        = String.Format("{0}", Invoice.Amount5);
            row5.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row6.Cells[4].Value        = String.Format("{0}", Invoice.Amount6);
            row6.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row7.Cells[4].Value        = String.Format("{0}", Invoice.Amount7);
            row7.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);


            // row1.Cells[1].Value = "Amount [AED]";
            // row1.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            // row1.Cells[1].ColumnSpan = 4;


            PdfBorders border = new PdfBorders();

            border.All = new PdfPen(Color.Transparent);

            foreach (PdfGridRow pgr in grid.Rows)
            {
                foreach (PdfGridCell pgc in pgr.Cells)
                {
                    pgc.Style.Borders = border;
                }
            }

            grid.Draw(page, new PointF(2, 135));


            /*   row0.Cells[0].Value = "Item";
             * row0.Cells[0].ColumnSpan = 2;
             *
             *
             * row0.Cells[2].Value = "Quantity";
             * //     row0.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
             *
             *
             * row0.Cells[3].Value = "Rate [AED]";
             * //    row0.Cells[4].Style.Font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold | FontStyle.Italic), true);
             * //    row0.Cells[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
             * //    row0.Cells[3].Style.BackgroundBrush = PdfBrushes.LightGreen;
             *
             *
             * row0.Cells[4].Value = "Amount [AED]";
             * //  row1.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
             * //  row1.Cells[4].ColumnSpan = 4;
             *
             * row1.Cells[0].Value = "This is an Item yo";
             * row1.Cells[0].ColumnSpan = 2; */


            //save the file
            doc.SaveToFile("Invoice.pdf");

/*
 *          PdfDocument destDoc = new PdfDocument();
 *          float top = 20;
 *          float bottom = 20;
 *          float left = 20;
 *          float right = 20;
 *
 *          foreach (PdfPageBase page in doc.Pages)
 *                  {
 *                  PdfPageBase newPage = destDoc.Pages.Add(page.Size, new PdfMargins(0));
 *                  newPage.Canvas.ScaleTransform((page.ActualSize.Width - left - right) / page.ActualSize.Width,
 *                      (page.ActualSize.Height - top - bottom) / page.ActualSize.Height);
 *                  newPage.Canvas.DrawTemplate(page.CreateTemplate(), new PointF(left, top));
 *                  }
 *
 *          destDoc.SaveToFile("result.pdf", FileFormat.PDF);*/



            // System.Diagnostics.Process.Start("PdfHeader.pdf");

            //let the user save it and open it and yeah
            // save the file with a unique name each time -- why? let it get over written each time np

            // return Page();
            using (var fileStream = new FileStream(Path.Combine(fileName), FileMode.Open))
            {
                await fileStream.CopyToAsync(memoryStream);
            }
            memoryStream.Position = 0;
            return(File(memoryStream, "application/pdf", fileName));
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document and load file from disk
            PdfDocument document = new PdfDocument();

            document.LoadFromFile(@"..\..\..\..\..\..\Data\ChangeBorderColor.pdf");
            //Get the first page
            PdfPageBase page = document.Pages[0];

            String[] data
                =
                {
                "VendorName;Address1;City;State;Country",
                "Cacor Corporation;161 Southfield Rd;Southfield;OH;U.S.A.",
                "Underwater;50 N 3rd Street;Indianapolis;IN;U.S.A.",
                "J.W.  Luscher Mfg.;65 Addams Street;Berkely;MA;U.S.A.",
                "Scuba Professionals;3105 East Brace;Rancho Dominguez;CA;U.S.A.",
                "Divers'  Supply Shop;5208 University Dr;Macon;GA;U.S.A.",
                "Techniques;52 Dolphin Drive;Redwood City;CA;U.S.A.",
                "Perry Scuba;3443 James Ave;Hapeville;GA;U.S.A.",
                "Beauchat, Inc.;45900 SW 2nd Ave;Ft Lauderdale;FL;U.S.A.",
                "Amor Aqua;42 West 29th Street;New York;NY;U.S.A.",
                "Aqua Research Corp.;P.O. Box 998;Cornish;NH;U.S.A.",
                "B&K Undersea Photo;116 W 7th Street;New York;NY;U.S.A.",
                "Diving International Unlimited;1148 David Drive;San Diego;DA;U.S.A.",
                "Nautical Compressors;65 NW 167 Street;Miami;FL;U.S.A.",
                "Glen Specialties, Inc.;17663 Campbell Lane;Huntington Beach;CA;U.S.A.",
                "Dive Time;20 Miramar Ave;Long Beach;CA;U.S.A.",
                "Undersea Systems, Inc.;18112 Gotham Street;Huntington Beach;CA;U.S.A.",
                "Felix Diving;310 S Michigan Ave;Chicago;IL;U.S.A.",
                "Central Valley Skin Divers;160 Jameston Ave;Jamaica;NY;U.S.A.",
                "Parkway Dive Shop;241 Kelly Street;South Amboy;NJ;U.S.A.",
                "Marine Camera & Dive;117 South Valley Rd;San Diego;CA;U.S.A.",
                "Dive Canada;275 W Ninth Ave;Vancouver;British Columbia;Canada",
                "Dive & Surf;P.O. Box 20210;Indianapolis;IN;U.S.A.",
                "Fish Research Labs;29 Wilkins Rd Dept. SD;Los Banos;CA;U.S.A."
                };

            //Create a grid
            PdfGrid grid = new PdfGrid();

            //Add rows
            for (int r = 0; r < data.Length; r++)
            {
                PdfGridRow row = grid.Rows.Add();
            }

            //Add columns
            grid.Columns.Add(5);


            grid.Columns[0].Width = 120;
            grid.Columns[1].Width = 120;
            grid.Columns[2].Width = 120;
            grid.Columns[3].Width = 50;
            grid.Columns[4].Width = 60;


            //set the height of rows
            float height = page.Canvas.ClientSize.Height - (grid.Rows.Count + 1);

            for (int i = 0; i < grid.Rows.Count; i++)
            {
                grid.Rows[i].Height = 12.5f;
            }

            //Insert data to grid
            for (int r = 0; r < data.Length; r++)
            {
                String[] rowData = data[r].Split(';');
                for (int c = 0; c < rowData.Length; c++)
                {
                    grid.Rows[r].Cells[c].Value = rowData[c];
                }
            }

            grid.Rows[0].Style.Font = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Bold), true);

            //Set color of border
            PdfBorders border = new PdfBorders();

            border.All = new PdfPen(Color.LightBlue);

            foreach (PdfGridRow pgr in grid.Rows)
            {
                foreach (PdfGridCell pgc in pgr.Cells)
                {
                    pgc.Style.Borders = border;
                }
            }

            //Draw the grid
            grid.Draw(page, new PointF(50, 330));

            //Save the pdf document
            document.SaveToFile("BorderColor.pdf");

            //Launch the pdf file
            PDFDocumentViewer("BorderColor.pdf");
        }