PrinterDialog.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. using ComPDFKit.Controls.Helper;
  2. using ComPDFKit.Controls.Printer;
  3. using ComPDFKit.PDFDocument;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Drawing.Printing;
  8. using System.Linq;
  9. using System.Runtime.CompilerServices;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. namespace ComPDFKit.Controls.PDFControl
  13. {
  14. /// <summary>
  15. /// Interaction logic for PrinterDialog.xaml
  16. /// </summary>
  17. public partial class PrinterDialog : Window, INotifyPropertyChanged
  18. {
  19. private PrinterSettings printerSettings;
  20. private PrintSettingsInfo printSettingsInfo = new PrintSettingsInfo();
  21. private SizeModeInfo sizeModeInfo = new SizeModeInfo();
  22. private PosterModeInfo posterModeInfo = new PosterModeInfo();
  23. private MultipleModeInfo multipleModeInfo = new MultipleModeInfo();
  24. private BookletModeInfo bookletModeInfo = new BookletModeInfo();
  25. private PrintDocument printDoc = new PrintDocument();
  26. private int _sizeScale = 100;
  27. public int SizeScale
  28. {
  29. get { return _sizeScale; }
  30. set
  31. {
  32. if (UpdateProper(ref _sizeScale, value))
  33. {
  34. sizeModeInfo.Scale = value;
  35. ctlPreview.Init(printSettingsInfo);
  36. }
  37. }
  38. }
  39. private string _pageRangeText;
  40. public string PageRangeText
  41. {
  42. get => _pageRangeText;
  43. set
  44. {
  45. _pageRangeText = value;
  46. if (!CommonHelper.GetPagesInRange(ref printSettingsInfo.PageRangeList, PageRangeText, Document.PageCount, new char[] { ',' }, new char[] { '-' }))
  47. {
  48. printSettingsInfo.PageRangeList = Enumerable.Range(0, Document.PageCount).ToList();
  49. }
  50. ctlPreview.Init(printSettingsInfo, true);
  51. }
  52. }
  53. private bool _isSheetEnabled = false;
  54. public bool IsSheetEnabled
  55. {
  56. get => _isSheetEnabled;
  57. set
  58. {
  59. UpdateProper(ref _isSheetEnabled, value);
  60. }
  61. }
  62. private string _sheetColumn = 2.ToString();
  63. public string SheetColumn
  64. {
  65. get => _sheetColumn;
  66. set
  67. {
  68. if (UpdateProper(ref _sheetColumn, value))
  69. {
  70. if (SheetColumn != string.Empty && SheetRow != string.Empty)
  71. {
  72. multipleModeInfo.Sheet = new MultipleModeInfo.SheetPair(int.Parse(SheetColumn), int.Parse(SheetRow));
  73. }
  74. }
  75. }
  76. }
  77. private string _sheetRow = 2.ToString();
  78. public string SheetRow
  79. {
  80. get => _sheetRow;
  81. set
  82. {
  83. if (UpdateProper(ref _sheetRow, value))
  84. {
  85. if (SheetColumn != string.Empty && SheetRow != string.Empty)
  86. {
  87. multipleModeInfo.Sheet = new MultipleModeInfo.SheetPair(int.Parse(SheetColumn), int.Parse(SheetRow));
  88. }
  89. }
  90. }
  91. }
  92. public CPDFDocument Document
  93. {
  94. set => printSettingsInfo.Document = value;
  95. private get => printSettingsInfo.Document;
  96. }
  97. public PrinterDialog()
  98. {
  99. InitializeComponent();
  100. InitPaperList();
  101. DataContext = this;
  102. }
  103. #region printer settings
  104. private void InitPrinterNameList()
  105. {
  106. List<string> printerList = Enumerable.Reverse(PrinterSettings.InstalledPrinters.Cast<string>()).ToList();
  107. cmbPrinterName.ItemsSource = printerList;
  108. cmbPrinterName.SelectedIndex = printerList.Contains("Microsoft Print to PDF") ? printerList.IndexOf("Microsoft Print to PDF") : 0;
  109. }
  110. #endregion
  111. private void PrinterDialog_Loaded(object sender, RoutedEventArgs e)
  112. {
  113. InitPrinterNameList();
  114. InitPrinterSettingsMode();
  115. ctlPreview.Init(printSettingsInfo);
  116. }
  117. private void InitPrinterSettingsMode()
  118. {
  119. printSettingsInfo.Copies = 1;
  120. printSettingsInfo.PrinterName = cmbPrinterName.SelectedItem.ToString();
  121. printSettingsInfo.PrintMode = sizeModeInfo;
  122. printSettingsInfo.PrintOrientation = PrintOrientation.Portrait;
  123. printSettingsInfo.PaperSize = printerSettings.PaperSizes[cmbPaper.SelectedIndex];
  124. printSettingsInfo.IsPrintAnnot = true;
  125. printSettingsInfo.IsPrintForm = true;
  126. printSettingsInfo.IsReverseOrder = false;
  127. printSettingsInfo.IsGrayscale = false;
  128. printSettingsInfo.IsDuplex = false;
  129. printSettingsInfo.DuplexPrintMod = DuplexPrintMod.None;
  130. printSettingsInfo.PageRangeList = Enumerable.Range(0, Document.PageCount).ToList();
  131. printSettingsInfo.PrintDocument = printDoc;
  132. printSettingsInfo.NeedRerendering = true;
  133. printSettingsInfo.IsPaperSizeChanged = false;
  134. printSettingsInfo.NeedReversePage = false;
  135. printSettingsInfo.PrintMode = sizeModeInfo;
  136. }
  137. private void InitPaperList()
  138. {
  139. printerSettings = printDoc.PrinterSettings;
  140. foreach (PaperSize paperSize in printerSettings.PaperSizes)
  141. {
  142. cmbPaper.Items.Add(new ComboBoxItem
  143. {
  144. Content = $"{paperSize.PaperName} - {paperSize.Width} x {paperSize.Height}"
  145. });
  146. }
  147. int IndexOfA4 = printerSettings.PaperSizes.Cast<PaperSize>().ToList().FindIndex(x => x.PaperName == "A4");
  148. cmbPaper.SelectedIndex = IndexOfA4 != -1 ? IndexOfA4 : 0;
  149. }
  150. private void cmbPrinterName_SelectionChanged(object sender, SelectionChangedEventArgs e)
  151. {
  152. printSettingsInfo.PrinterName = e.AddedItems[0].ToString();
  153. }
  154. private void cmbOritation_SelectionChanged(object sender, SelectionChangedEventArgs e)
  155. {
  156. if (!IsLoaded)
  157. {
  158. return;
  159. }
  160. string tag = (e.AddedItems[0] as ComboBoxItem).Tag.ToString();
  161. switch (tag)
  162. {
  163. case "Portrait":
  164. printSettingsInfo.PrintOrientation = PrintOrientation.Portrait;
  165. break;
  166. case "Landscape":
  167. printSettingsInfo.PrintOrientation = PrintOrientation.Landscape;
  168. break;
  169. case "Auto":
  170. printSettingsInfo.PrintOrientation = PrintOrientation.Auto;
  171. break;
  172. }
  173. ctlPreview.Init(printSettingsInfo);
  174. }
  175. private void cmbContent_SelectionChanged(object sender, SelectionChangedEventArgs e)
  176. {
  177. if (!IsLoaded)
  178. {
  179. return;
  180. }
  181. string tag = (e.AddedItems[0] as ComboBoxItem).Tag.ToString();
  182. switch (tag)
  183. {
  184. case "Document":
  185. printSettingsInfo.IsPrintAnnot = false;
  186. printSettingsInfo.IsPrintForm = false;
  187. break;
  188. case "Document and Markups":
  189. printSettingsInfo.IsPrintAnnot = true;
  190. printSettingsInfo.IsPrintForm = true;
  191. break;
  192. case "Document and Stamps":
  193. printSettingsInfo.IsPrintAnnot = true;
  194. printSettingsInfo.IsPrintForm = false;
  195. break;
  196. }
  197. ctlPreview.Init(printSettingsInfo, true);
  198. }
  199. private void chkReversePage_Click(object sender, RoutedEventArgs e)
  200. {
  201. var chk = sender as CheckBox;
  202. printSettingsInfo.IsReverseOrder = chk.IsChecked.Value;
  203. ctlPreview.Init(printSettingsInfo, true);
  204. }
  205. private void chkPrintBorder_Click(object sender, RoutedEventArgs e)
  206. {
  207. var chk = sender as CheckBox;
  208. }
  209. private void chkGrayScale_Click(object sender, RoutedEventArgs e)
  210. {
  211. var chk = sender as CheckBox;
  212. printSettingsInfo.IsGrayscale = chk.IsChecked.Value;
  213. ctlPreview.Init(printSettingsInfo);
  214. }
  215. private void chkDuplex_Click(object sender, RoutedEventArgs e)
  216. {
  217. var chk = sender as CheckBox;
  218. printSettingsInfo.IsDuplex = chk.IsChecked.Value;
  219. if (rdoLongEdge.IsChecked.GetValueOrDefault())
  220. {
  221. printSettingsInfo.DuplexPrintMod = DuplexPrintMod.FlipLongEdge;
  222. }
  223. else
  224. {
  225. printSettingsInfo.DuplexPrintMod = DuplexPrintMod.FlipShortEdge;
  226. }
  227. }
  228. private void rdoDuplex_Click(object sender, RoutedEventArgs e)
  229. {
  230. var chk = sender as RadioButton;
  231. if (chk.IsChecked.GetValueOrDefault())
  232. {
  233. printSettingsInfo.DuplexPrintMod = chk.Tag.ToString() == "LongEdge" ? DuplexPrintMod.FlipLongEdge : DuplexPrintMod.FlipShortEdge;
  234. }
  235. }
  236. private void cmbPaper_SelectionChanged(object sender, SelectionChangedEventArgs e)
  237. {
  238. if (!IsLoaded)
  239. {
  240. return;
  241. }
  242. sbyte index = (sbyte)cmbPaper.SelectedIndex;
  243. printSettingsInfo.PaperSize = printerSettings.PaperSizes[index];
  244. ctlPreview.Init(printSettingsInfo);
  245. }
  246. private void rdoPageRange_Click(object sender, RoutedEventArgs e)
  247. {
  248. var rdo = sender as RadioButton;
  249. string rdoTag = rdo.Tag.ToString();
  250. switch (rdoTag)
  251. {
  252. case "All":
  253. printSettingsInfo.PageRangeList = Enumerable.Range(0, Document.PageCount).ToList();
  254. break;
  255. case "Odd":
  256. printSettingsInfo.PageRangeList = Enumerable.Range(0, Document.PageCount).Where(x => x % 2 == 0).ToList();
  257. break;
  258. case "Even":
  259. printSettingsInfo.PageRangeList = Enumerable.Range(0, Document.PageCount).Where(x => x % 2 != 0).ToList();
  260. break;
  261. case "Custom":
  262. if (!CommonHelper.GetPagesInRange(ref printSettingsInfo.PageRangeList, PageRangeText, Document.PageCount, new char[] { ',' }, new char[] { '-' }))
  263. {
  264. printSettingsInfo.PageRangeList = Enumerable.Range(0, Document.PageCount).ToList();
  265. }
  266. break;
  267. }
  268. ctlPreview.Init(printSettingsInfo, true);
  269. }
  270. private void SizeMode_Click(object sender, RoutedEventArgs e)
  271. {
  272. var rdo = sender as RadioButton;
  273. string rdoTag = rdo.Tag.ToString();
  274. switch (rdoTag)
  275. {
  276. case "AutoAdapt":
  277. sizeModeInfo.SizeType = SizeType.Adaptive;
  278. break;
  279. case "ActualSize":
  280. sizeModeInfo.SizeType = SizeType.Actural;
  281. break;
  282. case "CustomScale":
  283. sizeModeInfo.SizeType = SizeType.Customized;
  284. sizeModeInfo.Scale = SizeScale;
  285. break;
  286. }
  287. ctlPreview.Init(printSettingsInfo);
  288. }
  289. private void cmbPageOrder_SelectionChanged(object sender, SelectionChangedEventArgs e)
  290. {
  291. if (!IsLoaded)
  292. {
  293. return;
  294. }
  295. string tag = (e.AddedItems[0] as ComboBoxItem).Tag.ToString();
  296. switch (tag)
  297. {
  298. case "Horizontal":
  299. multipleModeInfo.PageOrder = PageOrder.Horizontal;
  300. break;
  301. case "Horizontal Reverse":
  302. multipleModeInfo.PageOrder = PageOrder.HorizontalReverse;
  303. break;
  304. case "TopToBottom":
  305. multipleModeInfo.PageOrder = PageOrder.Vertical;
  306. break;
  307. case "BottomToTop":
  308. multipleModeInfo.PageOrder = PageOrder.VerticalReverse;
  309. break;
  310. }
  311. }
  312. public event PropertyChangedEventHandler PropertyChanged;
  313. protected bool UpdateProper<T>(ref T properValue,
  314. T newValue,
  315. [CallerMemberName] string properName = "")
  316. {
  317. if (object.Equals(properValue, newValue))
  318. return false;
  319. properValue = newValue;
  320. OnPropertyChanged(properName);
  321. return true;
  322. }
  323. protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
  324. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  325. private void btnConfirm_Click(object sender, RoutedEventArgs e)
  326. {
  327. PrintHelper.PrintDocument(printSettingsInfo);
  328. Close();
  329. }
  330. private void btnCancel_Click(object sender, RoutedEventArgs e)
  331. {
  332. Close();
  333. }
  334. private void cmbOritation_Loaded(object sender, RoutedEventArgs e)
  335. {
  336. cmbOritation.SelectedIndex = 0;
  337. }
  338. private void cmbContent_Loaded(object sender, RoutedEventArgs e)
  339. {
  340. cmbContent.SelectedIndex = 1;
  341. }
  342. }
  343. }