HomePagePrinterDocumentContentViewModel.cs 146 KB

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