Esempio n. 1
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);
            }
        }
Esempio n. 2
0
        private void button1_Click(object sender, System.EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            TUITextDecoration Underline = new TUITextDecoration(TUIUnderline.Single);
            PdfWriter         pdf       = new PdfWriter();

            using (FileStream file = new FileStream(saveFileDialog1.FileName, FileMode.Create))
            {
                pdf.Compress = true;
                pdf.BeginDoc(file);
                pdf.YAxisGrowsDown = true; //To keep it compatible with GDI+
                using (TUIFont f = TUIFont.Create("times new roman", (float)22.5, TUIFontStyle.Italic))
                {
                    using (TUIFont f2 = TUIFont.Create("Arial", (float)12, TUIFontStyle.Italic))
                    {
                        pdf.DrawString("This is the first line on a test of many lines.", f, Underline, Brushes.Navy, 100, 100);
                        pdf.DrawString("Some unicode: \u0e2a\u0e27\u0e31\u0e2a\u0e14\u0e35", f, Underline, Brushes.ForestGreen, 100, 200);
                        pdf.DrawString("More lines here!", f, Underline, Brushes.ForestGreen, 200, 300);
                        pdf.DrawString("And this is the last line.", f, Underline, Brushes.Black, 200, 400);
                        pdf.Properties.Author   = "Adrian";
                        pdf.Properties.Title    = "This is a test of FlexCel Api";
                        pdf.Properties.Keywords = "test\nflexcel\napi";
                        pdf.NewPage();
                        pdf.SaveState();
                        pdf.Rotate(200, 100, 45);
                        pdf.DrawString("Some rotated test", f, Underline, Brushes.Black, 200, 200);
                        pdf.RestoreState();
                        pdf.DrawString("Some NOT rotated text", f, Underline, Brushes.Black, 200, 200);
                        pdf.DrawString("Hello from FlexCel!", f2, Brushes.Black, 200, 50);

                        TPointF[] points = { new TPointF(200, 100), new TPointF(200, 50), new TPointF(500, 50), new TPointF(700, 100) };
                        pdf.DrawLines(Pens.DarkOrchid, points);

                        RectangleF Coords = new RectangleF(100, 300, 100, 100);
                        using (Brush Gradient = new LinearGradientBrush(Coords, Color.Red, Color.Blue, 0f))
                        {
                            pdf.DrawAndFillRectangle(Pens.Red, Gradient, 100, 300, 100, 100);
                        }
                        pdf.DrawRectangle(Pens.DarkSlateBlue, 100, 300, 50, 50);
                        pdf.DrawLine(Pens.Black, 100, 300, 200, 400);

                        string AssemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                        using (Image Img = Image.FromFile(AssemblyPath + Path.DirectorySeparatorChar + ".." + Path.DirectorySeparatorChar + ".." + Path.DirectorySeparatorChar + "test.jpg"))
                        {
                            pdf.DrawImage(Img, new RectangleF(200, 300, 200, 150), null);
                        }
                        pdf.IntersectClipRegion(new RectangleF(100, 100, 50, 50));
                        pdf.FillRectangle(Brushes.DarkTurquoise, 100, 100, 100, 100);

                        pdf.EndDoc();
                    }
                }
            }
            if (MessageBox.Show("Do you want to open the generated file?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Process.Start(saveFileDialog1.FileName);
            }
        }