HomePagePrinterDocumentContentViewModel.cs 148 KB

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