HomePagePrinterDocumentContentViewModel.cs 140 KB

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