HomePagePrinterDocumentContentViewModel.cs 148 KB

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