Example #1
0
    private void RenderSong(RenderOption option, object o = null)
    {
        try
        {
            AppSettings appSettings = null;
            PageSetup   pageSetup   = new PageSetup();

            if (_workingSettings != null)
            {
                appSettings = _workingSettings;
            }
            else
            {
                appSettings = _appSettings;
            }


            DrawingArea area = new DrawingArea();

            pageSetup.SetLeftMargin(0.5, Unit.Inch);
            pageSetup.SetTopMargin(0.5, Unit.Inch);
            pageSetup.SetRightMargin(0.5, Unit.Inch);
            pageSetup.SetBottomMargin(0, Unit.Inch);

            if (option == RenderOption.ScreenView)
            {
                area = (DrawingArea)o;
            }
            else
            {
                PageSetup defaultPageSetup = new PageSetup();

                // Alter our set dimensions by the printer default so that it lines up in the actual
                // print out like it does on the screen.
                pageSetup.SetLeftMargin(pageSetup.GetLeftMargin(Unit.Inch) - defaultPageSetup.GetLeftMargin(Unit.Inch), Unit.Inch);
                pageSetup.SetTopMargin(pageSetup.GetTopMargin(Unit.Inch) - defaultPageSetup.GetTopMargin(Unit.Inch), Unit.Inch);
                pageSetup.SetRightMargin(pageSetup.GetRightMargin(Unit.Inch) + defaultPageSetup.GetRightMargin(Unit.Inch), Unit.Inch);
                pageSetup.SetBottomMargin(pageSetup.GetBottomMargin(Unit.Inch) + defaultPageSetup.GetBottomMargin(Unit.Inch), Unit.Inch);


                //pageSetup.SetLeftMargin(0.25, Unit.Inch);
                //pageSetup.SetTopMargin(0.25, Unit.Inch);
                //pageSetup.SetRightMargin(0.75, Unit.Inch);
                //pageSetup.SetBottomMargin(0.75, Unit.Inch);

                area = null;
            }


            //CSGen.Operations.IPageRenderer renderer = new CSGen.PageRenderer (_currentSong, forDisplay: option == RenderOption.ScreenView);
            CSGen.Operations.IPageRenderer renderer = new CSGen.Operations.RenderEngine(_currentSong, appSettings, pageSetup, option);

            //            if (option == RenderOption.ScreenView)
            //            {
            //                area = (DrawingArea)o;
            //                pageSetup = renderer.PageSetup;
            //            }
            //            else
            //            {
            //                pageSetup = new PageSetup();
            //            }

            // TODO: Remove when new render engine is complete.
            // Load configuration options.
            //            if (_workingFonts != null)
            //                renderer.Fonts = _workingFonts;
            //            else
            //                renderer.Fonts = _fonts;
            //            renderer.AppSettings = appSettings;
            //
            //            pageSetup.SetLeftMargin(0.5, Unit.Inch);
            //            pageSetup.SetTopMargin(0.5, Unit.Inch);
            //            pageSetup.SetBottomMargin(0.5, Unit.Inch);
            //            pageSetup.SetRightMargin(0.5, Unit.Inch);
            // end TODO


            if (option == RenderOption.ScreenView)
            {
                Cairo.Context cr = Gdk.CairoHelper.Create(area.GdkWindow);
                renderer.CalculatePages(cr);

                area.WidthRequest  = renderer.DisplayWidthExtent;
                area.HeightRequest = renderer.DisplayHeightExtent;

                int offset = 0;
                for (int i = 0; i < renderer.Pages; i++)
                {
                    renderer.DrawPage(i, offset);
                    offset += renderer.DisplayPaperHeight;
                }

                ((IDisposable)cr.GetTarget()).Dispose();
                ((IDisposable)cr).Dispose();
            }
            else
            {
                PrintSettings printSettings = new PrintSettings();
                printSettings.PaperSize = pageSetup.PaperSize;


                using (PrintOperation print = new PrintOperation())
                {
                    print.DefaultPageSetup = pageSetup;
                    print.PrintSettings    = printSettings;
                    print.BeginPrint      += (object obj, BeginPrintArgs args) =>
                    {
                        renderer.CalculatePages(args.Context.CairoContext);
                        print.NPages = renderer.Pages;
                    };
                    print.DrawPage += (object obj, DrawPageArgs args) =>
                    {
                        renderer.DrawPage(args.PageNr);
                    };

                    if (option == RenderOption.Print)
                    {
                        print.Run(PrintOperationAction.PrintDialog, null);
                    }
                    else
                    {
                        print.Run(PrintOperationAction.Preview, null);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageDialog dialog = new MessageDialog(
                this, DialogFlags.DestroyWithParent, MessageType.Error, ButtonsType.Ok,
                "Message: {0}\n\n Stacktrace: {1}", ex.Message, ex.StackTrace);
            dialog.Run();
            dialog.Destroy();
        }
    }
Example #2
0
        public static bool Convert(string excelFileName, string pdfFileName, PageSetup p)
        {
            Excel2007.Application excelApp = null;
            Excel2007.Workbook    workBook = null;

            try
            {
                excelApp = new Excel2007.Application();
                workBook = excelApp.Workbooks.Open(excelFileName);

                if (p != null)
                {
                    Excel2007.Worksheet workSheet = null;
                    int sheetCount = workBook.Sheets.Count;
                    int i          = 0;
                    while (i <= sheetCount)
                    {
                        try
                        {
                            workSheet = workBook.Sheets[i];
                            break;
                        }
                        catch
                        {
                            i++;
                        }
                    }

                    workSheet.PageSetup.LeftMargin   = p.GetLeftMargin();
                    workSheet.PageSetup.RightMargin  = p.GetRightMargin();
                    workSheet.PageSetup.TopMargin    = p.GetTopMargin();
                    workSheet.PageSetup.BottomMargin = p.GetBottomMargin();
                }

                if (workBook != null)
                {
                    workBook.ExportAsFixedFormat(Excel2007.XlFixedFormatType.xlTypePDF, pdfFileName);
                }
                return(true);
            }
            catch (Exception e)
            {
                MsgBox.Show(Application.ProductName, "Không thể chuyển qua PDF. Yêu cầu phải cài Office Excel 2007 và Microsoft Save as PDF 2007 Add-in.", Common.IconType.Error);
                return(false);
            }
            finally
            {
                if (workBook != null)
                {
                    workBook.Close(false, Type.Missing, Type.Missing);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
                    workBook = null;
                }

                if (excelApp != null)
                {
                    excelApp.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
                    excelApp = null;
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }