Пример #1
0
        public static void PrintMultipage(FrameworkElement objectToPrint, string print_job_name)
        {
            double original_width  = objectToPrint.ActualWidth;
            double original_height = objectToPrint.ActualHeight;

            PrintDialog printDialog = new PrintDialog();

            if ((bool)printDialog.ShowDialog().GetValueOrDefault())
            {
                PrintCapabilities capabilities = printDialog.PrintQueue.GetPrintCapabilities(printDialog.PrintTicket);

                double DPI      = 200;
                double dpiScale = DPI / 96.0;

                FixedDocument document = new FixedDocument();
                document.DocumentPaginator.PageSize = new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight);

                // Convert the UI control into a bitmap at 300 dpi
                RenderTargetBitmap bmp = new RenderTargetBitmap(
                    Convert.ToInt32(objectToPrint.ActualWidth * dpiScale),
                    Convert.ToInt32(objectToPrint.ActualHeight * dpiScale),
                    DPI, DPI, PixelFormats.Pbgra32
                    );
                bmp.Render(objectToPrint);
                bmp.Freeze();

                // Convert the RenderTargetBitmap into a bitmap we can more readily use
                Bitmap bmp2 = BitmapImageTools.ConvertBitmapSourceToBitmap(bmp);
                bmp = null;

                // Break the bitmap down into pages
                int pageWidth  = Convert.ToInt32(capabilities.PageImageableArea.ExtentWidth * dpiScale);
                int pageHeight = Convert.ToInt32(capabilities.PageImageableArea.ExtentHeight * dpiScale);

                int bmp_width  = bmp2.Width;
                int bmp_height = (int)(pageHeight * bmp2.Width / pageWidth);

                int pageBreak         = 0;
                int previousPageBreak = 0;
                while (true)
                {
                    pageBreak += bmp_height;

                    // We can't read out more than the image
                    if (pageBreak > bmp2.Height)
                    {
                        pageBreak = bmp2.Height;
                    }

                    PageContent pageContent = generatePageContent(
                        bmp2,
                        previousPageBreak, pageBreak,
                        document.DocumentPaginator.PageSize.Width,
                        document.DocumentPaginator.PageSize.Height,
                        capabilities
                        );
                    document.Pages.Add(pageContent);

                    previousPageBreak = pageBreak;

                    // Are we done?
                    if (pageBreak >= bmp2.Height)
                    {
                        break;
                    }
                }

                printDialog.PrintDocument(document.DocumentPaginator, print_job_name);
            }
        }
        private void UpdateLibraryStatistics_Stats_Background_CoverFlow()
        {
            // The list of recommended items
            DocumentDisplayWorkManager ddwm = new DocumentDisplayWorkManager();
            {
                {
                    int ITEMS_IN_LIST = 5;

                    // Upcoming reading is:
                    //  interrupted
                    //  top priority
                    //  read again
                    //  recently added and no status

                    List <PDFDocument> pdf_documents_all = web_library_detail.library.PDFDocuments;
                    pdf_documents_all.Sort(PDFDocumentListSorters.DateAddedToDatabase);

                    foreach (string reading_stage in new string[] { Choices.ReadingStages_INTERRUPTED, Choices.ReadingStages_TOP_PRIORITY, Choices.ReadingStages_READ_AGAIN })
                    {
                        foreach (PDFDocument pdf_document in pdf_documents_all)
                        {
                            if (!pdf_document.DocumentExists)
                            {
                                continue;
                            }

                            if (pdf_document.ReadingStage == reading_stage)
                            {
                                if (!ddwm.ContainsPDFDocument(pdf_document))
                                {
                                    ddwm.AddDocumentDisplayWork(DocumentDisplayWork.StarburstColor.Pink, reading_stage, pdf_document);

                                    if (ddwm.Count >= ITEMS_IN_LIST)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }

                {
                    int ITEMS_IN_LIST = 3;

                    // Recently added
                    {
                        List <PDFDocument> pdf_documents = web_library_detail.library.PDFDocuments;
                        pdf_documents.Sort(PDFDocumentListSorters.DateAddedToDatabase);

                        int num_added = 0;
                        foreach (PDFDocument pdf_document in pdf_documents)
                        {
                            if (!pdf_document.DocumentExists)
                            {
                                continue;
                            }

                            if (!ddwm.ContainsPDFDocument(pdf_document))
                            {
                                ddwm.AddDocumentDisplayWork(DocumentDisplayWork.StarburstColor.Green, "Added Recently", pdf_document);

                                if (++num_added >= ITEMS_IN_LIST)
                                {
                                    break;
                                }
                            }
                        }
                    }

                    // Recently read
                    {
                        List <PDFDocument> pdf_documents = web_library_detail.library.PDFDocuments;
                        pdf_documents.Sort(PDFDocumentListSorters.DateLastRead);

                        int num_added = 0;
                        foreach (PDFDocument pdf_document in pdf_documents)
                        {
                            if (!pdf_document.DocumentExists)
                            {
                                continue;
                            }

                            if (!ddwm.ContainsPDFDocument(pdf_document))
                            {
                                ddwm.AddDocumentDisplayWork(DocumentDisplayWork.StarburstColor.Blue, "Read Recently", pdf_document);

                                if (++num_added >= ITEMS_IN_LIST)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }


                // And fill the placeholders
                WPFDoEvents.InvokeInUIThread(() => UpdateLibraryStatistics_Stats_Background_GUI_AddAllPlaceHolders(ddwm.ddws));

                // Now render each document
                using (Font font = new Font("Times New Roman", 11.0f))
                {
                    using (StringFormat string_format = new StringFormat
                    {
                        Alignment = StringAlignment.Center,
                        LineAlignment = StringAlignment.Center
                    })
                    {
                        var color_matrix = new ColorMatrix();
                        color_matrix.Matrix33 = 0.9f;
                        using (var image_attributes = new ImageAttributes())
                        {
                            image_attributes.SetColorMatrix(color_matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

                            foreach (DocumentDisplayWork ddw in ddwm.ddws)
                            {
                                try
                                {
                                    using (MemoryStream ms = new MemoryStream(ddw.pdf_document.PDFRenderer.GetPageByHeightAsImage(1, PREVIEW_IMAGE_HEIGHT / PREVIEW_IMAGE_PERCENTAGE)))
                                    {
                                        Bitmap page_bitmap = (Bitmap)System.Drawing.Image.FromStream(ms);
                                        page_bitmap = page_bitmap.Clone(new RectangleF {
                                            Width = page_bitmap.Width, Height = (int)Math.Round(page_bitmap.Height * PREVIEW_IMAGE_PERCENTAGE)
                                        }, page_bitmap.PixelFormat);

                                        using (Graphics g = Graphics.FromImage(page_bitmap))
                                        {
                                            int CENTER = 60;
                                            int RADIUS = 60;

                                            {
                                                BitmapImage starburst_bi = null;
                                                switch (ddw.starburst_color)
                                                {
                                                case DocumentDisplayWork.StarburstColor.Blue:
                                                    starburst_bi = Icons.GetAppIcon(Icons.PageCornerBlue);
                                                    break;

                                                case DocumentDisplayWork.StarburstColor.Green:
                                                    starburst_bi = Icons.GetAppIcon(Icons.PageCornerGreen);
                                                    break;

                                                case DocumentDisplayWork.StarburstColor.Pink:
                                                    starburst_bi = Icons.GetAppIcon(Icons.PageCornerPink);
                                                    break;

                                                default:
                                                    starburst_bi = Icons.GetAppIcon(Icons.PageCornerOrange);
                                                    break;
                                                }

                                                Bitmap starburst_image = BitmapImageTools.ConvertBitmapSourceToBitmap(starburst_bi);
                                                g.SmoothingMode = SmoothingMode.AntiAlias;
                                                g.DrawImage(
                                                    starburst_image,
                                                    new Rectangle(CENTER - RADIUS, CENTER - RADIUS, 2 * RADIUS, 2 * RADIUS),
                                                    0,
                                                    0,
                                                    starburst_image.Width,
                                                    starburst_image.Height,
                                                    GraphicsUnit.Pixel,
                                                    image_attributes
                                                    );
                                            }

                                            using (Matrix mat = new Matrix())
                                            {
                                                mat.RotateAt(-50, new PointF(CENTER / 2, CENTER / 2));
                                                g.Transform = mat;

                                                string wrapped_caption = ddw.starburst_caption;
                                                wrapped_caption = wrapped_caption.ToLower();
                                                wrapped_caption = Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(wrapped_caption);
                                                wrapped_caption = wrapped_caption.Replace(" ", "\n");
                                                g.DrawString(wrapped_caption, font, Brushes.Black, new PointF(CENTER / 2, CENTER / 2), string_format);
                                            }
                                        }

                                        BitmapSource page_bitmap_source = BitmapImageTools.CreateBitmapSourceFromImage(page_bitmap);

                                        ddw.page_bitmap_source = page_bitmap_source;
                                    }

                                    WPFDoEvents.InvokeInUIThread(() => UpdateLibraryStatistics_Stats_Background_GUI_FillPlaceHolder(ddw));
                                }
                                catch (Exception ex)
                                {
                                    Logging.Warn(ex, "There was a problem loading a preview image for document {0}", ddw.pdf_document.Fingerprint);
                                }
                            }
                        }
                    }
                }

                if (0 == ddwm.ddws.Count)
                {
                    WPFDoEvents.InvokeAsyncInUIThread(() =>
                    {
                        ButtonCoverFlow.IsChecked = false;
                        UpdateLibraryStatistics();
                    }
                                                      );
                }
            }
        }