Click here to Skip to main content
15,893,722 members
Articles / Desktop Programming / WPF

200% Reflective Class Diagram Creation Tool

Rate me:
Please Sign up or sign in to vote.
4.92/5 (200 votes)
20 Feb 2014CPOL22 min read 586.3K   11.5K   437  
WPF: Version II of my 100% Reflective class diagram creation tool.
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.IO;
using System.Printing;
using System.Windows;

namespace SUT.PrintEngine.Utils
{
    public class PrintUtility
    {
        private readonly CacheHelper _cacheHelper;

        public PrintUtility()
        {
            _cacheHelper = new CacheHelper();
        }

        public string GetSaveLocation(string printerFullName)
        {
            return String.Format("{0}//{1}_printTicket.xml", Constants.Print.SETTINGS_FOLDER, printerFullName.Replace("\\", "_"));
        }

        private PrinterSettings GetPrinterSettings(string currentPrinterName)
        {
            return new PrinterSettings {PrinterName = currentPrinterName};

            var key = String.Format("{0}:PrinterSettings", currentPrinterName);
            if (!_cacheHelper.Contains(key))
            {
                _cacheHelper.Add(key, new PrinterSettings { PrinterName = currentPrinterName });
            }
            return (PrinterSettings)_cacheHelper.GetData(key);
        }

        public Thickness GetPageMargin(string currentPrinterName)
        {
            var key = String.Format("{0}:PageMargin", currentPrinterName);
            if (!_cacheHelper.Contains(key))
            {
                float hardMarginX;
                float hardMarginY;
                var printerSettings = GetPrinterSettings(currentPrinterName);
                try
                {
                    hardMarginX = printerSettings.DefaultPageSettings.HardMarginX;
                    hardMarginY = printerSettings.DefaultPageSettings.HardMarginY;
                }
                catch (Exception)
                {
                    hardMarginX = 0;
                    hardMarginY = 0;
                }
                var thickness = new Thickness(hardMarginX + 5, hardMarginY + 5, printerSettings.DefaultPageSettings.Margins.Right, printerSettings.DefaultPageSettings.Margins.Bottom + 50);
                return thickness;

                _cacheHelper.Add(key, thickness);
            }
            var margin = (Thickness)_cacheHelper.GetData(key);
            ////TempFileLogger.Log(String.Format("Paper margin = ({0}, {1}, {2}, {3})", margin.Left, margin.Top, margin.Right, margin.Bottom));
            return margin;
        }

        public IList<PaperSize> GetPaperSizes(string currentPrinterName)
        {
            var paperSizes = new List<PaperSize>();
            var key = String.Format("{0}:PaperSizes", currentPrinterName);
            if (!_cacheHelper.Contains(key))
            {
                var sizes = GetPrinterSettings(currentPrinterName).PaperSizes;
                foreach (var ps in sizes)
                {
                    if (((PaperSize)ps).PaperName != "Custom Size")
                    {
                        paperSizes.Add((PaperSize)ps);
                    }
                }
                return paperSizes;

                _cacheHelper.Add(key, paperSizes);
            }
            ////TempFileLogger.Log("Paper sizes retrieved successfully.");
            return (IList<PaperSize>)_cacheHelper.GetData(key);
        }

        public PrintQueueCollection GetPrinters()
        {
            try
            {
                if (!_cacheHelper.Contains("Printers"))
                {
                    var printServer = new PrintServer();
                    var printQueueCollection = printServer.GetPrintQueues(new[] {EnumeratedPrintQueueTypes.Connections,EnumeratedPrintQueueTypes.Local});
                    return printQueueCollection;

                    _cacheHelper.Add("Printers", printQueueCollection);
                }
                var printers = (PrintQueueCollection)_cacheHelper.GetData("Printers");
                return printers;
            }
            catch (Exception ex)
            {
                ////TempFileLogger.LogException(ex);
                throw;
            }
        }

        public PrintQueue GetDefaultPrintQueue(string printerName)
        {
            return LocalPrintServer.GetDefaultPrintQueue();
            ////var printQueue = new PrintServer().GetPrintQueues().Where(pq => pq.FullName == printerName).SingleOrDefault();
            ////return printQueue ?? LocalPrintServer.GetDefaultPrintQueue();
        }

        public PrintTicket GetUserPrintTicket(string printerFullName)
        {
            if (File.Exists(GetSaveLocation(printerFullName)))
            {
                var fileStream = new FileStream(GetSaveLocation(printerFullName), FileMode.Open);
                var userPrintTicket = new PrintTicket(fileStream);
                fileStream.Close();

                return userPrintTicket;
            }

            return null;
        }

        public void SaveUserPrintTicket(PrintQueue currentPrinter)
        {
            Stream outStream = new FileStream(GetSaveLocation(currentPrinter.FullName), FileMode.Create);
            currentPrinter.UserPrintTicket.SaveTo(outStream);
            outStream.Close();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions