HomePagePrinterDocumentContentViewModel.cs 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155
  1. using ComPDFKit.PDFPage;
  2. using ComPDFKitViewer.PdfViewer;
  3. using ImageMagick.Formats;
  4. using ImTools;
  5. using PDF_Office.EventAggregators;
  6. using PDF_Office.Helper;
  7. using PDF_Office.Model;
  8. using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePagePrinter;
  9. using Prism.Commands;
  10. using Prism.Events;
  11. using Prism.Mvvm;
  12. using Prism.Regions;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Drawing;
  16. using System.Drawing.Imaging;
  17. using System.Drawing.Printing;
  18. using System.Linq;
  19. using System.Printing;
  20. using System.Reflection;
  21. using System.Threading.Tasks;
  22. using System.Windows;
  23. using System.Windows.Annotations;
  24. using System.Windows.Controls;
  25. using System.Windows.Forms;
  26. using System.Windows.Media.Imaging;
  27. using System.Windows.Shell;
  28. using static Dropbox.Api.TeamLog.PaperDownloadFormat;
  29. namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter
  30. {
  31. public class HomePagePrinterDocumentContentViewModel : BindableBase, INavigationAware
  32. {
  33. private CPDFViewer PDFViewer;
  34. private PrintQueue documentPrintQueue;
  35. private PrintDocument printDocument = new PrintDocument();
  36. public PrintSettingsInfo PrintSettingsInfo;
  37. public IEventAggregator printDocumentEvent;
  38. private string Unicode = null;
  39. /// <summary>
  40. /// 海报模式:打印纸
  41. /// </summary>
  42. int posterPrintPaperIndex = 0;
  43. /// <summary>
  44. /// 海报模式,水平纸张数
  45. /// </summary>
  46. public int HorizonPaperCount = 0;
  47. /// <summary>
  48. /// 海报模式,垂直纸张数
  49. /// </summary>
  50. public int VerticalPaperCount = 0;
  51. ///<summary>
  52. ///海报模式当前水平纸
  53. /// </summary>
  54. public int HorizonPaperIndex = 0;
  55. ///<summary>
  56. ///海报模式当前垂直纸
  57. /// </summary>
  58. public int VerticalPaperIndex = 0;
  59. /// <summary>
  60. /// 海报模式,总打印纸张数
  61. /// </summary>
  62. public int WholePaperCountModPoster = 0;
  63. public double PosterRatio = 0;
  64. public int PrintIndex;
  65. ///<summary>
  66. ///打印纸dpi96, PDF dpi 72,需要缩放
  67. /// </summary>
  68. public double dpiRatio = 96.0 / 72.0;
  69. Bitmap blankPageBitmap;
  70. private string _paperWidth;
  71. public string PaperWidth
  72. {
  73. get => _paperWidth;
  74. set => SetProperty(ref _paperWidth, value);
  75. }
  76. private string _paperHeight;
  77. public string PaperHeight
  78. {
  79. get => _paperHeight;
  80. set => SetProperty(ref _paperHeight, value);
  81. }
  82. private string _viewBoxWidth;
  83. public string ViewBoxWidth
  84. {
  85. get => _viewBoxWidth;
  86. set => SetProperty(ref _viewBoxWidth, value);
  87. }
  88. private string _viewBoxHeight;
  89. public string ViewBoxHeight
  90. {
  91. get => _viewBoxHeight;
  92. set => SetProperty(ref _viewBoxHeight, value);
  93. }
  94. private BitmapSource _bitmapSource;
  95. /// <summary>
  96. /// 渲染后的图
  97. /// </summary>
  98. public BitmapSource BitmapSource
  99. {
  100. get { return _bitmapSource; }
  101. set { SetProperty(ref _bitmapSource, value); }
  102. }
  103. private int _targetPaperIndex = 1;
  104. public int TargetPaperIndex
  105. {
  106. get => _targetPaperIndex;
  107. set
  108. {
  109. SetProperty(ref _targetPaperIndex, value);
  110. JumpToSelectedPage();
  111. }
  112. }
  113. /// <summary>
  114. /// 目标纸张
  115. /// TargetPaper[纸张编号]=正确的纸张
  116. /// </summary>
  117. public List<int> TargetPaperList = new List<int>();
  118. /// <summary>
  119. /// 打印的总页数
  120. /// </summary>
  121. private int _printedPageCount;
  122. public int PrintedPageCount
  123. {
  124. get => _printedPageCount;
  125. set => SetProperty(ref _printedPageCount, value);
  126. }
  127. public DelegateCommand<object> TurnPageCommand { get; set; }
  128. public DelegateCommand<object> JumpPageCommand { get; set; }
  129. public HomePagePrinterDocumentContentViewModel(IEventAggregator eventAggregator)
  130. {
  131. this.printDocumentEvent = eventAggregator;
  132. printDocumentEvent.GetEvent<SendPrintSettingsInfoEvent>().Subscribe(RecvPrintSettingsInfo, e => e.Unicode == Unicode);
  133. printDocumentEvent.GetEvent<SendPrintQueueEvent>().Subscribe(RecvPrintQueue, e => e.Unicode == Unicode);
  134. TurnPageCommand = new DelegateCommand<object>(TurnPage);
  135. JumpPageCommand = new DelegateCommand<object>(JumpPage);
  136. }
  137. public void TurnPage(object e)
  138. {
  139. var btn = e as System.Windows.Controls.Button;
  140. if (btn != null)
  141. {
  142. if (btn.Name == "PrePageBtn")
  143. {
  144. if (TargetPaperIndex >= 2)
  145. {
  146. TargetPaperIndex--;
  147. }
  148. }
  149. else
  150. {
  151. if (TargetPaperIndex < PrintedPageCount)
  152. {
  153. TargetPaperIndex++;
  154. }
  155. }
  156. }
  157. }
  158. public async void RecvPrintQueue(PrintQueueWithUnicode printQueueWithUnicode)
  159. {
  160. if (printQueueWithUnicode.printQueue != null)
  161. {
  162. PrintIndex = 0;
  163. this.documentPrintQueue = printQueueWithUnicode.printQueue;
  164. var printDocument = new PrintDocument();
  165. var printTicket = new PrintTicket();
  166. printDocument.PrinterSettings.PrinterName = printQueueWithUnicode.printQueue.Name;
  167. //TODO 这里要改
  168. if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
  169. {
  170. printDocument.DefaultPageSettings.Landscape = false;
  171. }
  172. else if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusLandscape)
  173. {
  174. printDocument.DefaultPageSettings.Landscape = true;
  175. }
  176. if (!PrintSettingsInfo.IsGrayscale && printQueueWithUnicode.printQueue.GetPrintCapabilities().OutputColorCapability.Contains(OutputColor.Color))
  177. {
  178. printDocument.DefaultPageSettings.Color = true;
  179. }
  180. else
  181. {
  182. printDocument.DefaultPageSettings.Color = false;
  183. }
  184. if (PrintSettingsInfo.IsDuplex || PrintSettingsInfo.printModInfo.EnumPrintMod == EnumPrintMod.StatusBooklet)
  185. {
  186. if (printQueueWithUnicode.printQueue.GetPrintCapabilities().DuplexingCapability.Contains(Duplexing.TwoSidedLongEdge))
  187. {
  188. printDocument.PrinterSettings.Duplex = Duplex.Horizontal;
  189. }
  190. else if (printQueueWithUnicode.printQueue.GetPrintCapabilities().DuplexingCapability.Contains(Duplexing.TwoSidedShortEdge))
  191. {
  192. printDocument.PrinterSettings.Duplex = Duplex.Vertical;
  193. }
  194. else
  195. {
  196. printTicket.Duplexing = Duplexing.OneSided;
  197. //TODO 手动双面打印
  198. }
  199. }
  200. else
  201. {
  202. printTicket.Duplexing = Duplexing.OneSided;
  203. }
  204. printQueueWithUnicode.printQueue.DefaultPrintTicket = printTicket;
  205. printDocument.DefaultPageSettings.PaperSize = new PaperSize(PrintSettingsInfo.PageMediaSize.PageMediaSizeName.ToString(), (int)(PrintSettingsInfo.PageMediaSize.Width * 100 / 96), (int)(PrintSettingsInfo.PageMediaSize.Height * 100 / 96));
  206. posterPrintPaperIndex = 0;
  207. printDocument.PrintPage += PrintDocumentByCurrentSettings;
  208. await Task.Run(() =>
  209. {
  210. printDocument.Print();
  211. });
  212. }
  213. }
  214. private Bitmap Crop(Bitmap src, Rectangle rectangle)
  215. {
  216. Bitmap targetBitmap = new Bitmap(rectangle.Width, rectangle.Height);
  217. using (Graphics g = Graphics.FromImage(targetBitmap))
  218. {
  219. g.DrawImage(src, new Rectangle(0, 0, targetBitmap.Width, targetBitmap.Height), rectangle, GraphicsUnit.Pixel);
  220. }
  221. return targetBitmap;
  222. }
  223. private void PrintDocumentModSize(PrintPageEventArgs e)
  224. {
  225. SizeInfo sizeInfo = (SizeInfo)PrintSettingsInfo.printModInfo;
  226. CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(PrintSettingsInfo.PageRangeList[PrintIndex]);
  227. System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
  228. if (cPDFPage != null)
  229. {
  230. Bitmap printBitmap = new Bitmap((int)(float.Parse(PaperWidth)), (int)float.Parse(PaperHeight));
  231. Bitmap bitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)(cPDFPage.PageSize.Width * dpiRatio), (int)(cPDFPage.PageSize.Height * dpiRatio), PrintSettingsInfo.PageRangeList[PrintIndex], PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  232. if (PrintSettingsInfo.IsGrayscale)
  233. {
  234. bitmap = ToGray(bitmap, 0);
  235. }
  236. ///<summary>
  237. ///自适应模式
  238. /// </summary>
  239. if (sizeInfo.EnumSizeType == EnumSizeType.StatusAdaptive)
  240. {
  241. double mmToDpiRatio = 96 / 25.4;
  242. int resizedHeight = 0;
  243. int resizedWidth = 0;
  244. ///重设大小
  245. if ((float)bitmap.Height / (float)bitmap.Width >= (float.Parse(PaperHeight) / float.Parse(PaperWidth)))
  246. {
  247. resizedHeight = (int)(float.Parse(PaperHeight) * mmToDpiRatio);
  248. resizedWidth = (int)((float.Parse(PaperHeight) / bitmap.Height * bitmap.Width) * mmToDpiRatio);
  249. }
  250. else
  251. {
  252. resizedWidth = (int)(float.Parse(PaperWidth) * mmToDpiRatio);
  253. resizedHeight = (int)((float.Parse(PaperWidth) / bitmap.Width * bitmap.Height) * mmToDpiRatio);
  254. }
  255. bitmap = Resize(bitmap, resizedWidth, resizedHeight);
  256. startPoint.X = (blankPageBitmap.Width - resizedWidth) / 2;
  257. startPoint.Y = (blankPageBitmap.Height - resizedHeight) / 2;
  258. printBitmap = CombineBitmap(blankPageBitmap, bitmap, startPoint);
  259. e.Graphics.DrawImage(printBitmap, e.PageBounds);
  260. }
  261. else if (sizeInfo.EnumSizeType == EnumSizeType.StatusActural)
  262. {
  263. startPoint.X = (blankPageBitmap.Width - bitmap.Width) / 2;
  264. startPoint.Y = (blankPageBitmap.Height - bitmap.Height) / 2;
  265. printBitmap = CombineBitmap(blankPageBitmap, bitmap, startPoint);
  266. e.Graphics.DrawImage(printBitmap, e.PageBounds);
  267. }
  268. else if (sizeInfo.EnumSizeType == EnumSizeType.StatusCustomized)
  269. {
  270. bitmap = Resize(bitmap, (int)(bitmap.Width * (sizeInfo.DisplayRatio / 100.0)), (int)(bitmap.Height * (sizeInfo.DisplayRatio / 100.0)));
  271. startPoint.X = (blankPageBitmap.Width - bitmap.Width) / 2;
  272. startPoint.Y = (blankPageBitmap.Height - bitmap.Height) / 2;
  273. printBitmap = CombineBitmap(blankPageBitmap, bitmap, startPoint);
  274. e.Graphics.DrawImage(printBitmap, e.PageBounds);
  275. }
  276. bitmap.Dispose();
  277. printBitmap.Dispose();
  278. }
  279. if (PrintIndex < PrintedPageCount - 1)
  280. {
  281. PrintIndex++;
  282. e.HasMorePages = true;
  283. }
  284. else
  285. {
  286. e.HasMorePages = false;
  287. }
  288. }
  289. private void PrintDocumentModPoster(PrintPageEventArgs e)
  290. {
  291. PosterInfo posterInfo = (PosterInfo)PrintSettingsInfo.printModInfo;
  292. var pagebound = printDocument.DefaultPageSettings.Bounds;
  293. System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
  294. double effectiveHeight = (double)PrintSettingsInfo.PageMediaSize.Height;
  295. double effectiveWidth = (double)PrintSettingsInfo.PageMediaSize.Width;
  296. double PreviewHeight = 0;
  297. double PreviewWidth = 0;
  298. Bitmap bitmap = null;
  299. Bitmap displayBitmap = null;
  300. effectiveHeight = effectiveHeight - 2 * posterInfo.OverLap * 0.5 * 96 / 25.4;
  301. effectiveWidth = effectiveWidth - 2 * posterInfo.OverLap * 0.5 * 96 / 25.4;
  302. if (posterInfo.HasLabel || posterInfo.HasCutMarks)
  303. {
  304. effectiveHeight = effectiveHeight - 0.5 * 96 / 25.4;
  305. effectiveWidth = effectiveWidth - 0.5 * 96 / 25.4;
  306. }
  307. ///无论是拆分还是平铺,页面缩放比例都会在预览中完成
  308. ///先渲染整图
  309. ///得到effective域大小
  310. ///根据effective的量和重叠量计算开始的bitmap
  311. ///加字和标记
  312. ///字加在顶部,标记加在四角
  313. ///结束后翻页
  314. ///对于Tile页数不定,但是比例相等
  315. ///对于split页数固定,但是比例变化
  316. ///平铺模式:
  317. ///从第一页开始,先计算当前页总纸张数
  318. ///根据纸张数打印
  319. ///打印到位时页数进一
  320. ///页数到顶,纸数到顶打印结束
  321. if (posterInfo.EnumPosterMod == EnumPosterMod.StatusTile)
  322. {
  323. CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(PrintSettingsInfo.PageRangeList[posterPrintPaperIndex]);
  324. if (cPDFPage != null)
  325. {
  326. bitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)(cPDFPage.PageSize.Width * dpiRatio * 100 / 96 * (posterInfo.TileRatio / 100.0)), (int)(cPDFPage.PageSize.Height * dpiRatio * 100 / 96 * (posterInfo.TileRatio / 100.0)), PrintSettingsInfo.PageRangeList[posterPrintPaperIndex], PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  327. HorizonPaperCount = (int)Math.Ceiling((double)bitmap.Width / effectiveWidth);
  328. VerticalPaperCount = (int)Math.Ceiling(((double)bitmap.Height / effectiveHeight));
  329. int wholeCountSinglePage = HorizonPaperCount * VerticalPaperCount;//每一轮的总页数
  330. PreviewWidth = (double)(effectiveWidth * HorizonPaperCount);
  331. PreviewHeight = (double)(effectiveHeight * VerticalPaperCount);
  332. displayBitmap = new Bitmap((int)PreviewWidth, (int)PreviewHeight);
  333. startPoint.X = (int)((PreviewWidth - bitmap.Width) / 2);
  334. startPoint.Y = (int)((PreviewHeight - bitmap.Height) / 2);
  335. if (PrintSettingsInfo.IsGrayscale)
  336. {
  337. bitmap = ToGray(bitmap, 0);
  338. }
  339. //取到和渲染一样的Bitmap
  340. displayBitmap = CombineBitmap(displayBitmap, bitmap, startPoint);
  341. ///当前页未打印完
  342. if (HorizonPaperIndex * VerticalPaperIndex < wholeCountSinglePage - 1)
  343. {
  344. ///取有效尺寸和重叠,边沿合并
  345. ///有效尺寸+重叠边
  346. Rectangle rectangle = new Rectangle((int)(HorizonPaperIndex * effectiveHeight), (int)(VerticalPaperIndex * effectiveHeight), (int)((HorizonPaperIndex + 1) * effectiveWidth), (int)((VerticalPaperIndex + 1) * effectiveHeight));
  347. ///左重叠
  348. if (HorizonPaperIndex != 0)
  349. {
  350. rectangle.Location = new System.Drawing.Point((int)(rectangle.Location.X - posterInfo.OverLap * 96 / 24.5), rectangle.Location.Y);
  351. }
  352. ///上重叠
  353. if (VerticalPaperIndex != 0)
  354. {
  355. rectangle.Size = new System.Drawing.Size(rectangle.Size.Width, (int)(rectangle.Size.Height + posterInfo.OverLap * 96 / 24.5));
  356. }
  357. ///右重叠
  358. if (HorizonPaperIndex != HorizonPaperCount)
  359. {
  360. rectangle.Size = new System.Drawing.Size((int)(rectangle.Size.Width + posterInfo.OverLap * 96 / 24.5), rectangle.Size.Height);
  361. }
  362. ///下重叠
  363. if (VerticalPaperIndex != VerticalPaperCount)
  364. {
  365. rectangle.Location = new System.Drawing.Point(rectangle.Location.X, (int)(rectangle.Location.Y - posterInfo.OverLap * 96 / 24.5));
  366. }
  367. Bitmap currentPrintbitmap = Crop(displayBitmap, rectangle);
  368. ///TODO暂时不管,先画
  369. e.Graphics.DrawImage(currentPrintbitmap, e.PageBounds);
  370. if (HorizonPaperIndex < HorizonPaperCount - 1)
  371. {
  372. HorizonPaperIndex++;
  373. e.HasMorePages = true;
  374. }
  375. else if (VerticalPaperIndex < VerticalPaperCount - 1)
  376. {
  377. HorizonPaperIndex = 0;
  378. VerticalPaperIndex++;
  379. e.HasMorePages = true;
  380. }
  381. else if (posterPrintPaperIndex < PrintedPageCount - 1)
  382. {
  383. ///当前总文件未打印完
  384. posterPrintPaperIndex++;
  385. HorizonPaperIndex = 0;
  386. VerticalPaperIndex = 0;
  387. e.HasMorePages = true;
  388. }
  389. else
  390. {
  391. e.HasMorePages = false;
  392. }
  393. }
  394. }
  395. }
  396. ///拆分模式
  397. ///该模式下
  398. ///渲染图片到指定大小
  399. ///直接计算所有需要打印的纸张数
  400. ///计算单页打印的纸张数
  401. ///进位
  402. ///根据当前水平垂直序号裁切图片
  403. ///继续打印表征:当前纸张序号小于总计纸张序号
  404. else
  405. {
  406. int wholeCountSinglePage = posterInfo.HorizontalSheetNumber * posterInfo.VerticalSheetNumber;//每一轮的总页数
  407. WholePaperCountModPoster = wholeCountSinglePage * PrintedPageCount;//总计打印纸数
  408. int PrintPageIndex = PrintIndex / wholeCountSinglePage;//当前打印纸张所在海报页 从0开始
  409. int PrintIndexInCurrentPage = PrintIndex % wholeCountSinglePage;//当前海报页中,打印纸的序号 从0开始
  410. HorizonPaperIndex = PrintIndexInCurrentPage % HorizonPaperCount;//行中的第几个
  411. VerticalPaperIndex = PrintIndexInCurrentPage / HorizonPaperCount;//第几行
  412. CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(PrintSettingsInfo.PageRangeList[PrintPageIndex]);//取得当前海报页
  413. PreviewWidth = (double)(effectiveWidth * HorizonPaperCount);
  414. PreviewHeight = (double)(effectiveHeight * VerticalPaperCount);
  415. displayBitmap = new Bitmap((int)PreviewWidth, (int)PreviewHeight);
  416. double resizedHeight = 0;
  417. double resizedWidth = 0;
  418. if ((cPDFPage.PageSize.Height / cPDFPage.PageSize.Width) >= PreviewHeight / PreviewWidth)
  419. {
  420. resizedHeight = PreviewHeight;
  421. resizedWidth = PreviewHeight / cPDFPage.PageSize.Height * cPDFPage.PageSize.Width;
  422. }
  423. else
  424. {
  425. resizedWidth = PreviewWidth;
  426. resizedHeight = PreviewWidth / cPDFPage.PageSize.Width * cPDFPage.PageSize.Height;
  427. }
  428. bitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, PrintSettingsInfo.PageRangeList[PrintPageIndex], PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  429. startPoint.X = (int)((PreviewWidth - bitmap.Width) / 2);
  430. startPoint.Y = (int)((PreviewHeight - bitmap.Height) / 2);
  431. if (PrintSettingsInfo.IsGrayscale)
  432. {
  433. bitmap = ToGray(bitmap, 0);
  434. }
  435. displayBitmap = CombineBitmap(displayBitmap, bitmap, startPoint);
  436. displayBitmap = CombineBitmap(displayBitmap, bitmap, startPoint);
  437. ///取有效尺寸和重叠,边沿合并
  438. ///有效尺寸+重叠边
  439. Rectangle rectangle = new Rectangle((int)(HorizonPaperIndex * effectiveHeight), (int)(VerticalPaperIndex * effectiveHeight), (int)((HorizonPaperIndex + 1) * effectiveWidth), (int)((VerticalPaperIndex + 1) * effectiveHeight));
  440. ///左重叠
  441. if (HorizonPaperIndex != 0)
  442. {
  443. rectangle.Location = new System.Drawing.Point((int)(rectangle.Location.X - posterInfo.OverLap * 96 / 24.5), rectangle.Location.Y);
  444. }
  445. ///上重叠
  446. if (VerticalPaperIndex != 0)
  447. {
  448. rectangle.Size = new System.Drawing.Size(rectangle.Size.Width, (int)(rectangle.Size.Height + posterInfo.OverLap * 96 / 24.5));
  449. }
  450. ///右重叠
  451. if (HorizonPaperIndex != HorizonPaperCount)
  452. {
  453. rectangle.Size = new System.Drawing.Size((int)(rectangle.Size.Width + posterInfo.OverLap * 96 / 24.5), rectangle.Size.Height);
  454. }
  455. ///下重叠
  456. if (VerticalPaperIndex != VerticalPaperCount)
  457. {
  458. rectangle.Location = new System.Drawing.Point(rectangle.Location.X, (int)(rectangle.Location.Y - posterInfo.OverLap * 96 / 24.5));
  459. }
  460. Bitmap currentPrintbitmap = Crop(displayBitmap, rectangle);
  461. ///TODO暂时不管,先画
  462. e.Graphics.DrawImage(currentPrintbitmap, e.PageBounds);
  463. if (PrintIndex < WholePaperCountModPoster - 1)
  464. {
  465. PrintIndex++;
  466. e.HasMorePages = true;
  467. }
  468. else
  469. {
  470. e.HasMorePages = false;
  471. }
  472. }
  473. }
  474. private void PrintDocumentModMultiple(PrintPageEventArgs e)
  475. {
  476. MultipleInfo multipleInfo = (MultipleInfo)PrintSettingsInfo.printModInfo;
  477. int singlePageHeight;
  478. int singlePageWidth;
  479. Bitmap printBitmap;
  480. int currentPage = 0;
  481. if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
  482. {
  483. singlePageHeight = (int)(PrintSettingsInfo.PageMediaSize.Height / multipleInfo.VerticalPageNumber);
  484. singlePageWidth = (int)(PrintSettingsInfo.PageMediaSize.Width / multipleInfo.HorizontalPageNumber);
  485. printBitmap = new Bitmap((int)PrintSettingsInfo.PageMediaSize.Width, (int)PrintSettingsInfo.PageMediaSize.Height);
  486. }
  487. else
  488. {
  489. singlePageHeight = (int)(PrintSettingsInfo.PageMediaSize.Width / multipleInfo.VerticalPageNumber);
  490. singlePageWidth = (int)(PrintSettingsInfo.PageMediaSize.Height / multipleInfo.HorizontalPageNumber);
  491. printBitmap = new Bitmap((int)PrintSettingsInfo.PageMediaSize.Height, (int)PrintSettingsInfo.PageMediaSize.Width);
  492. }
  493. System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
  494. int startPage = PrintSettingsInfo.PageRangeList[PrintIndex * (multipleInfo.VerticalPageNumber) * (multipleInfo.HorizontalPageNumber)];
  495. int resizedHeight = 0;
  496. int resizedWidth = 0;
  497. bool isNoSrcPage = false;
  498. Bitmap pageBitmap;
  499. ///水平:横向排列,先横后纵,纵轴不变横轴翻转
  500. if (multipleInfo.EnumPageOrder == EnumPageOrder.StatusHorizontalOrder || multipleInfo.EnumPageOrder == EnumPageOrder.StatusHorizontalReverseOrder)
  501. {
  502. for (int tempVerticalIndex = 0; tempVerticalIndex < multipleInfo.VerticalPageNumber; tempVerticalIndex++)
  503. {
  504. startPoint.Y = tempVerticalIndex * singlePageHeight;
  505. for (int tempHorizontalIndex = 0; tempHorizontalIndex < multipleInfo.HorizontalPageNumber; tempHorizontalIndex++)
  506. {
  507. try
  508. {
  509. currentPage =
  510. PrintSettingsInfo.PageRangeList[PrintIndex *
  511. (multipleInfo.VerticalPageNumber) *
  512. (multipleInfo.HorizontalPageNumber) +
  513. (tempVerticalIndex * multipleInfo.HorizontalPageNumber) +
  514. tempHorizontalIndex];
  515. }
  516. catch
  517. {
  518. break;
  519. }
  520. //横向
  521. if (multipleInfo.EnumPageOrder == EnumPageOrder.StatusHorizontalOrder)
  522. {
  523. startPoint.X = tempHorizontalIndex * singlePageWidth;
  524. }
  525. //横向倒序
  526. else
  527. {
  528. startPoint.X = (multipleInfo.HorizontalPageNumber - tempHorizontalIndex - 1) * singlePageWidth;
  529. }
  530. CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(currentPage);
  531. if (cPDFPage == null)
  532. {
  533. isNoSrcPage = true;
  534. break;
  535. }
  536. pageBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)(cPDFPage.PageSize.Width), (int)(cPDFPage.PageSize.Height), currentPage, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  537. ///重设大小
  538. if ((float)pageBitmap.Height / (float)pageBitmap.Width >= ((float)singlePageHeight / (float)singlePageWidth))
  539. {
  540. resizedHeight = (int)(singlePageHeight * 0.96);
  541. resizedWidth = (int)((float)singlePageHeight / pageBitmap.Height * pageBitmap.Width * 0.96);
  542. }
  543. else
  544. {
  545. resizedWidth = (int)(singlePageWidth * 0.96);
  546. resizedHeight = (int)((float)singlePageWidth / pageBitmap.Width * pageBitmap.Height * 0.96);
  547. }
  548. pageBitmap = Resize(pageBitmap, (int)resizedWidth, (int)resizedHeight);
  549. startPoint.X += (singlePageWidth - resizedWidth) / 2;
  550. startPoint.Y += (singlePageHeight - resizedHeight) / 2;
  551. if (PrintSettingsInfo.IsGrayscale)
  552. {
  553. pageBitmap = ToGray(pageBitmap, 0);
  554. }
  555. printBitmap = CombineBitmap(printBitmap, pageBitmap, startPoint);
  556. //绘制边界
  557. if (PrintSettingsInfo.IsPrintPageBorde)
  558. {
  559. using (Pen pen = new Pen(Color.Black, 4))
  560. {
  561. Graphics g = Graphics.FromImage(printBitmap);
  562. g.DrawRectangle(pen, new Rectangle(startPoint.X, startPoint.Y, resizedWidth, resizedHeight));
  563. g.Dispose();
  564. }
  565. }
  566. startPoint.Y -= (singlePageHeight - resizedHeight) / 2;
  567. }
  568. if (isNoSrcPage)
  569. {
  570. isNoSrcPage = false;
  571. break;
  572. }
  573. }
  574. }
  575. //垂直:纵向排列,先纵后横,纵轴不变横轴翻转
  576. else
  577. {
  578. for (int tempHorizontalIndex = 0; tempHorizontalIndex < multipleInfo.HorizontalPageNumber; tempHorizontalIndex++)
  579. {
  580. //纵向,垂直排列时先移位
  581. if (multipleInfo.EnumPageOrder == EnumPageOrder.StatusVerticalOrder)
  582. {
  583. startPoint.X = tempHorizontalIndex * singlePageWidth;
  584. }
  585. //纵向倒序
  586. else
  587. {
  588. startPoint.X = (multipleInfo.HorizontalPageNumber - tempHorizontalIndex - 1) * singlePageWidth;
  589. }
  590. for (int tempVerticalIndex = 0; tempVerticalIndex < multipleInfo.VerticalPageNumber; tempVerticalIndex++)
  591. {
  592. startPoint.Y = tempVerticalIndex * singlePageHeight;
  593. try
  594. {
  595. currentPage = PrintSettingsInfo.PageRangeList[PrintIndex *
  596. (multipleInfo.VerticalPageNumber) *
  597. (multipleInfo.HorizontalPageNumber) +
  598. (tempHorizontalIndex * multipleInfo.VerticalPageNumber) +
  599. tempVerticalIndex];
  600. }
  601. catch
  602. {
  603. break;
  604. }
  605. CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(currentPage);
  606. if (cPDFPage == null)
  607. {
  608. isNoSrcPage = true;
  609. break;
  610. }
  611. pageBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)(cPDFPage.PageSize.Width), (int)(cPDFPage.PageSize.Height), currentPage, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  612. Graphics graphics = Graphics.FromImage(pageBitmap);
  613. ///重设大小
  614. if ((float)pageBitmap.Height / (float)pageBitmap.Width >= ((float)singlePageHeight / (float)singlePageWidth))
  615. {
  616. resizedHeight = (int)(singlePageHeight * 0.96);
  617. resizedWidth = (int)((float)singlePageHeight / pageBitmap.Height * pageBitmap.Width * 0.96);
  618. }
  619. else
  620. {
  621. resizedWidth = (int)(singlePageWidth * 0.96);
  622. resizedHeight = (int)((float)singlePageWidth / pageBitmap.Width * pageBitmap.Height * 0.96);
  623. }
  624. pageBitmap = Resize(pageBitmap, (int)resizedWidth, (int)resizedHeight);
  625. graphics.Dispose();
  626. startPoint.X += (singlePageWidth - resizedWidth) / 2;
  627. startPoint.Y += (singlePageHeight - resizedHeight) / 2;
  628. if (PrintSettingsInfo.IsGrayscale)
  629. {
  630. pageBitmap = ToGray(pageBitmap, 0);
  631. }
  632. printBitmap = CombineBitmap(printBitmap, pageBitmap, startPoint);
  633. //绘制边界
  634. if (PrintSettingsInfo.IsPrintPageBorde)
  635. {
  636. using (Pen pen = new Pen(Color.Black, 4))
  637. {
  638. Graphics g = Graphics.FromImage(printBitmap);
  639. g.DrawRectangle(pen, new Rectangle(startPoint.X, startPoint.Y, resizedWidth, resizedHeight));
  640. g.Dispose();
  641. }
  642. }
  643. startPoint.X -= (singlePageWidth - resizedWidth) / 2;
  644. }
  645. if (isNoSrcPage)
  646. {
  647. isNoSrcPage = false;
  648. break;
  649. }
  650. }
  651. }
  652. e.Graphics.DrawImage(printBitmap, e.PageBounds);
  653. if (PrintIndex < PrintedPageCount - 1)
  654. {
  655. PrintIndex++;
  656. e.HasMorePages = true;
  657. }
  658. else
  659. {
  660. e.HasMorePages = false;
  661. }
  662. }
  663. private void PrintDocumentModBooklet(PrintPageEventArgs e)
  664. {
  665. BookletInfo bookletInfo = (BookletInfo)PrintSettingsInfo.printModInfo;
  666. ///空白画布
  667. Bitmap printBitmap;
  668. double singlePageHeight = 0;
  669. double singlePageWidth = 0;
  670. if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
  671. {
  672. printBitmap = new Bitmap((int)PrintSettingsInfo.PageMediaSize.Width, (int)PrintSettingsInfo.PageMediaSize.Height);
  673. singlePageHeight = (double)PrintSettingsInfo.PageMediaSize.Height;
  674. singlePageWidth = (double)(PrintSettingsInfo.PageMediaSize.Width / 2);
  675. }
  676. else
  677. {
  678. printBitmap = new Bitmap((int)PrintSettingsInfo.PageMediaSize.Height, (int)PrintSettingsInfo.PageMediaSize.Width);
  679. singlePageHeight = (double)PrintSettingsInfo.PageMediaSize.Width;
  680. singlePageWidth = (double)(PrintSettingsInfo.PageMediaSize.Height / 2);
  681. }
  682. System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
  683. int maxPaperNumber = (PDFViewer.Document.PageCount % 4 == 0) ? (PDFViewer.Document.PageCount / 4) : (PDFViewer.Document.PageCount / 4 + 1);
  684. Bitmap leftBitmap = null;
  685. Bitmap rightBitmap = null;
  686. double resizedHeight = 0;
  687. double resizedWidth = 0;
  688. CPDFPage leftPage = null;
  689. CPDFPage rightPage = null;
  690. int leftPageIndex = 0;
  691. int rightPageIndex = 0;
  692. ///<summary>
  693. ///左装订与右装订顺序相反
  694. /// </summary>
  695. if (bookletInfo.EnumBookletBinding == EnumBookletBinding.StatusLeft)
  696. {
  697. ///奇数页:
  698. ///右面为页面序号
  699. ///左面为[(总打印纸数*4)+1-打印页右侧页序号]
  700. if ((PrintIndex + 1) % 2 == 1)
  701. {
  702. try
  703. {
  704. leftPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - PrintIndex - 1];
  705. leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
  706. }
  707. catch
  708. {
  709. leftPage = null;
  710. }
  711. rightPageIndex = PrintSettingsInfo.PageRangeList[PrintIndex];
  712. //左面
  713. if (leftPage != null)
  714. {
  715. ///重设大小
  716. if ((leftPage.PageSize.Height / leftPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
  717. {
  718. resizedHeight = singlePageHeight;
  719. resizedWidth = singlePageHeight / leftPage.PageSize.Height * leftPage.PageSize.Width;
  720. }
  721. else
  722. {
  723. resizedWidth = singlePageWidth;
  724. resizedHeight = singlePageWidth / leftPage.PageSize.Width * leftPage.PageSize.Height;
  725. }
  726. leftBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  727. startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
  728. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  729. if (PrintSettingsInfo.IsGrayscale)
  730. {
  731. leftBitmap = ToGray(leftBitmap, 0);
  732. }
  733. printBitmap = CombineBitmap(printBitmap, leftBitmap, startPoint);
  734. }
  735. //右面
  736. rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
  737. startPoint.X = (int)singlePageWidth;
  738. if (rightPage != null)
  739. {
  740. ///重设大小
  741. if ((rightPage.PageSize.Height / rightPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
  742. {
  743. resizedHeight = singlePageHeight;
  744. resizedWidth = singlePageHeight / rightPage.PageSize.Height * rightPage.PageSize.Width;
  745. }
  746. else
  747. {
  748. resizedWidth = singlePageWidth;
  749. resizedHeight = singlePageWidth / rightPage.PageSize.Width * rightPage.PageSize.Height;
  750. }
  751. rightBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  752. startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
  753. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  754. if (PrintSettingsInfo.IsGrayscale)
  755. {
  756. rightBitmap = ToGray(rightBitmap, 0);
  757. }
  758. printBitmap = CombineBitmap(printBitmap, rightBitmap, startPoint);
  759. }
  760. }
  761. /// 偶数页:
  762. /// 左面为页面序号
  763. /// 右面为[(总打印纸数*4)+1-打印页右侧页序号]
  764. else
  765. {
  766. leftPageIndex = PrintSettingsInfo.PageRangeList[PrintIndex];
  767. //左面
  768. leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
  769. if (leftPage != null)
  770. {
  771. ///重设大小
  772. if ((leftPage.PageSize.Height / leftPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
  773. {
  774. resizedHeight = singlePageHeight;
  775. resizedWidth = singlePageHeight / leftPage.PageSize.Height * leftPage.PageSize.Width;
  776. }
  777. else
  778. {
  779. resizedWidth = singlePageWidth;
  780. resizedHeight = singlePageWidth / leftPage.PageSize.Width * leftPage.PageSize.Height;
  781. }
  782. leftBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)(resizedWidth), (int)resizedHeight, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  783. startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
  784. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  785. if (PrintSettingsInfo.IsGrayscale)
  786. {
  787. leftBitmap = ToGray(leftBitmap, 0);
  788. }
  789. printBitmap = CombineBitmap(printBitmap, leftBitmap, startPoint);
  790. }
  791. //右面
  792. try
  793. {
  794. rightPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - 1 - PrintIndex];
  795. rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
  796. }
  797. catch
  798. {
  799. rightPage = null;
  800. }
  801. startPoint.X = (int)singlePageWidth;
  802. if (rightPage != null)
  803. {
  804. ///重设大小
  805. if ((rightPage.PageSize.Height / rightPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
  806. {
  807. resizedHeight = singlePageHeight;
  808. resizedWidth = singlePageHeight / rightPage.PageSize.Height * rightPage.PageSize.Width;
  809. }
  810. else
  811. {
  812. resizedWidth = singlePageWidth;
  813. resizedHeight = singlePageWidth / rightPage.PageSize.Width * rightPage.PageSize.Height;
  814. }
  815. rightBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  816. startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
  817. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  818. if (PrintSettingsInfo.IsGrayscale)
  819. {
  820. rightBitmap = ToGray(rightBitmap, 0);
  821. }
  822. printBitmap = CombineBitmap(printBitmap, rightBitmap, startPoint);
  823. }
  824. }
  825. }
  826. ///<summary>
  827. ///右装订打印
  828. /// </summary>
  829. if (bookletInfo.EnumBookletBinding == EnumBookletBinding.StatusRight)
  830. {
  831. ///奇数页:
  832. ///右面为页面序号
  833. ///左面为[(总打印纸数*4)+1-打印页右侧页序号]
  834. if ((PrintIndex + 1) % 2 == 0)
  835. {
  836. try
  837. {
  838. leftPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - PrintIndex - 1];
  839. leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
  840. }
  841. catch
  842. {
  843. leftPage = null;
  844. }
  845. rightPageIndex = PrintSettingsInfo.PageRangeList[PrintIndex];
  846. //左面
  847. if (leftPage != null)
  848. {
  849. ///重设大小
  850. if ((leftPage.PageSize.Height / leftPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
  851. {
  852. resizedHeight = singlePageHeight;
  853. resizedWidth = singlePageHeight / leftPage.PageSize.Height * leftPage.PageSize.Width;
  854. }
  855. else
  856. {
  857. resizedWidth = singlePageWidth;
  858. resizedHeight = singlePageWidth / leftPage.PageSize.Width * leftPage.PageSize.Height;
  859. }
  860. leftBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  861. startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
  862. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  863. if (PrintSettingsInfo.IsGrayscale)
  864. {
  865. leftBitmap = ToGray(leftBitmap, 0);
  866. }
  867. printBitmap = CombineBitmap(printBitmap, leftBitmap, startPoint);
  868. }
  869. //右面
  870. rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
  871. startPoint.X = (int)singlePageWidth;
  872. if (rightPage != null)
  873. {
  874. ///重设大小
  875. if ((rightPage.PageSize.Height / rightPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
  876. {
  877. resizedHeight = singlePageHeight;
  878. resizedWidth = singlePageHeight / rightPage.PageSize.Height * rightPage.PageSize.Width;
  879. }
  880. else
  881. {
  882. resizedWidth = singlePageWidth;
  883. resizedHeight = singlePageWidth / rightPage.PageSize.Width * rightPage.PageSize.Height;
  884. }
  885. rightBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  886. startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
  887. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  888. if (PrintSettingsInfo.IsGrayscale)
  889. {
  890. rightBitmap = ToGray(rightBitmap, 0);
  891. }
  892. printBitmap = CombineBitmap(printBitmap, rightBitmap, startPoint);
  893. }
  894. }
  895. /// 偶数页:
  896. /// 左面为页面序号
  897. /// 右面为[(总打印纸数*4)+1-打印页右侧页序号]
  898. else
  899. {
  900. leftPageIndex = PrintSettingsInfo.PageRangeList[PrintIndex];
  901. //左面
  902. leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
  903. if (leftPage != null)
  904. {
  905. ///重设大小
  906. if ((leftPage.PageSize.Height / leftPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
  907. {
  908. resizedHeight = singlePageHeight;
  909. resizedWidth = singlePageHeight / leftPage.PageSize.Height * leftPage.PageSize.Width;
  910. }
  911. else
  912. {
  913. resizedWidth = singlePageWidth;
  914. resizedHeight = singlePageWidth / leftPage.PageSize.Width * leftPage.PageSize.Height;
  915. }
  916. leftBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)(resizedWidth), (int)resizedHeight, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  917. startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
  918. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  919. if (PrintSettingsInfo.IsGrayscale)
  920. {
  921. leftBitmap = ToGray(leftBitmap, 0);
  922. }
  923. printBitmap = CombineBitmap(printBitmap, leftBitmap, startPoint);
  924. }
  925. //右面
  926. try
  927. {
  928. rightPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - 1 - PrintIndex];
  929. rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
  930. }
  931. catch
  932. {
  933. rightPage = null;
  934. }
  935. startPoint.X = (int)singlePageWidth;
  936. if (rightPage != null)
  937. {
  938. ///重设大小
  939. if ((rightPage.PageSize.Height / rightPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
  940. {
  941. resizedHeight = singlePageHeight;
  942. resizedWidth = singlePageHeight / rightPage.PageSize.Height * rightPage.PageSize.Width;
  943. }
  944. else
  945. {
  946. resizedWidth = singlePageWidth;
  947. resizedHeight = singlePageWidth / rightPage.PageSize.Width * rightPage.PageSize.Height;
  948. }
  949. rightBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  950. startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
  951. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  952. if (PrintSettingsInfo.IsGrayscale)
  953. {
  954. rightBitmap = ToGray(rightBitmap, 0);
  955. }
  956. printBitmap = CombineBitmap(printBitmap, rightBitmap, startPoint);
  957. }
  958. }
  959. }
  960. e.Graphics.DrawImage(printBitmap, e.PageBounds);
  961. if (PrintIndex < PrintedPageCount - 1)
  962. {
  963. PrintIndex++;
  964. e.HasMorePages = true;
  965. }
  966. else
  967. {
  968. e.HasMorePages = false;
  969. }
  970. }
  971. private void PrintDocumentByCurrentSettings(object sender, PrintPageEventArgs e)
  972. {
  973. switch (PrintSettingsInfo.printModInfo.EnumPrintMod)
  974. {
  975. case EnumPrintMod.StatusPoster:
  976. PrintDocumentModPoster(e);
  977. break;
  978. case EnumPrintMod.StatusMultiple:
  979. PrintDocumentModMultiple(e);
  980. break;
  981. case EnumPrintMod.StatusBooklet:
  982. PrintDocumentModBooklet(e);
  983. break;
  984. case EnumPrintMod.StatusSize:
  985. default:
  986. PrintDocumentModSize(e);
  987. break;
  988. }
  989. }
  990. //跳转指定页
  991. public void JumpToSelectedPage()
  992. {
  993. if (TargetPaperIndex >= 1 && TargetPaperIndex <= PrintedPageCount)
  994. {
  995. PaintPageByCurrentPreviewIndex(TargetPaperIndex);
  996. }
  997. }
  998. //跳转指定页
  999. public void JumpPage(object e)
  1000. {
  1001. var txt = e as System.Windows.Controls.TextBox;
  1002. if (txt.Text != null)
  1003. {
  1004. try
  1005. {
  1006. TargetPaperIndex = Convert.ToInt32(txt.Text);
  1007. }
  1008. catch
  1009. {
  1010. TargetPaperIndex = 1;
  1011. }
  1012. }
  1013. if (TargetPaperIndex >= 1 && TargetPaperIndex <= PrintedPageCount)
  1014. {
  1015. PaintPageByCurrentPreviewIndex(TargetPaperIndex);
  1016. }
  1017. }
  1018. public void RecvPrintSettingsInfo(PrintSettingsInfoWithUnicode printSettingsInfoWIthUnicode)
  1019. {
  1020. if (PDFViewer != null && printSettingsInfoWIthUnicode.printSettingsInfo != null)
  1021. {
  1022. CaculatePrintedPageCount();
  1023. SetPaperCollection();
  1024. TargetPaperIndex = 1;
  1025. PaintPageByCurrentPreviewIndex(TargetPaperIndex);
  1026. }
  1027. }
  1028. public void SetViewBox(double height, double width)
  1029. {
  1030. if (height / width >= (248.0 / 180.0))
  1031. {
  1032. ViewBoxHeight = "248.0";
  1033. ViewBoxWidth = (width / height * 248.0).ToString();
  1034. }
  1035. else
  1036. {
  1037. ViewBoxWidth = "180.0";
  1038. ViewBoxHeight = (height / width * 180.0).ToString();
  1039. }
  1040. }
  1041. /// <summary>
  1042. /// bitmap->bitmapsource转换器,用于显示到image控件
  1043. /// </summary>
  1044. /// <param name="bmp">
  1045. /// Bitmap格式图片资源
  1046. /// </param>
  1047. /// <returns></returns>
  1048. public static BitmapSource ToBitmapSource(System.Drawing.Bitmap bmp)
  1049. {
  1050. IntPtr ptr = bmp.GetHbitmap();//obtain the Hbitmap
  1051. BitmapSource bmpsrc = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap
  1052. (
  1053. ptr,
  1054. IntPtr.Zero,
  1055. new Int32Rect(0, 0, bmp.Width, bmp.Height),
  1056. System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()
  1057. );
  1058. return bmpsrc;
  1059. }
  1060. public Bitmap ResizeBitmap(Bitmap bitmap)
  1061. {
  1062. Resize(bitmap, (int)float.Parse(ViewBoxHeight), (int)float.Parse(ViewBoxWidth));
  1063. return bitmap;
  1064. }
  1065. /// <summary>
  1066. /// 创建空白底图
  1067. /// </summary>
  1068. private void CreateBlankBitmap()
  1069. {
  1070. if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
  1071. {
  1072. blankPageBitmap = new Bitmap((int)PrintSettingsInfo.PageMediaSize.Width, (int)PrintSettingsInfo.PageMediaSize.Height);
  1073. }
  1074. else
  1075. {
  1076. blankPageBitmap = new Bitmap((int)PrintSettingsInfo.PageMediaSize.Height, (int)PrintSettingsInfo.PageMediaSize.Width);
  1077. }
  1078. }
  1079. /// <summary>
  1080. /// 设置预览(除海报)
  1081. /// </summary>
  1082. public void SetPreviewExceptPoster()
  1083. {
  1084. if (PrintSettingsInfo.printModInfo.EnumPrintMod != EnumPrintMod.StatusPoster)
  1085. {
  1086. if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
  1087. {
  1088. PaperWidth = String.Format("{0:F1}", (PrintSettingsInfo.PageMediaSize.Width) / 96 * 25.4);
  1089. PaperHeight = String.Format("{0:F1}", (PrintSettingsInfo.PageMediaSize.Height) / 96 * 25.4);
  1090. }
  1091. else
  1092. {
  1093. PaperHeight = String.Format("{0:F1}", (PrintSettingsInfo.PageMediaSize.Width) / 96 * 25.4);
  1094. PaperWidth = String.Format("{0:F1}", (PrintSettingsInfo.PageMediaSize.Height) / 96 * 25.4);
  1095. }
  1096. SetViewBox(double.Parse(PaperHeight), double.Parse(PaperWidth));
  1097. }
  1098. else
  1099. {
  1100. }
  1101. CreateBlankBitmap();
  1102. }
  1103. /// <summary>
  1104. /// 直接Resize到给定大小
  1105. /// </summary>
  1106. /// <param name="input"></param>
  1107. /// <param name="targetWidth"></param>
  1108. /// <param name="targetHeight"></param>
  1109. /// <returns></returns>
  1110. public static Bitmap Resize(Bitmap input, int targetWidth, int targetHeight)
  1111. {
  1112. try
  1113. {
  1114. var actualBitmap = new Bitmap(targetWidth, targetHeight);
  1115. var g = Graphics.FromImage(actualBitmap);
  1116. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default; //设定插值方式
  1117. g.DrawImage(input,
  1118. new Rectangle(0, 0, targetWidth, targetHeight),
  1119. new Rectangle(0, 0, input.Width, input.Height),
  1120. GraphicsUnit.Pixel);
  1121. g.Dispose();
  1122. return actualBitmap;
  1123. }
  1124. catch (Exception ex)
  1125. {
  1126. return null;
  1127. }
  1128. }
  1129. /// <summary>
  1130. /// 计算页数
  1131. /// </summary>
  1132. public void CaculatePrintedPageCount()
  1133. {
  1134. PrintedPageCount = PrintSettingsInfo.PageRangeList.Count;
  1135. if (PrintSettingsInfo.printModInfo.EnumPrintMod == EnumPrintMod.StatusMultiple)
  1136. {
  1137. MultipleInfo multipleInfo = (MultipleInfo)PrintSettingsInfo.printModInfo;
  1138. if (PrintedPageCount % (multipleInfo.HorizontalPageNumber * multipleInfo.VerticalPageNumber) != 0)
  1139. {
  1140. PrintedPageCount = PrintedPageCount / (multipleInfo.HorizontalPageNumber * multipleInfo.VerticalPageNumber) + 1;
  1141. }
  1142. else
  1143. {
  1144. PrintedPageCount = PrintedPageCount / (multipleInfo.HorizontalPageNumber * multipleInfo.VerticalPageNumber);
  1145. }
  1146. }
  1147. else if (PrintSettingsInfo.printModInfo.EnumPrintMod == EnumPrintMod.StatusBooklet)
  1148. {
  1149. BookletInfo bookletInfo = (BookletInfo)PrintSettingsInfo.printModInfo;
  1150. if (PrintedPageCount == 1)
  1151. {
  1152. PrintedPageCount = 1;
  1153. }
  1154. else
  1155. {
  1156. PrintedPageCount = (bookletInfo.EndPaperIndex - bookletInfo.BeginPaperIndex + 1) * 2;
  1157. if (bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusFrontSideOnly ||
  1158. bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusBackSideOnly)
  1159. {
  1160. PrintedPageCount /= 2;
  1161. }
  1162. }
  1163. }
  1164. }
  1165. /// <summary>
  1166. /// 转灰度
  1167. /// </summary>
  1168. /// <param name="bmp"></param>
  1169. /// <param name="mode"></param>
  1170. /// <returns></returns>
  1171. private Bitmap ToGray(Bitmap bmp, int mode)
  1172. {
  1173. if (bmp == null)
  1174. {
  1175. return null;
  1176. }
  1177. int w = bmp.Width;
  1178. int h = bmp.Height;
  1179. try
  1180. {
  1181. byte newColor = 0;
  1182. BitmapData srcData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  1183. unsafe
  1184. {
  1185. byte* p = (byte*)srcData.Scan0.ToPointer();
  1186. for (int y = 0; y < h; y++)
  1187. {
  1188. for (int x = 0; x < w; x++)
  1189. {
  1190. if (mode == 0) // 加权平均
  1191. {
  1192. newColor = (byte)((float)p[0] * 0.114f + (float)p[1] * 0.587f + (float)p[2] * 0.299f);
  1193. }
  1194. else // 算数平均
  1195. {
  1196. newColor = (byte)((float)(p[0] + p[1] + p[2]) / 3.0f);
  1197. }
  1198. p[0] = newColor;
  1199. p[1] = newColor;
  1200. p[2] = newColor;
  1201. p += 3;
  1202. }
  1203. p += srcData.Stride - w * 3;
  1204. }
  1205. bmp.UnlockBits(srcData);
  1206. return bmp;
  1207. }
  1208. }
  1209. catch
  1210. {
  1211. return null;
  1212. }
  1213. }
  1214. /// <summary>
  1215. /// 合并底图和前景
  1216. /// </summary>
  1217. /// <param name="background"></param>
  1218. /// <param name="foreground"></param>
  1219. /// <param name="point"></param>
  1220. /// <returns></returns>
  1221. public Bitmap CombineBitmap(Bitmap background, Bitmap foreground, System.Drawing.Point point)
  1222. {
  1223. if (background == null)
  1224. {
  1225. return null;
  1226. }
  1227. int bgWidth = background.Width;
  1228. int bgHeight = background.Height;
  1229. int fgWidth = foreground.Width;
  1230. int fgHeight = foreground.Height;
  1231. Bitmap newMap = new Bitmap(bgWidth, bgHeight);
  1232. Graphics graphics = Graphics.FromImage(newMap);
  1233. graphics.DrawImage(background, new System.Drawing.Point(0, 0));
  1234. graphics.DrawImage(foreground, point);
  1235. graphics.Dispose();
  1236. return newMap;
  1237. }
  1238. /// <summary>
  1239. /// size模式
  1240. /// </summary>
  1241. /// <param name="paperIndex"></param>
  1242. private async void PreviewModSize(int paperIndex)
  1243. {
  1244. SizeInfo sizeInfo = (SizeInfo)PrintSettingsInfo.printModInfo;
  1245. CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(PrintSettingsInfo.PageRangeList[paperIndex]);
  1246. System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
  1247. if (cPDFPage != null)
  1248. {
  1249. Bitmap printBitmap = new Bitmap((int)(float.Parse(PaperWidth)), (int)float.Parse(PaperHeight));
  1250. Bitmap bitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)(cPDFPage.PageSize.Width * dpiRatio), (int)(cPDFPage.PageSize.Height * dpiRatio), PrintSettingsInfo.PageRangeList[paperIndex], PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  1251. if (PrintSettingsInfo.IsGrayscale)
  1252. {
  1253. bitmap = ToGray(bitmap, 0);
  1254. }
  1255. ///<summary>
  1256. ///自适应模式
  1257. /// </summary>
  1258. if (sizeInfo.EnumSizeType == EnumSizeType.StatusAdaptive)
  1259. {
  1260. double mmToDpiRatio = 96 / 25.4;
  1261. int resizedHeight = 0;
  1262. int resizedWidth = 0;
  1263. ///重设大小
  1264. if ((float)bitmap.Height / (float)bitmap.Width >= (float.Parse(PaperHeight) / float.Parse(PaperWidth)))
  1265. {
  1266. resizedHeight = (int)(float.Parse(PaperHeight) * mmToDpiRatio);
  1267. resizedWidth = (int)((float.Parse(PaperHeight) / bitmap.Height * bitmap.Width) * mmToDpiRatio);
  1268. }
  1269. else
  1270. {
  1271. resizedWidth = (int)(float.Parse(PaperWidth) * mmToDpiRatio);
  1272. resizedHeight = (int)((float.Parse(PaperWidth) / bitmap.Width * bitmap.Height) * mmToDpiRatio);
  1273. }
  1274. bitmap = Resize(bitmap, resizedWidth, resizedHeight);
  1275. startPoint.X = (blankPageBitmap.Width - resizedWidth) / 2;
  1276. startPoint.Y = (blankPageBitmap.Height - resizedHeight) / 2;
  1277. printBitmap = CombineBitmap(blankPageBitmap, bitmap, startPoint);
  1278. BitmapSource = ToBitmapSource(printBitmap);
  1279. }
  1280. else if (sizeInfo.EnumSizeType == EnumSizeType.StatusActural)
  1281. {
  1282. startPoint.X = (blankPageBitmap.Width - bitmap.Width) / 2;
  1283. startPoint.Y = (blankPageBitmap.Height - bitmap.Height) / 2;
  1284. printBitmap = CombineBitmap(blankPageBitmap, bitmap, startPoint);
  1285. BitmapSource = ToBitmapSource(printBitmap);
  1286. }
  1287. else if (sizeInfo.EnumSizeType == EnumSizeType.StatusCustomized)
  1288. {
  1289. bitmap = Resize(bitmap, (int)(bitmap.Width * (sizeInfo.DisplayRatio / 100.0)), (int)(bitmap.Height * (sizeInfo.DisplayRatio / 100.0)));
  1290. startPoint.X = (blankPageBitmap.Width - bitmap.Width) / 2;
  1291. startPoint.Y = (blankPageBitmap.Height - bitmap.Height) / 2;
  1292. printBitmap = CombineBitmap(blankPageBitmap, bitmap, startPoint);
  1293. BitmapSource = ToBitmapSource(printBitmap);
  1294. }
  1295. bitmap.Dispose();
  1296. printBitmap.Dispose();
  1297. }
  1298. }
  1299. /// <summary>
  1300. /// 海报模式
  1301. /// </summary>
  1302. /// <param name="paperIndex"></param>
  1303. private async void PreviewModPoster(int paperIndex)
  1304. {
  1305. PosterInfo posterInfo = (PosterInfo)PrintSettingsInfo.printModInfo;
  1306. CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(PrintSettingsInfo.PageRangeList[paperIndex]);
  1307. double PreviewHeight = 0;
  1308. double PreviewWidth = 0;
  1309. System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
  1310. Bitmap displayBitmap = null;
  1311. //计算有效打印区域
  1312. double effectiveHeight = (double)PrintSettingsInfo.PageMediaSize.Height;
  1313. double effectiveWidth = (double)PrintSettingsInfo.PageMediaSize.Width;
  1314. //重叠
  1315. effectiveHeight = effectiveHeight - 2 * posterInfo.OverLap * 0.5 * 96 / 25.4;
  1316. effectiveWidth = effectiveWidth - 2 * posterInfo.OverLap * 0.5 * 96 / 25.4;
  1317. if (posterInfo.HasCutMarks)
  1318. {
  1319. effectiveHeight = effectiveHeight - 0.5 * 96 / 25.4 * 2;
  1320. effectiveWidth = effectiveWidth - 0.5 * 96 / 25.4 * 2;
  1321. }
  1322. if (!posterInfo.HasCutMarks && posterInfo.HasLabel)
  1323. {
  1324. effectiveHeight = effectiveHeight - 0.5 * 96 / 25.4;
  1325. }
  1326. if (cPDFPage != null)
  1327. {
  1328. //拆分模式
  1329. //根据页面大小,按最大有效打印区域划分为多块
  1330. //最大有效区域
  1331. //纸张大小-标记标签-重叠
  1332. if (posterInfo.EnumPosterMod == EnumPosterMod.StatusTile)
  1333. {
  1334. Bitmap bitmap;
  1335. //TODO 放大倍数过高引起int超限
  1336. try
  1337. {
  1338. bitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)(cPDFPage.PageSize.Width * dpiRatio * (posterInfo.TileRatio / 100.0)), (int)(cPDFPage.PageSize.Height * dpiRatio * (posterInfo.TileRatio / 100.0)), PrintSettingsInfo.PageRangeList[paperIndex], PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  1339. }
  1340. catch
  1341. {
  1342. bitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)(cPDFPage.PageSize.Width * dpiRatio), (int)(cPDFPage.PageSize.Height * dpiRatio), PrintSettingsInfo.PageRangeList[paperIndex], PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  1343. }
  1344. HorizonPaperCount = (int)Math.Ceiling((double)bitmap.Width / effectiveWidth);
  1345. VerticalPaperCount = (int)Math.Ceiling(((double)bitmap.Height / effectiveHeight));
  1346. PreviewWidth = (double)(PrintSettingsInfo.PageMediaSize.Width * HorizonPaperCount);
  1347. PreviewHeight = (double)(PrintSettingsInfo.PageMediaSize.Height * VerticalPaperCount);
  1348. //铺放纸张
  1349. SetViewBox(PreviewHeight, PreviewWidth);
  1350. //创建空白图像,大小等于纸张铺放后
  1351. displayBitmap = new Bitmap((int)PreviewWidth, (int)PreviewHeight);
  1352. //居中合并
  1353. startPoint.X = (int)((PreviewWidth - bitmap.Width) / 2);
  1354. startPoint.Y = (int)((PreviewHeight - bitmap.Height) / 2);
  1355. if (PrintSettingsInfo.IsGrayscale)
  1356. {
  1357. bitmap = ToGray(bitmap, 0);
  1358. }
  1359. displayBitmap = CombineBitmap(displayBitmap, bitmap, startPoint);
  1360. //画线
  1361. Graphics g = Graphics.FromImage(displayBitmap);
  1362. if (HorizonPaperCount > 1)
  1363. {
  1364. for (int tempX = 1; tempX < HorizonPaperCount; tempX++)
  1365. {
  1366. int horizonLocate = (int)(tempX * displayBitmap.Width / HorizonPaperCount);
  1367. g.DrawLine(new Pen(Color.Black, HorizonPaperCount * 5), new System.Drawing.Point(horizonLocate, 0), new System.Drawing.Point(horizonLocate, (int)PreviewHeight));
  1368. }
  1369. }
  1370. if (VerticalPaperCount > 1)
  1371. {
  1372. for (int tempY = 1; tempY < VerticalPaperCount; tempY++)
  1373. {
  1374. int verticalLocate = (int)(tempY * displayBitmap.Height / VerticalPaperCount);
  1375. g.DrawLine(new Pen(Color.Black, VerticalPaperCount * 5), new System.Drawing.Point(0, verticalLocate), new System.Drawing.Point((int)PreviewWidth, verticalLocate));
  1376. }
  1377. }
  1378. g.Dispose();
  1379. bitmap.Dispose();
  1380. PosterRatio = posterInfo.TileRatio / 100.0;
  1381. }
  1382. //平铺模式
  1383. else if (posterInfo.EnumPosterMod == EnumPosterMod.StatusSplit)
  1384. {
  1385. HorizonPaperCount = posterInfo.HorizontalSheetNumber;
  1386. VerticalPaperCount = posterInfo.VerticalSheetNumber;
  1387. PreviewWidth = (double)(PrintSettingsInfo.PageMediaSize.Width * HorizonPaperCount);
  1388. PreviewHeight = (double)(PrintSettingsInfo.PageMediaSize.Height * VerticalPaperCount);
  1389. //铺放纸张
  1390. SetViewBox(PreviewHeight, PreviewWidth);
  1391. //创建空白图像,大小等于纸张铺放后
  1392. displayBitmap = new Bitmap((int)PreviewWidth, (int)PreviewHeight);
  1393. //文件根据有效范围缩放
  1394. double wholeEffectiveHeight = effectiveHeight * VerticalPaperCount;
  1395. double wholeEffectiveWidth = effectiveWidth * HorizonPaperCount;
  1396. double resizedHeight = 0;
  1397. double resizedWidth = 0;
  1398. if ((cPDFPage.PageSize.Height / cPDFPage.PageSize.Width) >= wholeEffectiveHeight / wholeEffectiveWidth)
  1399. {
  1400. resizedHeight = wholeEffectiveHeight;
  1401. resizedWidth = wholeEffectiveHeight / cPDFPage.PageSize.Height * cPDFPage.PageSize.Width;
  1402. }
  1403. else
  1404. {
  1405. resizedWidth = wholeEffectiveWidth;
  1406. resizedHeight = wholeEffectiveWidth / cPDFPage.PageSize.Width * cPDFPage.PageSize.Height;
  1407. }
  1408. PosterRatio = resizedWidth / cPDFPage.PageSize.Width;
  1409. Bitmap bitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, PrintSettingsInfo.PageRangeList[paperIndex], PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  1410. startPoint.X = (int)((PreviewWidth - bitmap.Width) / 2);
  1411. startPoint.Y = (int)((PreviewHeight - bitmap.Height) / 2);
  1412. if (PrintSettingsInfo.IsGrayscale)
  1413. {
  1414. bitmap = ToGray(bitmap, 0);
  1415. }
  1416. displayBitmap = CombineBitmap(displayBitmap, bitmap, startPoint);
  1417. //画线
  1418. Graphics g = Graphics.FromImage(displayBitmap);
  1419. if (HorizonPaperCount > 1)
  1420. {
  1421. for (int tempX = 1; tempX < HorizonPaperCount; tempX++)
  1422. {
  1423. int horizonLocate = (int)(tempX * displayBitmap.Width / HorizonPaperCount);
  1424. g.DrawLine(new Pen(Color.Black, HorizonPaperCount * 5), new System.Drawing.Point(horizonLocate, 0), new System.Drawing.Point(horizonLocate, (int)PreviewHeight));
  1425. }
  1426. }
  1427. if (VerticalPaperCount > 1)
  1428. {
  1429. for (int tempY = 1; tempY < VerticalPaperCount; tempY++)
  1430. {
  1431. int verticalLocate = (int)(tempY * displayBitmap.Height / VerticalPaperCount);
  1432. g.DrawLine(new Pen(Color.Black, VerticalPaperCount * 5), new System.Drawing.Point(0, verticalLocate), new System.Drawing.Point((int)PreviewWidth, verticalLocate));
  1433. }
  1434. }
  1435. g.Dispose();
  1436. bitmap.Dispose();
  1437. }
  1438. }
  1439. BitmapSource = ToBitmapSource(displayBitmap);
  1440. displayBitmap.Dispose();
  1441. }
  1442. /// <summary>
  1443. /// 多页
  1444. /// </summary>
  1445. /// <param name="paperIndex"></param>
  1446. private async void PreviewModMultiple(int paperIndex)
  1447. {
  1448. MultipleInfo multipleInfo = (MultipleInfo)PrintSettingsInfo.printModInfo;
  1449. int singlePageHeight;
  1450. int singlePageWidth;
  1451. Bitmap printBitmap;
  1452. int currentPage = 0;
  1453. if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
  1454. {
  1455. singlePageHeight = (int)(PrintSettingsInfo.PageMediaSize.Height / multipleInfo.VerticalPageNumber);
  1456. singlePageWidth = (int)(PrintSettingsInfo.PageMediaSize.Width / multipleInfo.HorizontalPageNumber);
  1457. printBitmap = new Bitmap((int)PrintSettingsInfo.PageMediaSize.Width, (int)PrintSettingsInfo.PageMediaSize.Height);
  1458. }
  1459. else
  1460. {
  1461. singlePageHeight = (int)(PrintSettingsInfo.PageMediaSize.Width / multipleInfo.VerticalPageNumber);
  1462. singlePageWidth = (int)(PrintSettingsInfo.PageMediaSize.Height / multipleInfo.HorizontalPageNumber);
  1463. printBitmap = new Bitmap((int)PrintSettingsInfo.PageMediaSize.Height, (int)PrintSettingsInfo.PageMediaSize.Width);
  1464. }
  1465. System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
  1466. int startPage = PrintSettingsInfo.PageRangeList[paperIndex * (multipleInfo.VerticalPageNumber) * (multipleInfo.HorizontalPageNumber)];
  1467. int resizedHeight = 0;
  1468. int resizedWidth = 0;
  1469. bool isNoSrcPage = false;
  1470. Bitmap pageBitmap;
  1471. ///水平:横向排列,先横后纵,纵轴不变横轴翻转
  1472. if (multipleInfo.EnumPageOrder == EnumPageOrder.StatusHorizontalOrder || multipleInfo.EnumPageOrder == EnumPageOrder.StatusHorizontalReverseOrder)
  1473. {
  1474. for (int tempVerticalIndex = 0; tempVerticalIndex < multipleInfo.VerticalPageNumber; tempVerticalIndex++)
  1475. {
  1476. startPoint.Y = tempVerticalIndex * singlePageHeight;
  1477. for (int tempHorizontalIndex = 0; tempHorizontalIndex < multipleInfo.HorizontalPageNumber; tempHorizontalIndex++)
  1478. {
  1479. try
  1480. {
  1481. currentPage =
  1482. PrintSettingsInfo.PageRangeList[paperIndex *
  1483. (multipleInfo.VerticalPageNumber) *
  1484. (multipleInfo.HorizontalPageNumber) +
  1485. (tempVerticalIndex * multipleInfo.HorizontalPageNumber) +
  1486. tempHorizontalIndex];
  1487. }
  1488. catch
  1489. {
  1490. break;
  1491. }
  1492. //横向
  1493. if (multipleInfo.EnumPageOrder == EnumPageOrder.StatusHorizontalOrder)
  1494. {
  1495. startPoint.X = tempHorizontalIndex * singlePageWidth;
  1496. }
  1497. //横向倒序
  1498. else
  1499. {
  1500. startPoint.X = (multipleInfo.HorizontalPageNumber - tempHorizontalIndex - 1) * singlePageWidth;
  1501. }
  1502. CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(currentPage);
  1503. if (cPDFPage == null)
  1504. {
  1505. isNoSrcPage = true;
  1506. break;
  1507. }
  1508. pageBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)(cPDFPage.PageSize.Width), (int)(cPDFPage.PageSize.Height), currentPage, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  1509. ///重设大小
  1510. if ((float)pageBitmap.Height / (float)pageBitmap.Width >= ((float)singlePageHeight / (float)singlePageWidth))
  1511. {
  1512. resizedHeight = (int)(singlePageHeight * 0.96);
  1513. resizedWidth = (int)((float)singlePageHeight / pageBitmap.Height * pageBitmap.Width * 0.96);
  1514. }
  1515. else
  1516. {
  1517. resizedWidth = (int)(singlePageWidth * 0.96);
  1518. resizedHeight = (int)((float)singlePageWidth / pageBitmap.Width * pageBitmap.Height * 0.96);
  1519. }
  1520. pageBitmap = Resize(pageBitmap, (int)resizedWidth, (int)resizedHeight);
  1521. startPoint.X += (singlePageWidth - resizedWidth) / 2;
  1522. startPoint.Y += (singlePageHeight - resizedHeight) / 2;
  1523. if (PrintSettingsInfo.IsGrayscale)
  1524. {
  1525. pageBitmap = ToGray(pageBitmap, 0);
  1526. }
  1527. printBitmap = CombineBitmap(printBitmap, pageBitmap, startPoint);
  1528. //绘制边界
  1529. if (PrintSettingsInfo.IsPrintPageBorde)
  1530. {
  1531. using (Pen pen = new Pen(Color.Black, 4))
  1532. {
  1533. Graphics g = Graphics.FromImage(printBitmap);
  1534. g.DrawRectangle(pen, new Rectangle(startPoint.X, startPoint.Y, resizedWidth, resizedHeight));
  1535. g.Dispose();
  1536. }
  1537. }
  1538. startPoint.Y -= (singlePageHeight - resizedHeight) / 2;
  1539. }
  1540. if (isNoSrcPage)
  1541. {
  1542. isNoSrcPage = false;
  1543. break;
  1544. }
  1545. }
  1546. }
  1547. //垂直:纵向排列,先纵后横,纵轴不变横轴翻转
  1548. else
  1549. {
  1550. for (int tempHorizontalIndex = 0; tempHorizontalIndex < multipleInfo.HorizontalPageNumber; tempHorizontalIndex++)
  1551. {
  1552. //纵向,垂直排列时先移位
  1553. if (multipleInfo.EnumPageOrder == EnumPageOrder.StatusVerticalOrder)
  1554. {
  1555. startPoint.X = tempHorizontalIndex * singlePageWidth;
  1556. }
  1557. //纵向倒序
  1558. else
  1559. {
  1560. startPoint.X = (multipleInfo.HorizontalPageNumber - tempHorizontalIndex - 1) * singlePageWidth;
  1561. }
  1562. for (int tempVerticalIndex = 0; tempVerticalIndex < multipleInfo.VerticalPageNumber; tempVerticalIndex++)
  1563. {
  1564. startPoint.Y = tempVerticalIndex * singlePageHeight;
  1565. try
  1566. {
  1567. currentPage = PrintSettingsInfo.PageRangeList[paperIndex *
  1568. (multipleInfo.VerticalPageNumber) *
  1569. (multipleInfo.HorizontalPageNumber) +
  1570. (tempHorizontalIndex * multipleInfo.VerticalPageNumber) +
  1571. tempVerticalIndex];
  1572. }
  1573. catch
  1574. {
  1575. break;
  1576. }
  1577. CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(currentPage);
  1578. if (cPDFPage == null)
  1579. {
  1580. isNoSrcPage = true;
  1581. break;
  1582. }
  1583. pageBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)(cPDFPage.PageSize.Width), (int)(cPDFPage.PageSize.Height), currentPage, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  1584. Graphics graphics = Graphics.FromImage(pageBitmap);
  1585. ///重设大小
  1586. if ((float)pageBitmap.Height / (float)pageBitmap.Width >= ((float)singlePageHeight / (float)singlePageWidth))
  1587. {
  1588. resizedHeight = (int)(singlePageHeight * 0.96);
  1589. resizedWidth = (int)((float)singlePageHeight / pageBitmap.Height * pageBitmap.Width * 0.96);
  1590. }
  1591. else
  1592. {
  1593. resizedWidth = (int)(singlePageWidth * 0.96);
  1594. resizedHeight = (int)((float)singlePageWidth / pageBitmap.Width * pageBitmap.Height * 0.96);
  1595. }
  1596. pageBitmap = Resize(pageBitmap, (int)resizedWidth, (int)resizedHeight);
  1597. graphics.Dispose();
  1598. startPoint.X += (singlePageWidth - resizedWidth) / 2;
  1599. startPoint.Y += (singlePageHeight - resizedHeight) / 2;
  1600. if (PrintSettingsInfo.IsGrayscale)
  1601. {
  1602. pageBitmap = ToGray(pageBitmap, 0);
  1603. }
  1604. printBitmap = CombineBitmap(printBitmap, pageBitmap, startPoint);
  1605. //绘制边界
  1606. if (PrintSettingsInfo.IsPrintPageBorde)
  1607. {
  1608. using (Pen pen = new Pen(Color.Black, 4))
  1609. {
  1610. Graphics g = Graphics.FromImage(printBitmap);
  1611. g.DrawRectangle(pen, new Rectangle(startPoint.X, startPoint.Y, resizedWidth, resizedHeight));
  1612. g.Dispose();
  1613. }
  1614. }
  1615. startPoint.X -= (singlePageWidth - resizedWidth) / 2;
  1616. }
  1617. if (isNoSrcPage)
  1618. {
  1619. isNoSrcPage = false;
  1620. break;
  1621. }
  1622. }
  1623. }
  1624. WholePaperCountModPoster = PrintedPageCount * HorizonPaperCount * VerticalPaperCount;
  1625. BitmapSource = ToBitmapSource(printBitmap);
  1626. }
  1627. /// <summary>
  1628. /// 小册子模式
  1629. /// </summary>
  1630. /// <param name="paperIndex"></param>
  1631. private async void PreviewModBooklet(int paperIndex)
  1632. {
  1633. BookletInfo bookletInfo = (BookletInfo)PrintSettingsInfo.printModInfo;
  1634. ///空白画布
  1635. Bitmap printBitmap;
  1636. double singlePageHeight = 0;
  1637. double singlePageWidth = 0;
  1638. if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
  1639. {
  1640. printBitmap = new Bitmap((int)PrintSettingsInfo.PageMediaSize.Width, (int)PrintSettingsInfo.PageMediaSize.Height);
  1641. singlePageHeight = (double)PrintSettingsInfo.PageMediaSize.Height;
  1642. singlePageWidth = (double)(PrintSettingsInfo.PageMediaSize.Width / 2);
  1643. }
  1644. else
  1645. {
  1646. printBitmap = new Bitmap((int)PrintSettingsInfo.PageMediaSize.Height, (int)PrintSettingsInfo.PageMediaSize.Width);
  1647. singlePageHeight = (double)PrintSettingsInfo.PageMediaSize.Width;
  1648. singlePageWidth = (double)(PrintSettingsInfo.PageMediaSize.Height / 2);
  1649. }
  1650. System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
  1651. int maxPaperNumber = (PDFViewer.Document.PageCount % 4 == 0) ? (PDFViewer.Document.PageCount / 4) : (PDFViewer.Document.PageCount / 4 + 1);
  1652. Bitmap leftBitmap = null;
  1653. Bitmap rightBitmap = null;
  1654. double resizedHeight = 0;
  1655. double resizedWidth = 0;
  1656. CPDFPage leftPage = null;
  1657. CPDFPage rightPage = null;
  1658. int leftPageIndex = 0;
  1659. int rightPageIndex = 0;
  1660. ///<summary>
  1661. ///左装订与右装订顺序相反
  1662. /// </summary>
  1663. if (bookletInfo.EnumBookletBinding == EnumBookletBinding.StatusLeft)
  1664. {
  1665. ///奇数页:
  1666. ///右面为页面序号
  1667. ///左面为[(总打印纸数*4)+1-打印页右侧页序号]
  1668. if ((paperIndex + 1) % 2 == 1)
  1669. {
  1670. try
  1671. {
  1672. leftPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - paperIndex - 1];
  1673. leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
  1674. }
  1675. catch
  1676. {
  1677. leftPage = null;
  1678. }
  1679. rightPageIndex = PrintSettingsInfo.PageRangeList[paperIndex];
  1680. //左面
  1681. if (leftPage != null)
  1682. {
  1683. ///重设大小
  1684. if ((leftPage.PageSize.Height / leftPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
  1685. {
  1686. resizedHeight = singlePageHeight;
  1687. resizedWidth = singlePageHeight / leftPage.PageSize.Height * leftPage.PageSize.Width;
  1688. }
  1689. else
  1690. {
  1691. resizedWidth = singlePageWidth;
  1692. resizedHeight = singlePageWidth / leftPage.PageSize.Width * leftPage.PageSize.Height;
  1693. }
  1694. leftBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  1695. startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
  1696. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  1697. if (PrintSettingsInfo.IsGrayscale)
  1698. {
  1699. leftBitmap = ToGray(leftBitmap, 0);
  1700. }
  1701. printBitmap = CombineBitmap(printBitmap, leftBitmap, startPoint);
  1702. }
  1703. //右面
  1704. rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
  1705. startPoint.X = (int)singlePageWidth;
  1706. if (rightPage != null)
  1707. {
  1708. ///重设大小
  1709. if ((rightPage.PageSize.Height / rightPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
  1710. {
  1711. resizedHeight = singlePageHeight;
  1712. resizedWidth = singlePageHeight / rightPage.PageSize.Height * rightPage.PageSize.Width;
  1713. }
  1714. else
  1715. {
  1716. resizedWidth = singlePageWidth;
  1717. resizedHeight = singlePageWidth / rightPage.PageSize.Width * rightPage.PageSize.Height;
  1718. }
  1719. rightBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  1720. startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
  1721. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  1722. if (PrintSettingsInfo.IsGrayscale)
  1723. {
  1724. rightBitmap = ToGray(rightBitmap, 0);
  1725. }
  1726. printBitmap = CombineBitmap(printBitmap, rightBitmap, startPoint);
  1727. }
  1728. }
  1729. /// 偶数页:
  1730. /// 左面为页面序号
  1731. /// 右面为[(总打印纸数*4)+1-打印页右侧页序号]
  1732. else
  1733. {
  1734. try
  1735. {
  1736. leftPageIndex = PrintSettingsInfo.PageRangeList[paperIndex];
  1737. leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
  1738. }
  1739. catch
  1740. {
  1741. leftPage = null;
  1742. }
  1743. //左面
  1744. if (leftPage != null)
  1745. {
  1746. ///重设大小
  1747. if ((leftPage.PageSize.Height / leftPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
  1748. {
  1749. resizedHeight = singlePageHeight;
  1750. resizedWidth = singlePageHeight / leftPage.PageSize.Height * leftPage.PageSize.Width;
  1751. }
  1752. else
  1753. {
  1754. resizedWidth = singlePageWidth;
  1755. resizedHeight = singlePageWidth / leftPage.PageSize.Width * leftPage.PageSize.Height;
  1756. }
  1757. leftBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)(resizedWidth), (int)resizedHeight, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  1758. startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
  1759. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  1760. if (PrintSettingsInfo.IsGrayscale)
  1761. {
  1762. leftBitmap = ToGray(leftBitmap, 0);
  1763. }
  1764. printBitmap = CombineBitmap(printBitmap, leftBitmap, startPoint);
  1765. }
  1766. //右面
  1767. try
  1768. {
  1769. rightPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - 1 - paperIndex];
  1770. rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
  1771. }
  1772. catch
  1773. {
  1774. rightPage = null;
  1775. }
  1776. startPoint.X = (int)singlePageWidth;
  1777. if (rightPage != null)
  1778. {
  1779. ///重设大小
  1780. if ((rightPage.PageSize.Height / rightPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
  1781. {
  1782. resizedHeight = singlePageHeight;
  1783. resizedWidth = singlePageHeight / rightPage.PageSize.Height * rightPage.PageSize.Width;
  1784. }
  1785. else
  1786. {
  1787. resizedWidth = singlePageWidth;
  1788. resizedHeight = singlePageWidth / rightPage.PageSize.Width * rightPage.PageSize.Height;
  1789. }
  1790. rightBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  1791. startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
  1792. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  1793. if (PrintSettingsInfo.IsGrayscale)
  1794. {
  1795. rightBitmap = ToGray(rightBitmap, 0);
  1796. }
  1797. printBitmap = CombineBitmap(printBitmap, rightBitmap, startPoint);
  1798. }
  1799. }
  1800. }
  1801. ///<summary>
  1802. ///右装订打印
  1803. /// </summary>
  1804. if (bookletInfo.EnumBookletBinding == EnumBookletBinding.StatusRight)
  1805. {
  1806. ///奇数页:
  1807. ///右面为页面序号
  1808. ///左面为[(总打印纸数*4)+1-打印页右侧页序号]
  1809. if ((paperIndex + 1) % 2 == 0)
  1810. {
  1811. try
  1812. {
  1813. leftPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - paperIndex - 1];
  1814. leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
  1815. }
  1816. catch
  1817. {
  1818. leftPage = null;
  1819. }
  1820. rightPageIndex = PrintSettingsInfo.PageRangeList[paperIndex];
  1821. //左面
  1822. if (leftPage != null)
  1823. {
  1824. ///重设大小
  1825. if ((leftPage.PageSize.Height / leftPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
  1826. {
  1827. resizedHeight = singlePageHeight;
  1828. resizedWidth = singlePageHeight / leftPage.PageSize.Height * leftPage.PageSize.Width;
  1829. }
  1830. else
  1831. {
  1832. resizedWidth = singlePageWidth;
  1833. resizedHeight = singlePageWidth / leftPage.PageSize.Width * leftPage.PageSize.Height;
  1834. }
  1835. leftBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  1836. startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
  1837. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  1838. if (PrintSettingsInfo.IsGrayscale)
  1839. {
  1840. leftBitmap = ToGray(leftBitmap, 0);
  1841. }
  1842. printBitmap = CombineBitmap(printBitmap, leftBitmap, startPoint);
  1843. }
  1844. //右面
  1845. rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
  1846. startPoint.X = (int)singlePageWidth;
  1847. if (rightPage != null)
  1848. {
  1849. ///重设大小
  1850. if ((rightPage.PageSize.Height / rightPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
  1851. {
  1852. resizedHeight = singlePageHeight;
  1853. resizedWidth = singlePageHeight / rightPage.PageSize.Height * rightPage.PageSize.Width;
  1854. }
  1855. else
  1856. {
  1857. resizedWidth = singlePageWidth;
  1858. resizedHeight = singlePageWidth / rightPage.PageSize.Width * rightPage.PageSize.Height;
  1859. }
  1860. rightBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  1861. startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
  1862. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  1863. if (PrintSettingsInfo.IsGrayscale)
  1864. {
  1865. rightBitmap = ToGray(rightBitmap, 0);
  1866. }
  1867. printBitmap = CombineBitmap(printBitmap, rightBitmap, startPoint);
  1868. }
  1869. }
  1870. /// 偶数页:
  1871. /// 左面为页面序号
  1872. /// 右面为[(总打印纸数*4)+1-打印页右侧页序号]
  1873. else
  1874. {
  1875. leftPageIndex = PrintSettingsInfo.PageRangeList[paperIndex];
  1876. //左面
  1877. leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
  1878. if (leftPage != null)
  1879. {
  1880. ///重设大小
  1881. if ((leftPage.PageSize.Height / leftPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
  1882. {
  1883. resizedHeight = singlePageHeight;
  1884. resizedWidth = singlePageHeight / leftPage.PageSize.Height * leftPage.PageSize.Width;
  1885. }
  1886. else
  1887. {
  1888. resizedWidth = singlePageWidth;
  1889. resizedHeight = singlePageWidth / leftPage.PageSize.Width * leftPage.PageSize.Height;
  1890. }
  1891. leftBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)(resizedWidth), (int)resizedHeight, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  1892. startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
  1893. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  1894. if (PrintSettingsInfo.IsGrayscale)
  1895. {
  1896. leftBitmap = ToGray(leftBitmap, 0);
  1897. }
  1898. printBitmap = CombineBitmap(printBitmap, leftBitmap, startPoint);
  1899. }
  1900. //右面
  1901. try
  1902. {
  1903. rightPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - 1 - paperIndex];
  1904. rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
  1905. }
  1906. catch
  1907. {
  1908. rightPage = null;
  1909. }
  1910. startPoint.X = (int)singlePageWidth;
  1911. if (rightPage != null)
  1912. {
  1913. ///重设大小
  1914. if ((rightPage.PageSize.Height / rightPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
  1915. {
  1916. resizedHeight = singlePageHeight;
  1917. resizedWidth = singlePageHeight / rightPage.PageSize.Height * rightPage.PageSize.Width;
  1918. }
  1919. else
  1920. {
  1921. resizedWidth = singlePageWidth;
  1922. resizedHeight = singlePageWidth / rightPage.PageSize.Width * rightPage.PageSize.Height;
  1923. }
  1924. rightBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
  1925. startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
  1926. startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
  1927. if (PrintSettingsInfo.IsGrayscale)
  1928. {
  1929. rightBitmap = ToGray(rightBitmap, 0);
  1930. }
  1931. printBitmap = CombineBitmap(printBitmap, rightBitmap, startPoint);
  1932. }
  1933. }
  1934. }
  1935. BitmapSource = ToBitmapSource(printBitmap);
  1936. }
  1937. /// <summary>
  1938. /// 按照模式绘制指定纸张
  1939. /// </summary>
  1940. /// <param name="TargetPageIndex"></param>
  1941. private void PaintPageByCurrentPreviewIndex(int TargetPaperIndex)
  1942. {
  1943. if (PrintSettingsInfo.printModInfo.EnumPrintMod == EnumPrintMod.StatusSize)
  1944. {
  1945. SetPreviewExceptPoster();
  1946. PreviewModSize(TargetPaperList[TargetPaperIndex]);
  1947. }
  1948. else if (PrintSettingsInfo.printModInfo.EnumPrintMod == EnumPrintMod.StatusPoster)
  1949. {
  1950. PreviewModPoster(TargetPaperList[TargetPaperIndex]);
  1951. }
  1952. else if (PrintSettingsInfo.printModInfo.EnumPrintMod == EnumPrintMod.StatusMultiple)
  1953. {
  1954. SetPreviewExceptPoster();
  1955. PreviewModMultiple(TargetPaperList[TargetPaperIndex]);
  1956. }
  1957. else if (PrintSettingsInfo.printModInfo.EnumPrintMod == EnumPrintMod.StatusBooklet)
  1958. {
  1959. SetPreviewExceptPoster();
  1960. PreviewModBooklet(TargetPaperList[TargetPaperIndex]);
  1961. }
  1962. }
  1963. /// <summary>
  1964. /// 设置纸张集合
  1965. /// 大小模式:目标纸张和目标纸张页一致
  1966. /// 海报模式:一致
  1967. /// 多页模式: 一致
  1968. /// 小册子模式:不一致
  1969. /// 第a页-第b页:从第1页开始数,a页=(a-1)*2, b页=(b-1)*2
  1970. /// 面向:奇数页:paper%2 = 1
  1971. /// 背向:偶数页:Paper%2 = 0;
  1972. /// </summary>
  1973. public void SetPaperCollection()
  1974. {
  1975. if (PrintSettingsInfo.printModInfo.EnumPrintMod == EnumPrintMod.StatusSize ||
  1976. PrintSettingsInfo.printModInfo.EnumPrintMod == EnumPrintMod.StatusPoster ||
  1977. PrintSettingsInfo.printModInfo.EnumPrintMod == EnumPrintMod.StatusMultiple)
  1978. {
  1979. TargetPaperList.Clear();
  1980. TargetPaperList.Add(0);
  1981. for (int temp = 0; temp < PrintedPageCount; temp++)
  1982. {
  1983. TargetPaperList.Add(temp);
  1984. }
  1985. }
  1986. else
  1987. {
  1988. BookletInfo bookletInfo = (BookletInfo)PrintSettingsInfo.printModInfo;
  1989. if (bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusBothSides)
  1990. {
  1991. TargetPaperList.Clear();
  1992. TargetPaperList.Add(0);
  1993. for (int temp = bookletInfo.BeginPaperIndex * 2 - 2; temp < bookletInfo.EndPaperIndex * 2; temp++)
  1994. {
  1995. TargetPaperList.Add(temp);
  1996. }
  1997. }
  1998. else if (bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusFrontSideOnly)
  1999. {
  2000. TargetPaperList.Clear();
  2001. TargetPaperList.Add(0);
  2002. for (int temp = bookletInfo.BeginPaperIndex * 2 - 2; temp < bookletInfo.EndPaperIndex * 2; temp += 2)
  2003. {
  2004. TargetPaperList.Add(temp);
  2005. }
  2006. }
  2007. else
  2008. {
  2009. TargetPaperList.Clear();
  2010. TargetPaperList.Add(0);
  2011. for (int temp = bookletInfo.BeginPaperIndex * 2 - 1; temp < bookletInfo.EndPaperIndex * 2; temp += 2)
  2012. {
  2013. TargetPaperList.Add(temp);
  2014. }
  2015. }
  2016. }
  2017. }
  2018. public bool IsNavigationTarget(NavigationContext navigationContext)
  2019. {
  2020. return true;
  2021. }
  2022. public void OnNavigatedFrom(NavigationContext navigationContext)
  2023. {
  2024. this.printDocument.Dispose();
  2025. }
  2026. public void OnNavigatedTo(NavigationContext navigationContext)
  2027. {
  2028. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  2029. navigationContext.Parameters.TryGetValue<PrintSettingsInfo>(ParameterNames.PrintSettingsInfo, out PrintSettingsInfo);
  2030. navigationContext.Parameters.TryGetValue<string>("Unicode", out Unicode);
  2031. if (PDFViewer != null && PrintSettingsInfo != null)
  2032. {
  2033. SetPreviewExceptPoster();
  2034. CaculatePrintedPageCount();
  2035. SetPaperCollection();
  2036. PaintPageByCurrentPreviewIndex(1);
  2037. }
  2038. }
  2039. }
  2040. }