Exemple #1
0
        private bool addImage(string sFilename, iTextSharp.text.Document doc)
        {
            bool bReturn = false;
            iTextSharp.text.Image img;
            try
            {
                Paragraph p1 = new Paragraph(new Chunk(sFilename, FontFactory.GetFont(FontFactory.HELVETICA, 12)));
                doc.Add(p1);

#if !TEST
                Bitmap myBitmap = new Bitmap(sFilename);
                if (sFilename.ToLower().EndsWith("jpg"))
                    img = iTextSharp.text.Image.GetInstance(myBitmap, System.Drawing.Imaging.ImageFormat.Jpeg);
                else if (sFilename.ToLower().EndsWith("gif"))
                    img = iTextSharp.text.Image.GetInstance(myBitmap, System.Drawing.Imaging.ImageFormat.Gif);
                else if (sFilename.ToLower().EndsWith("bmp"))
                    img = iTextSharp.text.Image.GetInstance(myBitmap, System.Drawing.Imaging.ImageFormat.Bmp);
                else if (sFilename.ToLower().EndsWith("png"))
                    img = iTextSharp.text.Image.GetInstance(myBitmap, System.Drawing.Imaging.ImageFormat.Png);
                else
                    throw new NotSupportedException("Unsupported image format");
                //is the image to wide or to high?
                float fWidth=doc.Right - doc.Left - doc.RightMargin - doc.LeftMargin;
                float fHeight=doc.Top - doc.Bottom - doc.TopMargin - doc.BottomMargin;
                if ((myBitmap.Width > fWidth) || (myBitmap.Height>fHeight))
                    img.ScaleToFit(fWidth, fHeight);

#else
                img = iTextSharp.text.Image.GetInstance(sFilename);
#endif
                doc.Add(img);
                doc.NewPage(); //used to create a new page for every image
                bReturn = true;
            }
            catch (iTextSharp.text.BadElementException bx)
            {
                System.Diagnostics.Debug.WriteLine("BadElementException in doc.add() for '" + sFilename + "': " + bx.Message);
            }
            catch (iTextSharp.text.DocumentException dx)
            {
                System.Diagnostics.Debug.WriteLine("DocumentException in doc.add() for '" + sFilename + "': " + dx.Message);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception in doc.add() for '" + sFilename + "': " + ex.Message);
            }
            return bReturn;
        }
        public override void OnStartPage(PdfWriter writer, iTextSharp.text.Document document)
        {
            if (ctr == 0)
            {
                ctr++;
                return;
            }

            iTextSharp.text.Image header = iTextSharp.text.Image.GetInstance(PathResolver.MapPath("images/ADB_Logo.gif"));

            PdfPTable tableHeader = new PdfPTable(2);
            tableHeader.WidthPercentage = 100;

            PdfPCell imageHeaderCell = new PdfPCell(header);
            imageHeaderCell.Rowspan = 2;
            imageHeaderCell.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
            imageHeaderCell.Border = 0;
            imageHeaderCell.PaddingBottom = 3;
            tableHeader.AddCell(imageHeaderCell);

            iTextSharp.text.Font helvetica20 = FontFactory.GetFont(FontFactory.HELVETICA, 16);
            PdfPCell typeNameCell = new PdfPCell(new Phrase(""));
            typeNameCell.HorizontalAlignment = 2;
            typeNameCell.Border = 0;
            tableHeader.AddCell(typeNameCell);

            PdfPCell adbCell = new PdfPCell(new Phrase(GlobalStringResource.ADB, helvetica20));
            adbCell.HorizontalAlignment = 2;
            adbCell.Border = 0;
            tableHeader.AddCell(adbCell);

            PdfPCell lineCell = new PdfPCell();
            lineCell.Colspan = 2;
            lineCell.Border = 1;
            lineCell.PaddingBottom = 5;
            tableHeader.AddCell(lineCell);

            document.Add(tableHeader);

            PdfPTable table = new PdfPTable(7);
            table.SplitLate = false;
            table.KeepTogether = false;
            table.SkipFirstHeader = true;

            table.WidthPercentage = 100;
            table.SetWidths(new float[] {
                80,
                84,
                143,
                95,
                79,
                159,
                160
            });

            System.Drawing.Color c = ColorTranslator.FromHtml("#6495ED");
            BaseColor realColor = new BaseColor(c);
            iTextSharp.text.Font font = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10, BaseColor.WHITE);

            //table.AddCell(new PdfPCell(
            //    new Phrase("Type")) { BackgroundColor = realColor, Padding = 5 });
            table.AddCell(new PdfPCell(
                new Phrase("Agenda", font)) { BackgroundColor = realColor, Padding = 5 });
            table.AddCell(new PdfPCell(
                new Phrase("Business Policy", font)) { BackgroundColor = realColor, Padding = 5 });
            table.AddCell(new PdfPCell(
                new Phrase("Business Rule", font)) { BackgroundColor = realColor, Padding = 5 });
            table.AddCell(new PdfPCell(
                new Phrase("Process", font)) { BackgroundColor = realColor, Padding = 5 });
            table.AddCell(new PdfPCell(
                new Phrase("Application", font)) { BackgroundColor = realColor, Padding = 5 });
            table.AddCell(new PdfPCell(
                new Phrase("Sub-Process", font)) { BackgroundColor = realColor, Padding = 5 });
            table.AddCell(new PdfPCell(
                new Phrase("Module", font)) { BackgroundColor = realColor, Padding = 5 });

            document.Add(table);
        }
Exemple #3
0
        public static void AddValues2Paragraph(iTextSharp.text.Paragraph para, ArrayList values, Font font)
        {
            const string METHOD_NAME = "AddValues2Paragraph";
            try {

                iTextSharp.text.Phrase phrase = null;
                foreach (string s in values) {
                    if (s.Length > 0) {
                        if (phrase != null) {
                            phrase = new Phrase(@" \ " + s, font);
                        } else {
                            phrase = new Phrase(s, font);
                        }
                        para.Add(phrase);
                    }
                }
            }
            catch (Exception ex) {
                ApplicationException aex = new ApplicationException(MOD_NAME + METHOD_NAME, ex);
                throw(aex);
            }
        }
Exemple #4
0
        //PdfContentByte cb;
        //PdfTemplate template;
        //public override void OnOpenDocument(PdfWriter writer, Document document)
        //{
        //    cb = writer.DirectContent;
        //    template = cb.CreateTemplate(50, 50);
        //}
        public override void OnStartPage(iTextSharp.text.pdf.PdfWriter writer ,iTextSharp.text.Document document )
        {
            base.OnStartPage(writer, document);
            #region ENCABEZADO
            //agregar imagen
            iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(AppDomain.CurrentDomain.BaseDirectory.ToString() + "/img/cofiño_stahl.jpg");
            //jpg.Alignment = iTextSharp.text.Image.MIDDLE_ALIGN;
            jpg.ScalePercent(25f);

            //We are going to add two strings in header. Create separate Phrase object with font setting and string to be included
            Phrase p1Header = new Phrase("Facturación Electrónica", FontFactory.GetFont("verdana", 14));
            Phrase p2Header = new Phrase(DateTime.Now.ToLongDateString(), FontFactory.GetFont("verdana", 8));

            //create iTextSharp.text Image object using local image path
            iTextSharp.text.Image imgPDF = iTextSharp.text.Image.GetInstance(HttpRuntime.AppDomainAppPath + "\\img\\cofiño_stahl.jpg");
            imgPDF.ScalePercent(25f);
            //Create PdfTable object
            PdfPTable pdfTab = new PdfPTable(3);
            //We will have to create separate cells to include image logo and 2 separate strings
            PdfPCell pdfCell1 = new PdfPCell(imgPDF);
            PdfPCell pdfCell2 = new PdfPCell(p1Header);
            PdfPCell pdfCell3 = new PdfPCell(p2Header);
            //set the alignment of all three cells and set border to 0
            pdfCell1.HorizontalAlignment = Element.ALIGN_LEFT;
            pdfCell2.HorizontalAlignment = Element.ALIGN_LEFT;
            pdfCell3.HorizontalAlignment = Element.ALIGN_LEFT;
            pdfCell1.Border = 0;
            pdfCell2.Border = 0;
            pdfCell3.Border = 0;
            pdfCell2.VerticalAlignment = Element.ALIGN_MIDDLE;
            pdfCell3.VerticalAlignment = Element.ALIGN_BOTTOM;
            //establecer anchos
            float[] withsHead = new float[] { 14f, 49f, 21f };
            //add all three cells into PdfTable
            pdfTab.AddCell(pdfCell1);
            pdfTab.AddCell(pdfCell2);
            pdfTab.AddCell(pdfCell3);
            pdfTab.SetWidthPercentage(withsHead, document.PageSize);
            pdfTab.TotalWidth = document.PageSize.Width - 30;
            //call WriteSelectedRows of PdfTable. This writes rows from PdfWriter in PdfTable
            //first param is start row. -1 indicates there is no end row and all the rows to be included to write
            //Third and fourth param is x and y position to start writing
            pdfTab.WriteSelectedRows(0, -1, 30, document.PageSize.Height - 15, writer.DirectContent);
            //set pdfContent value
            PdfContentByte pdfContent = writer.DirectContent;
            //Move the pointer and draw line to separate header section from rest of page
            pdfContent.MoveTo(30, document.PageSize.Height - 45);
            pdfContent.LineTo(document.PageSize.Width - 40, document.PageSize.Height - 45);
            pdfContent.Stroke();
            document.Add(new Phrase(Chunk.NEWLINE));
            document.Add(new Phrase(Chunk.NEWLINE));
            document.Add(new Phrase(Chunk.NEWLINE));
            #endregion
        }
        /// <summary>
        /// Add the header page to the document.  This shows an example of a page containing
        /// both text and images.  The contents of the page are centered and the text is of
        /// various sizes.
        /// </summary>
        /// <param name="doc"></param>
        private void AddPageWithBasicFormatting(iTextSharp.text.Document doc)
        {
            // Write page content.  Note the use of fonts and alignment attributes.
            this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _largeFont, new iTextSharp.text.Chunk("\n\n"));
            this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _largeFont, new Chunk("MY SCIENCE PROJECT\n\n"));
            this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _standardFont, new Chunk("by M. Lichtenberg"));
            this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _largeFont, new Chunk("\n\n\n"));

            // Add a logo
            String appPath = System.IO.Directory.GetCurrentDirectory();
            iTextSharp.text.Image logoImage = iTextSharp.text.Image.GetInstance(appPath + "\\PaperAirplane.jpg");
            logoImage.Alignment = iTextSharp.text.Element.ALIGN_CENTER;
            doc.Add(logoImage);
            logoImage = null;

            // Write additional page content
            this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _largeFont, new Chunk("\n\n\n"));
            this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _largeFont, new Chunk("What kind of paper is the best for making paper airplanes?"));
            this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _largeFont, new Chunk("\n\n\n\n\n"));
            this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _smallFont, new Chunk("Generated " +
                DateTime.Now.Day.ToString() + " " +
                System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(DateTime.Now.Month) + " " +
                DateTime.Now.Year.ToString() + " " +
                DateTime.Now.ToShortTimeString()));
        }
        /// <summary>
        /// Add a page containing a single image.  Set the page size to match the image size.
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="imagePath"></param>
        private void AddPageWithImage(iTextSharp.text.Document doc, String imagePath)
        {
            // Read the image file
            iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(new Uri(imagePath));

            // Set the page size to the dimensions of the image BEFORE adding a new page to the document.
            // Pad the height a bit to leave room for the page header.
            float imageWidth = image.Width;
            float imageHeight = image.Height;
            doc.SetMargins(0, 0, 0, 0);
            doc.SetPageSize(new iTextSharp.text.Rectangle(imageWidth, imageHeight + 100));

            // Add a new page
            doc.NewPage();

            // The header at the top of the page is an anchor linked to by the table of contents.
            iTextSharp.text.Anchor contentsAnchor = new iTextSharp.text.Anchor("\nGRAPH\n\n", _largeFont);
            contentsAnchor.Name = "graph";

            // Add the anchor and image to the page
            this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _largeFont, contentsAnchor);
            doc.Add(image);
            image = null;
        }
        /// <summary>
        /// Add a page that includes a bullet list.
        /// </summary>
        /// <param name="doc"></param>
        private void AddPageWithBulletList(iTextSharp.text.Document doc)
        {
            // Add a new page to the document
            doc.NewPage();

            // The header at the top of the page is an anchor linked to by the table of contents.
            iTextSharp.text.Anchor contentsAnchor = new iTextSharp.text.Anchor("RESEARCH\n\n", _largeFont);
            contentsAnchor.Name = "research";

            // Add the header anchor to the page
            this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _largeFont, contentsAnchor);

            // Create an unordered bullet list.  The 10f argument separates the bullet from the text by 10 points
            iTextSharp.text.List list = new iTextSharp.text.List(iTextSharp.text.List.UNORDERED, 10f);
            list.SetListSymbol("\u2022");   // Set the bullet symbol (without this a hypen starts each list item)
            list.IndentationLeft = 20f;     // Indent the list 20 points
            list.Add(new ListItem("Lift, thrust, drag, and gravity are the four forces that act on a plane.", _standardFont));
            list.Add(new ListItem("A plane should be light to help fight against gravity's pull to the ground.", _standardFont));
            list.Add(new ListItem("Gravity will have less effect on a plane built from the lightest materials available.", _standardFont));
            list.Add(new ListItem("In order to fly well, airplanes must be stable.", _standardFont));
            list.Add(new ListItem("A plane that is unstable will either pitch up into a stall, or nose-dive.", _standardFont));
            doc.Add(list);  // Add the list to the page

            // Add some white space and another heading
            this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _largeFont, new Chunk("\n\n\n"));
            this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_CENTER, _largeFont, new Chunk("HYPOTHESIS\n\n"));

            // Add some final text to the page
            this.AddParagraph(doc, iTextSharp.text.Element.ALIGN_LEFT, _standardFont, new Chunk("Given five paper airplanes made out of newspaper, printer paper, construction paper, paper towel, and posterboard, the airplane made out of printer paper will fly the furthest."));
        }
        public override void OnStartPage(PdfWriter writer, iTextSharp.text.Document document)
        {
            iTextSharp.text.Image header = iTextSharp.text.Image.GetInstance(PathResolver.MapPath("images/ADB_Logo.gif"));

            PdfPTable tableHeader = new PdfPTable(2);
            tableHeader.WidthPercentage = 100;

            PdfPCell imageHeaderCell = new PdfPCell(header);
            imageHeaderCell.Rowspan = 2;
            imageHeaderCell.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
            imageHeaderCell.Border = 0;
            imageHeaderCell.PaddingBottom = 3;
            tableHeader.AddCell(imageHeaderCell);

            Font helvetica20 = FontFactory.GetFont(FontFactory.HELVETICA, 16);
            PdfPCell typeNameCell = new PdfPCell(new Phrase(TypesReader.GetDiagramName(this.type), helvetica20));
            typeNameCell.HorizontalAlignment = 2;
            typeNameCell.Border = 0;
            tableHeader.AddCell(typeNameCell);

            PdfPCell adbCell = new PdfPCell(new Phrase(GlobalStringResource.ADB, helvetica20));
            adbCell.HorizontalAlignment = 2;
            adbCell.Border = 0;
            tableHeader.AddCell(adbCell);

            PdfPCell lineCell = new PdfPCell();
            lineCell.Colspan = 2;
            lineCell.Border = 1;
            lineCell.PaddingBottom = 5;
            tableHeader.AddCell(lineCell);

            document.Add(tableHeader);
        }