HomePagePrinterDocumentContentViewModel.cs 148 KB

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