HomePagePrinterDialogViewModel.cs 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145
  1. using Prism.Commands;
  2. using Prism.Mvvm;
  3. using Prism.Regions;
  4. using Prism.Services.Dialogs;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Windows;
  8. using System.Drawing.Printing;
  9. using ComPDFKit.PDFDocument;
  10. using PDF_Office.Model;
  11. using System.Drawing;
  12. using System.Reflection;
  13. using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePagePrinter;
  14. using System.Threading.Tasks;
  15. using ComPDFKit.PDFPage;
  16. using PDF_Office.Helper;
  17. using System.Drawing.Drawing2D;
  18. using ComPDFKitViewer.PdfViewer;
  19. using System.Windows.Media.Imaging;
  20. using System.Drawing.Imaging;
  21. using PDF_Office.CustomControl;
  22. using Prism.Events;
  23. using ImTools;
  24. namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter
  25. {
  26. public class HomePagePrinterDialogViewModel : BindableBase, IDialogAware
  27. {
  28. #region Base
  29. public IDialogService Dialogs;
  30. private readonly IEventAggregator eventAggregator;
  31. public IRegionManager PrintModRegion;
  32. private Object locker = new Object();
  33. private CPDFViewer currentViewer;
  34. private string _currentPrintModName;
  35. public string CurrentPrintModName
  36. {
  37. get { return _currentPrintModName; }
  38. set
  39. {
  40. if (value == "ModSize")
  41. {
  42. IsEnableForModBooklet = true;
  43. IsEnableForModMultiple = true;
  44. IsEnableForModSize = false;
  45. }
  46. else if (value == "ModMultiple")
  47. {
  48. IsEnableForModBooklet = true;
  49. IsEnableForModMultiple = false;
  50. IsEnableForModSize = true;
  51. }
  52. else if (value == "ModBooklet")
  53. {
  54. IsEnableForModBooklet = false;
  55. IsEnableForModMultiple = true;
  56. IsEnableForModSize = true;
  57. }
  58. _currentPrintModName = value;
  59. }
  60. }
  61. public HomePagePrinterDialogModel homePagePrinterDialogModel;
  62. public HomePagePrinterPageSetDialogViewModel homePagePrinterPageSetDialogViewModel { get; set; }
  63. public PrintSettingsInfo printSettingsInfo;
  64. #endregion
  65. #region 界面和组件设置
  66. private string _printModRegionName;
  67. public string PrintModRegionName
  68. {
  69. get
  70. {
  71. return _printModRegionName;
  72. }
  73. set
  74. {
  75. SetProperty(ref _printModRegionName, value);
  76. }
  77. }
  78. private List<string> _printerNameList;
  79. public List<string> PrinterNameList
  80. {
  81. get { return _printerNameList; }
  82. set { _printerNameList = value; }
  83. }
  84. private List<string> _printContentList;
  85. public List<string> PrintContentList
  86. {
  87. get { return _printContentList; }
  88. set { _printContentList = value; }
  89. }
  90. private CPDFDocument document;
  91. private List<string> _printerOrientationList;
  92. public List<string> PrintOrientationList
  93. {
  94. get { return _printerOrientationList; }
  95. set
  96. {
  97. _printerOrientationList = value;
  98. }
  99. }
  100. private bool _isEnableForGetNextPage = true;
  101. public bool IsEnableForGetNextPage
  102. {
  103. get { return _isEnableForGetNextPage; }
  104. set
  105. {
  106. _isEnableForGetNextPage = value;
  107. RaisePropertyChanged();
  108. }
  109. }
  110. private bool _isEnableForGetPreviousPage = false;
  111. public bool IsEnableForGetPreviousPage
  112. {
  113. get { return _isEnableForGetPreviousPage; }
  114. set
  115. {
  116. _isEnableForGetPreviousPage = value;
  117. RaisePropertyChanged();
  118. }
  119. }
  120. private bool _isEnableForModSize = false;
  121. public bool IsEnableForModSize
  122. {
  123. get { return _isEnableForModSize; }
  124. set
  125. {
  126. SetProperty(ref _isEnableForModSize, value);
  127. }
  128. }
  129. private bool _isEnableForModPoster = false;
  130. public bool IsEnableForModPoster
  131. {
  132. get { return _isEnableForModPoster; }
  133. set { _isEnableForModPoster = value; }
  134. }
  135. private bool _isEnableForModMultiple = true;
  136. public bool IsEnableForModMultiple
  137. {
  138. get { return _isEnableForModMultiple; }
  139. set { SetProperty(ref _isEnableForModMultiple, value); }
  140. }
  141. private bool _isEnableForModBooklet = true;
  142. public bool IsEnableForModBooklet
  143. {
  144. get { return _isEnableForModBooklet; }
  145. set
  146. {
  147. SetProperty(ref _isEnableForModBooklet, value);
  148. }
  149. }
  150. #endregion
  151. #region 属性设置
  152. private int _paperHeight = 297;
  153. /// <summary>
  154. /// 纸张高度,默认A4
  155. /// </summary>
  156. public int PaperHeight
  157. {
  158. get { return _paperHeight; }
  159. set { SetProperty(ref _paperHeight, value); }
  160. }
  161. private int _paperWidth = 210;
  162. public int PaperWidth
  163. {
  164. get { return _paperWidth; }
  165. set { SetProperty(ref _paperWidth, value); }
  166. }
  167. private int _canvasHeight = 297;
  168. public int CanvasHeight
  169. {
  170. get { return _canvasHeight; }
  171. set { SetProperty(ref _canvasHeight, value); }
  172. }
  173. private int _canvasWidth = 210;
  174. public int CanvasWidth
  175. {
  176. get { return _canvasWidth; }
  177. set { SetProperty(ref _canvasWidth, value); }
  178. }
  179. private int _viewBoxHeight = 296;
  180. /// <summary>
  181. /// 显示(高)度,默认A4
  182. /// </summary>
  183. public int ViewBoxHeight
  184. {
  185. get { return _viewBoxHeight; }
  186. set
  187. {
  188. SetProperty(ref _viewBoxHeight, value);
  189. }
  190. }
  191. private int _viewBoxWidth = 209;
  192. /// <summary>
  193. /// 显示宽度,默认A4
  194. /// </summary>
  195. public int ViewBoxWidth
  196. {
  197. get { return _viewBoxWidth; }
  198. set
  199. {
  200. SetProperty(ref _viewBoxWidth, value);
  201. }
  202. }
  203. private EnumPaperSize _enumPaperSize;
  204. /// <summary>
  205. /// 纸张尺寸枚举
  206. /// </summary>
  207. public EnumPaperSize EnumPaperSize
  208. {
  209. get { return _enumPaperSize; }
  210. set { _enumPaperSize = value; }
  211. }
  212. private int _leftMargin = 0;
  213. /// <summary>
  214. /// 纸张左边距
  215. /// </summary>
  216. public int LeftMargin
  217. {
  218. get { return _leftMargin; }
  219. set { _leftMargin = value; }
  220. }
  221. private int _rightMargin = 0;
  222. /// <summary>
  223. /// 纸张右边距
  224. /// </summary>
  225. public int RightMargin
  226. {
  227. get { return _rightMargin; }
  228. set { _rightMargin = value; }
  229. }
  230. private int _bottomMargin = 0;
  231. /// <summary>
  232. /// 纸张下边距
  233. /// </summary>
  234. public int BottomMargin
  235. {
  236. get { return _bottomMargin; }
  237. set { _bottomMargin = value; }
  238. }
  239. private int _topMargin = 0;
  240. /// <summary>
  241. /// 纸张上边距
  242. /// </summary>
  243. public int TopMargin
  244. {
  245. get { return _topMargin; }
  246. set { _topMargin = value; }
  247. }
  248. private int _rotateAngle = 0;
  249. public int RotateAngle
  250. {
  251. get { return _rotateAngle; }
  252. set { SetProperty(ref _rotateAngle, value); }
  253. }
  254. private int _rotateCenterX = 0;
  255. public int RotateCenterX
  256. {
  257. get { return _rotateCenterX; }
  258. set { SetProperty(ref _rotateCenterX, value); }
  259. }
  260. private int _rotateCenterY = 0;
  261. public int RotateCenterY
  262. {
  263. get { return _rotateCenterY; }
  264. set { SetProperty(ref _rotateCenterY, value); }
  265. }
  266. private Visibility _printModVisible = Visibility.Collapsed;
  267. /// <summary>
  268. /// PrintMod region可见
  269. /// </summary>
  270. public Visibility PrintModVisible
  271. {
  272. get { return _printModVisible; }
  273. set { SetProperty(ref _printModVisible, value); }
  274. }
  275. private BitmapSource _bitmapSource;
  276. /// <summary>
  277. /// 渲染后的图
  278. /// </summary>
  279. public BitmapSource BitmapSource
  280. {
  281. get { return _bitmapSource; }
  282. set { SetProperty(ref _bitmapSource, value); }
  283. }
  284. private int _printerIndex = 0;
  285. /// <summary>
  286. /// 打印机索引
  287. /// </summary>
  288. public int PrinterIndex
  289. {
  290. get { return _printerIndex; }
  291. set { _printerIndex = value; }
  292. }
  293. private string _maxPageNumber;
  294. /// <summary>
  295. /// 总页面数
  296. /// </summary>
  297. public string MaxPageNumber
  298. {
  299. get
  300. {
  301. return _maxPageNumber;
  302. }
  303. set
  304. {
  305. if (TargetPages.Count != 0)
  306. {
  307. _maxPageNumber = value.ToString();
  308. }
  309. else
  310. {
  311. _maxPageNumber = "NaN";
  312. }
  313. RaisePropertyChanged();
  314. }
  315. }
  316. private string _customPageRange;
  317. /// <summary>
  318. /// 页面范围:自定义页面
  319. /// </summary>
  320. public string CustomPageRange
  321. {
  322. get { return _customPageRange; }
  323. set { _customPageRange = value; }
  324. }
  325. private int _pageRangeIndex = 0;
  326. /// <summary>
  327. ///<para>页面范围:下拉框索引</para>
  328. ///<para>0:全部页面</para>
  329. ///<para>1:当前页面</para>
  330. ///<para>2:奇数页</para>
  331. ///<para>3:偶数页</para>
  332. ///<para>4:自定义页面</para>
  333. /// </summary>
  334. public int PageRangeIndex
  335. {
  336. get { return _pageRangeIndex; }
  337. set
  338. {
  339. _pageRangeIndex = value;
  340. RaisePropertyChanged();
  341. }
  342. }
  343. private bool _isBlackAndWhite;
  344. /// <summary>
  345. /// 是否黑白打印
  346. /// </summary>
  347. public bool IsBlackAndWhite
  348. {
  349. get { return _isBlackAndWhite; }
  350. set { _isBlackAndWhite = value; }
  351. }
  352. private int _printContentIndex = 1;
  353. /// <summary>
  354. /// 打印内容索引
  355. /// <para>0:文档</para>
  356. /// <para>1:文档和标记</para>
  357. /// <para>2:文档和图章</para>
  358. /// <para>3:文档和表单</para>
  359. /// </summary>
  360. public int PrintContentIndex
  361. {
  362. get { return _printContentIndex; }
  363. set { _printContentIndex = value; }
  364. }
  365. private bool _isPageIndexReverse;
  366. /// <summary>
  367. /// 是否逆序打印(属性)
  368. /// </summary>
  369. public bool IsPageIndexReverse
  370. {
  371. get { return _isPageIndexReverse; }
  372. set { _isPageIndexReverse = value; }
  373. }
  374. private bool _needPageIndexReverse;
  375. public bool NeedPageIndexReverse
  376. {
  377. get { return _needPageIndexReverse; }
  378. set { _needPageIndexReverse = value; }
  379. }
  380. private int _printOrientationIndex;
  381. /// <summary>
  382. ///<para>打印方向索引</para>
  383. ///<para>0:纵向(默认)</para>
  384. ///<para>1:横向</para>
  385. /// </summary>
  386. public int PrintOrientationIndex
  387. {
  388. get { return _printOrientationIndex; }
  389. set
  390. {
  391. if (value != _printOrientationIndex)
  392. {
  393. if(value == 0)
  394. {
  395. RotatePreviewPaperWhenPortrait();
  396. }
  397. if (value == 1)
  398. {
  399. RotatePreviewPaperWhenLandscape();
  400. }
  401. }
  402. _printOrientationIndex = value;
  403. if (flagIsAutoOrientation == true)
  404. {
  405. flagIsAutoOrientation = false;
  406. }
  407. RaisePropertyChanged(nameof(PrintOrientationIndex));
  408. }
  409. }
  410. #endregion
  411. #region 打印
  412. private PrintDocument printDocument = new PrintDocument();
  413. private PrinterSettings printerSettings = new PrinterSettings();
  414. private PageSettings pageSettings = new PageSettings();
  415. private int PrintIndex = 0;
  416. private int printListIndex = 0;
  417. private PageSettings PageSettings = new PageSettings();
  418. /// <summary>
  419. ///待打印页面集合,存储页面索引值
  420. /// </summary>
  421. private List<int> TargetPages = new List<int>();
  422. /// <summary>
  423. /// 当前显示的页码索引, 该值被设置到边界时将修改翻页按钮的可用性
  424. /// </summary>
  425. private int _currentPreviewIndex;
  426. public int CurrentPreviewIndex
  427. {
  428. get { return _currentPreviewIndex; }
  429. set
  430. {
  431. if (currentListIndex > 0)
  432. {
  433. IsEnableForGetPreviousPage = true;
  434. }
  435. else
  436. {
  437. IsEnableForGetPreviousPage = false;
  438. }
  439. if (currentListIndex < int.Parse(MaxPageNumber) - 1)
  440. {
  441. IsEnableForGetNextPage = true;
  442. }
  443. else
  444. {
  445. IsEnableForGetNextPage = false;
  446. }
  447. _currentPreviewIndex = value;
  448. }
  449. }
  450. /// <summary>
  451. /// 预览显示的页码编号,由TargetPages映射
  452. /// </summary>
  453. public int currentListIndex = 0;
  454. /// <summary>
  455. /// <para>Current表示当前选中的,Existent表示目前存在的</para>
  456. /// <para>一次改变后,current成为existent,existent变成previous</para>
  457. /// </summary>
  458. private string _existentPrintModName = "ModSize";
  459. public string ExistentPrintModName
  460. {
  461. get { return _existentPrintModName; }
  462. set { _existentPrintModName = value; }
  463. }
  464. /// <summary>
  465. /// 预览显示的页码编号,由TargetPages映射
  466. /// </summary>
  467. private int _currentListIndex = 1;
  468. public string CurrentListIndexString
  469. {
  470. get { return _currentListIndex.ToString(); }
  471. set
  472. {
  473. if (value != "")
  474. {
  475. if (int.Parse(value) >= 0 && int.Parse(value) < int.Parse(MaxPageNumber))
  476. {
  477. _currentListIndex = int.Parse(value);
  478. currentListIndex = _currentListIndex;
  479. SetProperty(ref _currentListIndex, int.Parse(value));
  480. }
  481. else
  482. {
  483. System.Windows.MessageBox.Show("error num");
  484. }
  485. }
  486. else
  487. {
  488. System.Windows.MessageBox.Show("error space");
  489. }
  490. }
  491. }
  492. /// <summary>
  493. /// 海报模式,水平需要的纸张计数
  494. /// </summary>
  495. private int _horizontalPaperCountModPoster;
  496. public int HorizontalPaperCountModPoster
  497. {
  498. get { return _horizontalPaperCountModPoster; }
  499. set { _horizontalPaperCountModPoster = value; }
  500. }
  501. /// <summary>
  502. /// 海报模式:垂直需要的纸张计数
  503. /// </summary>
  504. private int _verticalPaperCountModPoster;
  505. public int VerticalPaperCountModPoster
  506. {
  507. get { return _verticalPaperCountModPoster; }
  508. set { _verticalPaperCountModPoster = value; }
  509. }
  510. private int _printIndexModMultiple = 0;
  511. /// <summary>
  512. /// 多页模式下打印原PDF文件页面顺序索引
  513. /// </summary>
  514. public int PrintIndexModMultiple
  515. {
  516. get { return _printIndexModMultiple; }
  517. set { _printIndexModMultiple = value; }
  518. }
  519. System.Drawing.Point location = new System.Drawing.Point(10, 10);
  520. System.Drawing.Size size = new System.Drawing.Size(600, 150);
  521. #endregion
  522. #region 字典
  523. public Dictionary<string, string> GetContentByPrintMod;
  524. public Dictionary<int, string> GetPrinterNameByPrinterIndex;
  525. #endregion
  526. #region 杂项和其他标记
  527. /// <summary>
  528. /// 标记:自动旋转-仅未修改旋转方向时生效
  529. /// </summary>
  530. private bool flagIsAutoOrientation = true;
  531. /// <summary>
  532. /// 标记:防止控件相互调用多次渲染
  533. /// </summary>
  534. private bool flagForNotRenderTwice = true;
  535. /// <summary>
  536. /// 标记:第一次启动窗口,防止其他控件唤起渲染
  537. /// </summary>
  538. private bool flagIsFirstView = true;
  539. /// <summary>
  540. ///
  541. /// </summary>
  542. private int flagPreviousRenderPageIndex;
  543. #endregion
  544. #region 委托
  545. public DelegateCommand SetPaperSizeCommand { get; set; }
  546. public DelegateCommand SetIsBlackAndWhiteCommand { get; set; }
  547. public DelegateCommand SetPrintOrientationCommand { get; set; }
  548. public DelegateCommand SetPrintContentCommand { get; set; }
  549. public DelegateCommand SetIsPageIndexReverseCommand { get; set; }
  550. public DelegateCommand SetPageRangeCommand { get; set; }
  551. public DelegateCommand<object> ChangePrintModCommand { get; set; }
  552. public DelegateCommand GetNextPagePreviewCommand { get; set; }
  553. public DelegateCommand GetPreviousPagePreviewCommand { get; set; }
  554. public DelegateCommand GetPagePreviewWhenIndexChangedCommand { get; set; }
  555. public DelegateCommand SubscribeCommand { get; set; }
  556. public DelegateCommand ConfirmPrintCommand { get; set; }
  557. #endregion
  558. public HomePagePrinterDialogViewModel(IDialogService dialogService, IRegionManager regionManager, IEventAggregator eventAggregator)
  559. {
  560. Dialogs = dialogService;
  561. PrintModRegion = regionManager;
  562. PrintModVisible = Visibility.Visible;
  563. homePagePrinterDialogModel = new HomePagePrinterDialogModel();
  564. homePagePrinterPageSetDialogViewModel = new HomePagePrinterPageSetDialogViewModel();
  565. PrintModRegionName = Guid.NewGuid().ToString();
  566. SetPaperSizeCommand = new DelegateCommand(SetPaperSize);
  567. SetIsBlackAndWhiteCommand = new DelegateCommand(SetIsBlackAndWhite);
  568. SetPrintOrientationCommand = new DelegateCommand(SetPrintOrientation);
  569. SetPageRangeCommand = new DelegateCommand(SetPageRange);
  570. SetPrintContentCommand = new DelegateCommand(SetPrintContent);
  571. SetIsPageIndexReverseCommand = new DelegateCommand(SetIsPageIndexReverse);
  572. ChangePrintModCommand = new DelegateCommand<object>(ChangePrintMod);
  573. eventAggregator.GetEvent<ModInfoSendEvent>().Subscribe(SubscribeEvent);
  574. GetPagePreviewWhenIndexChangedCommand = new DelegateCommand(GetPagePreviewWhenIndexChanged);
  575. ConfirmPrintCommand = new DelegateCommand(ConfirmPrint);
  576. printSettingsInfo = new PrintSettingsInfo();
  577. GetContentByPrintMod = new Dictionary<string, string>();
  578. InitGetContentByPrintMod(ref GetContentByPrintMod);
  579. GetPrinterNameByPrinterIndex = new Dictionary<int, string>();
  580. InitGetPrinterNameByPrinterIndex(ref GetPrinterNameByPrinterIndex);
  581. InitComponent();
  582. }
  583. #region 组件初始化
  584. private void InitComponent()
  585. {
  586. InitPrinterNameList();
  587. InitPrintContentList();
  588. InitPrintContentList();
  589. InitPrintOrientationList();
  590. }
  591. private void PaintPageByCurrentPreviewIndex()
  592. {
  593. if (flagIsAutoOrientation)
  594. {
  595. if (document.GetPageSize(CurrentPreviewIndex).Width > document.GetPageSize(CurrentPreviewIndex).Height)
  596. {
  597. //printSettingsInfo.EnumPrintOrientation = EnumPrintOrientation.StatusLandscape;
  598. PrintOrientationIndex = 1;
  599. }
  600. }
  601. if (NeedPageIndexReverse)
  602. {
  603. CurrentPreviewIndex = TargetPages.Count - CurrentPreviewIndex - 1;
  604. }
  605. lock (locker)
  606. {
  607. switch (CurrentPrintModName)
  608. {
  609. case "ModPoster":
  610. PreviewModPoster(CurrentPreviewIndex);
  611. break;
  612. case "ModMultiple":
  613. PreviewModMultiple(CurrentPreviewIndex);
  614. break;
  615. case "ModBooklet":
  616. PreviewModBooklet(CurrentPreviewIndex);
  617. break;
  618. case "ModSize":
  619. default:
  620. PreviewModSize(CurrentPreviewIndex);
  621. break;
  622. }
  623. }
  624. }
  625. private void InitPrinterNameList()
  626. {
  627. PrinterNameList = new List<string>();
  628. PrintDocument printDocument = new PrintDocument();
  629. string defaultPrinterName = printDocument.PrinterSettings.PrinterName;
  630. foreach (string printerListItem in PrinterSettings.InstalledPrinters)
  631. {
  632. PrinterNameList.Add(printerListItem);
  633. }
  634. }
  635. private void InitPrintContentList()
  636. {
  637. PrintContentList = new List<string>();
  638. PrintContentList.Add("文档");
  639. PrintContentList.Add("文档和标记");
  640. PrintContentList.Add("文档和图章");
  641. PrintContentList.Add("文档和表单");
  642. }
  643. private void InitPrintOrientationList()
  644. {
  645. PrintOrientationList = new List<string>();
  646. PrintOrientationList.Add("纵向");
  647. PrintOrientationList.Add("横向");
  648. }
  649. private void InitGetContentByPrintMod(ref Dictionary<string, string> dictionary)
  650. {
  651. dictionary.Add("ModSize", "HomePagePrinterModSizeContent");
  652. dictionary.Add("ModPoster", "HomePagePrinterModPosterContent");
  653. dictionary.Add("ModMultiple", "HomePagePrinterModMultipleContent");
  654. dictionary.Add("ModBooklet", "HomePagePrinterModBookletContent");
  655. }
  656. private void InitGetPrinterNameByPrinterIndex(ref Dictionary<int, string> dictionary)
  657. {
  658. int index = 0;
  659. PrinterNameList = new List<string>();
  660. PrintDocument printDocument = new PrintDocument();
  661. string defaultPrinterName = printDocument.PrinterSettings.PrinterName;
  662. foreach (string printerListItem in PrinterSettings.InstalledPrinters)
  663. {
  664. dictionary.Add(index++, printerListItem);
  665. }
  666. if (index == 0)
  667. {
  668. #if DEBUG
  669. MessageBoxEx.Show("No Printer!");
  670. #endif
  671. }
  672. }
  673. #endregion
  674. #region 状态初始化
  675. /// <summary>
  676. /// 初始化PrintSettingsInfo表
  677. /// </summary>
  678. public void InitPrintParams()
  679. {
  680. printSettingsInfo.PrinterName = GetPrinterNameByPrinterIndex[PrinterIndex];
  681. printSettingsInfo.Copies = 1;
  682. printSettingsInfo.IsBlackAndWhite = false;
  683. printSettingsInfo.IsReverseOrder = false;
  684. printSettingsInfo.PrintWithAnnot = true;
  685. printSettingsInfo.PrintWithForm = true;
  686. printSettingsInfo.EnumPrintOrientation = EnumPrintOrientation.StatusPortrait;
  687. SizeInfo sizeSetttingsInfo = new SizeInfo();
  688. sizeSetttingsInfo.EnumSizeType = EnumSizeType.StatusAdaptive;
  689. sizeSetttingsInfo.DisplayRatio = 100;
  690. printSettingsInfo.printMod = sizeSetttingsInfo;
  691. }
  692. /// <summary>
  693. /// 初始化预览
  694. /// </summary>
  695. public void InitRender()
  696. {
  697. CurrentPreviewIndex = TargetPages[0];
  698. PaintPageByCurrentPreviewIndex();
  699. flagIsFirstView = false;
  700. }
  701. #endregion
  702. #region 打印预览
  703. public void RotatePreviewPaperWhenLandscape()
  704. {
  705. RotateAngle = 90;
  706. RotateCenterX = ViewBoxWidth / 2;
  707. RotateCenterY = ViewBoxHeight / 2;
  708. int temp;
  709. temp = PaperWidth;
  710. PaperWidth = PaperHeight;
  711. PaperHeight = temp;
  712. }
  713. public void RotatePreviewPaperWhenPortrait()
  714. {
  715. RotateAngle = 0;
  716. RotateCenterX = 0;
  717. RotateCenterY = 0;
  718. }
  719. /// <summary>
  720. /// 翻页刷新
  721. /// </summary>
  722. public void GetPagePreviewWhenIndexChanged()
  723. {
  724. if (CurrentPrintModName == "ModSize" && CurrentPrintModName == "ModPoster")
  725. {
  726. CurrentPreviewIndex = TargetPages[currentListIndex];
  727. }
  728. else
  729. {
  730. CurrentPreviewIndex = currentListIndex;
  731. }
  732. PaintPageByCurrentPreviewIndex();
  733. }
  734. public double GetDpiByGraphics()
  735. {
  736. BindingFlags bindingAttr = BindingFlags.Static | BindingFlags.NonPublic;
  737. PropertyInfo property = typeof(SystemParameters).GetProperty("Dpi", bindingAttr);
  738. return (int)property.GetValue(null, null);
  739. }
  740. /// <summary>
  741. /// bitmap->bitmapsource转换器,用于显示到image控件
  742. /// </summary>
  743. /// <param name="image">
  744. /// Bitmap格式图片资源
  745. /// </param>
  746. /// <returns></returns>
  747. public static BitmapSource ToBitmapSource(System.Drawing.Bitmap image)
  748. {
  749. IntPtr ptr = image.GetHbitmap();//obtain the Hbitmap
  750. BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap
  751. (
  752. ptr,
  753. IntPtr.Zero,
  754. Int32Rect.Empty,
  755. System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()
  756. );
  757. return bs;
  758. }
  759. /// <summary>
  760. /// 重新调整图片大小(比例不变) 缩放图片
  761. /// 可以直接传入目标区域大小,不会丢失原图比例
  762. /// </summary>
  763. /// <param name="image"></param>
  764. /// <param name="size"></param>
  765. /// <returns></returns>
  766. public System.Drawing.Image ResizeImage(System.Drawing.Image image, System.Drawing.Size size)
  767. {
  768. //获取图片宽度
  769. int sourceWidth = image.Width;
  770. //获取图片高度
  771. int sourceHeight = image.Height;
  772. float nPercent = 0;
  773. float nPercentW = 0;
  774. float nPercentH = 0;
  775. //计算宽度的缩放比例
  776. //nPercentW = ((float)size.Width * (float)GetDpiByGraphics()/120/ (float)sourceWidth);
  777. ////计算高度的缩放比例
  778. //nPercentH = ((float)size.Height*(float)GetDpiByGraphics() / 120 / (float)sourceHeight);
  779. nPercentW = ((float)size.Width / (float)sourceWidth);
  780. //计算高度的缩放比例
  781. nPercentH = ((float)size.Height / (float)sourceHeight);
  782. if (nPercentH < nPercentW)
  783. nPercent = nPercentH;
  784. else
  785. nPercent = nPercentW;
  786. //期望的宽度
  787. int destWidth = (int)(sourceWidth * nPercent);
  788. //期望的高度
  789. int destHeight = (int)(sourceHeight * nPercent);
  790. System.Drawing.Bitmap b = new System.Drawing.Bitmap(destWidth, destHeight);
  791. Graphics g = Graphics.FromImage((System.Drawing.Image)b);
  792. //g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  793. g.SmoothingMode = SmoothingMode.None; //抗锯齿
  794. g.InterpolationMode = InterpolationMode.HighQualityBicubic; //高质量的双三次插值
  795. g.CompositingQuality = CompositingQuality.HighQuality; //高质量、低速度复合
  796. //绘制图像
  797. g.DrawImage(image, 0, 0, destWidth, destHeight);
  798. g.Dispose();
  799. return b;
  800. }
  801. /// <summary>
  802. /// 转灰度
  803. /// </summary>
  804. /// <param name="bmp"></param>
  805. /// <param name="mode"></param>
  806. /// <returns></returns>
  807. private Bitmap ToGray(Bitmap bmp, int mode)
  808. {
  809. if (bmp == null)
  810. {
  811. return null;
  812. }
  813. int w = bmp.Width;
  814. int h = bmp.Height;
  815. try
  816. {
  817. byte newColor = 0;
  818. BitmapData srcData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  819. unsafe
  820. {
  821. byte* p = (byte*)srcData.Scan0.ToPointer();
  822. for (int y = 0; y < h; y++)
  823. {
  824. for (int x = 0; x < w; x++)
  825. {
  826. if (mode == 0) // 加权平均
  827. {
  828. newColor = (byte)((float)p[0] * 0.114f + (float)p[1] * 0.587f + (float)p[2] * 0.299f);
  829. }
  830. else    // 算数平均
  831. {
  832. newColor = (byte)((float)(p[0] + p[1] + p[2]) / 3.0f);
  833. }
  834. p[0] = newColor;
  835. p[1] = newColor;
  836. p[2] = newColor;
  837. p += 3;
  838. }
  839. p += srcData.Stride - w * 3;
  840. }
  841. bmp.UnlockBits(srcData);
  842. return bmp;
  843. }
  844. }
  845. catch
  846. {
  847. return null;
  848. }
  849. }
  850. /// <summary>
  851. /// Size模式下的当前页预览
  852. /// </summary>
  853. /// <param name="paperIndex"></param>
  854. private async void PreviewModSize(int paperIndex)
  855. {
  856. SizeInfo sizeInfo = (SizeInfo)printSettingsInfo.printMod;
  857. CPDFPage cPDFPage = document.PageAtIndex(paperIndex);
  858. if (cPDFPage != null)
  859. {
  860. double left = 0;
  861. double top = 0;
  862. double ratio = 0.55;
  863. int dpi = (int)GetDpiByGraphics();
  864. Bitmap bitmap = await ToolMethod.RenderPageBitmap(document, (int)(cPDFPage.PageSize.Width * ratio * 1.4), (int)(cPDFPage.PageSize.Height * ratio * 1.4), paperIndex, printSettingsInfo.PrintWithAnnot, printSettingsInfo.PrintWithForm);
  865. var pageBound = printDocument.DefaultPageSettings.Bounds;
  866. if (printSettingsInfo.IsBlackAndWhite)
  867. {
  868. bitmap = ToGray(bitmap, 0);
  869. }
  870. double ZoomRate = (double)sizeInfo.DisplayRatio / 100.0;
  871. if (printSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusLandscape)
  872. {
  873. bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  874. bitmap = (Bitmap)ResizeImage(bitmap, new System.Drawing.Size((int)(pageBound.Width * ratio), (int)(pageBound.Height * ratio)));
  875. }
  876. System.Drawing.Size newSize = new System.Drawing.Size();
  877. if (sizeInfo.EnumSizeType == EnumSizeType.StatusAdaptive)
  878. {
  879. float zoompercent = 0;
  880. float percentW = 0;
  881. float percentH = 0;
  882. percentW = ((float)(pageBound.Width * ratio) / (float)bitmap.Width);
  883. percentH = ((float)(pageBound.Height * ratio) / (float)bitmap.Height);
  884. zoompercent = percentH > percentW ? percentW : percentH;
  885. newSize = new System.Drawing.Size((int)(bitmap.Width * zoompercent), (int)(bitmap.Height * zoompercent));
  886. }
  887. else if (sizeInfo.EnumSizeType == EnumSizeType.StatusActural)
  888. {
  889. newSize = new System.Drawing.Size((int)(bitmap.Width), (int)(bitmap.Height));
  890. }
  891. else
  892. {
  893. newSize = new System.Drawing.Size((int)(bitmap.Width * (double)(sizeInfo.DisplayRatio / 100)), (int)(bitmap.Height * (double)(sizeInfo.DisplayRatio / 100)));
  894. }
  895. bitmap = (Bitmap)ResizeImage(bitmap, newSize);
  896. left = (pageBound.Width * ratio - bitmap.Width) / 2;
  897. top = (pageBound.Height * ratio - bitmap.Height) / 2;
  898. int pwidth = (int)(pageBound.Width * ratio);
  899. int pheight = (int)(pageBound.Height * ratio);
  900. Bitmap bmp = new Bitmap(pwidth, pheight);
  901. Graphics g = Graphics.FromImage(bmp);
  902. g.DrawImage(bitmap, (float)left, (float)top, newSize.Width, bitmap.Height);
  903. BitmapSource = ToBitmapSource(bitmap);
  904. }
  905. }
  906. /// <summary>
  907. /// 海报模式预览
  908. /// </summary>
  909. /// <param name="paperIndex"></param>
  910. private async void PreviewModPoster(int paperIndex)
  911. {
  912. PosterInfo PosterInfo = (PosterInfo)printSettingsInfo.printMod;
  913. if (PosterInfo.EnumPosterMod == EnumPosterMod.StatusTile)
  914. {
  915. PostModTileInfo postModTileInfo = (PostModTileInfo)PosterInfo;
  916. double zoomRatio = (double)postModTileInfo.TileRatio / 100.0;
  917. double Ratio = 0.35;
  918. int dpi = (int)GetDpiByGraphics();
  919. //重叠部分的像素
  920. double overLap = Ratio * ((double)postModTileInfo.OverLap / 10) / 2.54 * dpi;
  921. var pagebound = printDocument.DefaultPageSettings.Bounds;
  922. //裁剪标记的矩形边长和位置
  923. int side = 20;
  924. int margin = -side / 2;
  925. //预览图的纸张大小 已缩小
  926. Rectangle printBound = new Rectangle(pagebound.X, pagebound.Y, (int)(pagebound.Width * Ratio), (int)(pagebound.Height * Ratio));
  927. CPDFPage cPDFPage = document.PageAtIndex(paperIndex);
  928. Bitmap bitmap = await ToolMethod.RenderPageBitmap(document, (int)(cPDFPage.PageSize.Width * Ratio * zoomRatio * 1.4), (int)(cPDFPage.PageSize.Height * Ratio * zoomRatio * 1.4), paperIndex, printSettingsInfo.PrintWithAnnot, printSettingsInfo.PrintWithForm);
  929. if (printSettingsInfo.IsBlackAndWhite)
  930. {
  931. bitmap = ToGray(bitmap, 0);
  932. }
  933. int horizontalMargin = 0;
  934. int verticalMargin = 0;
  935. if (postModTileInfo.HasCutMarks || postModTileInfo.HasLabel)
  936. {
  937. horizontalMargin = side * HorizontalPaperCountModPoster;
  938. verticalMargin = side * VerticalPaperCountModPoster;
  939. }
  940. if (bitmap.Width + horizontalMargin > printBound.Width || bitmap.Height + verticalMargin > printBound.Height)
  941. {
  942. HorizontalPaperCountModPoster = (int)((bitmap.Width + horizontalMargin + overLap) % printBound.Width == 0 ? (bitmap.Width + horizontalMargin + overLap) / printBound.Width : (bitmap.Width + horizontalMargin + overLap) / printBound.Width + 1);
  943. VerticalPaperCountModPoster = (int)((bitmap.Height + verticalMargin + overLap) % printBound.Height == 0 ? (bitmap.Height + verticalMargin + overLap) / printBound.Height : (bitmap.Height + horizontalMargin + overLap) / printBound.Height + 1);
  944. }
  945. }
  946. }
  947. /// <summary>
  948. /// 多页模式
  949. /// </summary>
  950. private async void PreviewModMultiple(int currentPreviewIndex)
  951. {
  952. MultipleInfo multipleInfo = (MultipleInfo)this.printSettingsInfo.printMod;
  953. int pageCount = multipleInfo.HorizontalPageNumber * multipleInfo.VerticalPageNumber;
  954. printDocument.DefaultPageSettings.PaperSize = new PaperSize("CustomisePaperSize", PaperWidth * 4, PaperHeight * 4);
  955. Rectangle pageBound = printDocument.DefaultPageSettings.Bounds;
  956. double ratio = 0.5;
  957. double adjustRatio = 0.25;
  958. //每个区域的大小
  959. double eachPageWidth = pageBound.Width * 1.0 * ratio / multipleInfo.HorizontalPageNumber;
  960. double eachPageHeight = pageBound.Height * 1.0 * ratio / multipleInfo.VerticalPageNumber;
  961. System.Drawing.Size newSize = new System.Drawing.Size(Convert.ToInt32(eachPageWidth), Convert.ToInt32(eachPageHeight));
  962. int indexOfList = currentPreviewIndex;
  963. if (currentPreviewIndex != 0)
  964. {
  965. indexOfList = currentPreviewIndex * pageCount;
  966. }
  967. Bitmap bitmapWhenDefault = new Bitmap((int)(pageBound.Width * ratio), (int)(pageBound.Height * ratio));
  968. var dpi = GetDpiByGraphics();
  969. Graphics g = Graphics.FromImage(bitmapWhenDefault);
  970. try
  971. {
  972. if ((int)multipleInfo.EnumPageOrder < 2)//横向
  973. {
  974. for (int tempCurrentVerticalPageIndex = 0; tempCurrentVerticalPageIndex < multipleInfo.VerticalPageNumber; tempCurrentVerticalPageIndex++)
  975. {
  976. for (int tempCurrentHorizonPageIndex = 0; tempCurrentHorizonPageIndex < multipleInfo.HorizontalPageNumber; tempCurrentHorizonPageIndex++)
  977. {
  978. if (indexOfList > document.PageCount - 1)
  979. {
  980. BitmapSource = ToBitmapSource(bitmapWhenDefault);
  981. return;
  982. }
  983. CPDFPage cPDFPage = document.PageAtIndex(TargetPages[indexOfList]);
  984. double pageWidth = cPDFPage.PageSize.Width * 1.4;
  985. double pageHeight = cPDFPage.PageSize.Height * 1.4;
  986. Bitmap bitmapWhenSuccessful =
  987. await ToolMethod.RenderPageBitmap(
  988. document,
  989. (int)(cPDFPage.PageSize.Width * ratio * 1.4),
  990. (int)(cPDFPage.PageSize.Height * ratio * 1.4),
  991. TargetPages[indexOfList],
  992. printSettingsInfo.PrintWithAnnot,
  993. printSettingsInfo.PrintWithForm);
  994. if (printSettingsInfo.IsBlackAndWhite == true)
  995. {
  996. bitmapWhenSuccessful = ToGray(bitmapWhenSuccessful, 0);
  997. }
  998. if (printSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusLandscape)
  999. {
  1000. bitmapWhenSuccessful.RotateFlip(RotateFlipType.Rotate270FlipNone);
  1001. }
  1002. if (multipleInfo.IsAutoRotate &&
  1003. (newSize.Width - newSize.Height) * (bitmapWhenSuccessful.Width * bitmapWhenSuccessful.Height) < 0)
  1004. {
  1005. bitmapWhenSuccessful.RotateFlip(RotateFlipType.Rotate270FlipNone);
  1006. }
  1007. bitmapWhenSuccessful = (Bitmap)ResizeImage(bitmapWhenSuccessful, newSize);
  1008. int LocationMarginLeft = (newSize.Width - bitmapWhenSuccessful.Width) / 2;
  1009. int LocationMarginTop = (newSize.Height - bitmapWhenSuccessful.Height) / 2;
  1010. if (multipleInfo.EnumPageOrder == EnumPageOrder.StatusHorizontalOrder)
  1011. {
  1012. g.DrawImage(
  1013. bitmapWhenSuccessful,
  1014. (int)(tempCurrentHorizonPageIndex * eachPageWidth + LocationMarginLeft),
  1015. (int)(tempCurrentVerticalPageIndex * eachPageHeight + LocationMarginTop),
  1016. bitmapWhenSuccessful.Width,
  1017. bitmapWhenSuccessful.Height);
  1018. }
  1019. else
  1020. {
  1021. g.DrawImage(
  1022. bitmapWhenSuccessful,
  1023. (int)((multipleInfo.HorizontalPageNumber - 1 - tempCurrentHorizonPageIndex) * eachPageWidth + LocationMarginLeft),
  1024. (int)(tempCurrentVerticalPageIndex * eachPageHeight + LocationMarginTop),
  1025. bitmapWhenSuccessful.Width,
  1026. bitmapWhenSuccessful.Height);
  1027. }
  1028. indexOfList++;
  1029. }
  1030. }
  1031. }
  1032. else
  1033. {
  1034. for (int tempCurrentHorizonPageIndex = 0; tempCurrentHorizonPageIndex < multipleInfo.HorizontalPageNumber; tempCurrentHorizonPageIndex++)
  1035. {
  1036. for (int tempCurrentVerticalPageIndex = 0; tempCurrentVerticalPageIndex < multipleInfo.VerticalPageNumber; tempCurrentVerticalPageIndex++)
  1037. {
  1038. if (indexOfList > document.PageCount - 1)
  1039. {
  1040. BitmapSource = ToBitmapSource(bitmapWhenDefault);
  1041. return;
  1042. }
  1043. CPDFPage cPDFPage = document.PageAtIndex(indexOfList);
  1044. Bitmap bitmapWhenSuccessful = await ToolMethod.RenderPageBitmap(
  1045. document,
  1046. (int)(cPDFPage.PageSize.Width * ratio * 1.4),
  1047. (int)(cPDFPage.PageSize.Height * ratio * 1.4),
  1048. indexOfList,
  1049. printSettingsInfo.PrintWithAnnot, printSettingsInfo.PrintWithForm);
  1050. if (printSettingsInfo.IsBlackAndWhite)
  1051. {
  1052. bitmapWhenSuccessful = ToGray(bitmapWhenSuccessful, 0);
  1053. }
  1054. if (multipleInfo.IsAutoRotate && (newSize.Width - newSize.Height) * (bitmapWhenSuccessful.Width - bitmapWhenSuccessful.Height) < 0)
  1055. {
  1056. bitmapWhenSuccessful.RotateFlip(RotateFlipType.Rotate270FlipNone);
  1057. }
  1058. bitmapWhenSuccessful = (Bitmap)ResizeImage(bitmapWhenSuccessful, newSize);
  1059. int LocationMarginLeft = (newSize.Width - bitmapWhenSuccessful.Width) / 2;
  1060. int LocationMarginTop = (newSize.Height - bitmapWhenSuccessful.Height) / 2;
  1061. if (multipleInfo.EnumPageOrder == EnumPageOrder.StatusVerticalOrder)
  1062. {
  1063. g.DrawImage(
  1064. bitmapWhenSuccessful,
  1065. (int)(tempCurrentHorizonPageIndex * eachPageWidth + LocationMarginLeft),
  1066. (int)(tempCurrentVerticalPageIndex * eachPageHeight + LocationMarginTop),
  1067. bitmapWhenSuccessful.Width,
  1068. bitmapWhenSuccessful.Height);
  1069. }
  1070. else
  1071. {
  1072. g.DrawImage(
  1073. bitmapWhenSuccessful,
  1074. (int)((multipleInfo.HorizontalPageNumber - 1 - tempCurrentHorizonPageIndex) * eachPageWidth + LocationMarginLeft),
  1075. (int)(tempCurrentVerticalPageIndex * eachPageHeight + LocationMarginTop),
  1076. bitmapWhenSuccessful.Width,
  1077. bitmapWhenSuccessful.Height);
  1078. }
  1079. indexOfList++;
  1080. bitmapWhenSuccessful.Dispose();
  1081. }
  1082. }
  1083. }
  1084. }
  1085. catch
  1086. {
  1087. BitmapSource = ToBitmapSource(bitmapWhenDefault);
  1088. }
  1089. BitmapSource = ToBitmapSource(bitmapWhenDefault);
  1090. }
  1091. /// <summary>
  1092. /// 小册子模式预览
  1093. /// PDF Reader Pro原代码,基本未作改动
  1094. /// </summary>
  1095. /// <param name="paperIndex"></param>
  1096. private async void PreviewModBooklet(int currentPreviewIndex)
  1097. {
  1098. BookletInfo bookletInfo = (BookletInfo)printSettingsInfo.printMod;
  1099. printDocument.DefaultPageSettings.PaperSize = new PaperSize("CustomisePaperSize", PaperWidth * 4, PaperHeight * 4);
  1100. Rectangle pageBound = printDocument.DefaultPageSettings.Bounds;
  1101. double ratio = 0.5;
  1102. pageBound.Width = (int)(pageBound.Width * ratio);
  1103. pageBound.Height = (int)(pageBound.Height * ratio);
  1104. double eachPageWidth = 0;
  1105. double eachPageHeight = 0;
  1106. if (printSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
  1107. {
  1108. eachPageWidth = pageBound.Width * 1.0 / 2;
  1109. eachPageHeight = pageBound.Height;
  1110. }
  1111. else
  1112. {
  1113. eachPageWidth = pageBound.Height * 1.0 / 2;
  1114. eachPageHeight = pageBound.Width;
  1115. }
  1116. System.Drawing.Size eachPageSize = new System.Drawing.Size((int)eachPageWidth, (int)eachPageHeight);
  1117. List<int> newPages = TargetPages;
  1118. if (bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusFrontSideOnly && newPages.Count > 1)
  1119. {
  1120. currentPreviewIndex = 2 * currentPreviewIndex;
  1121. }
  1122. else if (bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusBackSideOnly && newPages.Count > 1)
  1123. {
  1124. currentPreviewIndex = 2 * currentPreviewIndex + 1;
  1125. }
  1126. if (newPages.Count % 4 != 0)
  1127. {
  1128. int tag = newPages.Count % 4;
  1129. if (newPages.Count == 1)
  1130. {
  1131. tag = 3;
  1132. }
  1133. for (int i = 0; i < 4 - tag; i++)
  1134. {
  1135. newPages.Add(-1);
  1136. }
  1137. }
  1138. Bitmap bmp = new Bitmap(pageBound.Height, pageBound.Width);
  1139. if (!printDocument.DefaultPageSettings.Landscape)
  1140. bmp.RotateFlip(RotateFlipType.Rotate270FlipNone);
  1141. Graphics g = Graphics.FromImage(bmp);
  1142. var dpi = GetDpiByGraphics();
  1143. for (int i = newPages[currentPreviewIndex], k = 0; k < 2; i = newPages[newPages.Count - 1 - currentPreviewIndex], k++)//为了减少代码量,凑的for循环(跑两次)
  1144. {
  1145. CPDFPage kMPDFPage = document.PageAtIndex(i);
  1146. if (kMPDFPage == null) continue;//表示填充的空白页
  1147. //Rect renderRect = new Rect(0.0, 0.0, (int)(kMPDFPage.PageSize.Width*Rate / 72.0 * dpi * 0.85), (int)(kMPDFPage.PageSize.Height*Rate / 72.0 * dpi * 0.85));
  1148. //Bitmap bitmap = KMPDFWrapper.RenderPDFPageToBitmap(Doc, i, renderRect,adjustRate * dpi / 72);//这里的参数不能直接用Rate 要手动调试得出
  1149. Bitmap bitmap = await ToolMethod.RenderPageBitmap(document, (int)(kMPDFPage.PageSize.Width * ratio * 1.4), (int)(kMPDFPage.PageSize.Height * ratio * 1.4), i, printSettingsInfo.PrintWithAnnot, printSettingsInfo.PrintWithForm);
  1150. if (printSettingsInfo.IsBlackAndWhite)//灰度打印
  1151. bitmap = ToGray(bitmap, 0);
  1152. if (bookletInfo.IsAutoRotate && ((eachPageSize.Width - eachPageSize.Height) * (bitmap.Width - bitmap.Height) < 0))
  1153. {
  1154. bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  1155. bitmap = (Bitmap)ResizeImage(bitmap, eachPageSize);
  1156. }
  1157. else
  1158. bitmap = (Bitmap)ResizeImage(bitmap, eachPageSize);//重新调整大小
  1159. int top = 0;
  1160. int left = 0;
  1161. //left = (new_size.Width - bitmap.Width) / 2;
  1162. left = (int)(eachPageSize.Width - bitmap.Width);
  1163. top = (eachPageSize.Height - bitmap.Height) / 2;
  1164. if (bookletInfo.EnumBookletBinding == EnumBookletBinding.StatusLeft)//左装订
  1165. {
  1166. if (bookletInfo.EnumBookletSubset < (EnumBookletSubset)2 || newPages.Count == 2)//仅双面打印的时候需要左右装订的方式交替打印,单面打印的时候不需要
  1167. {
  1168. if (currentPreviewIndex == 0 || currentPreviewIndex % 2 == 0 || bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusFrontSideOnly)//仅打印正面时,不需要左右装订轮着打
  1169. g.DrawImage(bitmap, (float)((1 - k) * eachPageSize.Width + k * left), (float)(top), bitmap.Width, bitmap.Height);//左订
  1170. else
  1171. g.DrawImage(bitmap, (float)(k * eachPageSize.Width + ((k + 1) % 2) * left), (float)(top), bitmap.Width, bitmap.Height);
  1172. }
  1173. else//仅反面
  1174. {
  1175. g.DrawImage(bitmap, (float)(k * eachPageSize.Width + ((k + 1) % 2) * left), (float)(top), bitmap.Width, bitmap.Height);
  1176. }
  1177. }
  1178. else//右装订
  1179. {
  1180. if (bookletInfo.EnumBookletSubset < (EnumBookletSubset)2 || newPages.Count == 2)//等于2说明 只有一张图和一张空白页
  1181. {
  1182. if (currentPreviewIndex == 0 || currentPreviewIndex % 2 == 0 || bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusFrontSideOnly)
  1183. g.DrawImage(bitmap, (float)(k * eachPageSize.Width + ((k + 1) % 2) * left), (float)(top), bitmap.Width, bitmap.Height);
  1184. else
  1185. g.DrawImage(bitmap, (float)((1 - k) * eachPageSize.Width + k * left), (float)(top), bitmap.Width, bitmap.Height);
  1186. }
  1187. else//仅反面
  1188. {
  1189. g.DrawImage(bitmap, (float)((1 - k) * eachPageSize.Width + k * left), (float)(top), bitmap.Width, bitmap.Height);
  1190. }
  1191. }
  1192. }
  1193. BitmapSource = ToBitmapSource(bmp);
  1194. }
  1195. #endregion
  1196. #region 打印
  1197. public System.Drawing.Size Resize(System.Drawing.Size sourceSize, System.Drawing.Size targetSize)
  1198. {
  1199. int sourceWidth = sourceSize.Width;
  1200. //获取图片高度
  1201. int sourceHeight = sourceSize.Height;
  1202. float nPercent = 0;
  1203. float nPercentW = 0;
  1204. float nPercentH = 0;
  1205. nPercentW = ((float)targetSize.Width / (float)sourceWidth);
  1206. //计算高度的缩放比例
  1207. nPercentH = ((float)targetSize.Height / (float)sourceHeight);
  1208. if (nPercentH < nPercentW)
  1209. nPercent = nPercentH;
  1210. else
  1211. nPercent = nPercentW;
  1212. int destWidth = (int)(sourceWidth * nPercent);
  1213. int destHeight = (int)(sourceHeight * nPercent);
  1214. return new System.Drawing.Size(destWidth, destHeight);
  1215. }
  1216. private async void ConfirmPrint()
  1217. {
  1218. try
  1219. {
  1220. printerSettings.PrinterName = PrinterNameList[PrinterIndex];
  1221. if (CurrentPrintModName == "ModBooklet")
  1222. {
  1223. BookletInfo bookletInfo = (BookletInfo)printSettingsInfo.printMod;
  1224. pageSettings.Landscape = true;
  1225. if (printerSettings.CanDuplex && bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusBothSides)
  1226. {
  1227. printerSettings.Duplex = Duplex.Horizontal;
  1228. }
  1229. printDocument.DefaultPageSettings = pageSettings;
  1230. printDocument.PrinterSettings = printerSettings;
  1231. currentListIndex = 0;
  1232. CurrentPreviewIndex = 0;
  1233. printListIndex = 0;
  1234. }
  1235. await Task.Run(() =>
  1236. {
  1237. printDocument.Print();
  1238. });
  1239. }
  1240. catch (Exception ex)
  1241. {
  1242. }
  1243. }
  1244. public void InitPrinterSettings()
  1245. {
  1246. location = new System.Drawing.Point((int)(18 * GetDpiByGraphics() / 120), (int)(66 * GetDpiByGraphics() / 120));
  1247. size = new System.Drawing.Size((int)(624 * GetDpiByGraphics() / 120), (int)(130 * GetDpiByGraphics() / 120));
  1248. pageSettings = printDocument.DefaultPageSettings;
  1249. printerSettings = printDocument.PrinterSettings;
  1250. printDocument.DocumentName = document.FileName;
  1251. printDocument.PrintPage += PrintDocument_PrintPage;
  1252. }
  1253. private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
  1254. {
  1255. switch (CurrentPrintModName)
  1256. {
  1257. case "ModPoster":
  1258. PrintDocumentModPoster(e);
  1259. break;
  1260. case "ModMultiple":
  1261. PrintDocumentModMultiple(e);
  1262. break;
  1263. case "ModBooklet":
  1264. PrintDocumentModBooklet(e);
  1265. break;
  1266. case "ModSize"://普通 大小模式
  1267. default:
  1268. PrintDocumentModSize(e);
  1269. break;
  1270. }
  1271. }
  1272. private void PrintDocumentModSize(PrintPageEventArgs e)
  1273. {
  1274. SizeInfo sizeInfo = (SizeInfo)printSettingsInfo.printMod;
  1275. PrintIndex = TargetPages[printListIndex];
  1276. if (printSettingsInfo.IsIndexReverse)
  1277. {
  1278. PrintIndex = TargetPages[TargetPages.Count - 1 - printListIndex];
  1279. }
  1280. if (printListIndex < TargetPages.Count)
  1281. {
  1282. CPDFPage kMPDFPage = document.PageAtIndex(PrintIndex);
  1283. double left = 0;
  1284. double top = 0;//用于居中设置的边距
  1285. double pagewidth = kMPDFPage.PageSize.Width * 1.4;
  1286. double pageheight = kMPDFPage.PageSize.Height * 1.4;
  1287. int dpi = (int)GetDpiByGraphics();
  1288. double rate = e.Graphics.DpiX / 100;
  1289. //打印不需要跟根据DPI缩放 参数最好手动转int 不然底层库容易报错 0.85是为了调整拿到图和万兴的大小差不多。 0.51是为了调整接口拿到整个图片
  1290. // Rect renderRect = new Rect(0.0, 0.0, (int)(kMPDFPage.PageSize.Width / 72.0 * dpi * 0.85), (int)(kMPDFPage.PageSize.Height / 72.0 * dpi * 0.85));
  1291. //Bitmap bitmap = KMPDFWrapper.RenderPDFPageToBitmap(Doc, PrintIndex, renderRect, 0.51 * dpi / 72);//小数参数需要手动调试
  1292. Bitmap bitmap = ToolMethod.RenderPageBitmapNoWait(document, (int)(pagewidth * rate), (int)(pageheight * rate), PrintIndex, printSettingsInfo.PrintWithAnnot, printSettingsInfo.PrintWithForm);
  1293. var pageBound = printDocument.DefaultPageSettings.Bounds;
  1294. //根据选项 进行微处理
  1295. if (printSettingsInfo.IsBlackAndWhite)//灰度打印
  1296. bitmap = ToGray(bitmap, 0);
  1297. System.Drawing.Size newSize = new System.Drawing.Size();
  1298. if (sizeInfo.EnumSizeType == EnumSizeType.StatusCustomized)//自定义尺寸
  1299. {
  1300. newSize = new System.Drawing.Size((int)(pagewidth * sizeInfo.DisplayRatio), (int)(pageheight * sizeInfo.DisplayRatio));
  1301. //bitmap = (bitmap)resizeimage(bitmap, newsize);
  1302. }
  1303. else if (sizeInfo.EnumSizeType == EnumSizeType.StatusAdaptive)
  1304. {
  1305. float zoompercent = 0;
  1306. float percentW = 0;
  1307. float percentH = 0;
  1308. percentW = ((float)pageBound.Width / (float)pagewidth);
  1309. percentH = ((float)pageBound.Height / (float)pageheight);
  1310. zoompercent = percentH > percentW ? percentW : percentH;
  1311. newSize = new System.Drawing.Size((int)(pagewidth * zoompercent), (int)(pageheight * zoompercent));
  1312. //bitmap = (Bitmap)ResizeImage(bitmap, newSize);
  1313. }
  1314. else
  1315. {
  1316. newSize = new System.Drawing.Size((int)pagewidth, (int)pageheight);
  1317. }
  1318. var r = printDocument.DefaultPageSettings.PrintableArea;
  1319. left = (r.Width - newSize.Width) / 2;
  1320. top = (r.Height - newSize.Height) / 2;
  1321. if (printDocument.DefaultPageSettings.Landscape)//横向打印/横图 分自动旋转和非自动旋转
  1322. {
  1323. if (pageheight > pagewidth)
  1324. {
  1325. e.Graphics.DrawImage(bitmap, new Rectangle((int)left, (int)top, newSize.Width, newSize.Height), new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
  1326. }
  1327. else
  1328. {
  1329. e.Graphics.DrawImage(bitmap, new Rectangle((int)left, (int)top, newSize.Width, newSize.Height), new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
  1330. }
  1331. }
  1332. else
  1333. {
  1334. e.Graphics.DrawImage(bitmap, new Rectangle((int)left, (int)top, newSize.Width, newSize.Height), new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
  1335. }
  1336. bitmap.Dispose();
  1337. if (printListIndex < TargetPages.Count - 1)
  1338. {
  1339. e.HasMorePages = true;
  1340. }
  1341. printListIndex++;
  1342. }
  1343. }
  1344. private void PrintDocumentModPoster(PrintPageEventArgs e)
  1345. {
  1346. }
  1347. private void PrintDocumentModMultiple(PrintPageEventArgs e)
  1348. {
  1349. MultipleInfo multipleInfo = (MultipleInfo)printSettingsInfo.printMod;
  1350. if (printListIndex < TargetPages.Count)
  1351. {
  1352. //一张纸的页面数
  1353. int h = multipleInfo.VerticalPageNumber;//行
  1354. int v = multipleInfo.HorizontalPageNumber;//列
  1355. int num = h * v;
  1356. //纸张大小
  1357. var pageBound = printDocument.DefaultPageSettings.Bounds;
  1358. //每个区域的大小
  1359. double size_width = pageBound.Width * 1.0 / v;
  1360. double size_height = pageBound.Height * 1.0 / h;
  1361. System.Drawing.Size new_size = new System.Drawing.Size(Convert.ToInt32(size_width), Convert.ToInt32(size_height));
  1362. int indexofList = printListIndex;
  1363. if (printListIndex != 0)
  1364. indexofList = printListIndex * num;
  1365. var dpi = GetDpiByGraphics();
  1366. Graphics g = e.Graphics;
  1367. try
  1368. {
  1369. if ((int)multipleInfo.EnumPageOrder < 2)//横向
  1370. {
  1371. for (int j = 0; j < h; j++)
  1372. for (int i = 0; i < v; i++)
  1373. {
  1374. if (indexofList > TargetPages.Count - 1)
  1375. {
  1376. return;
  1377. }
  1378. int pageindex = 0;
  1379. CPDFPage kMPDFPage = document.PageAtIndex(pageindex);
  1380. double pagewidth = kMPDFPage.PageSize.Width * 1.4;
  1381. double pageheight = kMPDFPage.PageSize.Height * 1.4;
  1382. var rate = e.Graphics.DpiX / 100;
  1383. //Rect renderRect = new Rect(0.0, 0.0, (int)(kMPDFPage.PageSize.Width / 72.0 * dpi * 0.85), (int)(kMPDFPage.PageSize.Height / 72.0 * dpi * 0.85));
  1384. //Bitmap bitmap = KMPDFWrapper.RenderPDFPageToBitmap(Doc, pageindex, renderRect, 0.51 * dpi / 72);//这里的参数不能直接用Rate 要手动调试得出
  1385. Bitmap bitmap = ToolMethod.RenderPageBitmapNoWait(document, (int)(pagewidth * rate), (int)(pageheight * rate), pageindex, printSettingsInfo.PrintWithAnnot, printSettingsInfo.PrintWithForm);
  1386. if (printSettingsInfo.IsBlackAndWhite)//灰度打印
  1387. bitmap = ToGray(bitmap, 0);
  1388. //图片的宽高和容器的宽高大小关系不一致时
  1389. if (multipleInfo.IsAutoRotate && ((new_size.Width - new_size.Height) * (bitmap.Width - bitmap.Height) < 0))
  1390. {
  1391. bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  1392. var temp = Resize(new System.Drawing.Size((int)pageheight, (int)pagewidth), new_size);
  1393. pagewidth = temp.Width;
  1394. pageheight = temp.Height;
  1395. }
  1396. else
  1397. {
  1398. //重新调整
  1399. var temp = Resize(new System.Drawing.Size((int)pagewidth, (int)pageheight), new_size);
  1400. pagewidth = temp.Width;
  1401. pageheight = temp.Height;
  1402. }
  1403. int top = 0;
  1404. int left = 0;
  1405. left = (int)(new_size.Width - pagewidth) / 2;
  1406. top = (int)(new_size.Height - pageheight) / 2;
  1407. if (multipleInfo.EnumPageOrder == EnumPageOrder.StatusHorizontalOrder)
  1408. {
  1409. // g.DrawImage(bitmap, (float)(i * size_width + left), (float)(j * size_height + top), bitmap.Width, bitmap.Height);
  1410. g.DrawImage(bitmap, new Rectangle((int)(i * size_width + left), (int)(j * size_height + top), (int)pagewidth, (int)pageheight), new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
  1411. }
  1412. else//横向倒序
  1413. {
  1414. //g.DrawImage(bitmap, (float)((v - 1 - i) * size_width + left), (float)((j) * size_height + top), bitmap.Width, bitmap.Height);
  1415. g.DrawImage(bitmap, new Rectangle((int)((v - 1 - i) * size_width + left), (int)((j) * size_height + top), (int)pagewidth, (int)pageheight), new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
  1416. }
  1417. indexofList++;
  1418. bitmap.Dispose();
  1419. }
  1420. }
  1421. else//纵向
  1422. {
  1423. for (int j = 0; j < v; j++)
  1424. for (int i = 0; i < h; i++)
  1425. {
  1426. if (indexofList > TargetPages.Count - 1)
  1427. {
  1428. return;
  1429. }
  1430. int pageindex = 0;
  1431. CPDFPage kMPDFPage = document.PageAtIndex(pageindex);
  1432. double pagewidth = kMPDFPage.PageSize.Width * 1.4;
  1433. double pageheight = kMPDFPage.PageSize.Height * 1.4;
  1434. var rate = e.Graphics.DpiX / 100;
  1435. //Rect renderRect = new Rect(0.0, 0.0, (int)(kMPDFPage.PageSize.Width / 72.0 * dpi * 0.85), (int)(kMPDFPage.PageSize.Height / 72.0 * dpi * 0.85));
  1436. //Bitmap bitmap = KMPDFWrapper.RenderPDFPageToBitmap(Doc, pageindex, renderRect, 0.51 * dpi / 72);//这里的参数不能直接用Rate 要手动调试得出
  1437. Bitmap bitmap = ToolMethod.RenderPageBitmapNoWait(document, (int)(pagewidth * rate), (int)(pageheight * rate), pageindex, printSettingsInfo.PrintWithAnnot, printSettingsInfo.PrintWithForm);
  1438. if (printSettingsInfo.IsBlackAndWhite)//灰度打印
  1439. bitmap = ToGray(bitmap, 0);
  1440. if (multipleInfo.IsAutoRotate && ((new_size.Width - new_size.Height) * (bitmap.Width - bitmap.Height) < 0))
  1441. {
  1442. bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  1443. // bitmap = (Bitmap)ResizeImage(bitmap, new_size);
  1444. var temp = Resize(new System.Drawing.Size((int)pageheight, (int)pagewidth), new_size);
  1445. pagewidth = temp.Width;
  1446. pageheight = temp.Height;
  1447. }
  1448. else
  1449. {
  1450. var temp = Resize(new System.Drawing.Size((int)pagewidth, (int)pageheight), new_size);
  1451. pagewidth = temp.Width;
  1452. pageheight = temp.Height;
  1453. }
  1454. int top = 0;
  1455. int left = 0;
  1456. left = (new_size.Width - (int)pagewidth) / 2;
  1457. top = (new_size.Height - (int)pageheight) / 2;
  1458. if (multipleInfo.EnumPageOrder == EnumPageOrder.StatusVerticalOrder)//纵向
  1459. {
  1460. g.DrawImage(bitmap, new Rectangle((int)(j * size_width + left), (int)(i * size_height + top), (int)pagewidth, (int)pageheight), new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
  1461. }
  1462. else//纵向倒序
  1463. {
  1464. g.DrawImage(bitmap, new Rectangle((int)((v - 1 - j) * size_width + left), (int)((i) * size_height + top), (int)pagewidth, (int)pageheight), new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
  1465. }
  1466. indexofList++;
  1467. bitmap.Dispose();
  1468. }
  1469. }
  1470. }
  1471. catch (Exception ex)
  1472. {
  1473. }
  1474. if ((printListIndex + 1) * num < TargetPages.Count)
  1475. {
  1476. e.HasMorePages = true;
  1477. }
  1478. printListIndex++;
  1479. }
  1480. }
  1481. private void PrintDocumentModBooklet(PrintPageEventArgs e)
  1482. {
  1483. BookletInfo bookletInfo = (BookletInfo)printSettingsInfo.printMod;
  1484. if (printListIndex < TargetPages.Count)
  1485. {
  1486. int index = printListIndex;
  1487. var pagebound = printDocument.DefaultPageSettings.Bounds;
  1488. double size_width = 0;
  1489. double size_height = 0;
  1490. //已经设置了横置
  1491. size_width = pagebound.Width * 1.0 / 2;
  1492. size_height = pagebound.Height;
  1493. System.Drawing.Size new_size = new System.Drawing.Size((int)size_width, (int)size_height);
  1494. if (bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusFrontSideOnly && TargetPages.Count > 1)
  1495. index = 2 * index;
  1496. else if ((int)bookletInfo.EnumBookletSubset == 2 && TargetPages.Count > 1)
  1497. index = 2 * index + 1;
  1498. if (TargetPages.Count % 4 != 0)//如果不能整除 要添加空白页
  1499. {
  1500. int tag = TargetPages.Count % 4;
  1501. if (TargetPages.Count == 1)
  1502. tag = 3;//补一张空白页
  1503. for (int i = 0; i < 4 - tag; i++)
  1504. {
  1505. TargetPages.Add(-1);
  1506. }
  1507. }
  1508. //拿首尾两张图片
  1509. //Bitmap bmp = new Bitmap(pagebound.Width, pagebound.Height);
  1510. //if (!printDocument.DefaultPageSettings.Landscape)
  1511. // bmp.RotateFlip(RotateFlipType.Rotate270FlipNone);//没有横置时。 实际打印不用这么麻烦,必然是横置的
  1512. Graphics g = e.Graphics;
  1513. var dpi = GetDpiByGraphics();
  1514. for (int i = TargetPages[index], k = 0; k < 2; i = TargetPages[TargetPages.Count - 1 - index], k++)//为了减少代码量,凑的for循环(跑两次)
  1515. {
  1516. CPDFPage kMPDFPage = document.PageAtIndex(i);
  1517. if (kMPDFPage == null) continue;//表示填充的空白页
  1518. var pagewidth = kMPDFPage.PageSize.Width * 1.4;
  1519. var pageheight = kMPDFPage.PageSize.Height * 1.4;
  1520. var rate = e.Graphics.DpiX / 100;
  1521. //Rect renderRect = new Rect(0.0, 0.0, (int)(kMPDFPage.PageSize.Width / 72.0 * dpi * 0.85), (int)(kMPDFPage.PageSize.Height / 72.0 * dpi * 0.85));
  1522. //Bitmap bitmap = KMPDFWrapper.RenderPDFPageToBitmap(Doc, i, renderRect, 0.51 * dpi / 72);//这里的参数不能直接用Rate 要手动调试得出
  1523. Bitmap bitmap = ToolMethod.RenderPageBitmapNoWait(document, (int)(pagewidth * rate), (int)(pageheight * rate), i, printSettingsInfo.PrintWithAnnot, printSettingsInfo.PrintWithForm);
  1524. if (printSettingsInfo.IsBlackAndWhite)//灰度打印
  1525. bitmap = ToGray(bitmap, 0);
  1526. if (bookletInfo.IsAutoRotate && ((new_size.Width - new_size.Height) * (bitmap.Width - bitmap.Height) < 0))
  1527. {
  1528. bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  1529. var temp = Resize(new System.Drawing.Size((int)pageheight, (int)pagewidth), new_size);
  1530. pagewidth = temp.Width;
  1531. pageheight = temp.Height;
  1532. }
  1533. else
  1534. {
  1535. var temp = Resize(new System.Drawing.Size((int)pagewidth, (int)pageheight), new_size);
  1536. pagewidth = temp.Width;
  1537. pageheight = temp.Height;
  1538. }
  1539. int top = 0;
  1540. int left = 0;
  1541. left = (new_size.Width - (int)pagewidth) / 2;
  1542. top = (new_size.Height - (int)pageheight) / 2;
  1543. if (bookletInfo.EnumBookletBinding == EnumBookletBinding.StatusLeft)//左装订
  1544. {
  1545. if ((int)bookletInfo.EnumBookletSubset < 2 || TargetPages.Count == 2)//仅双面打印的时候需要左右装订的方式交替打印,单面打印的时候不不需要
  1546. {
  1547. if (index == 0 || index % 2 == 0 || bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusFrontSideOnly)//仅打印正面时,不需要左右装订轮着打
  1548. {
  1549. g.DrawImage(bitmap, new Rectangle((int)((1 - k) * size_width + left), (int)(top), (int)pagewidth, (int)pageheight), new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
  1550. }
  1551. // g.DrawImage(bitmap, (float)((1 - k) * size_width + left), (float)(top), bitmap.Width, bitmap.Height);//左订
  1552. else
  1553. {
  1554. g.DrawImage(bitmap, new Rectangle((int)(k * size_width + left), (int)(top), (int)pagewidth, (int)pageheight), new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
  1555. }
  1556. //g.DrawImage(bitmap, (float)(k * size_width + left), (float)(top), bitmap.Width, bitmap.Height);
  1557. }
  1558. else//仅反面
  1559. {
  1560. g.DrawImage(bitmap, new Rectangle((int)(k * size_width + left), (int)(top), (int)pagewidth, (int)pageheight), new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
  1561. //g.DrawImage(bitmap, (float)(k * size_width + left), (float)(top), bitmap.Width, bitmap.Height);
  1562. }
  1563. }
  1564. else//右装订
  1565. {
  1566. if ((int)bookletInfo.EnumBookletSubset < 2 || TargetPages.Count == 2)
  1567. {
  1568. if (index == 0 || index % 2 == 0 || bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusFrontSideOnly)
  1569. {
  1570. g.DrawImage(bitmap, new Rectangle((int)(k * size_width + left), (int)(top), (int)pagewidth, (int)pageheight), new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
  1571. }
  1572. else
  1573. {
  1574. g.DrawImage(bitmap, new Rectangle((int)((1 - k) * size_width + left), (int)(top), (int)pagewidth, (int)pageheight), new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
  1575. }
  1576. //g.DrawImage(bitmap, (float)(k * size_width + left), (float)(top), bitmap.Width, bitmap.Height);
  1577. //else
  1578. // g.DrawImage(bitmap, (float)((1 - k) * size_width + left), (float)(top), bitmap.Width, bitmap.Height);
  1579. }
  1580. else//仅反面
  1581. {
  1582. g.DrawImage(bitmap, new Rectangle((int)((1 - k) * size_width + left), (int)(top), (int)pagewidth, (int)pageheight), new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
  1583. //g.DrawImage(bitmap, (float)((1 - k) * size_width + left), (float)(top), bitmap.Width, bitmap.Height);
  1584. }
  1585. }
  1586. bitmap.Dispose();
  1587. }
  1588. }
  1589. int MaxPageIndex = ((document.PageCount % 4 == 0) ? (document.PageCount / 4) : (document.PageCount / 4 + 1)) * 2;
  1590. if (printListIndex + 1 < MaxPageIndex)
  1591. {
  1592. e.HasMorePages = true;
  1593. }
  1594. printListIndex++;
  1595. }
  1596. #endregion
  1597. #region 参数设置辅助函数
  1598. /// <summary>
  1599. /// 获取目标页码:从全部页面
  1600. /// </summary>
  1601. public void GetTargetPageIndexFromAll()
  1602. {
  1603. TargetPages.Clear();
  1604. for (int temp = 0; temp < document.PageCount; temp++)
  1605. {
  1606. TargetPages.Add(temp);
  1607. }
  1608. MaxPageNumber = TargetPages.Count.ToString();
  1609. }
  1610. /// <summary>
  1611. /// 获取目标页码:从当前页面
  1612. /// </summary>
  1613. public void GetTargePageIndexFromCurrentPage()
  1614. {
  1615. TargetPages.Clear();
  1616. TargetPages.Add(CurrentPreviewIndex);
  1617. MaxPageNumber = TargetPages.Count.ToString();
  1618. }
  1619. public void GetTargetPageIndexFromOddPage()
  1620. {
  1621. TargetPages.Clear();
  1622. for (int temp = 0; temp < document.PageCount; temp += 2)
  1623. {
  1624. TargetPages.Add(temp);
  1625. }
  1626. MaxPageNumber = TargetPages.Count.ToString();
  1627. }
  1628. public void GetTargetPageIndexFromEvenPage()
  1629. {
  1630. TargetPages.Clear();
  1631. if (document.PageCount > 2)
  1632. {
  1633. for (int temp = 1; temp < document.PageCount; temp += 2)
  1634. {
  1635. TargetPages.Add(temp);
  1636. }
  1637. MaxPageNumber = TargetPages.Count.ToString();
  1638. }
  1639. else
  1640. {
  1641. TargetPages.Clear();
  1642. for (int temp = 0; temp < document.PageCount; temp++)
  1643. {
  1644. TargetPages.Add(temp);
  1645. }
  1646. MaxPageNumber = TargetPages.Count.ToString();
  1647. }
  1648. }
  1649. #endregion
  1650. #region 事件
  1651. /// <summary>
  1652. /// 事件:来自PrintMod Region的参数修改
  1653. /// </summary>
  1654. /// <param name="printMod"></param>
  1655. private void SubscribeEvent(Model.Dialog.HomePageToolsDialogs.HomePagePrinter.PrintMod printMod)
  1656. {
  1657. this.printSettingsInfo.printMod = printMod;
  1658. if (CurrentPrintModName == "ModMultiple")
  1659. {
  1660. MultipleInfo multipleInfo = (MultipleInfo)this.printSettingsInfo.printMod;
  1661. MaxPageNumber = ((TargetPages.Count % (multipleInfo.HorizontalPageNumber * multipleInfo.VerticalPageNumber) == 0) ? (TargetPages.Count / (multipleInfo.HorizontalPageNumber * multipleInfo.VerticalPageNumber)) : (TargetPages.Count / (multipleInfo.HorizontalPageNumber * multipleInfo.VerticalPageNumber) + 1)).ToString();
  1662. }
  1663. else if (CurrentPrintModName == "ModBooklet")
  1664. {
  1665. if (TargetPages.Count < 2)
  1666. {
  1667. MaxPageNumber = (1).ToString();
  1668. }
  1669. else
  1670. {
  1671. MaxPageNumber = (((TargetPages.Count % 4 == 0) ? (TargetPages.Count / 4) : (TargetPages.Count / 4 + 1)) * 2).ToString();
  1672. }
  1673. }
  1674. else
  1675. {
  1676. MaxPageNumber = TargetPages.Count.ToString();
  1677. }
  1678. //TODO:
  1679. PaintPageByCurrentPreviewIndex();
  1680. }
  1681. #endregion
  1682. #region 参数设置
  1683. /// <summary>
  1684. /// 唤起页面设置窗口
  1685. /// </summary>
  1686. public void SetPaperSize()
  1687. {
  1688. int previousLeftMargin = LeftMargin;
  1689. int previousRightMargin = RightMargin;
  1690. int previousTopMargin = TopMargin;
  1691. int previousBottomMargin = BottomMargin;
  1692. int previousPaperHeight = PaperHeight;
  1693. int previousPaperWidth = PaperWidth;
  1694. EnumPaperSize previousEnumPaperSize = EnumPaperSize;
  1695. DialogParameters value = new DialogParameters();
  1696. Dialogs.ShowDialog(DialogNames.HomePagePrinterPageSetDialog, value, e =>
  1697. {
  1698. if (e.Result == ButtonResult.OK)
  1699. {
  1700. LeftMargin = e.Parameters.GetValue<int>("LeftMargin");
  1701. TopMargin = e.Parameters.GetValue<int>("TopMargin");
  1702. RightMargin = e.Parameters.GetValue<int>("RightMargin");
  1703. BottomMargin = e.Parameters.GetValue<int>("BottomMargin");
  1704. PaperWidth = e.Parameters.GetValue<int>("PaperWidth");
  1705. PaperHeight = e.Parameters.GetValue<int>("PaperHeight");
  1706. EnumPaperSize = (EnumPaperSize)e.Parameters.GetValue<int>("PaperSizeIndex");
  1707. }
  1708. });
  1709. if (!(previousLeftMargin == LeftMargin &&
  1710. previousRightMargin == RightMargin &&
  1711. previousTopMargin == TopMargin &&
  1712. previousBottomMargin == BottomMargin &&
  1713. previousPaperHeight == PaperHeight &&
  1714. previousPaperWidth == PaperWidth &&
  1715. previousEnumPaperSize == EnumPaperSize))
  1716. {
  1717. if ((PaperHeight * 1.0 / PaperWidth) >= (297 * 1.0 / 210))
  1718. {
  1719. CanvasHeight = 297;
  1720. CanvasWidth = 297 * PaperWidth / PaperHeight;
  1721. }
  1722. else
  1723. {
  1724. CanvasWidth = 210;
  1725. CanvasHeight = PaperHeight * 210 / PaperWidth;
  1726. }
  1727. ViewBoxWidth = CanvasWidth - LeftMargin - RightMargin;
  1728. ViewBoxHeight = CanvasHeight - TopMargin - BottomMargin;
  1729. PaintPageByCurrentPreviewIndex();
  1730. }
  1731. }
  1732. /// <summary>
  1733. /// 设置黑白色
  1734. /// </summary>
  1735. public void SetIsBlackAndWhite()
  1736. {
  1737. printSettingsInfo.IsBlackAndWhite = IsBlackAndWhite;
  1738. PaintPageByCurrentPreviewIndex();
  1739. }
  1740. /// <summary>
  1741. /// 设置页面范围
  1742. /// </summary>
  1743. public void SetPageRange()
  1744. {
  1745. switch (PageRangeIndex)
  1746. {
  1747. case 1:
  1748. {
  1749. GetTargePageIndexFromCurrentPage();
  1750. break;
  1751. }
  1752. case 2:
  1753. {
  1754. GetTargetPageIndexFromOddPage();
  1755. break;
  1756. }
  1757. case 3:
  1758. {
  1759. GetTargetPageIndexFromEvenPage();
  1760. break;
  1761. }
  1762. case 0:
  1763. default:
  1764. {
  1765. GetTargetPageIndexFromAll();
  1766. break;
  1767. }
  1768. }
  1769. currentListIndex = 0;
  1770. CurrentPreviewIndex = TargetPages[currentListIndex];
  1771. PaintPageByCurrentPreviewIndex();
  1772. }
  1773. /// <summary>
  1774. /// 设置页面旋转
  1775. /// </summary>
  1776. public void SetPrintOrientation()
  1777. {
  1778. if (PrintOrientationIndex == 0)
  1779. {
  1780. printSettingsInfo.EnumPrintOrientation = EnumPrintOrientation.StatusPortrait;
  1781. }
  1782. else
  1783. {
  1784. printSettingsInfo.EnumPrintOrientation = EnumPrintOrientation.StatusLandscape;
  1785. }
  1786. if (!flagIsFirstView)
  1787. {
  1788. PaintPageByCurrentPreviewIndex();
  1789. }
  1790. }
  1791. /// <summary>
  1792. /// 设置打印内容
  1793. /// </summary>
  1794. public void SetPrintContent()
  1795. {
  1796. switch (PrintContentIndex)
  1797. {
  1798. case 1:
  1799. {
  1800. printSettingsInfo.PrintWithAnnot = true;
  1801. printSettingsInfo.PrintWithForm = true;
  1802. break;
  1803. }
  1804. case 2:
  1805. {
  1806. printSettingsInfo.PrintWithAnnot = true;
  1807. printSettingsInfo.PrintWithForm = false;
  1808. break;
  1809. }
  1810. case 3:
  1811. {
  1812. printSettingsInfo.PrintWithAnnot = false;
  1813. printSettingsInfo.PrintWithForm = true;
  1814. break;
  1815. }
  1816. case 0:
  1817. default:
  1818. {
  1819. printSettingsInfo.PrintWithAnnot = false;
  1820. printSettingsInfo.PrintWithForm = false;
  1821. break;
  1822. }
  1823. }
  1824. if (!flagIsFirstView)
  1825. {
  1826. PaintPageByCurrentPreviewIndex();
  1827. }
  1828. }
  1829. /// <summary>
  1830. /// 设置逆序打印
  1831. /// </summary>
  1832. public void SetIsPageIndexReverse()
  1833. {
  1834. bool previewIsPageIndexReverse = printSettingsInfo.IsIndexReverse;
  1835. printSettingsInfo.IsIndexReverse = IsPageIndexReverse;
  1836. if (printSettingsInfo.IsIndexReverse || (previewIsPageIndexReverse && (printSettingsInfo.IsIndexReverse == false)))
  1837. {
  1838. NeedPageIndexReverse = true;
  1839. currentListIndex = 0;
  1840. }
  1841. else
  1842. {
  1843. NeedPageIndexReverse = false;
  1844. }
  1845. if (!flagIsFirstView)
  1846. {
  1847. PaintPageByCurrentPreviewIndex();
  1848. }
  1849. }
  1850. #endregion
  1851. #region 打印模式导航
  1852. private void EnterSelectedMod(string currentPrinterModContent)
  1853. {
  1854. if (ExistentPrintModName != currentPrinterModContent)
  1855. {
  1856. NavigationParameters param = new NavigationParameters();
  1857. PrintModVisible = Visibility.Collapsed;
  1858. NavigationParameters keyValuePairs = param;
  1859. keyValuePairs.Add("document", this.document);
  1860. PrintModRegion.RequestNavigate(PrintModRegionName, GetContentByPrintMod[currentPrinterModContent], keyValuePairs);
  1861. PrintModVisible = Visibility.Visible;
  1862. ExistentPrintModName = currentPrinterModContent;
  1863. //TODO:
  1864. PaintPageByCurrentPreviewIndex();
  1865. }
  1866. }
  1867. private void ChangePrintMod(object e)
  1868. {
  1869. var args = e as System.Windows.Controls.Button;
  1870. if (args != null)
  1871. {
  1872. CurrentPrintModName = args.Name;
  1873. EnterSelectedMod(CurrentPrintModName);
  1874. //TODO:
  1875. }
  1876. }
  1877. #endregion
  1878. #region 初始化和结束
  1879. public string Title => "";
  1880. public event Action<IDialogResult> RequestClose;
  1881. public bool CanCloseDialog()
  1882. {
  1883. return true;
  1884. }
  1885. public void OnDialogClosed()
  1886. {
  1887. if (ExistentPrintModName != "ModSize")
  1888. {
  1889. CurrentPrintModName = "ModSize";
  1890. NavigationParameters param = new NavigationParameters();
  1891. PrintModVisible = Visibility.Collapsed;
  1892. NavigationParameters keyValuePairs = param;
  1893. keyValuePairs.Add("document", this.document);
  1894. PrintModRegion.RequestNavigate(PrintModRegionName, GetContentByPrintMod["ModSize"], keyValuePairs);
  1895. PrintModVisible = Visibility.Visible;
  1896. ExistentPrintModName = "ModSize";
  1897. }
  1898. }
  1899. public void OnDialogOpened(IDialogParameters parameters)
  1900. {
  1901. CPDFViewer viewer = null;
  1902. string filepath = "";
  1903. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out viewer);
  1904. parameters.TryGetValue<string>(ParameterNames.FilePath, out filepath);
  1905. if (viewer != null && viewer.Document != null)
  1906. {
  1907. currentViewer = viewer;
  1908. document = currentViewer.Document;
  1909. if (CurrentPrintModName == null)
  1910. {
  1911. CurrentPrintModName = "ModSize";
  1912. }
  1913. PrintModRegion.RequestNavigate(PrintModRegionName, GetContentByPrintMod[CurrentPrintModName]);
  1914. InitPrintParams();
  1915. GetTargetPageIndexFromAll();
  1916. InitRender();
  1917. InitPrinterSettings();
  1918. }
  1919. }
  1920. #endregion
  1921. }
  1922. }