HomePagePrinterDocumentContentViewModel.cs 147 KB

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