Пример #1
0
        /// <summary>
        /// Calculates the actual spreadsheet range that will be printed. This is given by:
        /// 1)If you specified non zero values on PrintRange, this will be used.
        /// 2)If any value in PrintRange is zero and there is a Print Area defined on the
        /// spreadsheet, the Print Area will be used.
        /// 3)If there is no PrintRange and no Print Area defined, the visible cells on the
        /// sheet will be printed.
        /// </summary>
        ///<returns>The area that will be exported.</returns>
        public TXlsCellRange CalcPrintArea()
        {
            Workbook.Recalc(false);
            PdfCanvas = new PdfWriter();
            PdfGraphics RealCanvas = new PdfGraphics(PdfCanvas);

            PrepareCanvas();
            IFlxGraphics aCanvas = RealCanvas;

            FRenderer.SetCanvas(aCanvas);
            try
            {
                FRenderer.CreateFontCache();
                try
                {
                    return(FRenderer.InternalCalcPrintArea(FPrintRange)[0]);
                }
                finally
                {
                    FRenderer.DisposeFontCache();
                }
            }
            finally
            {
                FRenderer.SetCanvas(null);
            }
        }
Пример #2
0
        private static void DrawAdaptativeString(IFlxGraphics Canvas, TAdaptativeFormats AdaptativeFormats, string Text, Font AFont, Brush TextBrush, real x, real y)
        {
            int start = 0;

            foreach (TCharAndPos cp in AdaptativeFormats.Separators)
            {
                if (cp.Pos >= Text.Length)
                {
                    break;                        //just a security measure.
                }
                string SubText = Text.Substring(start, cp.Pos - start);
                Canvas.DrawString(SubText, AFont, TextBrush, x, y);
                start = cp.Pos + 1;
                x    += Canvas.MeasureString(SubText + Text[cp.Pos], AFont).Width;
                if (start - 1 < Text.Length && CharUtils.IsSurrogatePair(Text, start - 1))
                {
                    start++;
                }
            }

            if (start < Text.Length)
            {
                Canvas.DrawString(Text.Substring(start), AFont, TextBrush, x, y);
            }
        }
Пример #3
0
        internal static void FitOneLine(IFlxGraphics Canvas, TFontCache FontCache, real Zoom100, TRichString Text, Font AFont, real w, int idx, ref int fit, ref real StrWidth)
        {
            if (StrWidth > w)
            {
                real md = 0;

                fit = Math.Max((int)Math.Round(fit * w / StrWidth), 1);
                CharUtils.SameOrMore(Text.Value, idx, ref fit);
                StrWidth = CalcTextExtent(Canvas, FontCache, Zoom100, AFont, Text.Substring(idx, fit), out md).Width;

                if (StrWidth > w)
                {
                    while (fit > 1 && StrWidth > w)
                    {
                        fit--;
                        CharUtils.SameOrLess(Text.Value, idx, ref fit);
                        StrWidth = CalcTextExtent(Canvas, FontCache, Zoom100, AFont, Text.Substring(idx, fit), out md).Width;
                    }
                }
                else
                {
                    while (StrWidth < w) //Will not overflow, as wc>w for fit+idx=MaxLength.
                    {
                        int fit1 = fit + 1;
                        CharUtils.SameOrMore(Text.Value, idx, ref fit1);
                        StrWidth = CalcTextExtent(Canvas, FontCache, Zoom100, AFont, Text.Substring(idx, fit1), out md).Width;
                        if (StrWidth < w)
                        {
                            fit = fit1;
                        }
                    }
                }
            }
        }
Пример #4
0
 internal real Offset(IFlxGraphics Canvas, Font MyFont)
 {
     if (FOffset != 0)
     {
         SizeF Sz = Canvas.MeasureString("Mg", MyFont);
         return(Sz.Height / FOffset);
     }
     return(0);
 }
Пример #5
0
 internal TGraphicCanvas()
 {
     FontCache        = new TFontCache();
     bmp              = BitmapConstructor.CreateBitmap(1, 1);
     imgData          = Graphics.FromImage(bmp);
     imgData.PageUnit = GraphicsUnit.Point;
     Canvas           = new GdiPlusGraphics(imgData);
     Canvas.CreateSFormat();
 }
Пример #6
0
        private TImgExportInfo GetExportInfo(IFlxGraphics aCanvas)
        {
            TImgExportInfo Result = new TImgExportInfo();

            if (AllVisibleSheets)
            {
                int FirstVisibleSheet = -1;
                int SaveActiveSheet   = Workbook.ActiveSheet;
                try
                {
                    Result.Sheets = new TOneImgExportInfo[Workbook.SheetCount];
                    for (int sheet = 1; sheet <= Workbook.SheetCount; sheet++)
                    {
                        Workbook.ActiveSheet = sheet;
                        if (Workbook.SheetVisible != TXlsSheetVisible.Visible)
                        {
                            continue;
                        }
                        if (FirstVisibleSheet < 0)
                        {
                            FirstVisibleSheet = sheet;
                        }

                        TOneImgExportInfo OneResult = new TOneImgExportInfo();

                        OneResult.FPrintRanges = FRenderer.InternalCalcPrintArea(FPrintRange);
                        TPaperDimensions pd = GetRealPageSize();
                        OneResult.FPageBounds = new RectangleF(0, 0, pd.Width, pd.Height);

                        FRenderer.InitializePrint(aCanvas, OneResult.PageBounds, OneResult.PageBounds, OneResult.PrintRanges, out OneResult.FPaintClipRect, out OneResult.FTotalPages, out OneResult.FPagePrintRange);
                        Result.Sheets[sheet - 1] = OneResult;
                    }
                }
                finally
                {
                    Workbook.ActiveSheet = SaveActiveSheet;
                }

                Result.CurrentSheet = FirstVisibleSheet;
            }
            else
            {
                TOneImgExportInfo OneResult = new TOneImgExportInfo();
                OneResult.FPrintRanges = FRenderer.InternalCalcPrintArea(FPrintRange);
                TPaperDimensions pd = GetRealPageSize();
                OneResult.FPageBounds = new RectangleF(0, 0, pd.Width, pd.Height);

                FRenderer.InitializePrint(aCanvas, OneResult.PageBounds, OneResult.PageBounds, OneResult.PrintRanges, out OneResult.FPaintClipRect, out OneResult.FTotalPages, out OneResult.FPagePrintRange);
                Result.Sheets       = new TOneImgExportInfo[1];
                Result.Sheets[0]    = OneResult;
                Result.CurrentSheet = Workbook.ActiveSheet;
            }
            Result.ResetCurrentPage();
            LastInitSheet = 0;
            return(Result);
        }
Пример #7
0
        internal static void AddWilcardtoLine(IFlxGraphics Canvas, TFontCache FontCache, real Zoom100, Font AFont, real MaxTextWidth, ref SizeF TextExtent, TXRichString LineWithWildcard)
        {
            if (LineWithWildcard == null || LineWithWildcard.AdaptFormat == null)
            {
                return;
            }
            int WildcardPos = LineWithWildcard.AdaptFormat.WildcardPos;

            if (WildcardPos < 0)
            {
                return;
            }

            string s1           = LineWithWildcard.s.ToString();
            string WildcardChar = String.Empty + s1[WildcardPos];

            if (CharUtils.IsSurrogatePair(s1, WildcardPos) && WildcardPos + 1 < s1.Length)
            {
                WildcardChar += s1[WildcardPos + 1];
            }
            string sOrg = s1;

            s1 = s1.Remove(WildcardPos, WildcardChar.Length);//consider the case the string has 0 wildcards.
            string sOld = s1;
            real   wc;
            real   OldWc = LineWithWildcard.XExtent;

            do
            {
                real md;
                wc = RenderMetrics.CalcTextExtent(Canvas, FontCache, Zoom100, AFont, new TRichString(s1), out md).Width;
                if (wc < MaxTextWidth)
                {
                    sOld  = s1;
                    OldWc = wc;
                    s1    = s1.Insert(WildcardPos, WildcardChar);
                }
            }while (wc < MaxTextWidth);

            LineWithWildcard.s = new TRichString(sOld);
            if (sOrg.Length > sOld.Length)
            {
                LineWithWildcard.AdaptFormat.RemovedPosition(WildcardPos, sOrg.Length - sOld.Length);                            //a line with a wildcard can have 0 characters in the wildcard.
            }
            if (sOrg.Length < sOld.Length)
            {
                LineWithWildcard.AdaptFormat.InsertedPosition(WildcardPos, sOld.Length - sOrg.Length);
            }
            LineWithWildcard.XExtent = OldWc;
            if (TextExtent.Width < OldWc)
            {
                TextExtent.Width = OldWc;
            }
        }
Пример #8
0
        private int TotalPagesInSheet(PrintPageEventArgs e, IFlxGraphics aCanvas, RectangleF VisibleClipInches100)
        {
            TXlsCellRange[] MyPrintRange = FRenderer.InternalCalcPrintArea(FPrintRange);
            TXlsCellRange   PagePrintRange;

            RectangleF[] PaintClipRect;

            int  TotalPages;
            bool Landscape = (Workbook.PrintOptions & TPrintOptions.Orientation) == 0;

            FRenderer.InitializePrint(aCanvas, ConvertToUnits(e.PageBounds, Landscape != e.PageSettings.Landscape),
                                      ConvertToMargins(e.Graphics, e.PageBounds, VisibleClipInches100, e.PageSettings.Landscape != Landscape),
                                      MyPrintRange, out PaintClipRect, out TotalPages, out PagePrintRange);

            return(TotalPages);
        }
Пример #9
0
        private static void WriteJustText(ExcelFile Workbook, IFlxGraphics Canvas, TFontCache FontCache, real Zoom100, bool ReverseRightToLeftStrings, Font AFont, Color AFontColor, real y, real SubOfs, RectangleF CellRect, real Clp, TRichString OutText, real XExtent, real MaxDescent, bool Distributed, TAdaptativeFormats AdaptativeFormats)
        {
            List <TRichString> Words = new List <TRichString>();

            OutText = GetVisualString(OutText, ReverseRightToLeftStrings);
            string s = OutText.Value;
            int    p = 0; int p1 = 0;

            while ((p1 = s.IndexOf(' ', p)) >= 0)
            {
                Words.Add(OutText.Substring(p, p1 - p + 1));
                p = p1 + 1;
            }

            if (p < s.Length)
            {
                Words.Add(OutText.Substring(p));
            }

            real md;
            real wc = XExtent; // CalcTextExtent(AFont, OutText, out md).Width;

            real Spaces = 0;

            if (Words.Count - 1 > 0)
            {
                Spaces = (CellRect.Width - 2 - 2 * Clp - wc) / (Words.Count - 1);
            }

            real x = CellRect.Left + 1 + Clp;

            if (Words.Count == 1 && Distributed)  //Center when it is one word
            {
                x = (CellRect.Left + CellRect.Right - 2 - 2 * Clp - wc) / 2;
            }
            for (int i = 0; i < Words.Count; i++)
            {
                TRichString wo = Words[i];
                WriteText(Workbook, Canvas, FontCache, Zoom100, AFont, AFontColor, x, y, SubOfs, wo, 0, MaxDescent, AdaptativeFormats);
                x += RenderMetrics.CalcTextExtent(Canvas, FontCache, Zoom100, AFont, wo, out md).Width;
                if (Spaces > 0)
                {
                    x += Spaces;
                }
            }
        }
Пример #10
0
        internal static void CalcTextBox(IFlxGraphics Canvas, TFontCache FontCache, real Zoom100, RectangleF CellRect, real Clp, bool MultiLine, real Alpha, bool Vertical, TRichString OutText, Font AFont, TAdaptativeFormats AdaptativeFormats, out SizeF TextExtent, out TXRichStringList TextLines, out TFloatList MaxDescent)
        {
            TextExtent = new SizeF(0, 0);
            TextLines  = new TXRichStringList();

            MaxDescent = new TFloatList();
            real MaxTextWidth;

            if (MultiLine || Vertical)
            {
                real Md;
                real TextHeight = RenderMetrics.CalcTextExtent(Canvas, FontCache, Zoom100, AFont, new TRichString("M"), out Md).Height; //not perfect since the rich string might have different fonts, but a good approx.
                MaxTextWidth = CalcMaxTextWidth(ref CellRect, Clp, Alpha, TextHeight);

                RenderMetrics.SplitText(Canvas, FontCache, Zoom100, OutText, AFont, MaxTextWidth, TextLines, out TextExtent, Vertical, MaxDescent, AdaptativeFormats);
            }
            else
            {
                TextLines.Add(new TXRichString(OutText, false, 0, 0, AdaptativeFormats));
                real mdx = 0;
                TextExtent           = RenderMetrics.CalcTextExtent(Canvas, FontCache, Zoom100, AFont, OutText, out mdx);
                TextLines[0].XExtent = TextExtent.Width;
                TextLines[0].YExtent = TextExtent.Height;
                MaxTextWidth         = CalcMaxTextWidth(ref CellRect, Clp, Alpha, TextExtent.Height);
                MaxDescent.Add(mdx);
            }

            if (AdaptativeFormats != null && AdaptativeFormats.WildcardPos >= 0)
            {
                MaxTextWidth -= 2 * Clp; //Add some extra clipping so the text doesn't go through the line.
                if (Vertical)
                {
                }
                else
                {
                    AddWildcard(Canvas, FontCache, Zoom100, AFont, MaxTextWidth, TextLines, ref TextExtent);
                }
            }
        }
Пример #11
0
        /// <summary>
        /// Returns the number of pages to print on the whole workbook or sheet (depending on the <see cref="AllVisibleSheets"/> value).
        /// </summary>
        /// <returns></returns>
        private int GetPagesToPrint(PrintPageEventArgs e, IFlxGraphics aCanvas, RectangleF VisibleClipInches100)
        {
            int TotalPages      = 0;
            int SaveActiveSheet = Workbook.ActiveSheet;

            try
            {
                for (int sheet = 1; sheet <= Workbook.SheetCount; sheet++)
                {
                    Workbook.ActiveSheet = sheet;
                    if (Workbook.SheetVisible != TXlsSheetVisible.Visible)
                    {
                        continue;
                    }
                    TotalPages += TotalPagesInSheet(e, aCanvas, VisibleClipInches100);
                }
            }
            finally
            {
                Workbook.ActiveSheet = SaveActiveSheet;
            }
            return(TotalPages);
        }
Пример #12
0
        private static void AddWildcard(IFlxGraphics Canvas, TFontCache FontCache, real Zoom100,
                                        Font AFont, real MaxTextWidth, TXRichStringList TextLines, ref SizeF TextExtent)
        {
            TXRichString LineWithWildcard = null;

            foreach (TXRichString line in TextLines)
            {
                if (line.s == null || line.s.RTFRunCount > 0)
                {
                    return;                                           //wildcards only apply to non formatted lines.
                }
                if (line.AdaptFormat != null && line.AdaptFormat.WildcardPos >= 0)
                {
                    LineWithWildcard = line;
                    break;
                }
            }

            if (LineWithWildcard == null)
            {
                return;
            }
            AddWilcardtoLine(Canvas, FontCache, Zoom100, AFont, MaxTextWidth, ref TextExtent, LineWithWildcard);
        }
Пример #13
0
        internal static void WriteText(ExcelFile Workbook, IFlxGraphics Canvas, TFontCache FontCache, real Zoom100, Font AFont, Color AFontColor, real x, real y, real SubOfs, TRichString OutText, real Alpha, real MaxDescent, TAdaptativeFormats AdaptativeFormats)
        {
            if (OutText.Length == 0)
            {
                return;
            }

            if (Alpha != 0)
            {
                Canvas.SaveTransform();
            }
            try
            {
                if (Alpha != 0)
                {
                    Canvas.Rotate(x, y, Alpha);
                }

                using (Brush TextBrush = new SolidBrush(AFontColor))
                {
                    if (OutText.RTFRunCount == 0)
                    {
                        if (AdaptativeFormats == null || AdaptativeFormats.Separators == null || AdaptativeFormats.Separators.Length == 0) //formats are only applied to non rich text cells.
                        {
                            Canvas.DrawString(OutText.Value, AFont, TextBrush, x, y + SubOfs);
                        }
                        else
                        {
                            DrawAdaptativeString(Canvas, AdaptativeFormats, OutText.Value, AFont, TextBrush, x, y + SubOfs);
                        }
                    }
                    else
                    {
                        SizeF Result;

                        string s1 = OutText.Value.Substring(0, OutText.RTFRun(0).FirstChar);
                        if (s1.Length > 0)
                        {
                            Result = Canvas.MeasureString(s1, AFont, new TPointF(x, y));
                            Canvas.DrawString(s1, AFont, TextBrush, x, y + SubOfs - (MaxDescent - Canvas.FontDescent(AFont)));
                            x += Result.Width;
                        }

                        for (int i = 0; i < OutText.RTFRunCount - 1; i++)
                        {
                            TFlxFont       Fx     = OutText.GetFont(OutText.RTFRun(i).FontIndex);
                            TSubscriptData Sub    = new TSubscriptData(Fx.Style);
                            Font           MyFont = FontCache.GetFont(Fx, Zoom100 * Sub.Factor);
                            {
                                using (Brush MyBrush = new SolidBrush(GetColor(Workbook, Fx.Color)))
                                {
                                    int Start = OutText.RTFRun(i).FirstChar;
                                    if (Start >= OutText.Length)
                                    {
                                        Start = OutText.Length;
                                    }
                                    int Len = OutText.RTFRun(i + 1).FirstChar;
                                    if (Len >= OutText.Length)
                                    {
                                        Len = OutText.Length;
                                    }
                                    Len -= Start;

                                    string s2 = OutText.Value.Substring(Start, Len);
                                    Result = Canvas.MeasureString(s2, MyFont, new TPointF(x, y));
                                    Canvas.DrawString(s2, MyFont, MyBrush, x, y + Sub.Offset(Canvas, MyFont) - (MaxDescent - Canvas.FontDescent(MyFont)));
                                    x += Result.Width;
                                }
                            }
                        }
                        TFlxFont       Fy      = OutText.GetFont(OutText.RTFRun(OutText.RTFRunCount - 1).FontIndex);
                        TSubscriptData Suby    = new TSubscriptData(Fy.Style);
                        Font           MyFont2 = FontCache.GetFont(Fy, Zoom100 * Suby.Factor);
                        {
                            using (Brush MyBrush = new SolidBrush(GetColor(Workbook, Fy.Color)))
                            {
                                int Start = OutText.RTFRun(OutText.RTFRunCount - 1).FirstChar;
                                if (Start >= OutText.Length)
                                {
                                    Start = OutText.Length;
                                }

                                string s3 = OutText.Value.Substring(Start);
                                Result = Canvas.MeasureString(s3, MyFont2, new TPointF(x, y));
                                Canvas.DrawString(s3, MyFont2, MyBrush, x, y + Suby.Offset(Canvas, MyFont2) - (MaxDescent - Canvas.FontDescent(MyFont2)));
                            }
                        }
                    }
                }
            }
            finally
            {
                if (Alpha != 0)
                {
                    Canvas.ResetTransform();
                }
            }
        }
Пример #14
0
        internal static void SplitText(IFlxGraphics Canvas, TFontCache FontCache, real Zoom100,
                                       TRichString Text, Font AFont, real w, TXRichStringList TextLines, out SizeF TextExtent, bool Vertical, TFloatList MaxDescent, TAdaptativeFormats AdaptFormat)
        {
            TextLines.Clear();
            MaxDescent.Clear();

            int idx = 0;
            int fit = 0;


            TextExtent = new SizeF(0, 0);
            if (w <= 0 || Text.Value.Length <= 0)
            {
                return;
            }

            while (idx < Text.Value.Length)
            {
                int Enter     = Text.Value.IndexOf((char)0x0A, idx);
                int MaxLength = Text.Value.Length - idx;
                if (Enter >= 0)
                {
                    MaxLength = Enter - idx;
                }

                if (Vertical)
                {
                    fit = 1;
                    CharUtils.SameOrMore(Text.Value, idx, ref fit);
                }
                else
                {
                    //Not a really efficient way, but...
                    //First Guess. whole string.
                    fit = MaxLength;

                    real md;
                    real wc = CalcTextExtent(Canvas, FontCache, Zoom100, AFont, Text.Substring(idx, fit), out md).Width;
                    FitOneLine(Canvas, FontCache, Zoom100, Text, AFont, w, idx, ref fit, ref wc);

                    if (fit <= 0)
                    {
                        fit = 1;                               //avoid infinite loop
                    }
                    CharUtils.SameOrMore(Text.Value, idx, ref fit);


                    if (Text.Value.IndexOf(' ', idx, fit) >= 0)
                    {
                        int minfit = 1;
                        CharUtils.SameOrMore(Text.Value, idx, ref minfit);
                        while (fit > minfit && fit < MaxLength && Text.Value[idx + fit] != ' ')
                        {
                            fit--;
                        }
                    }
                    while (fit < MaxLength && Text.Value[idx + fit] == ' ')
                    {
                        fit++;
                    }
                    //No need to adjust fit for surrogates. it will always be at the start of one.
                }

                //int Enter=Text.Value.IndexOf((char)0x0A, idx, fit);
                //if (Enter>0) fit=Enter-idx;
                int TextLen = Math.Min(MaxLength, fit);
                TextLines.Add(new TXRichString(Text.Substring(idx, TextLen), true, 0, 0, TAdaptativeFormats.CopyTo(AdaptFormat, idx, TextLen)));

                if (fit + idx < Text.Value.Length && Text.Value[idx + fit] == (char)0x0A)
                {
                    TextLines[TextLines.Count - 1].Split = false;
                    if (idx + fit < Text.Value.Length - 1)                  //An Enter at the end behaves different, it means we have a new empty line.
                    {
                        idx++;
                    }
                }
                if (fit + idx >= Text.Value.Length)
                {
                    TextLines[TextLines.Count - 1].Split = false;
                }

                idx += fit;

                //Recalculate dx
                real        mdx   = 0;
                TRichString sx    = TextLines[TextLines.Count - 1].s;
                SizeF       bSize = CalcTextExtent(Canvas, FontCache, Zoom100, AFont, sx, out mdx);
                if (bSize.Height == 0)
                {
                    real  mdx3;
                    SizeF bSize3 = CalcTextExtent(Canvas, FontCache, Zoom100, AFont, new TRichString("M"), out mdx3);
                    bSize.Height = bSize3.Height;
                }

                SizeF bSize2 = bSize;
                if (sx != null && sx.Length > 0 && sx.ToString()[sx.Length - 1] == ' ')             //This is to right align line with spaces at the end.
                {
                    real mdx2;
                    bSize2 = CalcTextExtent(Canvas, FontCache, Zoom100, AFont, sx.RightTrim(), out mdx2);
                }
                TextLines[TextLines.Count - 1].XExtent = bSize2.Width;
                TextLines[TextLines.Count - 1].YExtent = bSize.Height;                 // not bSize2.Height; This might be even 0.

                MaxDescent.Add(mdx);
                if (TextExtent.Width < bSize.Width)
                {
                    TextExtent.Width = bSize.Width;
                }
                TextExtent.Height += bSize.Height;
            }             //while
        }
Пример #15
0
        internal static SizeF CalcTextExtent(IFlxGraphics Canvas, TFontCache FontCache, real Zoom100,
                                             Font AFont, TRichString Text, out real MaxDescent)
        {
            MaxDescent = Canvas.FontDescent(AFont);
            if (Text.RTFRunCount == 0)
            {
                return(Canvas.MeasureStringEmptyHasHeight(Text.Value, AFont));
            }

            real  x = 0;
            real  y = 0;
            SizeF Result;

            Result = Canvas.MeasureString(Text.Value.Substring(0, Text.RTFRun(0).FirstChar), AFont, new TPointF(0, 0));
            x      = Result.Width; y = Result.Height;


            for (int i = 0; i < Text.RTFRunCount - 1; i++)
            {
                TFlxFont       Fx     = Text.GetFont(Text.RTFRun(i).FontIndex);
                TSubscriptData Sub    = new TSubscriptData(Fx.Style);
                Font           MyFont = FontCache.GetFont(Fx, Zoom100 * Sub.Factor);
                {
                    int Start = Text.RTFRun(i).FirstChar;
                    if (Start >= Text.Length)
                    {
                        Start = Text.Length;
                    }
                    int Len = Text.RTFRun(i + 1).FirstChar;
                    if (Len >= Text.Length)
                    {
                        Len = Text.Length;
                    }
                    Len -= Start;
                    if (Len < 0)
                    {
                        continue;       //wrong file, (i+1)FirstChar < (i).FirstChar
                    }
                    Result     = Canvas.MeasureString(Text.Value.Substring(Start, Len), MyFont, new TPointF(0, 0));
                    x         += Result.Width; y = Math.Max(y + Sub.Offset(Canvas, MyFont), Result.Height);
                    MaxDescent = Math.Max(MaxDescent, Canvas.FontDescent(MyFont) + Sub.Offset(Canvas, MyFont));
                }
            }


            TFlxFont       Fy      = Text.GetFont(Text.RTFRun(Text.RTFRunCount - 1).FontIndex);
            TSubscriptData Suby    = new TSubscriptData(Fy.Style);
            Font           MyFont2 = FontCache.GetFont(Fy, Zoom100 * Suby.Factor);

            {
                int Start = Text.RTFRun(Text.RTFRunCount - 1).FirstChar;
                if (Start >= Text.Length)
                {
                    Start = Text.Length;
                }
                Result     = Canvas.MeasureStringEmptyHasHeight(Text.Value.Substring(Start), MyFont2);
                x         += Result.Width; y = Math.Max(y + Suby.Offset(Canvas, MyFont2), Result.Height);
                MaxDescent = Math.Max(MaxDescent, Canvas.FontDescent(MyFont2) + Suby.Offset(Canvas, MyFont2));
            }

            return(new SizeF(x, y));
        }
Пример #16
0
        /// <summary>
        /// Exports the active sheet on the current XlsFile. You can define which is the first page to print and the global count of pages, so the
        /// page numbers on headers and footers of the excel file correspond with the actual pages on the pdf.
        /// </summary>
        /// <param name="startPage">Fist page that the headers and footers on the xls file will show. If you are exporting only one sheet to the pdf file,
        /// this can be 1. If you are exporting more than one sheet to the same pdf file, you will want to set StartPage to the actual page on the pdf.</param>
        /// <param name="totalPages">The total number of pages to display on Excel headers and footers. If you are exporting only one sheet to the pdf file, set it to -1, and it will be calculated automatically. If not, please suply here the total number of pages the file will have so FlexCel can show footers like "page 1 of 50"</param>
        public void ExportSheet(int startPage, int totalPages)
        {
            if (PdfCanvas == null)
            {
                FlxMessages.ThrowException(FlxErr.ErrBeginExportNotCalled);
            }
            Workbook.Recalc(false);

            FirstPageInSheet = startPage - 1;

            PdfGraphics RealCanvas = new PdfGraphics(PdfCanvas);

            LoadPageSize();
            if (FirstPage)
            {
                OnBeforeNewPage(new PageEventArgs(PdfCanvas, CurrentTotalPage, CurrentTotalPage - FirstPageInSheet));
                PrepareCanvas();
                PdfCanvas.BeginDoc(FPdfStream);
                PdfCanvas.PageLayout = FPageLayout;
                FPdfStream           = null;
            }

            IFlxGraphics aCanvas = RealCanvas;

            FRenderer.SetCanvas(aCanvas);
            try
            {
                RectangleF[]    PaintClipRect;
                TXlsCellRange[] MyPrintRange = FRenderer.InternalCalcPrintArea(FPrintRange);

                TXlsCellRange PagePrintRange;

                int PagesInSheet;
                FRenderer.InitializePrint(aCanvas, PdfGraphics.ConvertToUnits(PdfCanvas.PageSize),
                                          PdfGraphics.ConvertToUnits(PdfCanvas.PageSize),
                                          MyPrintRange, out PaintClipRect, out PagesInSheet, out PagePrintRange);

                if (totalPages < 0)
                {
                    totalPages = PagesInSheet;
                }
                FProgress.SetTotalPages(totalPages);

                int PrintArea = 0;
                for (int i = 0; i < PagesInSheet; i++)
                {
                    if (Canceled)
                    {
                        return;
                    }
                    LoadPageSize();

                    if (!FirstPage)
                    {
                        CurrentTotalPage++;
                        OnBeforeNewPage(new PageEventArgs(PdfCanvas, CurrentTotalPage, CurrentTotalPage - FirstPageInSheet));
                        PrepareCanvas();
                        PdfCanvas.NewPage();
                    }
                    OnBeforeGeneratePage(new PageEventArgs(PdfCanvas, CurrentTotalPage, CurrentTotalPage - FirstPageInSheet));
                    PrepareCanvas();
                    PdfCanvas.SaveState();

                    FirstPage = false;

                    if (Canceled)
                    {
                        return;
                    }
                    FProgress.SetPage(startPage + i);
                    FRenderer.GenericPrint(aCanvas, PdfGraphics.ConvertToUnits(PdfCanvas.PageSize), MyPrintRange, startPage + i,
                                           PaintClipRect, totalPages, true, PagePrintRange, ref PrintArea);
                    if (Canceled)
                    {
                        return;
                    }

                    PdfCanvas.RestoreState();
                    OnAfterGeneratePage(new PageEventArgs(PdfCanvas, CurrentTotalPage, CurrentTotalPage - FirstPageInSheet));
                    PrepareCanvas();
                }
            }
            finally
            {
                FRenderer.SetCanvas(null);
            }
        }
Пример #17
0
        internal static void DrawRichText(ExcelFile Workbook, IFlxGraphics Canvas, TFontCache FontCache, real Zoom100, bool ReverseRightToLeftStrings,
                                          ref RectangleF CellRect, ref RectangleF PaintClipRect, ref RectangleF TextRect, ref RectangleF ContainingRect,
                                          real Clp, THFlxAlignment HJustify, TVFlxAlignment VJustify, real Alpha,
                                          Color DrawFontColor, TSubscriptData SubData, TRichString OutText, SizeF TextExtent,
                                          TXRichStringList TextLines,
                                          Font AFont, TFloatList MaxDescent, real[] X, real[] Y)
        {
            RectangleF FinalRect = CellRect;

            if (ContainingRect.Right > PaintClipRect.Left &&
                ContainingRect.Left < PaintClipRect.Right &&
                Intersect(TextRect, PaintClipRect, out FinalRect))
            {
                real dy             = 0;
                int  TextLinesCount = TextLines.Count;

                if (VJustify == TVFlxAlignment.justify || VJustify == TVFlxAlignment.distributed)
                {
                    if (TextLinesCount > 1)
                    {
                        dy = (CellRect.Height - TextExtent.Height - 2 * Clp) / (TextLinesCount - 1);
                    }
                }

                float eps = 0.0001F; //Tolerance for the comparison, these are floats, not doubles, and we get rounding errors soon.
                if (TextRect.Left - eps <= ContainingRect.Left && TextRect.Right + eps >= ContainingRect.Right && TextRect.Top - eps <= ContainingRect.Top && TextRect.Bottom + eps >= ContainingRect.Bottom)
                {
                    Canvas.SetClipReplace(PaintClipRect);                      //This will improve pdf export a lot, since we can now join all the BT/ET tags inside one, and then we can also join fonts inside the ET/BT tags.
                }
                else
                {
                    if (Alpha == 0 || Alpha == 90)
                    {
                        Canvas.SetClipReplace(FinalRect);
                    }
                    else
                    {
                        Canvas.SetClipReplace(RectangleF.FromLTRB(PaintClipRect.Left, FinalRect.Top, PaintClipRect.Right, FinalRect.Bottom));                         //rotated text can move to other used cells horizontally.
                    }
                }

                real AcumDy = 0;
                for (int i = 0; i < TextLinesCount; i++)
                {
                    TXRichString TextLine = TextLines[i];
                    if ((Alpha == 0) &&
                        (HJustify == THFlxAlignment.justify && TextLine.Split) ||
                        (HJustify == THFlxAlignment.distributed)
                        )
                    {
                        Canvas.SetClipReplace(FinalRect);
                        WriteJustText(Workbook, Canvas, FontCache, Zoom100, ReverseRightToLeftStrings,
                                      AFont, DrawFontColor, Y[i] + AcumDy, SubData.Offset(Canvas, AFont), CellRect, Clp, TextLine.s,
                                      TextLine.XExtent, MaxDescent[i], HJustify == THFlxAlignment.distributed, TextLine.AdaptFormat);
                    }
                    else
                    {
                        WriteText(Workbook, Canvas, FontCache, Zoom100, AFont, DrawFontColor, X[i],
                                  Y[i] + AcumDy, SubData.Offset(Canvas, AFont), GetVisualString(TextLine.s, ReverseRightToLeftStrings),
                                  Alpha, MaxDescent[i], TextLine.AdaptFormat);
                    }
                    AcumDy += dy;
                }
            }
        }
Пример #18
0
        internal static void DrawPlainText(IFlxGraphics Canvas, ExcelFile Workbook, TShapeProperties ShProp, RectangleF Coords, TShadowInfo ShadowInfo, TClippingStyle Clipping, float Zoom100)
        {
            string Text = GetGeoText(ShProp);

            if (Text == null)
            {
                return;
            }
            Text = Text.Replace("\n", String.Empty);
            string[] Lines = Text.Split('\r');
            if (Lines == null || Lines.Length <= 0)
            {
                return;
            }
            int LinesLength = Lines[Lines.Length - 1].Length == 0? Lines.Length - 1: Lines.Length;               //Last line is an empty enter.

            if (LinesLength <= 0)
            {
                return;
            }

            using (Font TextFont = GetGeoFont(ShProp))
            {
                using (Pen pe = GetPen(ShProp, Workbook, ShadowInfo))
                {
                    Canvas.SaveTransform();
                    try
                    {
                        float   LineGap = Canvas.FontLinespacing(TextFont);
                        SizeF[] Sizes   = new SizeF[LinesLength];
                        Sizes[0]         = Canvas.MeasureStringEmptyHasHeight(Lines[0], TextFont);
                        Sizes[0].Height -= LineGap; //Linespacing is not included here.

                        SizeF sz = Sizes[0];
                        for (int i = 1; i < LinesLength; i++)
                        {
                            Sizes[i] = Canvas.MeasureStringEmptyHasHeight(Lines[i], TextFont);
                            if (Sizes[i].Width > sz.Width)
                            {
                                sz.Width = Sizes[i].Width;
                            }
                            sz.Height += Sizes[i].Height;
                        }

                        if (sz.Width <= 0 || sz.Height <= 0 || Coords.Width <= 0 || Coords.Height <= 0)
                        {
                            return;
                        }
                        float rx = Coords.Width / sz.Width;
                        float ry = Coords.Height / sz.Height;
                        Canvas.Scale(rx, ry);

                        using (Brush br = GetBrush(new RectangleF(Coords.Left / rx, Coords.Top / ry, sz.Width, sz.Height), ShProp, Workbook, ShadowInfo, Zoom100)) //Mast be selected AFTER scaling, so gradients work.
                        {
                            float y = LineGap;
                            for (int i = 0; i < LinesLength; i++)
                            {
                                y += Sizes[i].Height;
                                float x = (sz.Width - Sizes[i].Width) / 2f;
                                Canvas.DrawString(Lines[i], TextFont, pe, br, Coords.Left / rx + x, Coords.Top / ry + y);
                            }
                        }
                    }
                    finally
                    {
                        Canvas.ResetTransform();
                    }
                }
            }
        }