Beispiel #1
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="attrs"></param>
        public BlockStyle(XmlAttributeCollection attrs) : base(attrs)
        {
            if (attrs != null)
            {
                if (PDFDraw.XmlHelper.GetAttributeValue(BorderColorAttributeConstant, attrs, null) != null)
                {
                    BorderColor = PDFDraw.XmlHelper.GetAttributeColor(BorderColorAttributeConstant, attrs, "White");
                }
                this.BorderWidth    = PDFDraw.XmlHelper.GetFloatAttributeValue(BorderWithAttributeConstant, attrs, 1.0F);
                this.FillBackground = PDFDraw.XmlHelper.GetAttributeBoolean(FillBackgroundAttributeConstant, attrs, false);

                this.Borders = PDFDrawItextSharpHelper.Border(Moon.PDFDraw.XmlHelper.GetStringArray(BorderAttributeConstant, attrs));
                if (this.Borders != iTextSharp.text.Rectangle.NO_BORDER)
                {
                    if (BorderWidth <= 0)
                    {
                        BorderWidth = 1.0F;                                     //defaults
                    }
                    if (BorderColor == null)
                    {
                        BorderColor = Color.Black;                                        //defaults.
                    }
                }
            }
        }
        /// <summary>
        /// Add a row of pdfpcell to table.
        /// return True, current page have enought size for that row
        /// return False, current page not enought size for that row, row will be remove and need to draw on
        /// next page
        /// 20130606 :: jaimelopez :: mellorasinxelas to support absolute footer.
        /// </summary>
        /// <param name="table"></param>
        /// <param name="tableRowElement"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        protected bool DrawTableRow(
            PdfPTable table,
            TableRow tableRowElement,
            IDictionary data)
        {
            bool enoughSpace = true;

            PDFDrawItextSharp.PDFDrawItextSharp pdfDraw = (PDFDrawItextSharp.PDFDrawItextSharp)pdfDrawer;
            foreach (TableCell tableCell in tableRowElement.TableCells)
            {
                PdfPCell cell = pdfDraw.CreateTableCell(tableCell.Attributes, data);
                foreach (DrawElement drawElement in tableCell.DrawElements)
                {
                    if (drawElement is TextBox)
                    {
                        //iTextSharp.text.Phrase phrase = _pdfDraw.CreatePhrase(
                        //    ((PDFTemplate.TextBox)drawElement).GetText(data), drawElement.FontAttributes);
                        Paragraph paragraph = pdfDraw.CreateParagraph(((TextBox)drawElement).GetText(data), drawElement.FontAttributes);
                        paragraph.Alignment = PDFDrawItextSharpHelper.Align(Helper.GetAttributeValue("align", drawElement.Attributes, "Left"));

                        cell.AddElement(paragraph);
                    }
                    else if (drawElement is PDFTemplate.Image)
                    {
                        //iTextSharp.text.Image image = pdfDraw.CreateImageFromAttribute(drawElement.Attributes);
                        //image.Alignment = PDFDrawItextSharpHelper.Align(Helper.GetAttributeValue("align", drawElement.Attributes, "Left"));

                        //cell.AddElement(image);

                        // 2017-11-23 : fix error image in table
                        string src = Moon.PDFDraw.Helper.GetAttributeValue("src", drawElement.Attributes, "");
                        if (data.Contains(src))
                        {
                            iTextSharp.text.Image image = pdfDraw.CreateImageFromAttribute(data[src].ToString(), drawElement.Attributes);
                            image.Alignment = PDFDrawItextSharpHelper.Align(Helper.GetAttributeValue("align", drawElement.Attributes, "Left"));

                            cell.AddElement(image);
                        }
                    }
                }
                table.AddCell(cell);
            }
            table.CompleteRow();

            //if(table.row
            //fixme need to check if any row span
            if (pdfDrawer.isNoMoreY(table.TotalHeight, DocumentGroup.Table))
            {
                enoughSpace = false;
                table.DeleteLastRow();
            }

            return(enoughSpace);
        }
        /// <summary>
        /// implement for PDFTemplate
        /// this will call when initialize PDFTemplate object!
        /// </summary>
        protected override void SetPageDefWidthHeight(Orientation orientation)
        {
            // this will call from PDFTemplate._buildPageDef()
            if (pageSize == null)
            {
                pageSize = PDFDrawItextSharpHelper.PageSize(Helper.GetAttributeValue("pagesize", PageDefinition.PageDefAttrs, "A4"));
            }

            if (orientation != CurrentOrientation)
            {
                this.CurrentOrientation = orientation;

                pageSize = pageSize.Rotate();
                PdfDrawer?.RotatePage();
            }

            PageDefinition.Width  = pageSize.Width;
            PageDefinition.Height = pageSize.Height;
        }
        private void DrawPageNumber(Font font)
        {
#if DEBUG
            //Console.WriteLine("pageNumberBox: " + pageNumberBox.X + ", " + pageNumberBox.Y);
#endif
            PdfReader    reader  = new PdfReader(stream.ToArray());
            MemoryStream _stream = new MemoryStream();
            PdfStamper   stamper = new PdfStamper(reader, _stream);

            int currentPage = 1;
            int totalPage   = 0;
            int drawPage    = 1;

            PDFDrawItextSharp.PDFDrawItextSharp pDraw = (PDFDrawItextSharp.PDFDrawItextSharp)PdfDrawer;
            FontSelector selector = pDraw.FontSelector(font);

            foreach (int eachCount in EachPageCount)
            {
                if (drawPage == 1)
                {
                    currentPage = 1;
                    totalPage   = eachCount;
                }
                else
                {
                    currentPage = 1;
                    totalPage   = eachCount - totalPage;
                }
                while (drawPage <= eachCount)
                {
#if DEBUG
                    //Console.WriteLine("PDFTemplateItextSharp.DrawPageNumber: " + currentPage + "/" + totalPage);
#endif
                    if (PageNumberBoxes.Count < currentPage)
                    {
                        break;
                    }
                    var pageNumberBox = PageNumberBoxes[currentPage - 1];

                    Hashtable data = new Hashtable
                    {
                        { "{__PAGE__}", currentPage },
                        { "{__TOTALPAGE__}", totalPage }
                    };

                    PdfContentByte cb = stamper.GetOverContent(drawPage);
                    //iTextSharp.text.Phrase phrase = new iTextSharp.text.Chunk(pageNumberBox.GetText(data), font);
                    Phrase phrase = selector.Process(pageNumberBox.GetText(data));

                    //Console.WriteLine("chunk.Content: " + chunk.Content);
                    //iTextSharp.text.pdf.ColumnText column = new iTextSharp.text.pdf.ColumnText(cb);
                    int align = PDFDrawItextSharpHelper.Align(
                        Helper.GetAttributeValue(
                            "align",
                            pageNumberBox.Attributes, "Left"));

                    //20130610 :: mellorasinxelas :: change to static class
                    //20130721 :: cmwong :: change to PDFDrawItextSharp.DrawString()
                    pDraw.DrawString(cb, phrase, pageNumberBox.X, pageNumberBox.Y, pageNumberBox.Width, align, null);

                    currentPage++;
                    drawPage++;
                }
            }
            stamper.Close();
            stream = _stream;
        }