HomePagePrinterDocumentContentViewModel.cs 147 KB

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