コード例 #1
0
        public virtual void CreatePdf(String dest)
        {
            // Initialize PDF document
            PdfDocument pdf      = new PdfDocument(new PdfWriter(dest));
            Document    document = new Document(pdf);

            iText.Layout.Element.Image img1 = new Image(ImageDataFactory.Create(TEST1));
            img1.ScaleToFit(100, 100).SetDestination("Top");
            document.Add(img1);
            iText.Layout.Element.Image img2 = new iText.Layout.Element.Image(ImageDataFactory.Create(TEST2));
            img2.SetHeight(300);
            document.Add(img2);
            iText.Layout.Element.Image img3 = new iText.Layout.Element.Image(ImageDataFactory.Create(TEST3));
            img3.ScaleToFit(100, 100);
            img3.SetBackgroundColor(ColorConstants.BLUE);
            document.Add(img3);
            iText.Layout.Element.Image img4 = new iText.Layout.Element.Image(ImageDataFactory.Create(TEST4));
            img4.ScaleToFit(100, 100);
            img4.SetBackgroundColor(ColorConstants.RED);
            document.Add(img4);
            iText.Layout.Element.Image img5 = new iText.Layout.Element.Image(ImageDataFactory.Create(TEST5));
            img5.ScaleToFit(50, 50);
            Style style = new Style();

            style.SetBorderRight(new SolidBorder(2));
            img5.AddStyle(style);
            document.Add(img5);
            iText.Layout.Element.Image img6 = new iText.Layout.Element.Image(ImageDataFactory.Create(TEST6));
            PdfAction top = PdfAction.CreateGoTo("Top");

            img6.ScaleToFit(100, 100).SetAction(top);
            document.Add(img6);
            document.Close();
        }
コード例 #2
0
ファイル: ImageTest.cs プロジェクト: zymemail/itext7-dotnet
        public virtual void ImageTest23()
        {
            String      outFileName = destinationFolder + "imageTest23.pdf";
            String      cmpFileName = sourceFolder + "cmp_imageTest23.pdf";
            PdfWriter   writer      = new PdfWriter(outFileName);
            PdfDocument pdfDoc      = new PdfDocument(writer);
            Document    doc         = new Document(pdfDoc);

            iText.Layout.Element.Image image = new iText.Layout.Element.Image(ImageDataFactory.Create(sourceFolder + "encoded_tiff.tiff"
                                                                                                      ));
            image.ScaleToFit(500, 500);
            doc.Add(image);
            doc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
                                                                             , "diff"));
        }
コード例 #3
0
        private void SetLogo(PdfFormField toSet, iText.Kernel.Pdf.PdfDocument pdfDoc, string filename, int pagina)
        {
            var b          = toSet as PdfButtonFormField;
            var afmetingen = b.GetWidgets().SelectMany(f => f.GetRectangle()).ToArray();
            var x          = (int)Convert.ToDouble(afmetingen[0].ToString().Replace(".", ","));

            if (x < 10)
            {
                x = 100;
            }
            var y         = (int)Convert.ToDouble(afmetingen[1].ToString().Replace(".", ","));
            var wWidth    = (int)Convert.ToDouble(afmetingen[2].ToString().Replace(".", ","));
            var pageWidth = (int)pdfDoc.GetPage(1).GetPageSizeWithRotation().GetWidth();

            if (wWidth > pageWidth - 20)
            {
                wWidth = pageWidth - 20;
            }
            var wHeight = (int)Convert.ToDouble(afmetingen[3].ToString().Replace(".", ","));

            if (pagina == 1)
            {
                wHeight -= 10;
            }

            ImageData img          = ImageDataFactory.Create(filename);
            var       pdfImage     = new iText.Layout.Element.Image(img);
            var       scaled       = pdfImage.ScaleToFit(wWidth, wHeight - y);
            var       scaledWidth  = scaled.GetImageScaledWidth();
            var       scaledHeight = scaled.GetImageScaledHeight();
            Document  d            = new Document(pdfDoc);

            var berekendeX = (x + wWidth - scaledWidth) / 2;
            var berekendeY = (y + wHeight - scaledHeight) / 2;

            scaled.SetFixedPosition(pagina, berekendeX, berekendeY);
            d.Add(scaled);
            b.SetValue("");
        }
コード例 #4
0
        public string crear_pdf(Entities.Model.Order order, List <CartItem> carrito, Shipping ship)
        {
            MemoryStream ms          = new MemoryStream();
            PdfWriter    pw          = new PdfWriter(ms);
            PdfDocument  pdfdocument = new PdfDocument(pw);
            Document     doc         = new Document(pdfdocument, PageSize.A4);

            doc.SetMargins(75, 35, 70, 35);

            //PdfFont font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);

            string pathlogo = Server.MapPath("~/Content/assets/img/core-img/logo.png");

            iText.Layout.Element.Image img = new iText.Layout.Element.Image(ImageDataFactory.Create(pathlogo));

            //Cabecera
            iText.Layout.Element.Cell  separador = new iText.Layout.Element.Cell(1, 1);
            iText.Layout.Element.Table table     = new iText.Layout.Element.Table(2).UseAllAvailableWidth() /*.SetBorder(Border.NO_BORDER)*/;
            iText.Layout.Element.Cell  cell      = new iText.Layout.Element.Cell(4, 1).SetTextAlignment(TextAlignment.LEFT).SetBorder(Border.NO_BORDER);
            cell.Add(img.ScaleToFit(150, 150));
            table.AddCell(cell);
            cell = new iText.Layout.Element.Cell(2, 2).Add(new Paragraph("Factura No: 0177-" + string.Format("{0:00000000}", order.Id)))
                   .SetTextAlignment(TextAlignment.RIGHT)
                   .SetBorder(Border.NO_BORDER);
            table.AddCell(cell);
            cell = new iText.Layout.Element.Cell(2, 2).Add(new Paragraph("Fecha: " + DateTime.Today.ToString("dd-MM-yyyy", CultureInfo.InvariantCulture)))
                   .SetTextAlignment(TextAlignment.RIGHT).SetBorder(Border.NO_BORDER);
            table.AddCell(cell);

            doc.Add(table);


            //Datos Cliente
            iText.Layout.Element.Table _clienttable = new iText.Layout.Element.Table(1).UseAllAvailableWidth().SetMarginTop(20) /*.SetBorder(Border.NO_BORDER)*/;
            _clienttable.AddCell(separador);
            iText.Layout.Element.Cell _clientecell = new iText.Layout.Element.Cell(1, 1);
            _clientecell.Add(new Paragraph("Nombre: " + ship.FirstName + " " + ship.LastName))
            .SetTextAlignment(TextAlignment.LEFT)
            .SetMarginBottom(10).SetBorder(Border.NO_BORDER);
            _clienttable.AddCell(_clientecell);
            _clientecell = new iText.Layout.Element.Cell(1, 1).Add(new Paragraph("Dirección: " + ship.Address))
                           .SetTextAlignment(TextAlignment.LEFT).SetMarginBottom(10).SetBorder(Border.NO_BORDER);

            _clienttable.AddCell(_clientecell);
            _clientecell = new iText.Layout.Element.Cell(1, 1).Add(new Paragraph("Ciudad: " + ship.City))
                           .SetTextAlignment(TextAlignment.LEFT).SetMarginBottom(10).SetBorder(Border.NO_BORDER);
            _clienttable.AddCell(_clientecell);
            _clientecell = new iText.Layout.Element.Cell(1, 1).Add(new Paragraph("País: " + ship.Country))
                           .SetTextAlignment(TextAlignment.LEFT).SetMarginBottom(10).SetBorder(Border.NO_BORDER);
            _clienttable.AddCell(_clientecell);
            _clientecell = new iText.Layout.Element.Cell(1, 1).Add(new Paragraph("Email: " + ship.Email))
                           .SetTextAlignment(TextAlignment.LEFT).SetMarginBottom(10).SetBorder(Border.NO_BORDER);
            _clienttable.AddCell(_clientecell);
            _clienttable.AddCell(separador);
            doc.Add(_clienttable);


            //doc.Add(ls);
            //Datos Product Encabezado
            iText.Layout.Element.Table _prodtable = new iText.Layout.Element.Table(6).UseAllAvailableWidth().SetMarginTop(20) /*.SetBorder(Border.NO_BORDER)*/;

            iText.Layout.Element.Cell _prodecell = new iText.Layout.Element.Cell(1, 1).Add(new Paragraph("# Item"))
                                                   .SetTextAlignment(TextAlignment.CENTER)
                                                   .SetMarginBottom(10);
            _prodtable.AddCell(_prodecell);
            _prodecell = new iText.Layout.Element.Cell(1, 1).Add(new Paragraph("Codigo Articulo"))
                         .SetTextAlignment(TextAlignment.CENTER).SetMarginBottom(10);
            _prodtable.AddCell(_prodecell);
            _prodecell = new iText.Layout.Element.Cell(1, 1).Add(new Paragraph("Descripcion"))
                         .SetTextAlignment(TextAlignment.CENTER).SetMarginBottom(10);
            _prodtable.AddCell(_prodecell);
            _prodecell = new iText.Layout.Element.Cell(1, 1).Add(new Paragraph("Precio Unitario"))
                         .SetTextAlignment(TextAlignment.CENTER).SetMarginBottom(10);
            _prodtable.AddCell(_prodecell);
            _prodecell = new iText.Layout.Element.Cell(1, 1).Add(new Paragraph("Cantidad"))
                         .SetTextAlignment(TextAlignment.CENTER).SetMarginBottom(10);
            _prodtable.AddCell(_prodecell);
            _prodecell = new iText.Layout.Element.Cell(1, 1).Add(new Paragraph("Subtotal"))
                         .SetTextAlignment(TextAlignment.CENTER).SetMarginBottom(10);
            _prodtable.AddCell(_prodecell);

            int x = 0;

            foreach (CartItem item in carrito)
            {
                x++;
                _prodecell = new iText.Layout.Element.Cell(1, 1).Add(new Paragraph(x.ToString())).SetBorder(Border.NO_BORDER)
                             .SetTextAlignment(TextAlignment.CENTER).SetMarginBottom(10);
                _prodtable.AddCell(_prodecell);
                _prodecell = new iText.Layout.Element.Cell(1, 1).Add(new Paragraph(item.ProductId.ToString())).SetBorder(Border.NO_BORDER)
                             .SetTextAlignment(TextAlignment.CENTER).SetMarginBottom(10);
                _prodtable.AddCell(_prodecell);
                _prodecell = new iText.Layout.Element.Cell(1, 1).Add(new Paragraph(item.Product.Title.ToString())).SetBorder(Border.NO_BORDER)
                             .SetTextAlignment(TextAlignment.CENTER).SetMarginBottom(10);
                _prodtable.AddCell(_prodecell);
                _prodecell = new iText.Layout.Element.Cell(1, 1).Add(new Paragraph(item.Price.ToString())).SetBorder(Border.NO_BORDER)
                             .SetTextAlignment(TextAlignment.CENTER).SetMarginBottom(10);
                _prodtable.AddCell(_prodecell);
                _prodecell = new iText.Layout.Element.Cell(1, 1).Add(new Paragraph(item.Quantity.ToString())).SetBorder(Border.NO_BORDER)
                             .SetTextAlignment(TextAlignment.CENTER).SetMarginBottom(10);
                _prodtable.AddCell(_prodecell);
                _prodecell = new iText.Layout.Element.Cell(1, 1).Add(new Paragraph((item.Quantity * item.Price).ToString())).SetBorder(Border.NO_BORDER)
                             .SetTextAlignment(TextAlignment.CENTER).SetMarginBottom(10);
                _prodtable.AddCell(_prodecell);
            }
            doc.Add(_prodtable);

            //Total
            iText.Layout.Element.Table totaltable = new iText.Layout.Element.Table(1).UseAllAvailableWidth().SetMarginTop(20) /*.SetBorder(Border.NO_BORDER)*/;

            iText.Layout.Element.Cell totalcell = new iText.Layout.Element.Cell(1, 1).Add(new Paragraph("Total: $" + order.TotalPrice))
                                                  .SetTextAlignment(TextAlignment.RIGHT)
                                                  .SetMarginBottom(10).SetBorder(Border.NO_BORDER);
            totaltable.AddCell(totalcell);
            doc.Add(totaltable);
            //pdfdocument.AddEventHandler(PdfDocumentEvent.END_PAGE, new HeaderEventHandler1(img));

            //iText.Layout.Element.Table _table = new iText.Layout.Element.Table(1).UseAllAvailableWidth();

            doc.Close();

            byte[] byteStream    = ms.ToArray();
            var    inputAsString = Convert.ToString(Convert.ToBase64String(ms.ToArray()));

            ms = new MemoryStream();
            ms.Write(byteStream, 0, byteStream.Length);
            ms.Position = 0;
            var pdf = new FileStreamResult(ms, "application/pdf");

            /*return new FileStreamResult(ms, "application/pdf")*/;

            return(inputAsString.ToString());
        }