HomePagePrinterDocumentContentViewModel.cs 146 KB

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