123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- using ComPDFKit.Controls.Helper;
- using ComPDFKit.Controls.Printer;
- using ComPDFKit.PDFDocument;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing.Printing;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Windows;
- using System.Windows.Controls;
- namespace ComPDFKit.Controls.PDFControl
- {
- /// <summary>
- /// Interaction logic for PrinterDialog.xaml
- /// </summary>
- public partial class PrinterDialog : Window, INotifyPropertyChanged
- {
- private PrinterSettings printerSettings;
- private PrintSettingsInfo printSettingsInfo = new PrintSettingsInfo();
- private SizeModeInfo sizeModeInfo = new SizeModeInfo();
- private PosterModeInfo posterModeInfo = new PosterModeInfo();
- private MultipleModeInfo multipleModeInfo = new MultipleModeInfo();
- private BookletModeInfo bookletModeInfo = new BookletModeInfo();
- private PrintDocument printDoc = new PrintDocument();
- private int _sizeScale = 100;
- public int SizeScale
- {
- get { return _sizeScale; }
- set
- {
- if (UpdateProper(ref _sizeScale, value))
- {
- sizeModeInfo.Scale = value;
- ctlPreview.Init(printSettingsInfo);
- }
- }
- }
- private string _pageRangeText;
- public string PageRangeText
- {
- get => _pageRangeText;
- set
- {
- _pageRangeText = value;
- if (!CommonHelper.GetPagesInRange(ref printSettingsInfo.PageRangeList, PageRangeText, Document.PageCount, new char[] { ',' }, new char[] { '-' }))
- {
- printSettingsInfo.PageRangeList = Enumerable.Range(0, Document.PageCount).ToList();
- }
- ctlPreview.Init(printSettingsInfo, true);
- }
- }
- private bool _isSheetEnabled = false;
- public bool IsSheetEnabled
- {
- get => _isSheetEnabled;
- set
- {
- UpdateProper(ref _isSheetEnabled, value);
- }
- }
- private string _sheetColumn = 2.ToString();
- public string SheetColumn
- {
- get => _sheetColumn;
- set
- {
- if (UpdateProper(ref _sheetColumn, value))
- {
- if (SheetColumn != string.Empty && SheetRow != string.Empty)
- {
- multipleModeInfo.Sheet = new MultipleModeInfo.SheetPair(int.Parse(SheetColumn), int.Parse(SheetRow));
- }
- }
- }
- }
- private string _sheetRow = 2.ToString();
- public string SheetRow
- {
- get => _sheetRow;
- set
- {
- if (UpdateProper(ref _sheetRow, value))
- {
- if (SheetColumn != string.Empty && SheetRow != string.Empty)
- {
- multipleModeInfo.Sheet = new MultipleModeInfo.SheetPair(int.Parse(SheetColumn), int.Parse(SheetRow));
- }
- }
- }
- }
- public CPDFDocument Document
- {
- set => printSettingsInfo.Document = value;
- private get => printSettingsInfo.Document;
- }
- public PrinterDialog()
- {
- InitializeComponent();
- InitPaperList();
- DataContext = this;
- }
- #region printer settings
- private void InitPrinterNameList()
- {
- List<string> printerList = Enumerable.Reverse(PrinterSettings.InstalledPrinters.Cast<string>()).ToList();
- cmbPrinterName.ItemsSource = printerList;
- cmbPrinterName.SelectedIndex = printerList.Contains("Microsoft Print to PDF") ? printerList.IndexOf("Microsoft Print to PDF") : 0;
- }
- #endregion
- private void PrinterDialog_Loaded(object sender, RoutedEventArgs e)
- {
- InitPrinterNameList();
- InitPrinterSettingsMode();
- ctlPreview.Init(printSettingsInfo);
- }
- private void InitPrinterSettingsMode()
- {
- printSettingsInfo.Copies = 1;
- printSettingsInfo.PrinterName = cmbPrinterName.SelectedItem.ToString();
- printSettingsInfo.PrintMode = sizeModeInfo;
- printSettingsInfo.PrintOrientation = PrintOrientation.Portrait;
- printSettingsInfo.PaperSize = printerSettings.PaperSizes[cmbPaper.SelectedIndex];
- printSettingsInfo.IsPrintAnnot = true;
- printSettingsInfo.IsPrintForm = true;
- printSettingsInfo.IsReverseOrder = false;
- printSettingsInfo.IsGrayscale = false;
- printSettingsInfo.IsDuplex = false;
- printSettingsInfo.DuplexPrintMod = DuplexPrintMod.None;
- printSettingsInfo.PageRangeList = Enumerable.Range(0, Document.PageCount).ToList();
- printSettingsInfo.PrintDocument = printDoc;
- printSettingsInfo.NeedRerendering = true;
- printSettingsInfo.IsPaperSizeChanged = false;
- printSettingsInfo.NeedReversePage = false;
- printSettingsInfo.PrintMode = sizeModeInfo;
- }
- private void InitPaperList()
- {
- printerSettings = printDoc.PrinterSettings;
- foreach (PaperSize paperSize in printerSettings.PaperSizes)
- {
- cmbPaper.Items.Add(new ComboBoxItem
- {
- Content = $"{paperSize.PaperName} - {paperSize.Width} x {paperSize.Height}"
- });
- }
- int IndexOfA4 = printerSettings.PaperSizes.Cast<PaperSize>().ToList().FindIndex(x => x.PaperName == "A4");
- cmbPaper.SelectedIndex = IndexOfA4 != -1 ? IndexOfA4 : 0;
- }
- private void cmbPrinterName_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- printSettingsInfo.PrinterName = e.AddedItems[0].ToString();
- }
- private void cmbOritation_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (!IsLoaded)
- {
- return;
- }
- string tag = (e.AddedItems[0] as ComboBoxItem).Tag.ToString();
- switch (tag)
- {
- case "Portrait":
- printSettingsInfo.PrintOrientation = PrintOrientation.Portrait;
- break;
- case "Landscape":
- printSettingsInfo.PrintOrientation = PrintOrientation.Landscape;
- break;
- case "Auto":
- printSettingsInfo.PrintOrientation = PrintOrientation.Auto;
- break;
- }
- ctlPreview.Init(printSettingsInfo);
- }
- private void cmbContent_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (!IsLoaded)
- {
- return;
- }
- string tag = (e.AddedItems[0] as ComboBoxItem).Tag.ToString();
- switch (tag)
- {
- case "Document":
- printSettingsInfo.IsPrintAnnot = false;
- printSettingsInfo.IsPrintForm = false;
- break;
- case "Document and Markups":
- printSettingsInfo.IsPrintAnnot = true;
- printSettingsInfo.IsPrintForm = true;
- break;
- case "Document and Stamps":
- printSettingsInfo.IsPrintAnnot = true;
- printSettingsInfo.IsPrintForm = false;
- break;
- }
- ctlPreview.Init(printSettingsInfo, true);
- }
- private void chkReversePage_Click(object sender, RoutedEventArgs e)
- {
- var chk = sender as CheckBox;
- printSettingsInfo.IsReverseOrder = chk.IsChecked.Value;
- ctlPreview.Init(printSettingsInfo, true);
- }
- private void chkPrintBorder_Click(object sender, RoutedEventArgs e)
- {
- var chk = sender as CheckBox;
- }
- private void chkGrayScale_Click(object sender, RoutedEventArgs e)
- {
- var chk = sender as CheckBox;
- printSettingsInfo.IsGrayscale = chk.IsChecked.Value;
- ctlPreview.Init(printSettingsInfo);
- }
- private void chkDuplex_Click(object sender, RoutedEventArgs e)
- {
- var chk = sender as CheckBox;
- printSettingsInfo.IsDuplex = chk.IsChecked.Value;
- if (rdoLongEdge.IsChecked.GetValueOrDefault())
- {
- printSettingsInfo.DuplexPrintMod = DuplexPrintMod.FlipLongEdge;
- }
- else
- {
- printSettingsInfo.DuplexPrintMod = DuplexPrintMod.FlipShortEdge;
- }
- }
- private void rdoDuplex_Click(object sender, RoutedEventArgs e)
- {
- var chk = sender as RadioButton;
- if (chk.IsChecked.GetValueOrDefault())
- {
- printSettingsInfo.DuplexPrintMod = chk.Tag.ToString() == "LongEdge" ? DuplexPrintMod.FlipLongEdge : DuplexPrintMod.FlipShortEdge;
- }
- }
- private void cmbPaper_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (!IsLoaded)
- {
- return;
- }
- sbyte index = (sbyte)cmbPaper.SelectedIndex;
- printSettingsInfo.PaperSize = printerSettings.PaperSizes[index];
- ctlPreview.Init(printSettingsInfo);
- }
- private void rdoPageRange_Click(object sender, RoutedEventArgs e)
- {
- var rdo = sender as RadioButton;
- string rdoTag = rdo.Tag.ToString();
- switch (rdoTag)
- {
- case "All":
- printSettingsInfo.PageRangeList = Enumerable.Range(0, Document.PageCount).ToList();
- break;
- case "Odd":
- printSettingsInfo.PageRangeList = Enumerable.Range(0, Document.PageCount).Where(x => x % 2 == 0).ToList();
- break;
- case "Even":
- printSettingsInfo.PageRangeList = Enumerable.Range(0, Document.PageCount).Where(x => x % 2 != 0).ToList();
- break;
- case "Custom":
- if (!CommonHelper.GetPagesInRange(ref printSettingsInfo.PageRangeList, PageRangeText, Document.PageCount, new char[] { ',' }, new char[] { '-' }))
- {
- printSettingsInfo.PageRangeList = Enumerable.Range(0, Document.PageCount).ToList();
- }
- break;
- }
- ctlPreview.Init(printSettingsInfo, true);
- }
- private void SizeMode_Click(object sender, RoutedEventArgs e)
- {
- var rdo = sender as RadioButton;
- string rdoTag = rdo.Tag.ToString();
- switch (rdoTag)
- {
- case "AutoAdapt":
- sizeModeInfo.SizeType = SizeType.Adaptive;
- break;
- case "ActualSize":
- sizeModeInfo.SizeType = SizeType.Actural;
- break;
- case "CustomScale":
- sizeModeInfo.SizeType = SizeType.Customized;
- sizeModeInfo.Scale = SizeScale;
- break;
- }
- ctlPreview.Init(printSettingsInfo);
- }
- private void cmbPageOrder_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (!IsLoaded)
- {
- return;
- }
- string tag = (e.AddedItems[0] as ComboBoxItem).Tag.ToString();
- switch (tag)
- {
- case "Horizontal":
- multipleModeInfo.PageOrder = PageOrder.Horizontal;
- break;
- case "Horizontal Reverse":
- multipleModeInfo.PageOrder = PageOrder.HorizontalReverse;
- break;
- case "TopToBottom":
- multipleModeInfo.PageOrder = PageOrder.Vertical;
- break;
- case "BottomToTop":
- multipleModeInfo.PageOrder = PageOrder.VerticalReverse;
- break;
- }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- protected bool UpdateProper<T>(ref T properValue,
- T newValue,
- [CallerMemberName] string properName = "")
- {
- if (object.Equals(properValue, newValue))
- return false;
- properValue = newValue;
- OnPropertyChanged(properName);
- return true;
- }
- protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- private void btnConfirm_Click(object sender, RoutedEventArgs e)
- {
- PrintHelper.PrintDocument(printSettingsInfo);
- Close();
- }
- private void btnCancel_Click(object sender, RoutedEventArgs e)
- {
- Close();
- }
- private void cmbOritation_Loaded(object sender, RoutedEventArgs e)
- {
- cmbOritation.SelectedIndex = 0;
- }
- private void cmbContent_Loaded(object sender, RoutedEventArgs e)
- {
- cmbContent.SelectedIndex = 1;
- }
- }
- }
|