Пример #1
0
        private static PdfTemplate AddHeader(PdfDocument doc, string title, string description)
        {
            SizeF rect = new SizeF(doc.Pages[0].GetClientSize().Width, 50);

            //Create page template
            PdfTemplate header       = new PdfTemplate(rect);
            PdfFont     font         = new PdfStandardFont(PdfFontFamily.Helvetica, 24);
            float       doubleHeight = font.Height * 2;
            Color       activeColor  = Color.FromArgb(44, 71, 120);
            SizeF       imageSize    = new SizeF(110f, 35f);
            //Locating the logo on the right corner of the drawing surface
            PointF imageLocation = new PointF(doc.Pages[0].GetClientSize().Width - imageSize.Width - 20, 5);


            PdfSolidBrush brush = new PdfSolidBrush(activeColor);

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

            font = new PdfStandardFont(PdfFontFamily.Helvetica, 16, PdfFontStyle.Bold);

            //Set formatting for the text
            PdfStringFormat format = new PdfStringFormat();

            format.Alignment     = PdfTextAlignment.Center;
            format.LineAlignment = PdfVerticalAlignment.Middle;

            //Draw title
            header.Graphics.DrawString(title, font, brush, new RectangleF(0, 0, header.Width, header.Height), format);
            brush = new PdfSolidBrush(Color.Gray);
            font  = new PdfStandardFont(PdfFontFamily.Helvetica, 6, PdfFontStyle.Bold);

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

            //Draw description
            header.Graphics.DrawString(description, font, brush, new RectangleF(0, 0, header.Width, header.Height - 8), format);

            //Draw some lines in the header
            pen = new PdfPen(Color.DarkBlue, 0.7f);
            header.Graphics.DrawLine(pen, 0, 0, header.Width, 0);
            pen = new PdfPen(Color.DarkBlue, 2f);
            header.Graphics.DrawLine(pen, 0, 03, header.Width + 3, 03);
            pen = new PdfPen(Color.DarkBlue, 2f);
            header.Graphics.DrawLine(pen, 0, header.Height - 3, header.Width, header.Height - 3);
            header.Graphics.DrawLine(pen, 0, header.Height, header.Width, header.Height);

            return(header);
        }
Пример #2
0
        private async void ViewReciept_Clicked(object sender, EventArgs e)
        {
            //  await Navigation.PushAsync(new NavigationPage(new RecieptPage(products,saleproducts,  paymentname)) );
            #region Fields
            //Create border color
            PdfColor borderColor     = new PdfColor(Color.FromArgb(255, 51, 181, 75));
            PdfBrush lightGreenBrush = new PdfSolidBrush(new PdfColor(Color.FromArgb(255, 218, 218, 221)));

            PdfBrush darkGreenBrush = new PdfSolidBrush(new PdfColor(Color.FromArgb(255, 51, 181, 75)));

            PdfBrush whiteBrush = new PdfSolidBrush(new PdfColor(Color.FromArgb(255, 255, 255, 255)));
            PdfPen   borderPen  = new PdfPen(borderColor, 1f);
            Stream   fontStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("IttezanPos.Assets.arial.ttf");
            //Create TrueType font
            PdfTrueTypeFont headerFont       = new PdfTrueTypeFont(fontStream, 9, PdfFontStyle.Bold);
            PdfTrueTypeFont arialRegularFont = new PdfTrueTypeFont(fontStream, 9, PdfFontStyle.Regular);
            PdfTrueTypeFont arialBoldFont    = new PdfTrueTypeFont(fontStream, 11, PdfFontStyle.Bold);


            const float margin       = 30;
            const float lineSpace    = 7;
            const float headerHeight = 90;
            #endregion

            #region header and buyer infomation
            //Create PDF with PDF/A-3b conformance
            PdfDocument document = new PdfDocument(PdfConformanceLevel.Pdf_A3B);
            //Set ZUGFeRD profile
            document.ZugferdConformanceLevel = ZugferdConformanceLevel.Basic;

            //Add page to the PDF
            PdfPage page = document.Pages.Add();

            PdfGraphics graphics = page.Graphics;

            //Get the page width and height
            float pageWidth  = page.GetClientSize().Width;
            float pageHeight = page.GetClientSize().Height;
            //Draw page border
            graphics.DrawRectangle(borderPen, new RectangleF(0, 0, pageWidth, pageHeight));

            //Fill the header with light Brush
            graphics.DrawRectangle(lightGreenBrush, new RectangleF(0, 0, pageWidth, headerHeight));

            RectangleF headerAmountBounds = new RectangleF(400, 0, pageWidth - 400, headerHeight);

            graphics.DrawString("INVOICE", headerFont, whiteBrush, new PointF(margin, headerAmountBounds.Height / 3));
            Stream imageStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("IttezanPos.Assets.ic_launcher.png");

            //Create a new PdfBitmap instance
            PdfBitmap image = new PdfBitmap(imageStream);

            //Draw the image
            graphics.DrawImage(image, new PointF(margin + 90, headerAmountBounds.Height / 2));
            graphics.DrawRectangle(darkGreenBrush, headerAmountBounds);

            graphics.DrawString("Amount", arialRegularFont, whiteBrush, headerAmountBounds,
                                new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle));
            graphics.DrawString("$" + products.amount_paid.ToString(), arialBoldFont, whiteBrush,
                                new RectangleF(400, lineSpace, pageWidth - 400, headerHeight + 15), new
                                PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle));



            PdfTextElement textElement = new PdfTextElement("Invoice Number: " + products.id.ToString(), arialRegularFont);

            PdfLayoutResult layoutResult = textElement.Draw(page, new PointF(headerAmountBounds.X - margin, 120));

            textElement.Text = "Date : " + DateTime.Now.ToString("dddd dd, MMMM yyyy");
            textElement.Draw(page, new PointF(layoutResult.Bounds.X, layoutResult.Bounds.Bottom + lineSpace));
            var dbpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "MyDb.db");
            var db     = new SQLiteConnection(dbpath);
            if (products.client_id != null)
            {
                var client = (db.Table <Client>().ToList().Where(clien => clien.id == int.Parse(products.client_id)).FirstOrDefault());
                textElement.Text = "Bill To:";
                layoutResult     = textElement.Draw(page, new PointF(margin, 120));

                textElement.Text = client.enname;
                layoutResult     = textElement.Draw(page, new PointF(margin, layoutResult.Bounds.Bottom + lineSpace));
                textElement.Text = client.address;
                layoutResult     = textElement.Draw(page, new PointF(margin, layoutResult.Bounds.Bottom + lineSpace));
                textElement.Text = client.email;
                layoutResult     = textElement.Draw(page, new PointF(margin, layoutResult.Bounds.Bottom + lineSpace));
                textElement.Text = client.phone;
                layoutResult     = textElement.Draw(page, new PointF(margin, layoutResult.Bounds.Bottom + lineSpace));
            }

            #endregion

            #region Invoice data
            PdfGrid        grid            = new PdfGrid();
            DataTable      dataTable       = new DataTable("EmpDetails");
            List <Product> customerDetails = new List <Product>();
            //Add columns to the DataTable
            dataTable.Columns.Add("ID");
            dataTable.Columns.Add("Name");
            dataTable.Columns.Add("Price");
            dataTable.Columns.Add("Qty");
            dataTable.Columns.Add("Disc");
            dataTable.Columns.Add("Total");

            //Add rows to the DataTable.
            foreach (var item in products.products)
            {
                Product customer = new Product();
                customer.id          = products.products.IndexOf(item) + 1;
                customer.Enname      = item.Enname;
                customer.sale_price  = item.sale_price;
                customer.quantity    = item.quantity;
                customer.discount    = item.discount;
                customer.total_price = item.total_price;
                customerDetails.Add(customer);
                dataTable.Rows.Add(new string[] { customer.id.ToString(), customer.Enname, customer.sale_price.ToString(),
                                                  customer.quantity.ToString(), customer.discount.ToString(), customer.total_price.ToString() });
            }

            //Assign data source.
            grid.DataSource = dataTable;


            grid.Columns[1].Width      = 150;
            grid.Style.Font            = arialRegularFont;
            grid.Style.CellPadding.All = 5;

            grid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable1Light);

            layoutResult = grid.Draw(page, new PointF(0, layoutResult.Bounds.Bottom + 40));

            textElement.Text = "Grand Total: ";
            textElement.Font = arialBoldFont;
            layoutResult     = textElement.Draw(page, new PointF(headerAmountBounds.X - 40, layoutResult.Bounds.Bottom +
                                                                 lineSpace));

            float totalAmount = float.Parse(products.total_price);
            textElement.Text = "$" + totalAmount.ToString();
            layoutResult     = textElement.Draw(page, new PointF(layoutResult.Bounds.Right + 4, layoutResult.Bounds.Y));

            //graphics.DrawString("$" + totalAmount.ToString(), arialBoldFont, whiteBrush,
            //    new RectangleF(400, lineSpace, pageWidth - 400, headerHeight + 15), new
            //    PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle));
            textElement.Text = "Total Discount: ";
            textElement.Font = arialBoldFont;
            layoutResult     = textElement.Draw(page, new PointF(headerAmountBounds.X - 40, layoutResult.Bounds.Bottom +
                                                                 lineSpace));

            float totalDisc = float.Parse(products.discount);
            textElement.Text = "$" + totalDisc.ToString();
            layoutResult     = textElement.Draw(page, new PointF(layoutResult.Bounds.Right + 4, layoutResult.Bounds.Y));

            //graphics.DrawString("$" + totalDisc.ToString(), arialBoldFont, whiteBrush,
            //    new RectangleF(400, lineSpace, pageWidth - 400, headerHeight + 15), new
            //    PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle));


            #endregion

            #region Seller information
            borderPen.DashStyle   = PdfDashStyle.Custom;
            borderPen.DashPattern = new float[] { 3, 3 };

            PdfLine line = new PdfLine(borderPen, new PointF(0, 0), new PointF(pageWidth, 0));
            layoutResult = line.Draw(page, new PointF(0, pageHeight - 100));

            textElement.Text = "IttezanPos";
            textElement.Font = arialRegularFont;
            layoutResult     = textElement.Draw(page, new PointF(margin, layoutResult.Bounds.Bottom + (lineSpace * 3)));
            textElement.Text = "Buradah,  AlQassim, Saudi Arabia";
            layoutResult     = textElement.Draw(page, new PointF(margin, layoutResult.Bounds.Bottom + lineSpace));
            textElement.Text = "Any Questions? ittezan.com";
            layoutResult     = textElement.Draw(page, new PointF(margin, layoutResult.Bounds.Bottom + lineSpace));

            #endregion

            //#region Create ZUGFeRD XML

            ////Create //ZUGFeRD Invoice
            //ZugferdInvoice invoice = new ZugferdInvoice("2058557939", DateTime.Now, CurrencyCodes.USD);

            ////Set ZUGFeRD profile to basic
            //invoice.Profile = ZugferdProfile.Basic;

            ////Add buyer details
            //invoice.Buyer = new UserDetails
            //{
            //    ID = "Abraham_12",
            //    Name = "Abraham Swearegin",
            //    ContactName = "Swearegin",
            //    City = "United States, California",
            //    Postcode = "9920",
            //    Country = CountryCodes.US,
            //    Street = "9920 BridgePointe Parkway"
            //};

            ////Add seller details
            //invoice.Seller = new UserDetails
            //{
            //    ID = "Adventure_123",
            //    Name = "AdventureWorks",
            //    ContactName = "Adventure support",
            //    City = "Austin,TX",
            //    Postcode = "78721",
            //    Country = CountryCodes.US,
            //    Street = "800 Interchange Blvd"
            //};


            //IEnumerable<Product> products = saleproducts;

            //foreach (Product product in products)
            //    invoice.AddProduct(product);


            //invoice.TotalAmount = totalAmount;

            //MemoryStream zugferdXML = new MemoryStream();
            //invoice.Save(zugferdXML);
            //#endregion

            #region Embed ZUGFeRD XML to PDF

            //Attach ZUGFeRD XML to PDF
            //PdfAttachment attachment = new PdfAttachment("ZUGFeRD-invoice.xml", zugferdXML);
            //attachment.Relationship = PdfAttachmentRelationship.Alternative;
            //attachment.ModificationDate = DateTime.Now;
            //attachment.Description = "ZUGFeRD-invoice";
            //attachment.MimeType = "application/xml";
            //document.Attachments.Add(attachment);
            #endregion
            //Creates an attachment
            MemoryStream stream = new MemoryStream();
            //    Stream invoiceStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("Sample.Assets.Data.ZUGFeRD-invoice.xml");

            PdfAttachment attachment = new PdfAttachment("ZUGFeRD-invoice.xml", stream);

            attachment.Relationship = PdfAttachmentRelationship.Alternative;

            attachment.ModificationDate = DateTime.Now;

            attachment.Description = "ZUGFeRD-invoice";

            attachment.MimeType = "application/xml";

            document.Attachments.Add(attachment);

            //Save the document into memory stream



            document.Save(stream);

            //Close the document

            document.Close(true);

            await Xamarin.Forms.DependencyService.Get <ISave>().SaveAndView("تقرير العملاء.pdf", "application/pdf", stream);
        }
Пример #3
0
        private void DrawHeaderOfDocument(PdfGraphics graphics)
        {
            graphics.DrawLine(new PdfPen(Color.FromArgb(255, 0, 0, 0))
            {
                Width = 0.25f
            }, 0, 5, _document.PageSettings.Width - 20, 5);

            graphics.DrawString(header1, _font10Regular, _brushGreyishBrownTwo,
                                new PointF(0, 10));
            graphics.DrawString(header2, _font10Regular, _brushGreyishBrownTwo,
                                new PointF(0, 25));
            graphics.DrawString(header3, _font10Regular, _brushGreyishBrownTwo,
                                new PointF(0, 40));
            graphics.DrawString(header4, _font10Regular, _brushGreyishBrownTwo,
                                new PointF(0, 55));
            graphics.DrawString(header5, _font10Regular, _brushGreyishBrownTwo,
                                new PointF(0, 70));

            graphics.DrawLine(new PdfPen(Color.FromArgb(255, 0, 0, 0))
            {
                Width = 0.25f
            }, 0, 90, _document.PageSettings.Width - 10, 90);

            graphics.DrawString(AppResources.BillingAddress, _font14Bold, _brushGreyishBrownTwo,
                                new PointF(0, 100));

            graphics.DrawString(string.IsNullOrEmpty(Customer.Name) ? "" :Customer.Name, _font10Regular, _brushGreyishBrownTwo,
                                new PointF(0, 125));
            graphics.DrawString(string.IsNullOrEmpty(Customer.ContactAddress) ? "" : Customer.ContactAddress.Replace("\n", " "), _font10Regular, _brushGreyishBrownTwo,
                                new PointF(0, 140));
            graphics.DrawString(string.IsNullOrEmpty(Customer.Website) ? "" :Customer.Website, _font10Regular, _brushGreyishBrownTwo,
                                new PointF(0, 155));
            graphics.DrawString(string.IsNullOrEmpty(Customer.Phone) ? "" : Customer.Phone, _font10Regular, _brushGreyishBrownTwo,
                                new PointF(0, 170));
            graphics.DrawString(string.IsNullOrEmpty(Customer.Email) ? "" : Customer.Email, _font10Regular, _brushGreyishBrownTwo,
                                new PointF(0, 185));


            graphics.DrawString(AppResources.OrderNumber + SaleOrder.ClientOrderRef, _font14Bold, _brushGreyishBrownTwo,
                                new PointF(0, 210));

            graphics.DrawString(AppResources.OrderDate, _font10Regular, _brushBlack,
                                new PointF(0, 230));
            graphics.DrawString(SaleOrder.CreateDate.ToString("G"), _font10Regular, _brushGreyishBrownTwo,
                                new PointF(0, 245));

            graphics.DrawString(AppResources.Prescriber, _font10Regular, _brushBlack,
                                new PointF(150, 230));
            graphics.DrawString(string.IsNullOrEmpty(SaleOrder.TrainerName) ? "" : SaleOrder.TrainerName, _font10Regular, _brushGreyishBrownTwo,
                                new PointF(150, 245));


            graphics.DrawString(AppResources.PaymentMethod, _font10Regular, _brushBlack,
                                new PointF(0, 260));
            graphics.DrawString(string.IsNullOrEmpty(SaleOrder.PaymentMethod) ? "" : SaleOrder.PaymentMethod, _font10Regular, _brushGreyishBrownTwo,
                                new PointF(0, 275));

            graphics.DrawString(AppResources.PaymentNote, _font10Regular, _brushBlack,
                                new PointF(150, 260));
            graphics.DrawString(string.IsNullOrWhiteSpace(SaleOrder.PaymentNote) ? "" : SaleOrder.PaymentNote, _font10Regular, _brushGreyishBrownTwo,
                                new PointF(150, 275));
        }
Пример #4
0
        public byte[] CreatePdfFile(QuotationsModel saleOrder, List <ProductQuotationModel> items, Partner customer, byte[] signature, bool generateFullVersion)
        {
            SaleOrder           = saleOrder.SaleOrder;
            OrderItems          = items;
            Customer            = customer;
            model               = saleOrder;
            Signature           = signature;
            GenerateFullVersion = generateFullVersion;

            //Create a new document
            _document = new PdfDocument();
            _document.PageSettings.Size        = PdfPageSize.A4;
            _document.PageSettings.Orientation = PdfPageOrientation.Portrait;
            _document.PageSettings.SetMargins(10, 10);

            PdfPage     page     = _document.Pages.Add();
            PdfGraphics graphics = page.Graphics;


            if (true)
            {
                var format = new PdfStringFormat();
                //format.NoClip = true;
                format.Alignment     = PdfTextAlignment.Center;
                format.WordWrap      = PdfWordWrapType.Word;
                format.LineAlignment = PdfVerticalAlignment.Bottom;

                PdfPageTemplateElement footer = new PdfPageTemplateElement(_document.Pages[0].GetClientSize().Width - 100, 30);

                PdfFont  font  = new PdfStandardFont(PdfFontFamily.Helvetica, 7);
                PdfBrush brush = new PdfSolidBrush(Color.Black);

                footer.Alignment = PdfAlignmentStyle.BottomCenter;

                footer.Graphics.DrawLine(new PdfPen(Color.FromArgb(255, 0, 0, 0))
                {
                    Width = 0.25f
                }, 0, 0, _document.Pages[0].GetClientSize().Width - 20, 0);
                footer.Graphics.DrawString(footerPage, font, brush, new RectangleF(0, 0, _document.Pages[0].GetClientSize().Width - 100, 30), format);

                _document.Template.Bottom = footer;
            }

            //CONTENT PRINCIPAL PAGE
            DrawPage(page, graphics);

            var stream = new MemoryStream();

            //save the document into stream

            _document.Save(stream);

            PdfDocumentStream = stream;

            ArrayBytes = stream.ToArray();

            //close the document
            _document.Close(true);

            Debug.WriteLine("End of creating file");

            return(ArrayBytes);
        }