PrinterDialog.xaml.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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.Printing;
  10. using System.Runtime.CompilerServices;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. namespace ComPDFKit.Controls.PDFControl
  14. {
  15. /// <summary>
  16. /// Interaction logic for PrinterDialog.xaml
  17. /// </summary>
  18. public partial class PrinterDialog : Window, INotifyPropertyChanged
  19. {
  20. private bool isInited = false;
  21. private PrintServer PrintServer = new PrintServer();
  22. private PrintQueue printQueue;
  23. private PrinterSettings printerSettings;
  24. private PrintSettingsInfo printSettingsInfo = new PrintSettingsInfo();
  25. private SizeModeInfo sizeModeInfo = new SizeModeInfo();
  26. private PosterModeInfo posterModeInfo = new PosterModeInfo();
  27. private MultipleModeInfo multipleModeInfo = new MultipleModeInfo();
  28. private BookletModeInfo bookletModeInfo = new BookletModeInfo();
  29. private PrintDocument printDoc = new PrintDocument();
  30. private int _sizeScale = 100;
  31. public int SizeScale
  32. {
  33. get { return _sizeScale; }
  34. set
  35. {
  36. if (UpdateProper(ref _sizeScale, value))
  37. {
  38. sizeModeInfo.Scale = value;
  39. ctlPreview.Init(printSettingsInfo);
  40. }
  41. }
  42. }
  43. private string _pageRangeText;
  44. public string PageRangeText
  45. {
  46. get => _pageRangeText;
  47. set
  48. {
  49. _pageRangeText = value;
  50. if (!CommonHelper.GetPagesInRange(ref printSettingsInfo.PageRangeList, PageRangeText, Document.PageCount, new char[] { ',' }, new char[] { '-' }))
  51. {
  52. printSettingsInfo.PageRangeList = Enumerable.Range(0, Document.PageCount).ToList();
  53. }
  54. ctlPreview.Init(printSettingsInfo, true);
  55. }
  56. }
  57. private bool _isSheetEnabled = false;
  58. public bool IsSheetEnabled
  59. {
  60. get => _isSheetEnabled;
  61. set => UpdateProper(ref _isSheetEnabled, value);
  62. }
  63. private bool _isEvenEnabled = false;
  64. public bool IsEvenEnabled
  65. {
  66. get => _isEvenEnabled;
  67. set => UpdateProper(ref _isEvenEnabled, value);
  68. }
  69. private string _sheetColumn = 2.ToString();
  70. public string SheetColumn
  71. {
  72. get => _sheetColumn;
  73. set
  74. {
  75. if (UpdateProper(ref _sheetColumn, value))
  76. {
  77. if (SheetColumn != string.Empty && SheetRow != string.Empty)
  78. {
  79. multipleModeInfo.Sheet = new MultipleModeInfo.SheetPair(int.Parse(SheetColumn), int.Parse(SheetRow));
  80. }
  81. }
  82. }
  83. }
  84. private string _sheetRow = 2.ToString();
  85. public string SheetRow
  86. {
  87. get => _sheetRow;
  88. set
  89. {
  90. if (UpdateProper(ref _sheetRow, value))
  91. {
  92. if (SheetColumn != string.Empty && SheetRow != string.Empty)
  93. {
  94. multipleModeInfo.Sheet = new MultipleModeInfo.SheetPair(int.Parse(SheetColumn), int.Parse(SheetRow));
  95. }
  96. }
  97. }
  98. }
  99. public CPDFDocument Document
  100. {
  101. set
  102. {
  103. printSettingsInfo.Document = value;
  104. IsEvenEnabled = value.PageCount > 1;
  105. }
  106. private get => printSettingsInfo.Document;
  107. }
  108. public PrinterDialog()
  109. {
  110. InitializeComponent();
  111. InitPaperList();
  112. DataContext = this;
  113. }
  114. #region printer settings
  115. private void InitPrinterNameList()
  116. {
  117. List<string> printerList = Enumerable.Reverse(PrinterSettings.InstalledPrinters.Cast<string>()).ToList();
  118. cmbPrinterName.ItemsSource = printerList;
  119. cmbPrinterName.SelectedIndex = printerList.Contains("Microsoft Print to PDF") ? printerList.IndexOf("Microsoft Print to PDF") : 0;
  120. }
  121. #endregion
  122. private void PrinterDialog_Loaded(object sender, RoutedEventArgs e)
  123. {
  124. InitPrinterNameList();
  125. InitPrinterSettingsMode();
  126. ctlPreview.Init(printSettingsInfo);
  127. }
  128. private void InitPrinterSettingsMode()
  129. {
  130. printSettingsInfo.Copies = 1;
  131. printSettingsInfo.PrinterName = cmbPrinterName.SelectedItem.ToString();
  132. printSettingsInfo.PrintMode = sizeModeInfo;
  133. printSettingsInfo.PrintOrientation = PageOrientation.Portrait;
  134. printSettingsInfo.PaperSize = printerSettings.PaperSizes[cmbPaper.SelectedIndex];
  135. printSettingsInfo.IsPrintAnnot = true;
  136. printSettingsInfo.IsPrintForm = true;
  137. printSettingsInfo.IsReverseOrder = false;
  138. printSettingsInfo.IsGrayscale = false;
  139. printSettingsInfo.DuplexPrintMod = DuplexPrintMod.None;
  140. printSettingsInfo.PageRangeList = Enumerable.Range(0, Document.PageCount).ToList();
  141. printSettingsInfo.PrintDocument = printDoc;
  142. printSettingsInfo.NeedRerendering = true;
  143. printSettingsInfo.IsPaperSizeChanged = false;
  144. printSettingsInfo.NeedReversePage = false;
  145. printSettingsInfo.PrintMode = sizeModeInfo;
  146. isInited = true;
  147. }
  148. private void InitPaperList()
  149. {
  150. printerSettings = printDoc.PrinterSettings;
  151. foreach (PaperSize paperSize in printerSettings.PaperSizes)
  152. {
  153. cmbPaper.Items.Add(new ComboBoxItem
  154. {
  155. Content = $"{paperSize.PaperName} - {paperSize.Width} x {paperSize.Height}"
  156. });
  157. }
  158. int IndexOfA4 = printerSettings.PaperSizes.Cast<PaperSize>().ToList().FindIndex(x => x.PaperName == "A4");
  159. cmbPaper.SelectedIndex = IndexOfA4 != -1 ? IndexOfA4 : 0;
  160. printSettingsInfo.PaperSize = printerSettings.PaperSizes[cmbPaper.SelectedIndex];
  161. }
  162. private void cmbPrinterName_SelectionChanged(object sender, SelectionChangedEventArgs e)
  163. {
  164. printSettingsInfo.PrinterName = e.AddedItems[0].ToString();
  165. if (printSettingsInfo.PrinterName != string.Empty)
  166. {
  167. try
  168. {
  169. printQueue = PrintHelper.GetPrintQueue(PrintServer, printSettingsInfo.PrinterName);
  170. PrintHelper.printQueue = printQueue;
  171. }
  172. catch (Exception ex)
  173. {
  174. Console.WriteLine(ex.Message);
  175. return;
  176. }
  177. PrintTicket printTicket = printQueue.DefaultPrintTicket;
  178. PrintCapabilities printCapabilities = printQueue.GetPrintCapabilities();
  179. double printableWidth = printCapabilities.PageImageableArea.ExtentWidth / 0.96;
  180. double printableHeight = printCapabilities.PageImageableArea.ExtentHeight / 0.96;
  181. double originWidth = printCapabilities.PageImageableArea.OriginWidth / 0.96;
  182. double originHeight = printCapabilities.PageImageableArea.OriginHeight / 0.96;
  183. double marginRight = printSettingsInfo.PaperSize.Width - originWidth - printableWidth;
  184. double marginBottom = printSettingsInfo.PaperSize.Height - originHeight - printableHeight;
  185. printSettingsInfo.Margins = new Thickness() { Left = originWidth, Top = originHeight, Right = (marginRight >= 0) ? marginRight : 0, Bottom = (marginBottom >= 0) ? marginBottom : 0 };
  186. if (isInited)
  187. {
  188. ctlPreview.Init(printSettingsInfo);
  189. }
  190. }
  191. }
  192. private void cmbOrientation_SelectionChanged(object sender, SelectionChangedEventArgs e)
  193. {
  194. if (!IsLoaded)
  195. {
  196. return;
  197. }
  198. string tag = (e.AddedItems[0] as ComboBoxItem).Tag.ToString();
  199. switch (tag)
  200. {
  201. case "Portrait":
  202. printSettingsInfo.PrintOrientation = PageOrientation.Portrait;
  203. break;
  204. case "Landscape":
  205. printSettingsInfo.PrintOrientation = PageOrientation.Landscape;
  206. break;
  207. }
  208. ctlPreview.Init(printSettingsInfo);
  209. }
  210. private void cmbContent_SelectionChanged(object sender, SelectionChangedEventArgs e)
  211. {
  212. if (!IsLoaded)
  213. {
  214. return;
  215. }
  216. string tag = (e.AddedItems[0] as ComboBoxItem).Tag.ToString();
  217. switch (tag)
  218. {
  219. case "Document":
  220. printSettingsInfo.IsPrintAnnot = false;
  221. printSettingsInfo.IsPrintForm = false;
  222. break;
  223. case "Document and Markups":
  224. printSettingsInfo.IsPrintAnnot = true;
  225. printSettingsInfo.IsPrintForm = true;
  226. break;
  227. case "Document and Stamps":
  228. printSettingsInfo.IsPrintAnnot = true;
  229. printSettingsInfo.IsPrintForm = false;
  230. break;
  231. }
  232. ctlPreview.Init(printSettingsInfo, true);
  233. }
  234. private void chkReversePage_Click(object sender, RoutedEventArgs e)
  235. {
  236. var chk = sender as CheckBox;
  237. printSettingsInfo.IsReverseOrder = chk.IsChecked.Value;
  238. ctlPreview.Init(printSettingsInfo, true);
  239. }
  240. private void chkPrintBorder_Click(object sender, RoutedEventArgs e)
  241. {
  242. var chk = sender as CheckBox;
  243. }
  244. private void chkGrayScale_Click(object sender, RoutedEventArgs e)
  245. {
  246. var chk = sender as CheckBox;
  247. printSettingsInfo.IsGrayscale = chk.IsChecked.Value;
  248. ctlPreview.Init(printSettingsInfo);
  249. }
  250. private void chkDuplex_Click(object sender, RoutedEventArgs e)
  251. {
  252. var chk = sender as CheckBox;
  253. if (chk.IsChecked.GetValueOrDefault())
  254. {
  255. if (rdoLongEdge.IsChecked.GetValueOrDefault())
  256. {
  257. printSettingsInfo.DuplexPrintMod = DuplexPrintMod.FlipLongEdge;
  258. }
  259. else
  260. {
  261. printSettingsInfo.DuplexPrintMod = DuplexPrintMod.FlipShortEdge;
  262. }
  263. }
  264. else
  265. {
  266. printSettingsInfo.DuplexPrintMod = DuplexPrintMod.None;
  267. }
  268. }
  269. private void rdoDuplex_Click(object sender, RoutedEventArgs e)
  270. {
  271. var chk = sender as RadioButton;
  272. if (chkDuplex.IsChecked.GetValueOrDefault())
  273. {
  274. if (chk.IsChecked.GetValueOrDefault())
  275. {
  276. printSettingsInfo.DuplexPrintMod = chk.Tag.ToString() == "LongEdge" ? DuplexPrintMod.FlipLongEdge : DuplexPrintMod.FlipShortEdge;
  277. }
  278. }
  279. else
  280. {
  281. printSettingsInfo.DuplexPrintMod = DuplexPrintMod.None;
  282. }
  283. }
  284. private void cmbPaper_SelectionChanged(object sender, SelectionChangedEventArgs e)
  285. {
  286. if (!IsLoaded)
  287. {
  288. return;
  289. }
  290. sbyte index = (sbyte)cmbPaper.SelectedIndex;
  291. printSettingsInfo.PaperSize = printerSettings.PaperSizes[index];
  292. ctlPreview.Init(printSettingsInfo);
  293. }
  294. private void rdoPageRange_Click(object sender, RoutedEventArgs e)
  295. {
  296. var rdo = sender as RadioButton;
  297. string rdoTag = rdo.Tag.ToString();
  298. switch (rdoTag)
  299. {
  300. case "All":
  301. printSettingsInfo.PageRangeList = Enumerable.Range(0, Document.PageCount).ToList();
  302. break;
  303. case "Odd":
  304. printSettingsInfo.PageRangeList = Enumerable.Range(0, Document.PageCount).Where(x => x % 2 == 0).ToList();
  305. break;
  306. case "Even":
  307. printSettingsInfo.PageRangeList = Enumerable.Range(0, Document.PageCount).Where(x => x % 2 != 0).ToList();
  308. break;
  309. case "Custom":
  310. if (!CommonHelper.GetPagesInRange(ref printSettingsInfo.PageRangeList, PageRangeText, Document.PageCount, new char[] { ',' }, new char[] { '-' }))
  311. {
  312. printSettingsInfo.PageRangeList = Enumerable.Range(0, Document.PageCount).ToList();
  313. }
  314. break;
  315. }
  316. ctlPreview.Init(printSettingsInfo, true);
  317. }
  318. private void SizeMode_Click(object sender, RoutedEventArgs e)
  319. {
  320. var rdo = sender as RadioButton;
  321. string rdoTag = rdo.Tag.ToString();
  322. switch (rdoTag)
  323. {
  324. case "AutoAdapt":
  325. sizeModeInfo.SizeType = SizeType.Adaptive;
  326. break;
  327. case "ActualSize":
  328. sizeModeInfo.SizeType = SizeType.Actural;
  329. break;
  330. case "CustomScale":
  331. sizeModeInfo.SizeType = SizeType.Customized;
  332. sizeModeInfo.Scale = SizeScale;
  333. break;
  334. }
  335. ctlPreview.Init(printSettingsInfo);
  336. }
  337. private void cmbPageOrder_SelectionChanged(object sender, SelectionChangedEventArgs e)
  338. {
  339. if (!IsLoaded)
  340. {
  341. return;
  342. }
  343. string tag = (e.AddedItems[0] as ComboBoxItem).Tag.ToString();
  344. switch (tag)
  345. {
  346. case "Horizontal":
  347. multipleModeInfo.PageOrder = PageOrder.Horizontal;
  348. break;
  349. case "Horizontal Reverse":
  350. multipleModeInfo.PageOrder = PageOrder.HorizontalReverse;
  351. break;
  352. case "TopToBottom":
  353. multipleModeInfo.PageOrder = PageOrder.Vertical;
  354. break;
  355. case "BottomToTop":
  356. multipleModeInfo.PageOrder = PageOrder.VerticalReverse;
  357. break;
  358. }
  359. }
  360. public event PropertyChangedEventHandler PropertyChanged;
  361. protected bool UpdateProper<T>(ref T properValue,
  362. T newValue,
  363. [CallerMemberName] string properName = "")
  364. {
  365. if (object.Equals(properValue, newValue))
  366. return false;
  367. properValue = newValue;
  368. OnPropertyChanged(properName);
  369. return true;
  370. }
  371. protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
  372. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  373. private void btnConfirm_Click(object sender, RoutedEventArgs e)
  374. {
  375. PrintHelper.InitPrint();
  376. PrintHelper.PrintDocument(printSettingsInfo);
  377. Close();
  378. }
  379. private void btnCancel_Click(object sender, RoutedEventArgs e)
  380. {
  381. Close();
  382. }
  383. private void cmbOrientation_Loaded(object sender, RoutedEventArgs e)
  384. {
  385. bool isHeightWidthRatioGreaterThanOne = (printSettingsInfo.ActualHeight / printSettingsInfo.ActualWidth > 1) &&
  386. (Document.GetPageSize(0).height / Document.GetPageSize(0).width > 1);
  387. bool isHeightWidthRatioLessThanOne = (printSettingsInfo.ActualHeight / printSettingsInfo.ActualWidth < 1) &&
  388. (Document.GetPageSize(0).height / Document.GetPageSize(0).width < 1);
  389. cmbOrientation.SelectedIndex = (isHeightWidthRatioGreaterThanOne || isHeightWidthRatioLessThanOne) ? 0 : 1;
  390. }
  391. private void cmbContent_Loaded(object sender, RoutedEventArgs e)
  392. {
  393. cmbContent.SelectedIndex = 1;
  394. }
  395. private void chkBorderless_Click(object sender, RoutedEventArgs e)
  396. {
  397. var chk = sender as CheckBox;
  398. printSettingsInfo.IsBorderless = chk.IsChecked.Value;
  399. ctlPreview.Init(printSettingsInfo);
  400. }
  401. private void rdoFitPrintable_Checked(object sender, RoutedEventArgs e)
  402. {
  403. printSettingsInfo.IsBorderless = false;
  404. if (isInited)
  405. {
  406. ctlPreview.Init(printSettingsInfo);
  407. }
  408. }
  409. private void rdoFitPage_Checked(object sender, RoutedEventArgs e)
  410. {
  411. printSettingsInfo.IsBorderless = true;
  412. if (isInited)
  413. {
  414. ctlPreview.Init(printSettingsInfo);
  415. }
  416. }
  417. }
  418. }