Ejemplo n.º 1
0
        /// <summary>
        /// 处理单个页面。
        /// </summary>
        /// <param name="pgs"></param>
        /// <param name="items"></param>
        private void ProcessPage(Pages pgs, IEnumerable items)
        {
            foreach (PageItem pi in items)
            {
                //if (pi.SI.BackgroundImage != null)
                //{
                //    PageImage bgImg = pi.SI.BackgroundImage;
                //    iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(bgImg.ImageData);
                //    document.Add(image);
                //}

                if (pi is PageTextHtml)
                {
                    PageTextHtml pth = pi as PageTextHtml;
                    pth.Build(pgs.G);
                    ProcessPage(pgs, pth);
                    continue;
                }

                if (pi is PageText)
                {
                    PageText pt = pi as PageText;

                    iTextSharp.text.pdf.PdfContentByte cb = this.pdfWriter.DirectContent;

                    float y = getYvalue(pi.Y, pt.H);

                    //边线

                    if (pi.SI.BStyleLeft != BorderStyleEnum.None)
                    {
                        cb.MoveTo(pt.X, y);
                        cb.SetLineWidth(pi.SI.BWidthLeft);
                        cb.SetRGBColorStrokeF(pi.SI.BColorLeft.R, pi.SI.BColorLeft.G, pi.SI.BColorLeft.B);

                        cb.LineTo(pt.X, y + pt.H);

                        cb.Stroke();
                    }

                    if (pi.SI.BStyleRight != BorderStyleEnum.None)
                    {
                        cb.MoveTo(pt.X + pt.W, y);
                        cb.SetLineWidth(pi.SI.BWidthRight);
                        cb.SetRGBColorStrokeF(pi.SI.BColorRight.R, pi.SI.BColorRight.G, pi.SI.BColorRight.B);

                        cb.LineTo(pt.X + pt.W, y + pt.H);

                        cb.Stroke();
                    }

                    if (pi.SI.BStyleTop != BorderStyleEnum.None)
                    {
                        cb.MoveTo(pt.X, y + pt.H);
                        cb.SetLineWidth(pi.SI.BWidthTop);
                        cb.SetRGBColorStrokeF(pi.SI.BColorTop.R, pi.SI.BColorTop.G, pi.SI.BColorTop.B);

                        cb.LineTo(pt.X + pt.W, y + pt.H);

                        cb.Stroke();
                    }

                    if (pi.SI.BStyleBottom != BorderStyleEnum.None)
                    {
                        cb.MoveTo(pt.X, y);
                        cb.SetLineWidth(pi.SI.BWidthTop);
                        cb.SetRGBColorStrokeF(pi.SI.BColorTop.R, pi.SI.BColorTop.G, pi.SI.BColorTop.B);

                        cb.LineTo(pt.X + pt.W, y);

                        cb.Stroke();
                    }

                    //绝对定义文字

                    iTextSharp.text.Font font = TextUtility.GetFont(pi.SI, pt.Text);

                    float[]  widih;
                    string[] sa = MeasureString(pt, pgs.G, out widih);

                    int rows = sa.Length;

                    //x标准固定

                    float x     = pt.X + pi.SI.PaddingLeft;
                    int   align = iTextSharp.text.pdf.PdfContentByte.ALIGN_LEFT;

                    if (pi.SI.TextAlign == TextAlignEnum.Right)
                    {
                        align = iTextSharp.text.pdf.PdfContentByte.ALIGN_RIGHT;
                        x     = pt.X + pt.W - pi.SI.PaddingRight - 1;
                    }
                    else if (pi.SI.TextAlign == TextAlignEnum.Center)
                    {
                        align = iTextSharp.text.pdf.PdfContentByte.ALIGN_CENTER;
                        x     = pt.X + pt.W / 2;
                    }

                    cb.BeginText();
                    cb.SetFontAndSize(font.BaseFont, font.Size);

                    for (int i = 0; i < rows; i++)
                    {
                        float Yt = y + i * font.Size + 1;

                        if (pi.SI.VerticalAlign == VerticalAlignEnum.Top)
                        {
                            Yt = y + pt.H - font.Size * (rows - (i + 1)) - 1;
                        }
                        else if (pi.SI.VerticalAlign == VerticalAlignEnum.Middle)
                        {
                            Yt = y + (pt.H - font.Size * rows) / 2 + i * font.Size + 1;
                        }

                        cb.ShowTextAligned(align, sa[rows - i - 1], x, Yt, 0);
                        cb.EndText();
                    }

                    continue;
                }

                if (pi is PageLine)
                {
                    PageLine pl = pi as PageLine;

                    iTextSharp.text.pdf.PdfContentByte cb = this.pdfWriter.DirectContent;

                    float y1 = getYvalue(pl.Y, 0);
                    float y2 = getYvalue(pl.Y2, 0);

                    cb.MoveTo(pl.X, y1);
                    cb.LineTo(pl.X2, y2);

                    cb.Stroke();

                    continue;
                }

                if (pi is PageImage)
                {
                    PageImage i = pi as PageImage;

                    iTextSharp.text.pdf.PdfContentByte cb = this.pdfWriter.DirectContent;

                    float y = this.getYvalue(i.Y, i.H);

                    System.Drawing.RectangleF r2 = new System.Drawing.RectangleF(i.X + i.SI.PaddingLeft, y - i.SI.PaddingTop, i.W - i.SI.PaddingLeft - i.SI.PaddingRight, i.H - i.SI.PaddingTop - i.SI.PaddingBottom);

                    iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(i.ImageData);

                    image.SetAbsolutePosition(i.X, y);
                    image.ScaleAbsoluteHeight(i.H);
                    image.ScaleAbsoluteWidth(i.W);

                    cb.AddImage(image);

                    cb.Stroke();

                    continue;
                }

                if (pi is PageRectangle)
                {
                    PageRectangle pr = pi as PageRectangle;

                    iTextSharp.text.Rectangle r2 = new iTextSharp.text.Rectangle(pr.X, pr.Y, pr.W, pr.H);
                    r2.Border = 1;
                    document.Add(r2);
                    continue;
                }
            }
        }
Ejemplo n.º 2
0
        public void Textbox(Textbox tb, string t, Row r)
        {
            TableCell tableCell = tb.Parent.Parent as TableCell;

            if (tableCell != null)
            {
                string text = tb.RunText(this._Report, r);

                StyleInfo si = tb.Style.GetStyleInfo(_Report, r);

                iTextSharp.text.Font ft = TextUtility.GetFont(si, text);

                iTextSharp.text.Cell cell = new iTextSharp.text.Cell(new iTextSharp.text.Phrase(text, ft));
                cell.Colspan = tableCell.ColSpan;

                //水不对齐
                if (si.TextAlign == TextAlignEnum.Center)
                {
                    cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
                }
                else if (si.TextAlign == TextAlignEnum.Left)
                {
                    cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
                }
                else if (si.TextAlign == TextAlignEnum.Right)
                {
                    cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_RIGHT;
                }

                ////垂直对齐
                if (si.VerticalAlign == VerticalAlignEnum.Middle)
                {
                    cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
                }
                else if (si.VerticalAlign == VerticalAlignEnum.Top)
                {
                    cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_TOP;
                }
                else if (si.VerticalAlign == VerticalAlignEnum.Bottom)
                {
                    cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_BOTTOM;
                }

                //cell.BackgroundColor = new Color(si.BackgroundColor);

                cell.BorderColorLeft   = new Color(si.BColorLeft);
                cell.BorderColorRight  = new Color(si.BColorRight);
                cell.BorderColorTop    = new Color(si.BColorTop);
                cell.BorderColorBottom = new Color(si.BColorBottom);

                int border = iTextSharp.text.Rectangle.NO_BORDER;

                if (si.BStyleLeft != BorderStyleEnum.None)
                {
                    cell.BorderWidthLeft = si.BWidthLeft / 4;
                    border += iTextSharp.text.Rectangle.LEFT_BORDER;
                }
                else
                {
                    cell.BorderWidthLeft = 0;
                }

                if (si.BStyleRight != BorderStyleEnum.None)
                {
                    cell.BorderWidthRight = si.BWidthRight / 4;
                    border += iTextSharp.text.Rectangle.RIGHT_BORDER;
                }
                else
                {
                    cell.BorderWidthRight = 0;
                }

                if (si.BStyleTop != BorderStyleEnum.None)
                {
                    cell.BorderWidthTop = si.BWidthTop / 4;
                    border += iTextSharp.text.Rectangle.TOP_BORDER;
                }
                else
                {
                    cell.BorderWidthTop = 0;
                }

                if (si.BStyleBottom != BorderStyleEnum.None)
                {
                    cell.BorderWidthBottom = si.BWidthBottom / 4;
                    border += iTextSharp.text.Rectangle.BOTTOM_BORDER;
                }
                else
                {
                    cell.BorderWidthBottom = 0;
                }

                cell.Border = border;
                this.textTable.AddCell(cell);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 如果为西文,则直接使用默认字体。
        /// </summary>
        /// <param name="style"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        public static iTextSharp.text.Font GetFont(fyiReporting.RDL.StyleInfo style, string text)
        {
            string fontName = TextUtility.GetFontName(style.FontFamily);

            iTextSharp.text.Font font = null;

            if (fontName.IndexOf(".") > 0)
            {
                BaseFont baseFT = null;

                if (baseFonts.ContainsKey(fontName))
                {
                    baseFT = baseFonts[fontName];

                    if (baseFT != null)
                    {
                        font = new iTextSharp.text.Font(baseFT);
                    }
                    else
                    {
                        font = iTextSharp.text.FontFactory.GetFont("Helvetica");
                    }
                }
                else
                {
                    string windir = System.Environment.GetEnvironmentVariable("windir");
                    string path   = Path.Combine(windir, "Fonts");
                    path = Path.Combine(path, fontName);

                    if (File.Exists(path))
                    {
                        try
                        {
                            baseFT = BaseFont.CreateFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                        }
                        catch
                        {
                            //
                        }
                    }

                    if (baseFT == null)
                    {
                        try
                        {
                            baseFT = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
                        }
                        catch
                        {
                        }
                    }

                    if (baseFT != null)
                    {
                        baseFonts.Add(fontName, baseFT);
                        font = new iTextSharp.text.Font(baseFT);
                    }
                    else
                    {
                        font = iTextSharp.text.FontFactory.GetFont("Helvetica");
                        baseFonts.Add(fontName, null);
                    }
                }
            }
            else
            {
                font = iTextSharp.text.FontFactory.GetFont(fontName);
            }

            font.Color = new iTextSharp.text.Color(style.Color);
            font.Size  = style.FontSize;

            if (style.IsFontBold())
            {
                font.IsBold();
            }

            if (style.FontStyle == FontStyleEnum.Italic)
            {
                font.IsItalic();
            }

            if (style.TextDecoration == TextDecorationEnum.Underline)
            {
                font.IsUnderlined();
            }

            return(font);
        }