HomePagePrinterDocumentContentViewModel.cs 148 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967
  1. using ComPDFKit.PDFPage;
  2. using ComPDFKitViewer.PdfViewer;
  3. using ImageMagick.Formats;
  4. using ImTools;
  5. using Microsoft.Office.Core;
  6. using PDF_Master.CustomControl;
  7. using PDF_Master.EventAggregators;
  8. using PDF_Master.Helper;
  9. using PDF_Master.Model;
  10. using PDF_Master.Model.Dialog.HomePageToolsDialogs.HomePagePrinter;
  11. using Prism.Commands;
  12. using Prism.Events;
  13. using Prism.Mvvm;
  14. using Prism.Regions;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Drawing;
  18. using System.Drawing.Drawing2D;
  19. using System.Drawing.Imaging;
  20. using System.Drawing.Printing;
  21. using System.IO;
  22. using System.Linq;
  23. using System.Printing;
  24. using System.Reflection;
  25. using System.Threading.Tasks;
  26. using System.Windows;
  27. using System.Windows.Annotations;
  28. using System.Windows.Controls;
  29. using System.Windows.Forms;
  30. using System.Windows.Media.Imaging;
  31. using System.Windows.Shell;
  32. using System.Windows.Threading;
  33. using static Dropbox.Api.TeamLog.PaperDownloadFormat;
  34. using static PDF_Master.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter.HomePagePrinterDocumentContentViewModel;
  35. namespace PDF_Master.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter
  36. {
  37. public class HomePagePrinterDocumentContentViewModel : BindableBase, INavigationAware
  38. {
  39. private CPDFViewer PDFViewer;
  40. private PrintQueue documentPrintQueue;
  41. private PrintDocument printDocument = new PrintDocument();
  42. public PrintSettingsInfo PrintSettingsInfo;
  43. public IEventAggregator printDocumentEvent;
  44. private string Unicode = null;
  45. #region 打印过程中一些参数需要在函数外定义
  46. /// <summary>
  47. /// 海报模式:打印纸
  48. /// </summary>
  49. int posterPrintPaperIndex = 0;
  50. /// <summary>
  51. /// 海报模式,水平纸张数
  52. /// </summary>
  53. public int HorizonPaperCount = 0;
  54. /// <summary>
  55. /// 海报模式,垂直纸张数
  56. /// </summary>
  57. public int VerticalPaperCount = 0;
  58. /// <summary>
  59. /// 海报模式,单页总纸张数
  60. /// </summary>
  61. public int WholeCountSinglePage = 0;
  62. ///<summary>
  63. ///海报模式当前水平纸
  64. /// </summary>
  65. public int HorizonPaperIndex = 0;
  66. ///<summary>
  67. ///海报模式当前垂直纸
  68. /// </summary>
  69. public int VerticalPaperIndex = 0;
  70. /// <summary>
  71. /// 海报模式,总打印纸张数
  72. /// </summary>
  73. public int WholePaperCountModPoster = 0;
  74. /// <summary>
  75. /// 海报模式渲染出来的纸张,只有第一次进入和每次翻页才重新渲染,减少渲染次数
  76. /// </summary>
  77. public Bitmap PosterCurrentBitmap = null;
  78. /// <summary>
  79. /// 同理,海报模式实际被拆分的纸张,更改过比例,添加了白边
  80. /// </summary>
  81. public Bitmap DisplayBitmap = null;
  82. /// <summary>
  83. /// 海报模式,由预览决定的比例
  84. /// </summary>
  85. public double PosterRatio = 0;
  86. #endregion
  87. /// <summary>
  88. /// 转存防卡顿
  89. /// </summary>
  90. public class DocumentSettingsInfo
  91. {
  92. public int DocumentWidth = 0;
  93. public int DocumentHeight = 0;
  94. public Margins margins = new Margins();
  95. public Rectangle DocumentBounds = new Rectangle();
  96. public bool DocumentLandscape = false;
  97. public bool DocumentColor = false;
  98. public bool IsSettingsChanged = true;
  99. }
  100. public DocumentSettingsInfo documentSettingsInfo = new DocumentSettingsInfo();
  101. /// <summary>
  102. /// 记录打印设置改变前的总页数,用于判断是否需要跳转第一页,仅当总页数改变时跳转
  103. /// </summary>
  104. int PreviousPrintedPageCount = 0;
  105. public int PrintIndex;
  106. ///<summary>
  107. ///打印纸dpi100, PDF dpi 72,需要缩放
  108. /// </summary>
  109. public double PDFToMediaDpiRatio = 100.0 / 72.0;
  110. public double mmToDpiRatio = 100 / 25.4;
  111. Bitmap blankPageBitmap;
  112. Bitmap blankPageBitmapForPrint;
  113. private string _paperWidth;
  114. public string PaperWidth
  115. {
  116. get => _paperWidth;
  117. set => SetProperty(ref _paperWidth, value);
  118. }
  119. private string _paperHeight;
  120. public string PaperHeight
  121. {
  122. get => _paperHeight;
  123. set => SetProperty(ref _paperHeight, value);
  124. }
  125. private string _viewBoxWidth;
  126. public string ViewBoxWidth
  127. {
  128. get => _viewBoxWidth;
  129. set => SetProperty(ref _viewBoxWidth, value);
  130. }
  131. private string _viewBoxHeight;
  132. public string ViewBoxHeight
  133. {
  134. get => _viewBoxHeight;
  135. set => SetProperty(ref _viewBoxHeight, value);
  136. }
  137. private BitmapSource _bitmapSource;
  138. /// <summary>
  139. /// 渲染后的图
  140. /// </summary>
  141. public BitmapSource BitmapSource
  142. {
  143. get { return _bitmapSource; }
  144. set { SetProperty(ref _bitmapSource, value); }
  145. }
  146. private int _targetPaperIndex = 1;
  147. public int TargetPaperIndex
  148. {
  149. get => _targetPaperIndex;
  150. set
  151. {
  152. SetProperty(ref _targetPaperIndex, value);
  153. JumpToSelectedPage();
  154. }
  155. }
  156. /// <summary>
  157. /// 目标纸张
  158. /// TargetPaper[纸张编号]=正确的纸张
  159. /// </summary>
  160. public List<int> TargetPaperList = new List<int>();
  161. /// <summary>
  162. /// 打印的总页数
  163. /// </summary>
  164. private int _printedPageCount;
  165. public int PrintedPageCount
  166. {
  167. get => _printedPageCount;
  168. set => SetProperty(ref _printedPageCount, value);
  169. }
  170. private Visibility _errorPageTxtVisibility = Visibility.Collapsed;
  171. public Visibility ErrorPageTxtVisibility
  172. {
  173. get => _errorPageTxtVisibility;
  174. set => SetProperty(ref _errorPageTxtVisibility, value);
  175. }
  176. /// <summary>
  177. /// 手动双面打印
  178. /// </summary>
  179. public bool IsManualDuplex = false;
  180. public DelegateCommand<object> TurnPageCommand { get; set; }
  181. public DelegateCommand<object> JumpPageCommand { get; set; }
  182. public HomePagePrinterDocumentContentViewModel(IEventAggregator eventAggregator)
  183. {
  184. this.printDocumentEvent = eventAggregator;
  185. printDocumentEvent.GetEvent<SendPrintSettingsInfoEvent>().Subscribe(RecvPrintSettingsInfo, e => e.Unicode == Unicode);
  186. printDocumentEvent.GetEvent<SendPrintQueueEvent>().Subscribe(RecvPrintQueue, e => e.Unicode == Unicode);
  187. printDocumentEvent.GetEvent<SendLabelEvent>().Subscribe(RecvLabel, e => e.Unicode == Unicode);
  188. TurnPageCommand = new DelegateCommand<object>(TurnPage);
  189. JumpPageCommand = new DelegateCommand<object>(JumpPage);
  190. }
  191. public void TurnPage(object e)
  192. {
  193. var btn = e as System.Windows.Controls.Button;
  194. if (btn != null)
  195. {
  196. if (btn.Name == "PrePageBtn")
  197. {
  198. if (TargetPaperIndex >= 2)
  199. {
  200. TargetPaperIndex--;
  201. }
  202. }
  203. else
  204. {
  205. if (TargetPaperIndex < PrintedPageCount)
  206. {
  207. TargetPaperIndex++;
  208. }
  209. }
  210. }
  211. }
  212. public void RecvPrintQueue(PrintQueueWithUnicode printQueueWithUnicode)
  213. {
  214. if (printQueueWithUnicode.printQueue != null)
  215. {
  216. PrintIndex = 0;
  217. this.documentPrintQueue = printQueueWithUnicode.printQueue;
  218. var printDocument = new PrintDocument();
  219. var printTicket = new PrintTicket();
  220. printDocument.PrinterSettings.PrinterName = printQueueWithUnicode.printQueue.Name;
  221. if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
  222. {
  223. printDocument.DefaultPageSettings.Landscape = false;
  224. }
  225. else if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusLandscape)
  226. {
  227. printDocument.DefaultPageSettings.Landscape = true;
  228. }
  229. if (!PrintSettingsInfo.IsGrayscale && printQueueWithUnicode.printQueue.GetPrintCapabilities().OutputColorCapability.Contains(OutputColor.Color))
  230. {
  231. printDocument.DefaultPageSettings.Color = true;
  232. }
  233. else
  234. {
  235. printDocument.DefaultPageSettings.Color = false;
  236. }
  237. if (PrintSettingsInfo.PrintModInfo.EnumDuplexPrintMod == EnumDuplexPrintMod.StatusFlipLongEdge || PrintSettingsInfo.PrintModInfo.EnumDuplexPrintMod == EnumDuplexPrintMod.StatusFlipShortEdge || PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusBooklet)
  238. {
  239. ///无阶段,自动双面打印
  240. ///已在起始时验证
  241. if (printQueueWithUnicode.EnumBothSidesStage == EnumBothSidesStage.StatusNone)
  242. {
  243. if (PrintSettingsInfo.PrintModInfo.EnumDuplexPrintMod == EnumDuplexPrintMod.StatusFlipLongEdge)
  244. {
  245. ///Fix 13346: Duplex.Vertical, 这个是长边翻页
  246. printDocument.PrinterSettings.Duplex = Duplex.Vertical;
  247. }
  248. else
  249. {
  250. ///Fix 13346: Duplex.Horizontal, 这个是短边翻页
  251. printDocument.PrinterSettings.Duplex = Duplex.Horizontal;
  252. }
  253. }
  254. else
  255. {
  256. printDocument.PrinterSettings.Duplex = Duplex.Simplex;
  257. IsManualDuplex = true;
  258. //TODO 手动双面打印,先印单数页再印双数页
  259. if (printQueueWithUnicode.EnumBothSidesStage == EnumBothSidesStage.StatusFrontSide)
  260. {
  261. PrintIndex = 0;
  262. }
  263. else
  264. {
  265. PrintIndex = 1;
  266. }
  267. }
  268. }
  269. //海报模式,预设单面打印
  270. else
  271. {
  272. printDocument.PrinterSettings.Duplex = Duplex.Simplex;
  273. }
  274. printQueueWithUnicode.printQueue.DefaultPrintTicket = printTicket;
  275. printDocument.DefaultPageSettings.PaperSize = PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize;
  276. PaperSource pkSource;
  277. List<PaperSource> paperSources = new List<PaperSource>();
  278. paperSources.Clear();
  279. for (int i = 0; i < printDocument.PrinterSettings.PaperSources.Count; i++)
  280. {
  281. pkSource = printDocument.PrinterSettings.PaperSources[i];
  282. paperSources.Add(pkSource);
  283. }
  284. ///纸盒不为空,实体打印机默认设置纸盒序号0(主纸盒)
  285. if (paperSources.Count != 0)
  286. {
  287. printDocument.DefaultPageSettings.PaperSource = paperSources[0];
  288. }
  289. if (PrintSettingsInfo.Copies <= printQueueWithUnicode.printQueue.GetPrintCapabilities().MaxCopyCount && PrintSettingsInfo.Copies > 0)
  290. {
  291. printDocument.PrinterSettings.Copies = (short)PrintSettingsInfo.Copies;
  292. }
  293. else if (PrintSettingsInfo.Copies > printQueueWithUnicode.printQueue.GetPrintCapabilities().MaxCopyCount)
  294. {
  295. PrintSettingsInfo.Copies = (int)printQueueWithUnicode.printQueue.GetPrintCapabilities().MaxCopyCount;
  296. }
  297. else
  298. {
  299. PrintSettingsInfo.Copies = 1;
  300. }
  301. posterPrintPaperIndex = 0;
  302. printDocument.PrintPage += PrintDocumentByCurrentSettings;
  303. printDocument.PrinterSettings.PrintFileName = PDFViewer.Document.FileName;
  304. try
  305. {
  306. printDocument.Print();
  307. }
  308. catch
  309. {
  310. MessageBoxEx.Show("另一个进程正在占用该打印机或发生未知错误");
  311. }
  312. finally
  313. {
  314. printDocument.Dispose();
  315. }
  316. }
  317. }
  318. private void RecvLabel(LabelWithUnicode labelWithUnicode)
  319. {
  320. if (PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusPoster)
  321. {
  322. var posterInfo = (PosterInfo)PrintSettingsInfo.PrintModInfo;
  323. posterInfo.Label = labelWithUnicode.Label.ToString();
  324. PrintSettingsInfo.PrintModInfo = posterInfo;
  325. }
  326. }
  327. private Bitmap Crop(Bitmap src, Rectangle rectangle)
  328. {
  329. Bitmap targetBitmap = new Bitmap(rectangle.Width, rectangle.Height);
  330. using (Graphics g = Graphics.FromImage(targetBitmap))
  331. {
  332. g.DrawImage(src, new Rectangle(0, 0, targetBitmap.Width, targetBitmap.Height), rectangle, GraphicsUnit.Pixel);
  333. }
  334. return targetBitmap;
  335. }
  336. #region 打印各个模式
  337. private void PrintDocumentModSize(PrintPageEventArgs e)
  338. {
  339. ///Fix 13337 :
  340. ///打印的宽度和高度是1/100英寸
  341. ///图片的宽度和高度是像素
  342. ///假如打印机DPI宽高600,图片宽高600px
  343. ///那么用DrawImage打印图片的时候, 就会把图片600像素的宽,理解为了600百分之一英寸(6英寸),数值没变,只是单位的理解不一样
  344. ///于是打印6英寸宽,需要跨度6*600DPI=3600像素, 但是图片本身只有600像素,于是就把图片的宽放大到3600像素,同理高也是这样,需要放大.
  345. ///放大以后的位图就会出现锯齿
  346. ///所以如果要打印6英寸的一条线,DPI为600的情况下, 需要准备6 * 600 = 3600像素的线条,然后告诉打印机要打印的宽度是600(百分之一英寸) , 打印图像的3600像素, 元素图像的单位是像素.
  347. ///关键代码 e.Graphics.DrawImage(printBitmap, realBound, new Rectangle(0, 0, printBitmap.Width, printBitmap.Height), GraphicsUnit.Pixel);
  348. ///其他三个模式也一样
  349. int widthDpiRatio;
  350. int heightDpiRatio;
  351. ///TODO 可扩展:高低分辨率打印
  352. if (true)
  353. {
  354. widthDpiRatio = (int)e.Graphics.DpiX / 100;
  355. heightDpiRatio = (int)e.Graphics.DpiY / 100;
  356. }
  357. SizeInfo sizeInfo = (SizeInfo)PrintSettingsInfo.PrintModInfo;
  358. CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(PrintSettingsInfo.PageRangeList[PrintIndex]);
  359. Rectangle realBound = new Rectangle(documentSettingsInfo.margins.Left * widthDpiRatio,
  360. documentSettingsInfo.margins.Top * heightDpiRatio,
  361. e.PageBounds.Width - documentSettingsInfo.margins.Left - documentSettingsInfo.margins.Right,
  362. e.PageBounds.Height - documentSettingsInfo.margins.Top - documentSettingsInfo.margins.Bottom);
  363. System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
  364. blankPageBitmapForPrint = new Bitmap((int)documentSettingsInfo.DocumentWidth * widthDpiRatio, (int)documentSettingsInfo.DocumentHeight * heightDpiRatio);
  365. /////听说有效
  366. ////e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
  367. ////e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  368. ////e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
  369. if (cPDFPage != null)
  370. {
  371. Bitmap printBitmap = new Bitmap((int)(float.Parse(PaperWidth)) * widthDpiRatio, (int)float.Parse(PaperHeight) * heightDpiRatio);
  372. Bitmap bitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)(cPDFPage.PageSize.Width * PDFToMediaDpiRatio * widthDpiRatio), (int)(cPDFPage.PageSize.Height * PDFToMediaDpiRatio * heightDpiRatio), PrintSettingsInfo.PageRangeList[PrintIndex], PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  373. if (PrintSettingsInfo.IsGrayscale)
  374. {
  375. bitmap = ToGray(bitmap, 0);
  376. }
  377. ///< summary >
  378. /// 自适应模式
  379. ///</ summary >
  380. if (sizeInfo.EnumSizeType == EnumSizeType.StatusAdaptive)
  381. {
  382. int resizedHeight = 0;
  383. int resizedWidth = 0;
  384. ///重设大小
  385. if ((float)bitmap.Height / (float)bitmap.Width >= (float.Parse(PaperHeight) / float.Parse(PaperWidth)))
  386. {
  387. resizedHeight = (int)(float.Parse(PaperHeight) * mmToDpiRatio) * widthDpiRatio;
  388. resizedWidth = (int)((float.Parse(PaperHeight) / bitmap.Height * bitmap.Width) * mmToDpiRatio) * heightDpiRatio;
  389. }
  390. else
  391. {
  392. resizedWidth = (int)(float.Parse(PaperWidth) * mmToDpiRatio) * widthDpiRatio;
  393. resizedHeight = (int)((float.Parse(PaperWidth) / bitmap.Width * bitmap.Height) * mmToDpiRatio) * heightDpiRatio;
  394. }
  395. /// 直接调整bitmap会引起内存问题,所以先这样处理....
  396. Bitmap resizedBitmap = new Bitmap(bitmap, resizedWidth, resizedHeight);
  397. startPoint.X = (blankPageBitmapForPrint.Width - resizedWidth) / 2;
  398. startPoint.Y = (blankPageBitmapForPrint.Height - resizedHeight) / 2;
  399. printBitmap = CombineBitmap(blankPageBitmapForPrint, resizedBitmap, startPoint);
  400. resizedBitmap.Dispose();
  401. if (IsManualDuplex && PrintIndex % 2 == 1 && sizeInfo.EnumDuplexPrintMod == EnumDuplexPrintMod.StatusFlipShortEdge)
  402. {
  403. printBitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
  404. }
  405. e.Graphics.DrawImage(printBitmap, realBound, new Rectangle(0, 0, printBitmap.Width, printBitmap.Height), GraphicsUnit.Pixel);
  406. }
  407. else if (sizeInfo.EnumSizeType == EnumSizeType.StatusActural)
  408. {
  409. startPoint.X = (blankPageBitmapForPrint.Width - bitmap.Width) / 2;
  410. startPoint.Y = (blankPageBitmapForPrint.Height - bitmap.Height) / 2;
  411. printBitmap = CombineBitmap(blankPageBitmapForPrint, bitmap, startPoint);
  412. if (IsManualDuplex && PrintIndex % 2 == 1 && sizeInfo.EnumDuplexPrintMod == EnumDuplexPrintMod.StatusFlipShortEdge)
  413. {
  414. printBitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
  415. }
  416. e.Graphics.DrawImage(printBitmap, realBound, new Rectangle(0, 0, printBitmap.Width, printBitmap.Height), GraphicsUnit.Pixel);
  417. }
  418. else if (sizeInfo.EnumSizeType == EnumSizeType.StatusCustomized)
  419. {
  420. bitmap = Resize(bitmap, (int)(bitmap.Width * (sizeInfo.DisplayRatio / 100.0)), (int)(bitmap.Height * (sizeInfo.DisplayRatio / 100.0)));
  421. startPoint.X = (blankPageBitmapForPrint.Width - bitmap.Width) / 2;
  422. startPoint.Y = (blankPageBitmapForPrint.Height - bitmap.Height) / 2;
  423. printBitmap = CombineBitmap(blankPageBitmapForPrint, bitmap, startPoint);
  424. if (IsManualDuplex && PrintIndex % 2 == 1 && sizeInfo.EnumDuplexPrintMod == EnumDuplexPrintMod.StatusFlipShortEdge)
  425. {
  426. printBitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
  427. }
  428. e.Graphics.DrawImage(printBitmap, realBound, new Rectangle(0, 0, printBitmap.Width, printBitmap.Height), GraphicsUnit.Pixel);
  429. }
  430. bitmap.Dispose();
  431. printBitmap.Dispose();
  432. blankPageBitmapForPrint.Dispose();
  433. GC.Collect();
  434. //手动双面打印,且页码不为1
  435. if (IsManualDuplex && PrintSettingsInfo.PageRangeList.Count != 1)
  436. {
  437. if (PrintIndex < PrintedPageCount - 2)
  438. {
  439. PrintIndex += 2;
  440. e.HasMorePages = true;
  441. }
  442. else
  443. {
  444. e.HasMorePages = false;
  445. if (PrintIndex % 2 == 0)
  446. {
  447. this.printDocumentEvent.GetEvent<SendFinishedFrontSideEvent>().Publish(new FinishedFrontSide { Unicode = this.Unicode });
  448. }
  449. }
  450. }
  451. //单面打印
  452. else
  453. {
  454. if (PrintIndex < PrintedPageCount - 1)
  455. {
  456. PrintIndex++;
  457. e.HasMorePages = true;
  458. }
  459. else
  460. {
  461. e.HasMorePages = false;
  462. }
  463. }
  464. }
  465. }
  466. ///无论是拆分还是平铺,页面缩放比例都会在预览中完成
  467. ///先渲染整图
  468. ///得到effective域大小
  469. ///根据effective的量和重叠量计算开始的bitmap
  470. ///加字和标记
  471. ///字加在顶部,标记加在四角
  472. ///结束后翻页
  473. ///对于Tile页数不定,但是比例相等
  474. ///对于split页数固定,但是比例变化
  475. ///平铺模式:
  476. ///从第一页开始,先计算当前页总纸张数
  477. ///根据纸张数打印
  478. ///打印到位时页数进一
  479. ///页数到顶,纸数到顶打印结束
  480. ///当一页只有一张纸的时候,注意特殊处理
  481. ///
  482. ///内存问题:
  483. ///1、只有每次翻页才重新取整张图。
  484. ///取到的图片先不急着放大。
  485. ///根据原图大小取得图片,并进行处理后,进行放大
  486. ///再绘制到打印纸上
  487. private void PrintDocumentModPoster(PrintPageEventArgs e)
  488. {
  489. PosterInfo posterInfo = (PosterInfo)PrintSettingsInfo.PrintModInfo;
  490. var pageBound = documentSettingsInfo.DocumentBounds;
  491. int widthDpiRatio;
  492. int heightDpiRatio;
  493. ///TODO 可扩展:高低分辨率打印
  494. if (true)
  495. {
  496. //当前模式放大程度过高引发异常,暂时采用低质量打印
  497. widthDpiRatio = (int)e.Graphics.DpiX / 100;
  498. heightDpiRatio = (int)e.Graphics.DpiY / 100;
  499. if (widthDpiRatio > 2)
  500. {
  501. widthDpiRatio = 2;
  502. heightDpiRatio = 2;
  503. }
  504. }
  505. double effectiveHeight;
  506. double effectiveWidth;
  507. double PreviewWidth;
  508. double PreviewHeight;
  509. System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
  510. ///计算有效打印区域
  511. if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
  512. {
  513. effectiveHeight = documentSettingsInfo.DocumentHeight;
  514. effectiveWidth = documentSettingsInfo.DocumentWidth;
  515. }
  516. else
  517. {
  518. effectiveHeight = documentSettingsInfo.DocumentWidth;
  519. effectiveWidth = documentSettingsInfo.DocumentHeight;
  520. }
  521. effectiveHeight -= 2 * posterInfo.OverLap * mmToDpiRatio;
  522. effectiveWidth -= 2 * posterInfo.OverLap * mmToDpiRatio;
  523. if (posterInfo.HasLabel || posterInfo.HasCutMarks)
  524. {
  525. effectiveHeight -= 40;
  526. effectiveWidth -= 40;
  527. }
  528. effectiveHeight *= heightDpiRatio;
  529. effectiveWidth *= widthDpiRatio;
  530. ///平铺模式下:
  531. if (posterInfo.EnumPosterMod == EnumPosterMod.StatusTile)
  532. {
  533. CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(PrintSettingsInfo.PageRangeList[posterPrintPaperIndex]);
  534. if (cPDFPage != null)
  535. {
  536. ///仅当每一页第一张纸打印时,进行一次渲染,,更换打印底图
  537. if (HorizonPaperIndex == 0 && VerticalPaperIndex == 0)
  538. {
  539. PosterCurrentBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)(cPDFPage.PageSize.Width * PDFToMediaDpiRatio * (posterInfo.TileRatio / 100.0) * widthDpiRatio), (int)(cPDFPage.PageSize.Height * PDFToMediaDpiRatio * (posterInfo.TileRatio / 100.0) * heightDpiRatio), PrintSettingsInfo.PageRangeList[posterPrintPaperIndex], PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  540. if (PrintSettingsInfo.IsGrayscale)
  541. {
  542. PosterCurrentBitmap = ToGray(PosterCurrentBitmap, 0);
  543. }
  544. HorizonPaperCount = (int)Math.Ceiling((double)PosterCurrentBitmap.Width / effectiveWidth);
  545. VerticalPaperCount = (int)Math.Ceiling(((double)PosterCurrentBitmap.Height / effectiveHeight));
  546. WholeCountSinglePage = HorizonPaperCount * VerticalPaperCount;//每一轮的总页数
  547. if (WholeCountSinglePage != 0)
  548. {
  549. PreviewWidth = (double)(effectiveWidth * HorizonPaperCount);
  550. PreviewHeight = (double)(effectiveHeight * VerticalPaperCount);
  551. DisplayBitmap = new Bitmap((int)PreviewWidth, (int)PreviewHeight);
  552. }
  553. else
  554. {
  555. PreviewWidth = effectiveWidth;
  556. PreviewHeight = effectiveHeight;
  557. DisplayBitmap = new Bitmap((int)effectiveWidth, (int)effectiveHeight);
  558. }
  559. //取到和渲染一样的Bitmap
  560. startPoint.X = (int)((PreviewWidth - PosterCurrentBitmap.Width) / 2);
  561. startPoint.Y = (int)((PreviewHeight - PosterCurrentBitmap.Height) / 2);
  562. DisplayBitmap = CombineBitmap(DisplayBitmap, PosterCurrentBitmap, startPoint);
  563. }
  564. if (WholeCountSinglePage > 1)
  565. {
  566. /////当前页未打印完
  567. if (HorizonPaperIndex * VerticalPaperIndex < WholeCountSinglePage - 1)
  568. {
  569. ///取有效尺寸和重叠,边沿合并
  570. ///有效尺寸+重叠边
  571. Rectangle rectangle = new Rectangle((int)(HorizonPaperIndex * effectiveWidth), (int)(VerticalPaperIndex * effectiveHeight), (int)effectiveWidth, (int)effectiveHeight);
  572. ///左重叠
  573. if (HorizonPaperIndex != 0)
  574. {
  575. rectangle.Location = new System.Drawing.Point((int)(rectangle.Location.X - posterInfo.OverLap * mmToDpiRatio * widthDpiRatio), rectangle.Location.Y);
  576. }
  577. ///上重叠
  578. if (VerticalPaperIndex != 0)
  579. {
  580. rectangle.Size = new System.Drawing.Size(rectangle.Size.Width, (int)(rectangle.Size.Height + posterInfo.OverLap * mmToDpiRatio * heightDpiRatio));
  581. }
  582. ///右重叠
  583. if (HorizonPaperIndex != HorizonPaperCount)
  584. {
  585. rectangle.Size = new System.Drawing.Size((int)(rectangle.Size.Width + posterInfo.OverLap * mmToDpiRatio * widthDpiRatio), rectangle.Size.Height);
  586. }
  587. ///下重叠
  588. if (VerticalPaperIndex != VerticalPaperCount)
  589. {
  590. rectangle.Location = new System.Drawing.Point(rectangle.Location.X, (int)(rectangle.Location.Y - posterInfo.OverLap * mmToDpiRatio * heightDpiRatio));
  591. }
  592. ///当存在边沿时
  593. int margin = 0;
  594. ///Label和CutMarks
  595. ///
  596. if (posterInfo.HasLabel || posterInfo.HasCutMarks)
  597. {
  598. margin = 20;
  599. }
  600. if (posterInfo.HasLabel)
  601. {
  602. string sign = null;
  603. if (posterInfo.Label == null || posterInfo.Label == "")
  604. {
  605. sign = string.Format("({0},{1}) -{2} -{3}.pdf {4} {5}", HorizonPaperIndex + 1, VerticalPaperIndex + 1, PrintIndex + 1, PDFViewer.Document.FileName, System.DateTime.Today.ToString("yyyy/MM/dd"), System.DateTime.Now.ToString("HH:mm:ss"));
  606. }
  607. else
  608. {
  609. sign = posterInfo.Label;
  610. }
  611. e.Graphics.DrawString(sign, new Font("Helvetica", 10.0f), System.Drawing.Brushes.Black, margin + 5, 0);
  612. }
  613. if (posterInfo.HasCutMarks)
  614. {
  615. System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
  616. //除了四角之外都要添加裁切标记
  617. if (!(HorizonPaperIndex == 0 && VerticalPaperIndex == 0))
  618. {
  619. e.Graphics.DrawRectangle(pen, new Rectangle(-margin, -margin, 2 * margin, 2 * margin));
  620. }
  621. if (!(HorizonPaperIndex == HorizonPaperCount - 1 && VerticalPaperIndex == 0))
  622. {
  623. e.Graphics.DrawRectangle(pen, new Rectangle(pageBound.Width - margin, -margin, margin * 2, margin * 2));
  624. }
  625. if (!(HorizonPaperIndex == 0 && VerticalPaperIndex == VerticalPaperCount - 1))
  626. {
  627. e.Graphics.DrawRectangle(pen, new Rectangle(-margin, pageBound.Height - margin, margin * 2, margin * 2));
  628. }
  629. if (!(HorizonPaperIndex == HorizonPaperCount - 1 && VerticalPaperIndex == VerticalPaperCount - 1))
  630. {
  631. e.Graphics.DrawRectangle(pen, new Rectangle(pageBound.Width - margin, (int)pageBound.Height - margin, margin * 2, margin * 2));
  632. }
  633. pen.Dispose();
  634. }
  635. Bitmap currentPrintBitmap = Crop(DisplayBitmap, rectangle);
  636. e.Graphics.DrawImage(currentPrintBitmap, new Rectangle(margin, margin, pageBound.Width - 2 * margin, pageBound.Height - 2 * margin), new Rectangle(0, 0, currentPrintBitmap.Width, currentPrintBitmap.Height), GraphicsUnit.Pixel);
  637. currentPrintBitmap.Dispose();
  638. }
  639. ///三种进位:当前列,当前行,当前页
  640. if (HorizonPaperIndex < HorizonPaperCount - 1)
  641. {
  642. HorizonPaperIndex++;
  643. e.HasMorePages = true;
  644. }
  645. else if (VerticalPaperIndex < VerticalPaperCount - 1)
  646. {
  647. HorizonPaperIndex = 0;
  648. VerticalPaperIndex++;
  649. e.HasMorePages = true;
  650. }
  651. else if (posterPrintPaperIndex < PrintedPageCount - 1)
  652. {
  653. ///当前总文件未打印完
  654. posterPrintPaperIndex++;
  655. PosterCurrentBitmap.Dispose();
  656. DisplayBitmap.Dispose();
  657. GC.Collect();
  658. GC.WaitForPendingFinalizers();
  659. HorizonPaperIndex = 0;
  660. VerticalPaperIndex = 0;
  661. e.HasMorePages = true;
  662. }
  663. else
  664. {
  665. HorizonPaperIndex = 0;
  666. VerticalPaperIndex = 0;
  667. e.HasMorePages = false;
  668. }
  669. } //当一页只有一张打印纸的时候
  670. else
  671. {
  672. e.Graphics.DrawImage(DisplayBitmap, new Rectangle(0, 0, pageBound.Width, pageBound.Height), new Rectangle(0, 0, DisplayBitmap.Width, DisplayBitmap.Height), GraphicsUnit.Pixel);
  673. if (posterPrintPaperIndex < PrintedPageCount - 1)
  674. {
  675. ///当前总文件未打印完
  676. posterPrintPaperIndex++;
  677. PosterCurrentBitmap.Dispose();
  678. DisplayBitmap.Dispose();
  679. GC.Collect();
  680. GC.WaitForPendingFinalizers();
  681. HorizonPaperIndex = 0;
  682. VerticalPaperIndex = 0;
  683. e.HasMorePages = true;
  684. }
  685. else
  686. {
  687. e.HasMorePages = false;
  688. }
  689. }
  690. }
  691. }
  692. /// 拆分模式
  693. /// 该模式下
  694. /// 渲染图片到指定大小
  695. /// 直接计算所有需要打印的纸张数
  696. /// 计算单页打印的纸张数
  697. /// 进位
  698. /// 根据当前水平垂直序号裁切图片
  699. /// 继续打印表征:当前纸张序号小于总计纸张序号
  700. else
  701. {
  702. int wholeCountSinglePage = posterInfo.HorizontalSheetNumber * posterInfo.VerticalSheetNumber;//每一轮的总页数
  703. WholePaperCountModPoster = wholeCountSinglePage * PrintedPageCount;//总计打印纸数
  704. int PrintPageIndex = PrintIndex / wholeCountSinglePage;//当前打印纸张所在海报页 从0开始
  705. int PrintIndexInCurrentPage = PrintIndex % wholeCountSinglePage;//当前海报页中,打印纸的序号 从0开始
  706. HorizonPaperIndex = PrintIndexInCurrentPage % HorizonPaperCount;//行中的第几个
  707. VerticalPaperIndex = PrintIndexInCurrentPage / HorizonPaperCount;//第几行
  708. CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(PrintSettingsInfo.PageRangeList[PrintPageIndex]);//取得当前海报页
  709. PreviewWidth = (double)(effectiveWidth * HorizonPaperCount);
  710. PreviewHeight = (double)(effectiveHeight * VerticalPaperCount);
  711. DisplayBitmap = new Bitmap((int)PreviewWidth, (int)PreviewHeight);
  712. double resizedHeight = 0;
  713. double resizedWidth = 0;
  714. if ((cPDFPage.PageSize.Height / cPDFPage.PageSize.Width) >= PreviewHeight / PreviewWidth)
  715. {
  716. resizedHeight = PreviewHeight;
  717. resizedWidth = PreviewHeight / cPDFPage.PageSize.Height * cPDFPage.PageSize.Width;
  718. }
  719. else
  720. {
  721. resizedWidth = PreviewWidth;
  722. resizedHeight = PreviewWidth / cPDFPage.PageSize.Width * cPDFPage.PageSize.Height;
  723. }
  724. if ((HorizonPaperIndex == 0 && VerticalPaperIndex == 0) || HorizonPaperCount * VerticalPaperCount == 1)
  725. {
  726. PosterCurrentBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, PrintSettingsInfo.PageRangeList[PrintPageIndex], PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  727. }
  728. startPoint.X = (int)((PreviewWidth - PosterCurrentBitmap.Width) / 2);
  729. startPoint.Y = (int)((PreviewHeight - PosterCurrentBitmap.Height) / 2);
  730. if (PrintSettingsInfo.IsGrayscale)
  731. {
  732. PosterCurrentBitmap = ToGray(PosterCurrentBitmap, 0);
  733. }
  734. DisplayBitmap = CombineBitmap(DisplayBitmap, PosterCurrentBitmap, startPoint);
  735. ///取有效尺寸和重叠,边沿合并
  736. ///有效尺寸+重叠边
  737. Rectangle rectangle = new Rectangle((int)(HorizonPaperIndex * effectiveWidth), (int)(VerticalPaperIndex * effectiveHeight), (int)effectiveWidth, (int)effectiveHeight);
  738. ///左重叠
  739. if (HorizonPaperIndex != 0)
  740. {
  741. rectangle.Location = new System.Drawing.Point((int)(rectangle.Location.X - posterInfo.OverLap * mmToDpiRatio * widthDpiRatio), rectangle.Location.Y);
  742. }
  743. ///上重叠
  744. if (VerticalPaperIndex != 0)
  745. {
  746. rectangle.Size = new System.Drawing.Size(rectangle.Size.Width, (int)(rectangle.Size.Height + posterInfo.OverLap * mmToDpiRatio * heightDpiRatio));
  747. }
  748. ///右重叠
  749. if (HorizonPaperIndex != HorizonPaperCount)
  750. {
  751. rectangle.Size = new System.Drawing.Size((int)(rectangle.Size.Width + posterInfo.OverLap * mmToDpiRatio * widthDpiRatio), rectangle.Size.Height);
  752. }
  753. ///下重叠
  754. if (VerticalPaperIndex != VerticalPaperCount)
  755. {
  756. rectangle.Location = new System.Drawing.Point(rectangle.Location.X, (int)(rectangle.Location.Y - posterInfo.OverLap * mmToDpiRatio * heightDpiRatio));
  757. }
  758. ///当存在边沿时
  759. int margin = 0;
  760. ///Label和CutMarks
  761. ///
  762. if (posterInfo.HasLabel || posterInfo.HasCutMarks)
  763. {
  764. margin = 20;
  765. }
  766. if (posterInfo.HasLabel)
  767. {
  768. string sign = null;
  769. if (posterInfo.Label == null || posterInfo.Label == "")
  770. {
  771. ///Fix 13366: (行、列)文件名 时间 日期)
  772. sign = string.Format("({0},{1}) -{2} -{3}.pdf {4} {5}", HorizonPaperIndex + 1, VerticalPaperIndex + 1, PrintIndex + 1, PDFViewer.Document.FileName, System.DateTime.Today.ToString("yyyy/MM/dd"), System.DateTime.Now.ToString("HH:mm:ss"));
  773. }
  774. else
  775. {
  776. sign = posterInfo.Label;
  777. }
  778. e.Graphics.DrawString(sign, new Font("Helvetica", 10.0f), System.Drawing.Brushes.Black, margin + 5, 0);
  779. }
  780. if (posterInfo.HasCutMarks)
  781. {
  782. System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
  783. //除了四角之外都要添加裁切标记
  784. if (!(HorizonPaperIndex == 0 && VerticalPaperIndex == 0))
  785. {
  786. e.Graphics.DrawRectangle(pen, new Rectangle(-margin, -margin, 2 * margin, 2 * margin));
  787. }
  788. if (!(HorizonPaperIndex == HorizonPaperCount - 1 && VerticalPaperIndex == 0))
  789. {
  790. e.Graphics.DrawRectangle(pen, new Rectangle(pageBound.Width - margin, -margin, margin * 2, margin * 2));
  791. }
  792. if (!(HorizonPaperIndex == 0 && VerticalPaperIndex == VerticalPaperCount - 1))
  793. {
  794. e.Graphics.DrawRectangle(pen, new Rectangle(-margin, (int)pageBound.Height - margin, margin * 2, margin * 2));
  795. }
  796. if (!(HorizonPaperIndex == HorizonPaperCount - 1 && VerticalPaperIndex == VerticalPaperCount - 1))
  797. {
  798. e.Graphics.DrawRectangle(pen, new Rectangle(pageBound.Width - margin, (int)pageBound.Height - margin, margin * 2, margin * 2));
  799. }
  800. pen.Dispose();
  801. }
  802. Bitmap currentPrintBitmap = Crop(DisplayBitmap, rectangle);
  803. e.Graphics.DrawImage(currentPrintBitmap, new Rectangle(margin, margin, pageBound.Width - 2 * margin, pageBound.Height - 2 * margin), new Rectangle(0, 0, currentPrintBitmap.Width, currentPrintBitmap.Height), GraphicsUnit.Pixel);
  804. currentPrintBitmap.Dispose();
  805. if (PrintIndex < WholePaperCountModPoster - 1)
  806. {
  807. PrintIndex++;
  808. if (PrintIndex % wholeCountSinglePage == 0)
  809. {
  810. PosterCurrentBitmap.Dispose();
  811. DisplayBitmap.Dispose();
  812. GC.Collect();
  813. GC.WaitForPendingFinalizers();
  814. }
  815. e.HasMorePages = true;
  816. }
  817. else
  818. {
  819. e.HasMorePages = false;
  820. }
  821. }
  822. }
  823. /// <summary>
  824. /// 优化内存的小册子模式
  825. /// </summary>
  826. /// <param name="e"></param>
  827. private void PrintDocumentModMultiple(PrintPageEventArgs e)
  828. {
  829. MultipleInfo multipleInfo = (MultipleInfo)PrintSettingsInfo.PrintModInfo;
  830. int widthDpiRatio;
  831. int heightDpiRatio;
  832. ///TODO 可扩展:高低分辨率打印
  833. if (true)
  834. {
  835. widthDpiRatio = (int)e.Graphics.DpiX / 100;
  836. heightDpiRatio = (int)e.Graphics.DpiY / 100;
  837. }
  838. int singlePageHeight;
  839. int singlePageWidth;
  840. ///打印纸实际上可画范围
  841. Rectangle realBound = new Rectangle(documentSettingsInfo.margins.Left,
  842. documentSettingsInfo.margins.Top,
  843. e.PageBounds.Width - documentSettingsInfo.margins.Left - documentSettingsInfo.margins.Right,
  844. e.PageBounds.Height - documentSettingsInfo.margins.Top - documentSettingsInfo.margins.Bottom);
  845. ///每一个PDF页实际可用的范围
  846. singlePageHeight = realBound.Height / multipleInfo.VerticalPageNumber;
  847. singlePageWidth = realBound.Width / multipleInfo.HorizontalPageNumber;
  848. ///起画点
  849. System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
  850. int currentPage = 0;
  851. int startPage = PrintSettingsInfo.PageRangeList[PrintIndex * (multipleInfo.VerticalPageNumber) * (multipleInfo.HorizontalPageNumber)];
  852. int resizedHeight = 0;
  853. int resizedWidth = 0;
  854. bool isNoSrcPage = false;
  855. Bitmap pageBitmap;
  856. ///水平
  857. if (multipleInfo.EnumPageOrder == EnumPageOrder.StatusHorizontalOrder || multipleInfo.EnumPageOrder == EnumPageOrder.StatusHorizontalReverseOrder)
  858. {
  859. for (int tempVerticalIndex = 0; tempVerticalIndex < multipleInfo.VerticalPageNumber; tempVerticalIndex++)
  860. {
  861. startPoint.Y = tempVerticalIndex * singlePageHeight;
  862. for (int tempHorizontalIndex = 0; tempHorizontalIndex < multipleInfo.HorizontalPageNumber; tempHorizontalIndex++)
  863. {
  864. try
  865. {
  866. currentPage = PrintSettingsInfo.PageRangeList[PrintIndex * (multipleInfo.VerticalPageNumber) * (multipleInfo.HorizontalPageNumber) + (tempVerticalIndex * multipleInfo.HorizontalPageNumber) + tempHorizontalIndex];
  867. }
  868. catch
  869. {
  870. //当前文档打印结束
  871. break;
  872. }
  873. //横向
  874. if (multipleInfo.EnumPageOrder == EnumPageOrder.StatusHorizontalOrder)
  875. {
  876. startPoint.X = tempHorizontalIndex * singlePageWidth;
  877. }
  878. //横向倒序
  879. else
  880. {
  881. startPoint.X = (multipleInfo.HorizontalPageNumber - tempHorizontalIndex - 1) * singlePageWidth;
  882. }
  883. CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(currentPage);
  884. if (cPDFPage == null)
  885. {
  886. isNoSrcPage = true;
  887. break;
  888. }
  889. ///这些渲染都使用WidthDpi
  890. ///防止横竖Dpi不同引发图片拉伸
  891. pageBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)(cPDFPage.PageSize.Width * widthDpiRatio), (int)(cPDFPage.PageSize.Height * widthDpiRatio), currentPage, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  892. if (multipleInfo.IsAutoRotate)
  893. {
  894. if ((cPDFPage.PageSize.Height / cPDFPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
  895. (cPDFPage.PageSize.Height / cPDFPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
  896. {
  897. pageBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  898. }
  899. }
  900. ///重设大小
  901. ///计算出应有的大小,但是不马上应用
  902. if ((float)pageBitmap.Height / (float)pageBitmap.Width >= ((float)singlePageHeight / (float)singlePageWidth))
  903. {
  904. resizedHeight = (int)(singlePageHeight * 0.96);
  905. resizedWidth = (int)((float)singlePageHeight / pageBitmap.Height * pageBitmap.Width * 0.96);
  906. }
  907. else
  908. {
  909. resizedWidth = (int)(singlePageWidth * 0.96);
  910. resizedHeight = (int)((float)singlePageWidth / pageBitmap.Width * pageBitmap.Height * 0.96);
  911. }
  912. startPoint.X += (singlePageWidth - resizedWidth) / 2;
  913. startPoint.Y += (singlePageHeight - resizedHeight) / 2;
  914. if (PrintSettingsInfo.IsGrayscale)
  915. {
  916. pageBitmap = ToGray(pageBitmap, 0);
  917. }
  918. e.Graphics.DrawImage(pageBitmap, new Rectangle(realBound.X + startPoint.X, realBound.Y + startPoint.Y, resizedWidth, resizedHeight), new Rectangle(0, 0, pageBitmap.Width, pageBitmap.Height), GraphicsUnit.Pixel);
  919. //绘制边界
  920. if (PrintSettingsInfo.IsPrintPageBorde)
  921. {
  922. System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
  923. e.Graphics.DrawRectangle(pen, new Rectangle(realBound.X + startPoint.X - 2, realBound.Y + startPoint.Y - 2, resizedWidth + 4, resizedHeight + 4));
  924. pen.Dispose();
  925. }
  926. pageBitmap.Dispose();
  927. startPoint.Y -= (singlePageHeight - resizedHeight) / 2;
  928. }
  929. if (isNoSrcPage)
  930. {
  931. isNoSrcPage = false;
  932. break;
  933. }
  934. }
  935. }
  936. ///垂直
  937. else
  938. {
  939. for (int tempHorizontalIndex = 0; tempHorizontalIndex < multipleInfo.HorizontalPageNumber; tempHorizontalIndex++)
  940. {
  941. //纵向,垂直排列时先移位
  942. if (multipleInfo.EnumPageOrder == EnumPageOrder.StatusVerticalOrder)
  943. {
  944. startPoint.X = tempHorizontalIndex * singlePageWidth;
  945. }
  946. //纵向倒序
  947. else
  948. {
  949. startPoint.X = (multipleInfo.HorizontalPageNumber - tempHorizontalIndex - 1) * singlePageWidth;
  950. }
  951. for (int tempVerticalIndex = 0; tempVerticalIndex < multipleInfo.VerticalPageNumber; tempVerticalIndex++)
  952. {
  953. startPoint.Y = tempVerticalIndex * singlePageHeight;
  954. try
  955. {
  956. currentPage = PrintSettingsInfo.PageRangeList[PrintIndex *
  957. (multipleInfo.VerticalPageNumber) *
  958. (multipleInfo.HorizontalPageNumber) +
  959. (tempHorizontalIndex * multipleInfo.VerticalPageNumber) +
  960. tempVerticalIndex];
  961. }
  962. catch
  963. {
  964. break;
  965. }
  966. CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(currentPage);
  967. if (cPDFPage == null)
  968. {
  969. isNoSrcPage = true;
  970. break;
  971. }
  972. pageBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)(cPDFPage.PageSize.Width * widthDpiRatio), (int)(cPDFPage.PageSize.Height * widthDpiRatio), currentPage, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  973. if (multipleInfo.IsAutoRotate)
  974. {
  975. if ((cPDFPage.PageSize.Height / cPDFPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
  976. (cPDFPage.PageSize.Height / cPDFPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
  977. {
  978. pageBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  979. }
  980. }
  981. ///重设大小
  982. ///计算出应有的大小,但是不马上应用
  983. if ((float)pageBitmap.Height / (float)pageBitmap.Width >= ((float)singlePageHeight / (float)singlePageWidth))
  984. {
  985. resizedHeight = (int)(singlePageHeight * 0.96);
  986. resizedWidth = (int)((float)singlePageHeight / pageBitmap.Height * pageBitmap.Width * 0.96);
  987. }
  988. else
  989. {
  990. resizedWidth = (int)(singlePageWidth * 0.96);
  991. resizedHeight = (int)((float)singlePageWidth / pageBitmap.Width * pageBitmap.Height * 0.96);
  992. }
  993. startPoint.X += (singlePageWidth - resizedWidth) / 2;
  994. startPoint.Y += (singlePageHeight - resizedHeight) / 2;
  995. if (PrintSettingsInfo.IsGrayscale)
  996. {
  997. pageBitmap = ToGray(pageBitmap, 0);
  998. }
  999. e.Graphics.DrawImage(pageBitmap, new Rectangle(realBound.X + startPoint.X, realBound.Y + startPoint.Y, resizedWidth, resizedHeight), new Rectangle(0, 0, pageBitmap.Width, pageBitmap.Height), GraphicsUnit.Pixel);
  1000. //绘制边界
  1001. if (PrintSettingsInfo.IsPrintPageBorde)
  1002. {
  1003. System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
  1004. e.Graphics.DrawRectangle(pen, new Rectangle(realBound.X + startPoint.X - 2, realBound.Y + startPoint.Y - 2, resizedWidth + 4, resizedHeight + 4));
  1005. pen.Dispose();
  1006. }
  1007. pageBitmap.Dispose();
  1008. startPoint.X -= (singlePageWidth - resizedWidth) / 2;
  1009. }
  1010. if (isNoSrcPage)
  1011. {
  1012. isNoSrcPage = false;
  1013. break;
  1014. }
  1015. }
  1016. if (IsManualDuplex && PrintIndex % 2 == 1 && multipleInfo.EnumDuplexPrintMod == EnumDuplexPrintMod.StatusFlipShortEdge)
  1017. {
  1018. }
  1019. if (IsManualDuplex)
  1020. {
  1021. if (PrintIndex < PrintedPageCount - 2)
  1022. {
  1023. PrintIndex += 2;
  1024. e.HasMorePages = true;
  1025. }
  1026. else
  1027. {
  1028. e.HasMorePages = false;
  1029. if (PrintIndex % 2 == 0)
  1030. {
  1031. this.printDocumentEvent.GetEvent<SendFinishedFrontSideEvent>().Publish(new FinishedFrontSide { Unicode = this.Unicode });
  1032. }
  1033. }
  1034. }
  1035. //单面打印
  1036. else
  1037. {
  1038. if (PrintIndex < PrintedPageCount - 1)
  1039. {
  1040. PrintIndex++;
  1041. e.HasMorePages = true;
  1042. }
  1043. else
  1044. {
  1045. e.HasMorePages = false;
  1046. }
  1047. }
  1048. }
  1049. }
  1050. private void PrintDocumentModBooklet(PrintPageEventArgs e)
  1051. {
  1052. BookletInfo bookletInfo = (BookletInfo)PrintSettingsInfo.PrintModInfo;
  1053. int widthDpiRatio;
  1054. int heightDpiRatio;
  1055. ///TODO 可扩展:高低分辨率打印
  1056. if (true)
  1057. {
  1058. widthDpiRatio = (int)e.Graphics.DpiX / 100;
  1059. heightDpiRatio = (int)e.Graphics.DpiY / 100;
  1060. }
  1061. Bitmap printBitmap;
  1062. Rectangle realBound = new Rectangle(documentSettingsInfo.margins.Left, documentSettingsInfo.margins.Top, e.PageBounds.Width - documentSettingsInfo.margins.Left - documentSettingsInfo.margins.Right, e.PageBounds.Height - documentSettingsInfo.margins.Top - documentSettingsInfo.margins.Bottom);
  1063. double singlePageHeight;
  1064. double singlePageWidth;
  1065. if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
  1066. {
  1067. singlePageHeight = (double)documentSettingsInfo.DocumentHeight;
  1068. singlePageWidth = (double)(documentSettingsInfo.DocumentWidth / 2);
  1069. }
  1070. else
  1071. {
  1072. singlePageHeight = (double)documentSettingsInfo.DocumentWidth;
  1073. singlePageWidth = (double)(documentSettingsInfo.DocumentHeight / 2);
  1074. }
  1075. System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
  1076. int maxPaperNumber = (PDFViewer.Document.PageCount % 4 == 0) ? (PDFViewer.Document.PageCount / 4) : (PDFViewer.Document.PageCount / 4 + 1);
  1077. Bitmap leftBitmap = null;
  1078. Bitmap rightBitmap = null;
  1079. int resizedHeight = 0;
  1080. int resizedWidth = 0;
  1081. CPDFPage leftPage = null;
  1082. CPDFPage rightPage = null;
  1083. int leftPageIndex = 0;
  1084. int rightPageIndex = 0;
  1085. ///<summary>
  1086. ///左装订与右装订顺序相反
  1087. ///左装订:
  1088. /// </summary>
  1089. if (bookletInfo.EnumBookletBinding == EnumBookletBinding.StatusLeft)
  1090. {
  1091. ///奇数页:
  1092. ///右面为页面序号
  1093. ///左面为[(总打印纸数*4)+1-打印页右侧页序号]
  1094. if ((TargetPaperList[PrintIndex + 1] + 1) % 2 == 1)
  1095. {
  1096. try
  1097. {
  1098. leftPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - TargetPaperList[PrintIndex + 1] - 1];
  1099. leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
  1100. }
  1101. catch
  1102. {
  1103. leftPage = null;
  1104. }
  1105. //左面
  1106. if (leftPage != null)
  1107. {
  1108. leftBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)leftPage.PageSize.Width, (int)leftPage.PageSize.Height, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  1109. if (bookletInfo.IsAutoRotate)
  1110. {
  1111. if ((leftPage.PageSize.Height / leftPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
  1112. (leftPage.PageSize.Height / leftPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
  1113. {
  1114. leftBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  1115. }
  1116. }
  1117. ///重设大小
  1118. if (((float)leftBitmap.Height / leftBitmap.Width) >= singlePageHeight / singlePageWidth)
  1119. {
  1120. resizedHeight = (int)(singlePageHeight * 0.96);
  1121. resizedWidth = (int)(singlePageHeight / leftBitmap.Height * leftBitmap.Width * 0.96);
  1122. }
  1123. else
  1124. {
  1125. resizedWidth = (int)(singlePageWidth * 0.96);
  1126. resizedHeight = (int)(singlePageWidth / leftBitmap.Width * leftBitmap.Height * 0.96);
  1127. }
  1128. startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
  1129. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  1130. if (PrintSettingsInfo.IsGrayscale)
  1131. {
  1132. leftBitmap = ToGray(leftBitmap, 0);
  1133. }
  1134. e.Graphics.DrawImage(leftBitmap, new Rectangle(realBound.X + startPoint.X, realBound.Y + startPoint.Y, resizedWidth, resizedHeight), new Rectangle(0, 0, leftBitmap.Width, leftBitmap.Height), GraphicsUnit.Pixel);
  1135. if (PrintSettingsInfo.IsPrintPageBorde)
  1136. {
  1137. System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
  1138. e.Graphics.DrawRectangle(pen, new Rectangle(realBound.X + startPoint.X - 2, realBound.Y + startPoint.Y - 2, resizedWidth + 4, resizedHeight + 4));
  1139. pen.Dispose();
  1140. }
  1141. }
  1142. ///右面
  1143. ///
  1144. try
  1145. {
  1146. rightPageIndex = PrintSettingsInfo.PageRangeList[TargetPaperList[PrintIndex + 1]];
  1147. rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
  1148. }
  1149. catch
  1150. {
  1151. rightPage = null;
  1152. rightPage = null;
  1153. }
  1154. if (rightPage != null)
  1155. {
  1156. startPoint.X = (int)singlePageWidth;
  1157. rightBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)rightPage.PageSize.Width * widthDpiRatio, (int)rightPage.PageSize.Height * heightDpiRatio, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  1158. if (bookletInfo.IsAutoRotate)
  1159. {
  1160. if ((rightPage.PageSize.Height / rightPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
  1161. (rightPage.PageSize.Height / rightPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
  1162. {
  1163. rightBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  1164. }
  1165. }
  1166. ///重设大小
  1167. if (((float)rightBitmap.Height / rightBitmap.Width) >= singlePageHeight / singlePageWidth)
  1168. {
  1169. resizedHeight = (int)(singlePageHeight * 0.96);
  1170. resizedWidth = (int)(singlePageHeight / rightBitmap.Height * rightBitmap.Width * 0.96);
  1171. }
  1172. else
  1173. {
  1174. resizedWidth = (int)(singlePageWidth * 0.96);
  1175. resizedHeight = (int)(singlePageWidth / rightBitmap.Width * rightBitmap.Height * 0.96);
  1176. }
  1177. startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
  1178. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  1179. if (PrintSettingsInfo.IsGrayscale)
  1180. {
  1181. rightBitmap = ToGray(leftBitmap, 0);
  1182. }
  1183. e.Graphics.DrawImage(rightBitmap, new Rectangle(realBound.X + startPoint.X, realBound.Y + startPoint.Y, resizedWidth, resizedHeight), new Rectangle(0, 0, rightBitmap.Width, rightBitmap.Height), GraphicsUnit.Pixel);
  1184. if (PrintSettingsInfo.IsPrintPageBorde)
  1185. {
  1186. System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
  1187. e.Graphics.DrawRectangle(pen, new Rectangle(realBound.X + startPoint.X - 2, realBound.Y + startPoint.Y - 2, resizedWidth + 4, resizedHeight + 4));
  1188. pen.Dispose();
  1189. }
  1190. }
  1191. }
  1192. ///偶数页
  1193. else
  1194. {
  1195. ///左侧
  1196. try
  1197. {
  1198. leftPageIndex = PrintSettingsInfo.PageRangeList[TargetPaperList[PrintIndex + 1]];
  1199. leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
  1200. }
  1201. catch
  1202. {
  1203. leftPage = null;
  1204. }
  1205. if (leftPage != null)
  1206. {
  1207. leftBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)leftPage.PageSize.Width * widthDpiRatio, (int)leftPage.PageSize.Height * heightDpiRatio, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  1208. if (bookletInfo.IsAutoRotate)
  1209. {
  1210. if ((leftPage.PageSize.Height / leftPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
  1211. (leftPage.PageSize.Height / leftPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
  1212. {
  1213. leftBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  1214. }
  1215. }
  1216. ///重设大小
  1217. if (((float)leftBitmap.Height / leftBitmap.Width) >= singlePageHeight / singlePageWidth)
  1218. {
  1219. resizedHeight = (int)(singlePageHeight * 0.96);
  1220. resizedWidth = (int)(singlePageHeight / leftBitmap.Height * leftBitmap.Width * 0.96);
  1221. }
  1222. else
  1223. {
  1224. resizedWidth = (int)(singlePageWidth * 0.96);
  1225. resizedHeight = (int)(singlePageWidth / leftBitmap.Width * leftBitmap.Height * 0.96);
  1226. }
  1227. startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
  1228. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  1229. if (PrintSettingsInfo.IsGrayscale)
  1230. {
  1231. leftBitmap = ToGray(leftBitmap, 0);
  1232. }
  1233. e.Graphics.DrawImage(leftBitmap, new Rectangle(realBound.X + startPoint.X, realBound.Y + startPoint.Y, resizedWidth, resizedHeight), new Rectangle(0, 0, leftBitmap.Width, leftBitmap.Height), GraphicsUnit.Pixel);
  1234. if (PrintSettingsInfo.IsPrintPageBorde)
  1235. {
  1236. System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
  1237. e.Graphics.DrawRectangle(pen, new Rectangle(realBound.X + startPoint.X - 2, realBound.Y + startPoint.Y - 2, resizedWidth + 4, resizedHeight + 4));
  1238. pen.Dispose();
  1239. }
  1240. }
  1241. //右面
  1242. try
  1243. {
  1244. rightPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - TargetPaperList[PrintIndex + 1] - 1];
  1245. rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
  1246. }
  1247. catch
  1248. {
  1249. rightPage = null;
  1250. }
  1251. if (rightPage != null)
  1252. {
  1253. startPoint.X = (int)singlePageWidth;
  1254. rightBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)rightPage.PageSize.Width, (int)rightPage.PageSize.Height, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  1255. if (bookletInfo.IsAutoRotate)
  1256. {
  1257. if ((rightPage.PageSize.Height / rightPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
  1258. (rightPage.PageSize.Height / rightPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
  1259. {
  1260. rightBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  1261. }
  1262. }
  1263. ///重设大小
  1264. if (((float)rightBitmap.Height / rightBitmap.Width) >= singlePageHeight / singlePageWidth)
  1265. {
  1266. resizedHeight = (int)(singlePageHeight * 0.96);
  1267. resizedWidth = (int)(singlePageHeight / rightBitmap.Height * rightBitmap.Width * 0.96);
  1268. }
  1269. else
  1270. {
  1271. resizedWidth = (int)(singlePageWidth * 0.96);
  1272. resizedHeight = (int)(singlePageWidth / rightBitmap.Width * rightBitmap.Height * 0.96);
  1273. }
  1274. startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
  1275. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  1276. if (PrintSettingsInfo.IsGrayscale)
  1277. {
  1278. rightBitmap = ToGray(rightBitmap, 0);
  1279. }
  1280. e.Graphics.DrawImage(rightBitmap, new Rectangle(realBound.X + startPoint.X, realBound.Y + startPoint.Y, resizedWidth, resizedHeight), new Rectangle(0, 0, rightBitmap.Width, rightBitmap.Height), GraphicsUnit.Pixel);
  1281. if (PrintSettingsInfo.IsPrintPageBorde)
  1282. {
  1283. System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
  1284. e.Graphics.DrawRectangle(pen, new Rectangle(realBound.X + startPoint.X - 2, realBound.Y + startPoint.Y - 2, resizedWidth + 4, resizedHeight + 4));
  1285. pen.Dispose();
  1286. }
  1287. }
  1288. }
  1289. }
  1290. ///右装订
  1291. else
  1292. {
  1293. if ((TargetPaperList[PrintIndex + 1] + 1) % 2 == 1)
  1294. {
  1295. try
  1296. {
  1297. rightPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - TargetPaperList[PrintIndex + 1] - 1];
  1298. rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
  1299. }
  1300. catch
  1301. {
  1302. rightPage = null;
  1303. }
  1304. //左面
  1305. if (rightPage != null)
  1306. {
  1307. startPoint.X = (int)singlePageWidth;
  1308. rightBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)rightPage.PageSize.Width, (int)rightPage.PageSize.Height, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  1309. if (bookletInfo.IsAutoRotate)
  1310. {
  1311. if ((rightPage.PageSize.Height / rightPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
  1312. (rightPage.PageSize.Height / rightPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
  1313. {
  1314. rightBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  1315. }
  1316. }
  1317. ///重设大小
  1318. if (((float)rightBitmap.Height / rightBitmap.Width) >= singlePageHeight / singlePageWidth)
  1319. {
  1320. resizedHeight = (int)(singlePageHeight * 0.96);
  1321. resizedWidth = (int)(singlePageHeight / rightBitmap.Height * rightBitmap.Width * 0.96);
  1322. }
  1323. else
  1324. {
  1325. resizedWidth = (int)(singlePageWidth * 0.96);
  1326. resizedHeight = (int)(singlePageWidth / rightBitmap.Width * rightBitmap.Height * 0.96);
  1327. }
  1328. startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
  1329. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  1330. if (PrintSettingsInfo.IsGrayscale)
  1331. {
  1332. rightBitmap = ToGray(rightBitmap, 0);
  1333. }
  1334. e.Graphics.DrawImage(rightBitmap, new Rectangle(realBound.X + startPoint.X, realBound.Y + startPoint.Y, resizedWidth, resizedHeight), new Rectangle(0, 0, rightBitmap.Width, rightBitmap.Height), GraphicsUnit.Pixel);
  1335. if (PrintSettingsInfo.IsPrintPageBorde)
  1336. {
  1337. System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
  1338. e.Graphics.DrawRectangle(pen, new Rectangle(realBound.X + startPoint.X - 2, realBound.Y + startPoint.Y - 2, resizedWidth + 4, resizedHeight + 4));
  1339. pen.Dispose();
  1340. }
  1341. }
  1342. ///右面
  1343. ///
  1344. try
  1345. {
  1346. leftPageIndex = PrintSettingsInfo.PageRangeList[TargetPaperList[PrintIndex + 1]];
  1347. leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
  1348. }
  1349. catch
  1350. {
  1351. leftPage = null;
  1352. leftPage = null;
  1353. }
  1354. if (leftPage != null)
  1355. {
  1356. leftBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)leftPage.PageSize.Width * widthDpiRatio, (int)leftPage.PageSize.Height * heightDpiRatio, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  1357. if (bookletInfo.IsAutoRotate)
  1358. {
  1359. if ((leftPage.PageSize.Height / leftPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
  1360. (leftPage.PageSize.Height / leftPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
  1361. {
  1362. leftBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  1363. }
  1364. }
  1365. ///重设大小
  1366. if (((float)leftBitmap.Height / leftBitmap.Width) >= singlePageHeight / singlePageWidth)
  1367. {
  1368. resizedHeight = (int)(singlePageHeight * 0.96);
  1369. resizedWidth = (int)(singlePageHeight / leftBitmap.Height * leftBitmap.Width * 0.96);
  1370. }
  1371. else
  1372. {
  1373. resizedWidth = (int)(singlePageWidth * 0.96);
  1374. resizedHeight = (int)(singlePageWidth / leftBitmap.Width * leftBitmap.Height * 0.96);
  1375. }
  1376. startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
  1377. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  1378. if (PrintSettingsInfo.IsGrayscale)
  1379. {
  1380. leftBitmap = ToGray(leftBitmap, 0);
  1381. }
  1382. e.Graphics.DrawImage(leftBitmap, new Rectangle(realBound.X + startPoint.X, realBound.Y + startPoint.Y, resizedWidth, resizedHeight), new Rectangle(0, 0, leftBitmap.Width, leftBitmap.Height), GraphicsUnit.Pixel);
  1383. if (PrintSettingsInfo.IsPrintPageBorde)
  1384. {
  1385. System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
  1386. e.Graphics.DrawRectangle(pen, new Rectangle(realBound.X + startPoint.X - 2, realBound.Y + startPoint.Y - 2, resizedWidth + 4, resizedHeight + 4));
  1387. pen.Dispose();
  1388. }
  1389. }
  1390. }
  1391. ///偶数页
  1392. else
  1393. {
  1394. try
  1395. {
  1396. rightPageIndex = PrintSettingsInfo.PageRangeList[TargetPaperList[PrintIndex + 1]];
  1397. rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
  1398. }
  1399. catch
  1400. {
  1401. rightPage = null;
  1402. }
  1403. if (rightPage != null)
  1404. {
  1405. startPoint.X = (int)singlePageWidth;
  1406. rightBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)rightPage.PageSize.Width * widthDpiRatio, (int)rightPage.PageSize.Height * heightDpiRatio, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  1407. if (bookletInfo.IsAutoRotate)
  1408. {
  1409. if ((rightPage.PageSize.Height / rightPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
  1410. (rightPage.PageSize.Height / rightPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
  1411. {
  1412. rightBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  1413. }
  1414. }
  1415. ///重设大小
  1416. if (((float)rightBitmap.Height / rightBitmap.Width) >= singlePageHeight / singlePageWidth)
  1417. {
  1418. resizedHeight = (int)(singlePageHeight * 0.96);
  1419. resizedWidth = (int)(singlePageHeight / rightBitmap.Height * rightBitmap.Width * 0.96);
  1420. }
  1421. else
  1422. {
  1423. resizedWidth = (int)(singlePageWidth * 0.96);
  1424. resizedHeight = (int)(singlePageWidth / rightBitmap.Width * rightBitmap.Height * 0.96);
  1425. }
  1426. startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
  1427. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  1428. if (PrintSettingsInfo.IsGrayscale)
  1429. {
  1430. rightBitmap = ToGray(rightBitmap, 0);
  1431. }
  1432. e.Graphics.DrawImage(rightBitmap, new Rectangle(realBound.X + startPoint.X, realBound.Y + startPoint.Y, resizedWidth, resizedHeight), new Rectangle(0, 0, rightBitmap.Width, rightBitmap.Height), GraphicsUnit.Pixel);
  1433. if (PrintSettingsInfo.IsPrintPageBorde)
  1434. {
  1435. System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
  1436. e.Graphics.DrawRectangle(pen, new Rectangle(realBound.X + startPoint.X - 2, realBound.Y + startPoint.Y - 2, resizedWidth + 4, resizedHeight + 4));
  1437. pen.Dispose();
  1438. }
  1439. }
  1440. try
  1441. {
  1442. leftPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - TargetPaperList[PrintIndex + 1] - 1];
  1443. leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
  1444. }
  1445. catch
  1446. {
  1447. leftPage = null;
  1448. }
  1449. if (leftPage != null)
  1450. {
  1451. leftBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)leftPage.PageSize.Width, (int)leftPage.PageSize.Height, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  1452. if (bookletInfo.IsAutoRotate)
  1453. {
  1454. if ((leftPage.PageSize.Height / leftPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
  1455. (leftPage.PageSize.Height / leftPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
  1456. {
  1457. leftBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  1458. }
  1459. }
  1460. ///重设大小
  1461. if (((float)leftBitmap.Height / leftBitmap.Width) >= singlePageHeight / singlePageWidth)
  1462. {
  1463. resizedHeight = (int)(singlePageHeight * 0.96);
  1464. resizedWidth = (int)(singlePageHeight / leftBitmap.Height * leftBitmap.Width * 0.96);
  1465. }
  1466. else
  1467. {
  1468. resizedWidth = (int)(singlePageWidth * 0.96);
  1469. resizedHeight = (int)(singlePageWidth / leftBitmap.Width * leftBitmap.Height * 0.96);
  1470. }
  1471. startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
  1472. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  1473. if (PrintSettingsInfo.IsGrayscale)
  1474. {
  1475. leftBitmap = ToGray(leftBitmap, 0);
  1476. }
  1477. e.Graphics.DrawImage(leftBitmap, new Rectangle(realBound.X + startPoint.X, realBound.Y + startPoint.Y, resizedWidth, resizedHeight), new Rectangle(0, 0, leftBitmap.Width, leftBitmap.Height), GraphicsUnit.Pixel);
  1478. if (PrintSettingsInfo.IsPrintPageBorde)
  1479. {
  1480. System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
  1481. e.Graphics.DrawRectangle(pen, new Rectangle(realBound.X + startPoint.X - 2, realBound.Y + startPoint.Y - 2, resizedWidth + 4, resizedHeight + 4));
  1482. pen.Dispose();
  1483. }
  1484. }
  1485. }
  1486. }
  1487. if (IsManualDuplex)
  1488. {
  1489. if (PrintIndex < PrintedPageCount - 2)
  1490. {
  1491. PrintIndex += 2;
  1492. e.HasMorePages = true;
  1493. }
  1494. else
  1495. {
  1496. e.HasMorePages = false;
  1497. if (PrintIndex % 2 == 0)
  1498. {
  1499. this.printDocumentEvent.GetEvent<SendFinishedFrontSideEvent>().Publish(new FinishedFrontSide { Unicode = this.Unicode });
  1500. }
  1501. }
  1502. }
  1503. //单面打印或自动双面
  1504. else
  1505. {
  1506. if (PrintIndex < PrintedPageCount - 1)
  1507. {
  1508. PrintIndex++;
  1509. e.HasMorePages = true;
  1510. }
  1511. else
  1512. {
  1513. e.HasMorePages = false;
  1514. }
  1515. }
  1516. }
  1517. #endregion
  1518. private void PrintDocumentByCurrentSettings(object sender, PrintPageEventArgs e)
  1519. {
  1520. switch (PrintSettingsInfo.PrintModInfo.EnumPrintMod)
  1521. {
  1522. case EnumPrintMod.StatusPoster:
  1523. PrintDocumentModPoster(e);
  1524. break;
  1525. case EnumPrintMod.StatusMultiple:
  1526. PrintDocumentModMultiple(e);
  1527. break;
  1528. case EnumPrintMod.StatusBooklet:
  1529. PrintDocumentModBooklet(e);
  1530. break;
  1531. case EnumPrintMod.StatusSize:
  1532. default:
  1533. PrintDocumentModSize(e);
  1534. break;
  1535. }
  1536. }
  1537. //跳转指定页
  1538. public void JumpToSelectedPage()
  1539. {
  1540. if (TargetPaperIndex >= 1 && TargetPaperIndex <= PrintedPageCount)
  1541. {
  1542. PaintPageByCurrentPreviewIndex(TargetPaperIndex);
  1543. ErrorPageTxtVisibility = Visibility.Collapsed;
  1544. }
  1545. else
  1546. {
  1547. ErrorPageTxtVisibility = Visibility.Visible;
  1548. }
  1549. }
  1550. //跳转指定页
  1551. public void JumpPage(object e)
  1552. {
  1553. var txt = e as System.Windows.Controls.TextBox;
  1554. if (txt.Text != null)
  1555. {
  1556. try
  1557. {
  1558. TargetPaperIndex = Convert.ToInt32(txt.Text);
  1559. }
  1560. catch
  1561. {
  1562. TargetPaperIndex = 1;
  1563. }
  1564. }
  1565. if (TargetPaperIndex >= 1 && TargetPaperIndex <= PrintedPageCount)
  1566. {
  1567. PaintPageByCurrentPreviewIndex(TargetPaperIndex);
  1568. }
  1569. }
  1570. public void RecvPrintSettingsInfo(PrintSettingsInfoWithUnicode printSettingsInfoWIthUnicode)
  1571. {
  1572. if (PDFViewer != null && printSettingsInfoWIthUnicode.printSettingsInfo != null)
  1573. {
  1574. if (PrintSettingsInfo.IsPaperSizeChanged)
  1575. {
  1576. documentSettingsInfo.IsSettingsChanged = true;
  1577. }
  1578. if (documentSettingsInfo.IsSettingsChanged)
  1579. {
  1580. documentSettingsInfo.DocumentWidth = PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize.Width;
  1581. documentSettingsInfo.DocumentHeight = PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize.Height;
  1582. documentSettingsInfo.margins = PrintSettingsInfo.PrintDocument.DefaultPageSettings.Margins;
  1583. documentSettingsInfo.DocumentBounds = PrintSettingsInfo.PrintDocument.DefaultPageSettings.Bounds;
  1584. documentSettingsInfo.IsSettingsChanged = false;
  1585. }
  1586. CaculatePrintedPageCount();
  1587. SetPaperCollection();
  1588. if (PrintedPageCount != PreviousPrintedPageCount)
  1589. {
  1590. TargetPaperIndex = 1;
  1591. PreviousPrintedPageCount = PrintedPageCount;
  1592. }
  1593. PaintPageByCurrentPreviewIndex(TargetPaperIndex);
  1594. }
  1595. }
  1596. public void SetViewBox(double height, double width)
  1597. {
  1598. if (height / width >= (248.0 / 180.0))
  1599. {
  1600. ViewBoxHeight = "248.0";
  1601. ViewBoxWidth = (width / height * 248.0).ToString();
  1602. }
  1603. else
  1604. {
  1605. ViewBoxWidth = "180.0";
  1606. ViewBoxHeight = (height / width * 180.0).ToString();
  1607. }
  1608. }
  1609. #region 渲染具体函数
  1610. [System.Runtime.InteropServices.DllImport("gdi32.dll")]
  1611. public static extern bool DeleteObject(IntPtr hObject);
  1612. /// <summary>
  1613. /// bitmap->bitmapsource转换器,用于显示到image控件
  1614. ///
  1615. /// </summary>
  1616. /// <param name="bmp">
  1617. /// Bitmap格式图片资源
  1618. /// </param>
  1619. /// <returns></returns>
  1620. public BitmapSource ToBitmapSource(System.Drawing.Bitmap bmp)
  1621. {
  1622. IntPtr ptr = bmp.GetHbitmap();//obtain the Hbitmap
  1623. try
  1624. {
  1625. BitmapSource bmpsrc = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap
  1626. (
  1627. ptr,
  1628. IntPtr.Zero,
  1629. new Int32Rect(0, 0, bmp.Width, bmp.Height),
  1630. System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()
  1631. );
  1632. return bmpsrc;
  1633. }
  1634. finally
  1635. {
  1636. DeleteObject(ptr);
  1637. }
  1638. }
  1639. /// <summary>
  1640. /// 创建空白底图
  1641. /// </summary>
  1642. private Bitmap CreateBlankBitmap()
  1643. {
  1644. if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
  1645. {
  1646. blankPageBitmap = new Bitmap((int)documentSettingsInfo.DocumentWidth, (int)documentSettingsInfo.DocumentHeight);
  1647. }
  1648. else
  1649. {
  1650. blankPageBitmap = new Bitmap((int)documentSettingsInfo.DocumentHeight, (int)documentSettingsInfo.DocumentWidth);
  1651. }
  1652. return blankPageBitmap;
  1653. }
  1654. /// <summary>
  1655. /// 设置预览(除海报)
  1656. /// </summary>
  1657. public void SetPreview()
  1658. {
  1659. if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
  1660. {
  1661. PaperWidth = String.Format("{0:F1}", (documentSettingsInfo.DocumentWidth) / 100.0 * 25.4);
  1662. PaperHeight = String.Format("{0:F1}", (documentSettingsInfo.DocumentHeight) / 100.0 * 25.4);
  1663. }
  1664. else
  1665. {
  1666. PaperHeight = String.Format("{0:F1}", (documentSettingsInfo.DocumentWidth) / 100.0 * 25.4);
  1667. PaperWidth = String.Format("{0:F1}", (documentSettingsInfo.DocumentHeight) / 100.0 * 25.4);
  1668. }
  1669. if (PrintSettingsInfo.PrintModInfo.EnumPrintMod != EnumPrintMod.StatusPoster)
  1670. {
  1671. SetViewBox(double.Parse(PaperHeight), double.Parse(PaperWidth));
  1672. blankPageBitmap = CreateBlankBitmap();
  1673. }
  1674. }
  1675. public System.Drawing.Image ResizeImage(System.Drawing.Image image, System.Drawing.Size size)
  1676. {
  1677. //获取图片宽度
  1678. int sourceWidth = image.Width;
  1679. //获取图片高度
  1680. int sourceHeight = image.Height;
  1681. float nPercent = 0;
  1682. float nPercentW = 0;
  1683. float nPercentH = 0;
  1684. //计算宽度的缩放比例
  1685. //nPercentW = ((float)size.Width * (float)GetDpiByGraphics()/120/ (float)sourceWidth);
  1686. ////计算高度的缩放比例
  1687. //nPercentH = ((float)size.Height*(float)GetDpiByGraphics() / 120 / (float)sourceHeight);
  1688. nPercentW = ((float)size.Width / (float)sourceWidth);
  1689. //计算高度的缩放比例
  1690. nPercentH = ((float)size.Height / (float)sourceHeight);
  1691. if (nPercentH < nPercentW)
  1692. nPercent = nPercentH;
  1693. else
  1694. nPercent = nPercentW;
  1695. //期望的宽度
  1696. int destWidth = (int)(sourceWidth * nPercent);
  1697. //期望的高度
  1698. int destHeight = (int)(sourceHeight * nPercent);
  1699. System.Drawing.Bitmap b = new System.Drawing.Bitmap(destWidth, destHeight);
  1700. Graphics g = Graphics.FromImage((System.Drawing.Image)b);
  1701. //g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  1702. g.SmoothingMode = SmoothingMode.None; //抗锯齿
  1703. g.InterpolationMode = InterpolationMode.HighQualityBicubic; //高质量的双三次插值
  1704. g.CompositingQuality = CompositingQuality.HighQuality; //高质量、低速度复合
  1705. //绘制图像
  1706. g.DrawImage(image, 0, 0, destWidth, destHeight);
  1707. g.Dispose();
  1708. return (System.Drawing.Image)b;
  1709. }
  1710. /// <summary>
  1711. /// 直接Resize到给定大小
  1712. /// </summary>
  1713. /// <param name="input"></param>
  1714. /// <param name="targetWidth"></param>
  1715. /// <param name="targetHeight"></param>
  1716. /// <returns></returns>
  1717. public Bitmap Resize(Bitmap input, int targetWidth, int targetHeight)
  1718. {
  1719. try
  1720. {
  1721. var actualBitmap = new Bitmap(targetWidth, targetHeight);
  1722. var g = Graphics.FromImage(actualBitmap);
  1723. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //设定插值方式
  1724. g.DrawImage(input,
  1725. new Rectangle(0, 0, targetWidth, targetHeight),
  1726. new Rectangle(0, 0, input.Width, input.Height),
  1727. GraphicsUnit.Pixel);
  1728. g.Dispose();
  1729. return actualBitmap;
  1730. }
  1731. catch (Exception ex)
  1732. {
  1733. return null;
  1734. }
  1735. }
  1736. /// <summary>
  1737. /// 计算页数
  1738. /// </summary>
  1739. public void CaculatePrintedPageCount()
  1740. {
  1741. PrintedPageCount = PrintSettingsInfo.PageRangeList.Count;
  1742. if (PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusMultiple)
  1743. {
  1744. MultipleInfo multipleInfo = (MultipleInfo)PrintSettingsInfo.PrintModInfo;
  1745. if (PrintedPageCount % (multipleInfo.HorizontalPageNumber * multipleInfo.VerticalPageNumber) != 0)
  1746. {
  1747. PrintedPageCount = PrintedPageCount / (multipleInfo.HorizontalPageNumber * multipleInfo.VerticalPageNumber) + 1;
  1748. }
  1749. else
  1750. {
  1751. PrintedPageCount = PrintedPageCount / (multipleInfo.HorizontalPageNumber * multipleInfo.VerticalPageNumber);
  1752. }
  1753. }
  1754. else if (PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusBooklet)
  1755. {
  1756. BookletInfo bookletInfo = (BookletInfo)PrintSettingsInfo.PrintModInfo;
  1757. if (PrintedPageCount == 1)
  1758. {
  1759. PrintedPageCount = 1;
  1760. }
  1761. else
  1762. {
  1763. PrintedPageCount = (bookletInfo.EndPaperIndex - bookletInfo.BeginPaperIndex + 1) * 2;
  1764. if (bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusFrontSideOnly ||
  1765. bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusBackSideOnly)
  1766. {
  1767. PrintedPageCount /= 2;
  1768. }
  1769. }
  1770. }
  1771. }
  1772. /// <summary>
  1773. /// 转灰度
  1774. /// </summary>
  1775. /// <param name="bmp"></param>
  1776. /// <param name="mode"></param>
  1777. /// <returns></returns>
  1778. private Bitmap ToGray(Bitmap bmp, int mode)
  1779. {
  1780. if (bmp == null)
  1781. {
  1782. return null;
  1783. }
  1784. int w = bmp.Width;
  1785. int h = bmp.Height;
  1786. try
  1787. {
  1788. byte newColor = 0;
  1789. BitmapData srcData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  1790. unsafe
  1791. {
  1792. byte* p = (byte*)srcData.Scan0.ToPointer();
  1793. for (int y = 0; y < h; y++)
  1794. {
  1795. for (int x = 0; x < w; x++)
  1796. {
  1797. if (mode == 0) // 加权平均
  1798. {
  1799. newColor = (byte)((float)p[0] * 0.114f + (float)p[1] * 0.587f + (float)p[2] * 0.299f);
  1800. }
  1801. else // 算数平均
  1802. {
  1803. newColor = (byte)((float)(p[0] + p[1] + p[2]) / 3.0f);
  1804. }
  1805. p[0] = newColor;
  1806. p[1] = newColor;
  1807. p[2] = newColor;
  1808. p += 3;
  1809. }
  1810. p += srcData.Stride - w * 3;
  1811. }
  1812. bmp.UnlockBits(srcData);
  1813. return bmp;
  1814. }
  1815. }
  1816. catch
  1817. {
  1818. return null;
  1819. }
  1820. }
  1821. /// <summary>
  1822. /// 合并底图和前景
  1823. /// </summary>
  1824. /// <param name="background"></param>
  1825. /// <param name="foreground"></param>
  1826. /// <param name="point"></param>
  1827. /// <returns></returns>
  1828. public Bitmap CombineBitmap(Bitmap background, Bitmap foreground, System.Drawing.Point point)
  1829. {
  1830. if (background == null)
  1831. {
  1832. return null;
  1833. }
  1834. int bgWidth = background.Width;
  1835. int bgHeight = background.Height;
  1836. int fgWidth = foreground.Width;
  1837. int fgHeight = foreground.Height;
  1838. Bitmap newMap = new Bitmap(bgWidth, bgHeight);
  1839. Graphics graphics = Graphics.FromImage(newMap);
  1840. graphics.DrawImage(background, new System.Drawing.Point(0, 0));
  1841. graphics.DrawImage(foreground, point);
  1842. graphics.Dispose();
  1843. return newMap;
  1844. }
  1845. /// <summary>
  1846. /// size模式
  1847. /// </summary>
  1848. /// <param name="paperIndex"></param>
  1849. private async void PreviewModSize(int paperIndex)
  1850. {
  1851. SizeInfo sizeInfo = (SizeInfo)PrintSettingsInfo.PrintModInfo;
  1852. CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(PrintSettingsInfo.PageRangeList[paperIndex]);
  1853. System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
  1854. if (cPDFPage != null)
  1855. {
  1856. Bitmap printBitmap = new Bitmap((int)(float.Parse(PaperWidth)), (int)float.Parse(PaperHeight));
  1857. Bitmap bitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)(cPDFPage.PageSize.Width * PDFToMediaDpiRatio), (int)(cPDFPage.PageSize.Height * PDFToMediaDpiRatio), PrintSettingsInfo.PageRangeList[paperIndex], PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  1858. if (PrintSettingsInfo.IsGrayscale)
  1859. {
  1860. bitmap = ToGray(bitmap, 0);
  1861. }
  1862. ///<summary>
  1863. ///自适应模式
  1864. /// </summary>
  1865. if (sizeInfo.EnumSizeType == EnumSizeType.StatusAdaptive)
  1866. {
  1867. int resizedHeight = 0;
  1868. int resizedWidth = 0;
  1869. ///重设大小
  1870. if ((float)bitmap.Height / (float)bitmap.Width >= (float.Parse(PaperHeight) / float.Parse(PaperWidth)))
  1871. {
  1872. resizedHeight = (int)(float.Parse(PaperHeight) * mmToDpiRatio);
  1873. resizedWidth = (int)((float.Parse(PaperHeight) / bitmap.Height * bitmap.Width) * mmToDpiRatio);
  1874. }
  1875. else
  1876. {
  1877. resizedWidth = (int)(float.Parse(PaperWidth) * mmToDpiRatio);
  1878. resizedHeight = (int)((float.Parse(PaperWidth) / bitmap.Width * bitmap.Height) * mmToDpiRatio);
  1879. }
  1880. bitmap = Resize(bitmap, resizedWidth, resizedHeight);
  1881. startPoint.X = (blankPageBitmap.Width - resizedWidth) / 2;
  1882. startPoint.Y = (blankPageBitmap.Height - resizedHeight) / 2;
  1883. printBitmap = CombineBitmap(blankPageBitmap, bitmap, startPoint);
  1884. BitmapSource = ToBitmapSource(printBitmap);
  1885. }
  1886. else if (sizeInfo.EnumSizeType == EnumSizeType.StatusActural)
  1887. {
  1888. startPoint.X = (blankPageBitmap.Width - bitmap.Width) / 2;
  1889. startPoint.Y = (blankPageBitmap.Height - bitmap.Height) / 2;
  1890. printBitmap = CombineBitmap(blankPageBitmap, bitmap, startPoint);
  1891. BitmapSource = ToBitmapSource(printBitmap);
  1892. }
  1893. else if (sizeInfo.EnumSizeType == EnumSizeType.StatusCustomized)
  1894. {
  1895. bitmap = Resize(bitmap, (int)(bitmap.Width * (sizeInfo.DisplayRatio / 100.0)), (int)(bitmap.Height * (sizeInfo.DisplayRatio / 100.0)));
  1896. startPoint.X = (blankPageBitmap.Width - bitmap.Width) / 2;
  1897. startPoint.Y = (blankPageBitmap.Height - bitmap.Height) / 2;
  1898. printBitmap = CombineBitmap(blankPageBitmap, bitmap, startPoint);
  1899. BitmapSource = ToBitmapSource(printBitmap);
  1900. }
  1901. bitmap.Dispose();
  1902. printBitmap.Dispose();
  1903. }
  1904. }
  1905. /// <summary>
  1906. /// 海报模式
  1907. /// </summary>
  1908. /// <param name="paperIndex"></param>
  1909. private async void PreviewModPoster(int paperIndex)
  1910. {
  1911. PosterInfo posterInfo = (PosterInfo)PrintSettingsInfo.PrintModInfo;
  1912. CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(PrintSettingsInfo.PageRangeList[paperIndex]);
  1913. double PreviewHeight = 0;
  1914. double PreviewWidth = 0;
  1915. System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
  1916. Bitmap displayBitmap = null;
  1917. double effectiveHeight;
  1918. double effectiveWidth;
  1919. //计算有效打印区域
  1920. if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
  1921. {
  1922. effectiveHeight = documentSettingsInfo.DocumentHeight;
  1923. effectiveWidth = documentSettingsInfo.DocumentWidth;
  1924. }
  1925. else
  1926. {
  1927. effectiveHeight = documentSettingsInfo.DocumentWidth;
  1928. effectiveWidth = documentSettingsInfo.DocumentHeight;
  1929. }
  1930. //重叠
  1931. effectiveHeight = effectiveHeight - 2 * posterInfo.OverLap * 100 / 25.4;
  1932. effectiveWidth = effectiveWidth - 2 * posterInfo.OverLap * 100 / 25.4;
  1933. if (posterInfo.HasCutMarks || posterInfo.HasLabel)
  1934. {
  1935. ///如果边距小于预留尺寸
  1936. effectiveHeight = effectiveHeight - 40;
  1937. effectiveWidth = effectiveWidth - 40;
  1938. }
  1939. if (cPDFPage != null)
  1940. {
  1941. //平铺模式
  1942. //根据页面大小,按最大有效打印区域划分为多块
  1943. //最大有效区域
  1944. //纸张大小-标记标签-重叠
  1945. if (posterInfo.EnumPosterMod == EnumPosterMod.StatusTile)
  1946. {
  1947. Bitmap bitmap;
  1948. //TODO 放大倍数过高引起超限
  1949. try
  1950. {
  1951. bitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)(cPDFPage.PageSize.Width * PDFToMediaDpiRatio * (posterInfo.TileRatio / 100.0)), (int)(cPDFPage.PageSize.Height * PDFToMediaDpiRatio * (posterInfo.TileRatio / 100.0)), PrintSettingsInfo.PageRangeList[paperIndex], PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  1952. }
  1953. catch
  1954. {
  1955. bitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)(cPDFPage.PageSize.Width * PDFToMediaDpiRatio), (int)(cPDFPage.PageSize.Height * PDFToMediaDpiRatio), PrintSettingsInfo.PageRangeList[paperIndex], PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  1956. }
  1957. HorizonPaperCount = (int)Math.Ceiling((double)bitmap.Width / effectiveWidth);
  1958. VerticalPaperCount = (int)Math.Ceiling(((double)bitmap.Height / effectiveHeight));
  1959. PreviewWidth = (double)(effectiveWidth * HorizonPaperCount);
  1960. PreviewHeight = (double)(effectiveHeight * VerticalPaperCount);
  1961. //铺放纸张
  1962. SetViewBox(PreviewHeight, PreviewWidth);
  1963. //创建空白图像,大小等于纸张铺放后
  1964. displayBitmap = new Bitmap((int)PreviewWidth, (int)PreviewHeight);
  1965. //居中合并
  1966. startPoint.X = (int)((PreviewWidth - bitmap.Width) / 2);
  1967. startPoint.Y = (int)((PreviewHeight - bitmap.Height) / 2);
  1968. if (PrintSettingsInfo.IsGrayscale)
  1969. {
  1970. bitmap = ToGray(bitmap, 0);
  1971. }
  1972. displayBitmap = CombineBitmap(displayBitmap, bitmap, startPoint);
  1973. //画线
  1974. Graphics g = Graphics.FromImage(displayBitmap);
  1975. if (HorizonPaperCount > 1)
  1976. {
  1977. for (int tempX = 1; tempX < HorizonPaperCount; tempX++)
  1978. {
  1979. int horizonLocate = (int)(tempX * displayBitmap.Width / HorizonPaperCount);
  1980. g.DrawLine(new Pen(Color.Gray, HorizonPaperCount * 2), new System.Drawing.Point(horizonLocate, 0), new System.Drawing.Point(horizonLocate, (int)PreviewHeight));
  1981. }
  1982. }
  1983. if (VerticalPaperCount > 1)
  1984. {
  1985. for (int tempY = 1; tempY < VerticalPaperCount; tempY++)
  1986. {
  1987. int verticalLocate = (int)(tempY * displayBitmap.Height / VerticalPaperCount);
  1988. g.DrawLine(new Pen(Color.Gray, VerticalPaperCount * 2), new System.Drawing.Point(0, verticalLocate), new System.Drawing.Point((int)PreviewWidth, verticalLocate));
  1989. }
  1990. }
  1991. g.Dispose();
  1992. bitmap.Dispose();
  1993. PosterRatio = posterInfo.TileRatio / 100.0;
  1994. }
  1995. //拆分模式
  1996. else if (posterInfo.EnumPosterMod == EnumPosterMod.StatusSplit)
  1997. {
  1998. HorizonPaperCount = posterInfo.HorizontalSheetNumber;
  1999. VerticalPaperCount = posterInfo.VerticalSheetNumber;
  2000. PreviewWidth = (double)(effectiveWidth * HorizonPaperCount);
  2001. PreviewHeight = (double)(effectiveHeight * VerticalPaperCount);
  2002. //铺放纸张
  2003. SetViewBox(PreviewHeight, PreviewWidth);
  2004. //创建空白图像,大小等于纸张铺放后
  2005. displayBitmap = new Bitmap((int)PreviewWidth, (int)PreviewHeight);
  2006. //文件根据有效范围缩放
  2007. double wholeEffectiveHeight = effectiveHeight * VerticalPaperCount;
  2008. double wholeEffectiveWidth = effectiveWidth * HorizonPaperCount;
  2009. double resizedHeight = 0;
  2010. double resizedWidth = 0;
  2011. if ((cPDFPage.PageSize.Height / cPDFPage.PageSize.Width) >= wholeEffectiveHeight / wholeEffectiveWidth)
  2012. {
  2013. resizedHeight = wholeEffectiveHeight;
  2014. resizedWidth = wholeEffectiveHeight / cPDFPage.PageSize.Height * cPDFPage.PageSize.Width;
  2015. }
  2016. else
  2017. {
  2018. resizedWidth = wholeEffectiveWidth;
  2019. resizedHeight = wholeEffectiveWidth / cPDFPage.PageSize.Width * cPDFPage.PageSize.Height;
  2020. }
  2021. PosterRatio = resizedWidth / cPDFPage.PageSize.Width;
  2022. Bitmap bitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, PrintSettingsInfo.PageRangeList[paperIndex], PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  2023. startPoint.X = (int)((PreviewWidth - bitmap.Width) / 2);
  2024. startPoint.Y = (int)((PreviewHeight - bitmap.Height) / 2);
  2025. if (PrintSettingsInfo.IsGrayscale)
  2026. {
  2027. bitmap = ToGray(bitmap, 0);
  2028. }
  2029. displayBitmap = CombineBitmap(displayBitmap, bitmap, startPoint);
  2030. //画线
  2031. Graphics g = Graphics.FromImage(displayBitmap);
  2032. if (HorizonPaperCount > 1)
  2033. {
  2034. for (int tempX = 1; tempX < HorizonPaperCount; tempX++)
  2035. {
  2036. int horizonLocate = (int)(tempX * displayBitmap.Width / HorizonPaperCount);
  2037. g.DrawLine(new Pen(Color.Gray, HorizonPaperCount * 2), new System.Drawing.Point(horizonLocate, 0), new System.Drawing.Point(horizonLocate, (int)PreviewHeight));
  2038. }
  2039. }
  2040. if (VerticalPaperCount > 1)
  2041. {
  2042. for (int tempY = 1; tempY < VerticalPaperCount; tempY++)
  2043. {
  2044. int verticalLocate = (int)(tempY * displayBitmap.Height / VerticalPaperCount);
  2045. g.DrawLine(new Pen(Color.Gray, VerticalPaperCount * 2), new System.Drawing.Point(0, verticalLocate), new System.Drawing.Point((int)PreviewWidth, verticalLocate));
  2046. }
  2047. }
  2048. g.Dispose();
  2049. bitmap.Dispose();
  2050. }
  2051. }
  2052. BitmapSource = ToBitmapSource(displayBitmap);
  2053. displayBitmap.Dispose();
  2054. }
  2055. /// <summary>
  2056. /// 多页
  2057. /// </summary>
  2058. /// <param name="paperIndex"></param>
  2059. private async void PreviewModMultiple(int paperIndex)
  2060. {
  2061. MultipleInfo multipleInfo = (MultipleInfo)PrintSettingsInfo.PrintModInfo;
  2062. int singlePageHeight;
  2063. int singlePageWidth;
  2064. Bitmap printBitmap;
  2065. int currentPage = 0;
  2066. if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
  2067. {
  2068. singlePageHeight = (int)(documentSettingsInfo.DocumentHeight / multipleInfo.VerticalPageNumber);
  2069. singlePageWidth = (int)(documentSettingsInfo.DocumentWidth / multipleInfo.HorizontalPageNumber);
  2070. printBitmap = new Bitmap((int)documentSettingsInfo.DocumentWidth, (int)documentSettingsInfo.DocumentHeight);
  2071. }
  2072. else
  2073. {
  2074. singlePageHeight = (int)(documentSettingsInfo.DocumentWidth / multipleInfo.VerticalPageNumber);
  2075. singlePageWidth = (int)(documentSettingsInfo.DocumentHeight / multipleInfo.HorizontalPageNumber);
  2076. printBitmap = new Bitmap((int)documentSettingsInfo.DocumentHeight, (int)documentSettingsInfo.DocumentWidth);
  2077. }
  2078. System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
  2079. int startPage = PrintSettingsInfo.PageRangeList[paperIndex * (multipleInfo.VerticalPageNumber) * (multipleInfo.HorizontalPageNumber)];
  2080. int resizedHeight = 0;
  2081. int resizedWidth = 0;
  2082. bool isNoSrcPage = false;
  2083. Bitmap pageBitmap;
  2084. ///水平:横向排列,先横后纵,纵轴不变横轴翻转
  2085. if (multipleInfo.EnumPageOrder == EnumPageOrder.StatusHorizontalOrder || multipleInfo.EnumPageOrder == EnumPageOrder.StatusHorizontalReverseOrder)
  2086. {
  2087. for (int tempVerticalIndex = 0; tempVerticalIndex < multipleInfo.VerticalPageNumber; tempVerticalIndex++)
  2088. {
  2089. startPoint.Y = tempVerticalIndex * singlePageHeight;
  2090. for (int tempHorizontalIndex = 0; tempHorizontalIndex < multipleInfo.HorizontalPageNumber; tempHorizontalIndex++)
  2091. {
  2092. try
  2093. {
  2094. currentPage =
  2095. PrintSettingsInfo.PageRangeList[paperIndex *
  2096. (multipleInfo.VerticalPageNumber) *
  2097. (multipleInfo.HorizontalPageNumber) +
  2098. (tempVerticalIndex * multipleInfo.HorizontalPageNumber) +
  2099. tempHorizontalIndex];
  2100. }
  2101. catch
  2102. {
  2103. break;
  2104. }
  2105. //横向
  2106. if (multipleInfo.EnumPageOrder == EnumPageOrder.StatusHorizontalOrder)
  2107. {
  2108. startPoint.X = tempHorizontalIndex * singlePageWidth;
  2109. }
  2110. //横向倒序
  2111. else
  2112. {
  2113. startPoint.X = (multipleInfo.HorizontalPageNumber - tempHorizontalIndex - 1) * singlePageWidth;
  2114. }
  2115. CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(currentPage);
  2116. if (cPDFPage == null)
  2117. {
  2118. isNoSrcPage = true;
  2119. break;
  2120. }
  2121. pageBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)(cPDFPage.PageSize.Width), (int)(cPDFPage.PageSize.Height), currentPage, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  2122. if (multipleInfo.IsAutoRotate)
  2123. {
  2124. if ((cPDFPage.PageSize.Height / cPDFPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
  2125. (cPDFPage.PageSize.Height / cPDFPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
  2126. {
  2127. pageBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)(cPDFPage.PageSize.Width), (int)(cPDFPage.PageSize.Height), currentPage, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  2128. pageBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  2129. }
  2130. }
  2131. ///重设大小
  2132. if ((float)pageBitmap.Height / (float)pageBitmap.Width >= ((float)singlePageHeight / (float)singlePageWidth))
  2133. {
  2134. resizedHeight = (int)(singlePageHeight * 0.96);
  2135. resizedWidth = (int)((float)singlePageHeight / pageBitmap.Height * pageBitmap.Width * 0.96);
  2136. }
  2137. else
  2138. {
  2139. resizedWidth = (int)(singlePageWidth * 0.96);
  2140. resizedHeight = (int)((float)singlePageWidth / pageBitmap.Width * pageBitmap.Height * 0.96);
  2141. }
  2142. pageBitmap = Resize(pageBitmap, (int)resizedWidth, (int)resizedHeight);
  2143. startPoint.X += (singlePageWidth - resizedWidth) / 2;
  2144. startPoint.Y += (singlePageHeight - resizedHeight) / 2;
  2145. if (PrintSettingsInfo.IsGrayscale)
  2146. {
  2147. pageBitmap = ToGray(pageBitmap, 0);
  2148. }
  2149. printBitmap = CombineBitmap(printBitmap, pageBitmap, startPoint);
  2150. //绘制边界
  2151. if (PrintSettingsInfo.IsPrintPageBorde)
  2152. {
  2153. using (Pen pen = new Pen(Color.Black, 4))
  2154. {
  2155. Graphics g = Graphics.FromImage(printBitmap);
  2156. g.DrawRectangle(pen, new Rectangle(startPoint.X, startPoint.Y, resizedWidth, resizedHeight));
  2157. g.Dispose();
  2158. }
  2159. }
  2160. startPoint.Y -= (singlePageHeight - resizedHeight) / 2;
  2161. }
  2162. if (isNoSrcPage)
  2163. {
  2164. isNoSrcPage = false;
  2165. break;
  2166. }
  2167. }
  2168. }
  2169. //垂直:纵向排列,先纵后横,纵轴不变横轴翻转
  2170. else
  2171. {
  2172. for (int tempHorizontalIndex = 0; tempHorizontalIndex < multipleInfo.HorizontalPageNumber; tempHorizontalIndex++)
  2173. {
  2174. //纵向,垂直排列时先移位
  2175. if (multipleInfo.EnumPageOrder == EnumPageOrder.StatusVerticalOrder)
  2176. {
  2177. startPoint.X = tempHorizontalIndex * singlePageWidth;
  2178. }
  2179. //纵向倒序
  2180. else
  2181. {
  2182. startPoint.X = (multipleInfo.HorizontalPageNumber - tempHorizontalIndex - 1) * singlePageWidth;
  2183. }
  2184. for (int tempVerticalIndex = 0; tempVerticalIndex < multipleInfo.VerticalPageNumber; tempVerticalIndex++)
  2185. {
  2186. startPoint.Y = tempVerticalIndex * singlePageHeight;
  2187. try
  2188. {
  2189. currentPage = PrintSettingsInfo.PageRangeList[paperIndex *
  2190. (multipleInfo.VerticalPageNumber) *
  2191. (multipleInfo.HorizontalPageNumber) +
  2192. (tempHorizontalIndex * multipleInfo.VerticalPageNumber) +
  2193. tempVerticalIndex];
  2194. }
  2195. catch
  2196. {
  2197. break;
  2198. }
  2199. CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(currentPage);
  2200. if (cPDFPage == null)
  2201. {
  2202. isNoSrcPage = true;
  2203. break;
  2204. }
  2205. pageBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)(cPDFPage.PageSize.Width), (int)(cPDFPage.PageSize.Height), currentPage, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  2206. if (multipleInfo.IsAutoRotate)
  2207. {
  2208. if ((cPDFPage.PageSize.Height / cPDFPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
  2209. (cPDFPage.PageSize.Height / cPDFPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
  2210. {
  2211. pageBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  2212. }
  2213. }
  2214. Graphics graphics = Graphics.FromImage(pageBitmap);
  2215. ///重设大小
  2216. if ((float)pageBitmap.Height / (float)pageBitmap.Width >= ((float)singlePageHeight / (float)singlePageWidth))
  2217. {
  2218. resizedHeight = (int)(singlePageHeight * 0.96);
  2219. resizedWidth = (int)((float)singlePageHeight / pageBitmap.Height * pageBitmap.Width * 0.96);
  2220. }
  2221. else
  2222. {
  2223. resizedWidth = (int)(singlePageWidth * 0.96);
  2224. resizedHeight = (int)((float)singlePageWidth / pageBitmap.Width * pageBitmap.Height * 0.96);
  2225. }
  2226. pageBitmap = Resize(pageBitmap, (int)resizedWidth, (int)resizedHeight);
  2227. graphics.Dispose();
  2228. startPoint.X += (singlePageWidth - resizedWidth) / 2;
  2229. startPoint.Y += (singlePageHeight - resizedHeight) / 2;
  2230. if (PrintSettingsInfo.IsGrayscale)
  2231. {
  2232. pageBitmap = ToGray(pageBitmap, 0);
  2233. }
  2234. printBitmap = CombineBitmap(printBitmap, pageBitmap, startPoint);
  2235. //绘制边界
  2236. if (PrintSettingsInfo.IsPrintPageBorde)
  2237. {
  2238. using (Pen pen = new Pen(Color.Black, 4))
  2239. {
  2240. Graphics g = Graphics.FromImage(printBitmap);
  2241. g.DrawRectangle(pen, new Rectangle(startPoint.X, startPoint.Y, resizedWidth, resizedHeight));
  2242. g.Dispose();
  2243. }
  2244. }
  2245. startPoint.X -= (singlePageWidth - resizedWidth) / 2;
  2246. }
  2247. if (isNoSrcPage)
  2248. {
  2249. isNoSrcPage = false;
  2250. break;
  2251. }
  2252. }
  2253. }
  2254. WholePaperCountModPoster = PrintedPageCount * HorizonPaperCount * VerticalPaperCount;
  2255. BitmapSource = ToBitmapSource(printBitmap);
  2256. }
  2257. /// <summary>
  2258. /// 小册子模式
  2259. /// </summary>
  2260. /// <param name="paperIndex"></param>
  2261. private async void PreviewModBooklet(int paperIndex)
  2262. {
  2263. BookletInfo bookletInfo = (BookletInfo)PrintSettingsInfo.PrintModInfo;
  2264. ///空白画布
  2265. Bitmap printBitmap;
  2266. double singlePageHeight = 0;
  2267. double singlePageWidth = 0;
  2268. if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
  2269. {
  2270. printBitmap = new Bitmap((int)documentSettingsInfo.DocumentWidth, (int)documentSettingsInfo.DocumentHeight);
  2271. singlePageHeight = (double)documentSettingsInfo.DocumentHeight;
  2272. singlePageWidth = (double)(documentSettingsInfo.DocumentWidth / 2);
  2273. }
  2274. else
  2275. {
  2276. printBitmap = new Bitmap((int)documentSettingsInfo.DocumentHeight, (int)documentSettingsInfo.DocumentWidth);
  2277. singlePageHeight = (double)documentSettingsInfo.DocumentWidth;
  2278. singlePageWidth = (double)(documentSettingsInfo.DocumentHeight / 2);
  2279. }
  2280. System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
  2281. int maxPaperNumber = (PDFViewer.Document.PageCount % 4 == 0) ? (PDFViewer.Document.PageCount / 4) : (PDFViewer.Document.PageCount / 4 + 1);
  2282. Bitmap leftBitmap = null;
  2283. Bitmap rightBitmap = null;
  2284. double resizedHeight = 0;
  2285. double resizedWidth = 0;
  2286. CPDFPage leftPage = null;
  2287. CPDFPage rightPage = null;
  2288. int leftPageIndex = 0;
  2289. int rightPageIndex = 0;
  2290. ///<summary>
  2291. ///左装订与右装订顺序相反
  2292. /// </summary>
  2293. if (bookletInfo.EnumBookletBinding == EnumBookletBinding.StatusLeft)
  2294. {
  2295. ///奇数页:
  2296. ///右面为页面序号
  2297. ///左面为[(总打印纸数*4)+1-打印页右侧页序号]
  2298. if ((paperIndex + 1) % 2 == 1)
  2299. {
  2300. try
  2301. {
  2302. leftPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - paperIndex - 1];
  2303. leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
  2304. }
  2305. catch
  2306. {
  2307. leftPage = null;
  2308. }
  2309. rightPageIndex = PrintSettingsInfo.PageRangeList[paperIndex];
  2310. //左面
  2311. if (leftPage != null)
  2312. {
  2313. leftBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)leftPage.PageSize.Width, (int)leftPage.PageSize.Height, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  2314. if (bookletInfo.IsAutoRotate)
  2315. {
  2316. if ((leftPage.PageSize.Height / leftPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
  2317. (leftPage.PageSize.Height / leftPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
  2318. {
  2319. leftBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  2320. }
  2321. }
  2322. ///重设大小
  2323. if ((leftBitmap.Height / leftBitmap.Width) >= singlePageHeight / singlePageWidth)
  2324. {
  2325. resizedHeight = singlePageHeight * 0.96;
  2326. resizedWidth = singlePageHeight / leftBitmap.Height * leftBitmap.Width * 0.96;
  2327. }
  2328. else
  2329. {
  2330. resizedWidth = singlePageWidth * 0.96;
  2331. resizedHeight = singlePageWidth / leftBitmap.Width * leftBitmap.Height * 0.96;
  2332. }
  2333. leftBitmap = Resize(leftBitmap, (int)(resizedWidth), (int)(resizedHeight));
  2334. startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
  2335. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  2336. if (PrintSettingsInfo.IsGrayscale)
  2337. {
  2338. leftBitmap = ToGray(leftBitmap, 0);
  2339. }
  2340. printBitmap = CombineBitmap(printBitmap, leftBitmap, startPoint);
  2341. }
  2342. //右面
  2343. rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
  2344. startPoint.X = (int)singlePageWidth;
  2345. if (rightPage != null)
  2346. {
  2347. rightBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)rightPage.PageSize.Width, (int)rightPage.PageSize.Height, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  2348. if (bookletInfo.IsAutoRotate)
  2349. {
  2350. if ((rightPage.PageSize.Height / rightPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
  2351. (rightPage.PageSize.Height / rightPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
  2352. {
  2353. rightBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  2354. }
  2355. }
  2356. ///重设大小
  2357. if ((rightBitmap.Height / rightBitmap.Width) >= singlePageHeight / singlePageWidth)
  2358. {
  2359. resizedHeight = singlePageHeight * 0.96;
  2360. resizedWidth = singlePageHeight / rightBitmap.Height * rightBitmap.Width * 0.96;
  2361. }
  2362. else
  2363. {
  2364. resizedWidth = singlePageWidth * 0.96;
  2365. resizedHeight = singlePageWidth / rightBitmap.Width * rightBitmap.Height * 0.96;
  2366. }
  2367. rightBitmap = Resize(rightBitmap, (int)(resizedWidth), (int)(resizedHeight));
  2368. startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
  2369. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  2370. if (PrintSettingsInfo.IsGrayscale)
  2371. {
  2372. rightBitmap = ToGray(rightBitmap, 0);
  2373. }
  2374. printBitmap = CombineBitmap(printBitmap, rightBitmap, startPoint);
  2375. if (PrintSettingsInfo.IsPrintPageBorde)
  2376. {
  2377. using (Pen pen = new Pen(Color.Black, 4))
  2378. {
  2379. Graphics g = Graphics.FromImage(printBitmap);
  2380. g.DrawRectangle(pen, new Rectangle(startPoint.X, startPoint.Y, (int)resizedWidth, (int)resizedHeight));
  2381. g.Dispose();
  2382. }
  2383. }
  2384. }
  2385. }
  2386. /// 偶数页:
  2387. /// 左面为页面序号
  2388. /// 右面为[(总打印纸数*4)+1-打印页右侧页序号]
  2389. else
  2390. {
  2391. try
  2392. {
  2393. leftPageIndex = PrintSettingsInfo.PageRangeList[paperIndex];
  2394. leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
  2395. }
  2396. catch
  2397. {
  2398. leftPage = null;
  2399. }
  2400. //左面
  2401. if (leftPage != null)
  2402. {
  2403. leftBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)leftPage.PageSize.Width, (int)leftPage.PageSize.Height, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  2404. if (bookletInfo.IsAutoRotate)
  2405. {
  2406. if ((leftPage.PageSize.Height / leftPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
  2407. (leftPage.PageSize.Height / leftPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
  2408. {
  2409. leftBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  2410. }
  2411. }
  2412. ///重设大小
  2413. if ((leftBitmap.Height / leftBitmap.Width) >= singlePageHeight / singlePageWidth)
  2414. {
  2415. resizedHeight = singlePageHeight * 0.96;
  2416. resizedWidth = singlePageHeight / leftBitmap.Height * leftBitmap.Width * 0.96;
  2417. }
  2418. else
  2419. {
  2420. resizedWidth = singlePageWidth * 0.96;
  2421. resizedHeight = singlePageWidth / leftBitmap.Width * leftBitmap.Height * 0.96;
  2422. }
  2423. leftBitmap = Resize(leftBitmap, (int)(resizedWidth), (int)(resizedHeight));
  2424. startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
  2425. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  2426. if (PrintSettingsInfo.IsGrayscale)
  2427. {
  2428. leftBitmap = ToGray(leftBitmap, 0);
  2429. }
  2430. printBitmap = CombineBitmap(printBitmap, leftBitmap, startPoint);
  2431. if (PrintSettingsInfo.IsPrintPageBorde)
  2432. {
  2433. using (Pen pen = new Pen(Color.Black, 4))
  2434. {
  2435. Graphics g = Graphics.FromImage(printBitmap);
  2436. g.DrawRectangle(pen, new Rectangle(startPoint.X, startPoint.Y, (int)resizedWidth, (int)resizedHeight));
  2437. g.Dispose();
  2438. }
  2439. }
  2440. }
  2441. //右面
  2442. try
  2443. {
  2444. rightPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - 1 - paperIndex];
  2445. rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
  2446. }
  2447. catch
  2448. {
  2449. rightPage = null;
  2450. }
  2451. startPoint.X = (int)singlePageWidth;
  2452. if (rightPage != null)
  2453. {
  2454. ///重设大小
  2455. rightBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)rightPage.PageSize.Width, (int)rightPage.PageSize.Height, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  2456. if (bookletInfo.IsAutoRotate)
  2457. {
  2458. if ((rightPage.PageSize.Height / rightPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
  2459. (rightPage.PageSize.Height / rightPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
  2460. {
  2461. rightBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  2462. }
  2463. }
  2464. ///重设大小
  2465. if ((rightBitmap.Height / rightBitmap.Width) >= singlePageHeight / singlePageWidth)
  2466. {
  2467. resizedHeight = singlePageHeight * 0.96;
  2468. resizedWidth = singlePageHeight / rightBitmap.Height * rightBitmap.Width * 0.96;
  2469. }
  2470. else
  2471. {
  2472. resizedWidth = singlePageWidth * 0.96;
  2473. resizedHeight = singlePageWidth / rightBitmap.Width * rightBitmap.Height * 0.96;
  2474. }
  2475. rightBitmap = Resize(rightBitmap, (int)(resizedWidth), (int)(resizedHeight));
  2476. startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
  2477. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  2478. if (PrintSettingsInfo.IsGrayscale)
  2479. {
  2480. rightBitmap = ToGray(rightBitmap, 0);
  2481. }
  2482. printBitmap = CombineBitmap(printBitmap, rightBitmap, startPoint);
  2483. if (PrintSettingsInfo.IsPrintPageBorde)
  2484. {
  2485. using (Pen pen = new Pen(Color.Black, 4))
  2486. {
  2487. Graphics g = Graphics.FromImage(printBitmap);
  2488. g.DrawRectangle(pen, new Rectangle(startPoint.X, startPoint.Y, (int)resizedWidth, (int)resizedHeight));
  2489. g.Dispose();
  2490. }
  2491. }
  2492. }
  2493. }
  2494. }
  2495. ///<summary>
  2496. ///右装订打印
  2497. /// </summary>
  2498. if (bookletInfo.EnumBookletBinding == EnumBookletBinding.StatusRight)
  2499. {
  2500. ///奇数页:
  2501. ///右面为页面序号
  2502. ///左面为[(总打印纸数*4)+1-打印页右侧页序号]
  2503. if ((paperIndex + 1) % 2 == 0)
  2504. {
  2505. try
  2506. {
  2507. leftPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - paperIndex - 1];
  2508. leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
  2509. }
  2510. catch
  2511. {
  2512. leftPage = null;
  2513. }
  2514. rightPageIndex = PrintSettingsInfo.PageRangeList[paperIndex];
  2515. //左面
  2516. if (leftPage != null)
  2517. {
  2518. leftBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)leftPage.PageSize.Width, (int)leftPage.PageSize.Height, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  2519. if (bookletInfo.IsAutoRotate)
  2520. {
  2521. if ((leftPage.PageSize.Height / leftPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
  2522. (leftPage.PageSize.Height / leftPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
  2523. {
  2524. leftBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  2525. }
  2526. }
  2527. ///重设大小
  2528. if ((leftBitmap.Height / leftBitmap.Width) >= singlePageHeight / singlePageWidth)
  2529. {
  2530. resizedHeight = singlePageHeight * 0.96;
  2531. resizedWidth = singlePageHeight / leftBitmap.Height * leftBitmap.Width * 0.96;
  2532. }
  2533. else
  2534. {
  2535. resizedWidth = singlePageWidth;
  2536. resizedHeight = singlePageWidth / leftBitmap.Width * leftBitmap.Height;
  2537. }
  2538. leftBitmap = Resize(leftBitmap, (int)(resizedWidth), (int)(resizedHeight));
  2539. startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
  2540. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  2541. if (PrintSettingsInfo.IsGrayscale)
  2542. {
  2543. leftBitmap = ToGray(leftBitmap, 0);
  2544. }
  2545. printBitmap = CombineBitmap(printBitmap, leftBitmap, startPoint);
  2546. if (PrintSettingsInfo.IsPrintPageBorde)
  2547. {
  2548. using (Pen pen = new Pen(Color.Black, 4))
  2549. {
  2550. Graphics g = Graphics.FromImage(printBitmap);
  2551. g.DrawRectangle(pen, new Rectangle(startPoint.X, startPoint.Y, (int)resizedWidth, (int)resizedHeight));
  2552. g.Dispose();
  2553. }
  2554. }
  2555. }
  2556. //右面
  2557. rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
  2558. startPoint.X = (int)singlePageWidth;
  2559. if (rightPage != null)
  2560. {
  2561. ///重设大小
  2562. rightBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)rightPage.PageSize.Width, (int)rightPage.PageSize.Height, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  2563. if (bookletInfo.IsAutoRotate)
  2564. {
  2565. if ((rightPage.PageSize.Height / rightPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
  2566. (rightPage.PageSize.Height / rightPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
  2567. {
  2568. rightBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  2569. }
  2570. }
  2571. ///重设大小
  2572. if ((rightBitmap.Height / rightBitmap.Width) >= singlePageHeight / singlePageWidth)
  2573. {
  2574. resizedHeight = singlePageHeight * 0.96;
  2575. resizedWidth = singlePageHeight / rightBitmap.Height * rightBitmap.Width * 0.96;
  2576. }
  2577. else
  2578. {
  2579. resizedWidth = singlePageWidth * 0.96;
  2580. resizedHeight = singlePageWidth / rightBitmap.Width * rightBitmap.Height * 0.96;
  2581. }
  2582. rightBitmap = Resize(rightBitmap, (int)(resizedWidth), (int)(resizedHeight));
  2583. startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
  2584. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  2585. if (PrintSettingsInfo.IsGrayscale)
  2586. {
  2587. rightBitmap = ToGray(rightBitmap, 0);
  2588. }
  2589. printBitmap = CombineBitmap(printBitmap, rightBitmap, startPoint);
  2590. if (PrintSettingsInfo.IsPrintPageBorde)
  2591. {
  2592. using (Pen pen = new Pen(Color.Black, 4))
  2593. {
  2594. Graphics g = Graphics.FromImage(printBitmap);
  2595. g.DrawRectangle(pen, new Rectangle(startPoint.X, startPoint.Y, (int)resizedWidth, (int)resizedHeight));
  2596. g.Dispose();
  2597. }
  2598. }
  2599. }
  2600. }
  2601. /// 偶数页:
  2602. /// 左面为页面序号
  2603. /// 右面为[(总打印纸数*4)+1-打印页右侧页序号]
  2604. else
  2605. {
  2606. leftPageIndex = PrintSettingsInfo.PageRangeList[paperIndex];
  2607. //左面
  2608. leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
  2609. if (leftPage != null)
  2610. {
  2611. ///重设大小
  2612. leftBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)leftPage.PageSize.Width, (int)leftPage.PageSize.Height, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  2613. if (bookletInfo.IsAutoRotate)
  2614. {
  2615. if ((leftPage.PageSize.Height / leftPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
  2616. (leftPage.PageSize.Height / leftPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
  2617. {
  2618. leftBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  2619. }
  2620. }
  2621. ///重设大小
  2622. if ((leftBitmap.Height / leftBitmap.Width) >= singlePageHeight / singlePageWidth)
  2623. {
  2624. resizedHeight = singlePageHeight * 0.96;
  2625. resizedWidth = singlePageHeight / leftBitmap.Height * leftBitmap.Width * 0.96;
  2626. }
  2627. else
  2628. {
  2629. resizedWidth = singlePageWidth * 0.96;
  2630. resizedHeight = singlePageWidth / leftBitmap.Width * leftBitmap.Height * 0.96;
  2631. }
  2632. leftBitmap = Resize(leftBitmap, (int)(resizedWidth), (int)(resizedHeight)); startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
  2633. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  2634. if (PrintSettingsInfo.IsGrayscale)
  2635. {
  2636. leftBitmap = ToGray(leftBitmap, 0);
  2637. }
  2638. printBitmap = CombineBitmap(printBitmap, leftBitmap, startPoint);
  2639. if (PrintSettingsInfo.IsPrintPageBorde)
  2640. {
  2641. using (Pen pen = new Pen(Color.Black, 4))
  2642. {
  2643. Graphics g = Graphics.FromImage(printBitmap);
  2644. g.DrawRectangle(pen, new Rectangle(startPoint.X, startPoint.Y, (int)resizedWidth, (int)resizedHeight));
  2645. g.Dispose();
  2646. }
  2647. }
  2648. }
  2649. //右面
  2650. try
  2651. {
  2652. rightPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - 1 - paperIndex];
  2653. rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
  2654. }
  2655. catch
  2656. {
  2657. rightPage = null;
  2658. }
  2659. startPoint.X = (int)singlePageWidth;
  2660. if (rightPage != null)
  2661. {
  2662. ///重设大小
  2663. rightBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)rightPage.PageSize.Width, (int)rightPage.PageSize.Height, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  2664. if (bookletInfo.IsAutoRotate)
  2665. {
  2666. if ((rightPage.PageSize.Height / rightPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
  2667. (rightPage.PageSize.Height / rightPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
  2668. {
  2669. rightBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
  2670. }
  2671. }
  2672. ///重设大小
  2673. if ((rightBitmap.Height / rightBitmap.Width) >= singlePageHeight / singlePageWidth)
  2674. {
  2675. resizedHeight = singlePageHeight * 0.96;
  2676. resizedWidth = singlePageHeight / rightBitmap.Height * rightBitmap.Width * 0.96;
  2677. }
  2678. else
  2679. {
  2680. resizedWidth = singlePageWidth * 0.96;
  2681. resizedHeight = singlePageWidth / rightBitmap.Width * rightBitmap.Height * 0.96;
  2682. }
  2683. rightBitmap = Resize(rightBitmap, (int)(resizedWidth), (int)(resizedHeight)); startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
  2684. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  2685. if (PrintSettingsInfo.IsGrayscale)
  2686. {
  2687. rightBitmap = ToGray(rightBitmap, 0);
  2688. }
  2689. printBitmap = CombineBitmap(printBitmap, rightBitmap, startPoint);
  2690. if (PrintSettingsInfo.IsPrintPageBorde)
  2691. {
  2692. using (Pen pen = new Pen(Color.Black, 4))
  2693. {
  2694. Graphics g = Graphics.FromImage(printBitmap);
  2695. g.DrawRectangle(pen, new Rectangle(startPoint.X, startPoint.Y, (int)resizedWidth, (int)resizedHeight));
  2696. g.Dispose();
  2697. }
  2698. }
  2699. }
  2700. }
  2701. }
  2702. BitmapSource = ToBitmapSource(printBitmap);
  2703. }
  2704. #endregion
  2705. /// <summary>
  2706. /// 按照模式绘制指定纸张
  2707. /// </summary>
  2708. /// <param name="TargetPageIndex"></param>
  2709. private void PaintPageByCurrentPreviewIndex(int TargetPaperIndex)
  2710. {
  2711. lock (this)
  2712. {
  2713. if (PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusSize)
  2714. {
  2715. SetPreview();
  2716. PreviewModSize(TargetPaperList[TargetPaperIndex]);
  2717. }
  2718. else if (PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusPoster)
  2719. {
  2720. SetPreview();
  2721. PreviewModPoster(TargetPaperList[TargetPaperIndex]);
  2722. }
  2723. else if (PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusMultiple)
  2724. {
  2725. SetPreview();
  2726. PreviewModMultiple(TargetPaperList[TargetPaperIndex]);
  2727. }
  2728. else if (PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusBooklet)
  2729. {
  2730. SetPreview();
  2731. PreviewModBooklet(TargetPaperList[TargetPaperIndex]);
  2732. }
  2733. }
  2734. }
  2735. /// <summary>
  2736. /// 设置纸张集合
  2737. /// 大小模式:目标纸张和目标纸张页一致
  2738. /// 海报模式:一致
  2739. /// 多页模式: 一致
  2740. /// 小册子模式:不一致
  2741. /// 第a页-第b页:从第1页开始数,a页=(a-1)*2, b页=(b-1)*2
  2742. /// 面向:奇数页:paper%2 = 1
  2743. /// 背向:偶数页:Paper%2 = 0;
  2744. /// </summary>
  2745. public void SetPaperCollection()
  2746. {
  2747. if (PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusSize ||
  2748. PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusPoster ||
  2749. PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusMultiple)
  2750. {
  2751. TargetPaperList.Clear();
  2752. TargetPaperList.Add(0);
  2753. for (int temp = 0; temp < PrintSettingsInfo.PageRangeList.Count; temp++)
  2754. {
  2755. TargetPaperList.Add(temp);
  2756. }
  2757. }
  2758. else
  2759. {
  2760. BookletInfo bookletInfo = (BookletInfo)PrintSettingsInfo.PrintModInfo;
  2761. if (bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusBothSides)
  2762. {
  2763. TargetPaperList.Clear();
  2764. TargetPaperList.Add(0);
  2765. for (int temp = bookletInfo.BeginPaperIndex * 2 - 2; temp < bookletInfo.EndPaperIndex * 2; temp++)
  2766. {
  2767. TargetPaperList.Add(temp);
  2768. }
  2769. }
  2770. else if (bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusFrontSideOnly)
  2771. {
  2772. TargetPaperList.Clear();
  2773. TargetPaperList.Add(0);
  2774. for (int temp = bookletInfo.BeginPaperIndex * 2 - 2; temp < bookletInfo.EndPaperIndex * 2; temp += 2)
  2775. {
  2776. TargetPaperList.Add(temp);
  2777. }
  2778. }
  2779. else
  2780. {
  2781. TargetPaperList.Clear();
  2782. TargetPaperList.Add(0);
  2783. for (int temp = bookletInfo.BeginPaperIndex * 2 - 1; temp < bookletInfo.EndPaperIndex * 2; temp += 2)
  2784. {
  2785. TargetPaperList.Add(temp);
  2786. }
  2787. }
  2788. }
  2789. }
  2790. public bool IsNavigationTarget(NavigationContext navigationContext)
  2791. {
  2792. return true;
  2793. }
  2794. public void OnNavigatedFrom(NavigationContext navigationContext)
  2795. {
  2796. this.printDocument.Dispose();
  2797. }
  2798. public void OnNavigatedTo(NavigationContext navigationContext)
  2799. {
  2800. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  2801. navigationContext.Parameters.TryGetValue<PrintSettingsInfo>(ParameterNames.PrintSettingsInfo, out PrintSettingsInfo);
  2802. navigationContext.Parameters.TryGetValue<string>("Unicode", out Unicode);
  2803. if (PDFViewer != null && PrintSettingsInfo != null)
  2804. {
  2805. documentSettingsInfo.DocumentWidth = PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize.Width;
  2806. documentSettingsInfo.DocumentHeight = PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize.Height;
  2807. documentSettingsInfo.margins = PrintSettingsInfo.PrintDocument.DefaultPageSettings.Margins;
  2808. documentSettingsInfo.DocumentBounds = PrintSettingsInfo.PrintDocument.DefaultPageSettings.Bounds;
  2809. SetPreview();
  2810. CaculatePrintedPageCount();
  2811. SetPaperCollection();
  2812. PaintPageByCurrentPreviewIndex(1);
  2813. }
  2814. }
  2815. }
  2816. }