1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155 |
- using ComPDFKit.PDFPage;
- using ComPDFKitViewer.PdfViewer;
- using ImageMagick.Formats;
- using ImTools;
- using PDF_Office.EventAggregators;
- using PDF_Office.Helper;
- using PDF_Office.Model;
- using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePagePrinter;
- using Prism.Commands;
- using Prism.Events;
- using Prism.Mvvm;
- using Prism.Regions;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Drawing.Printing;
- using System.Linq;
- using System.Printing;
- using System.Reflection;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Annotations;
- using System.Windows.Controls;
- using System.Windows.Forms;
- using System.Windows.Media.Imaging;
- using System.Windows.Shell;
- using static Dropbox.Api.TeamLog.PaperDownloadFormat;
- namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter
- {
- public class HomePagePrinterDocumentContentViewModel : BindableBase, INavigationAware
- {
- private CPDFViewer PDFViewer;
- private PrintQueue documentPrintQueue;
- private PrintDocument printDocument = new PrintDocument();
- public PrintSettingsInfo PrintSettingsInfo;
- public IEventAggregator printDocumentEvent;
- private string Unicode = null;
- /// <summary>
- /// 海报模式:打印纸
- /// </summary>
- int posterPrintPaperIndex = 0;
- /// <summary>
- /// 海报模式,水平纸张数
- /// </summary>
- public int HorizonPaperCount = 0;
- /// <summary>
- /// 海报模式,垂直纸张数
- /// </summary>
- public int VerticalPaperCount = 0;
- ///<summary>
- ///海报模式当前水平纸
- /// </summary>
- public int HorizonPaperIndex = 0;
- ///<summary>
- ///海报模式当前垂直纸
- /// </summary>
- public int VerticalPaperIndex = 0;
- /// <summary>
- /// 海报模式,总打印纸张数
- /// </summary>
- public int WholePaperCountModPoster = 0;
- public double PosterRatio = 0;
- public int PrintIndex;
- ///<summary>
- ///打印纸dpi96, PDF dpi 72,需要缩放
- /// </summary>
- public double dpiRatio = 96.0 / 72.0;
- Bitmap blankPageBitmap;
- private string _paperWidth;
- public string PaperWidth
- {
- get => _paperWidth;
- set => SetProperty(ref _paperWidth, value);
- }
- private string _paperHeight;
- public string PaperHeight
- {
- get => _paperHeight;
- set => SetProperty(ref _paperHeight, value);
- }
- private string _viewBoxWidth;
- public string ViewBoxWidth
- {
- get => _viewBoxWidth;
- set => SetProperty(ref _viewBoxWidth, value);
- }
- private string _viewBoxHeight;
- public string ViewBoxHeight
- {
- get => _viewBoxHeight;
- set => SetProperty(ref _viewBoxHeight, value);
- }
- private BitmapSource _bitmapSource;
- /// <summary>
- /// 渲染后的图
- /// </summary>
- public BitmapSource BitmapSource
- {
- get { return _bitmapSource; }
- set { SetProperty(ref _bitmapSource, value); }
- }
- private int _targetPaperIndex = 1;
- public int TargetPaperIndex
- {
- get => _targetPaperIndex;
- set
- {
- SetProperty(ref _targetPaperIndex, value);
- JumpToSelectedPage();
- }
- }
- /// <summary>
- /// 目标纸张
- /// TargetPaper[纸张编号]=正确的纸张
- /// </summary>
- public List<int> TargetPaperList = new List<int>();
- /// <summary>
- /// 打印的总页数
- /// </summary>
- private int _printedPageCount;
- public int PrintedPageCount
- {
- get => _printedPageCount;
- set => SetProperty(ref _printedPageCount, value);
- }
- public DelegateCommand<object> TurnPageCommand { get; set; }
- public DelegateCommand<object> JumpPageCommand { get; set; }
- public HomePagePrinterDocumentContentViewModel(IEventAggregator eventAggregator)
- {
- this.printDocumentEvent = eventAggregator;
- printDocumentEvent.GetEvent<SendPrintSettingsInfoEvent>().Subscribe(RecvPrintSettingsInfo, e => e.Unicode == Unicode);
- printDocumentEvent.GetEvent<SendPrintQueueEvent>().Subscribe(RecvPrintQueue, e => e.Unicode == Unicode);
- TurnPageCommand = new DelegateCommand<object>(TurnPage);
- JumpPageCommand = new DelegateCommand<object>(JumpPage);
- }
- public void TurnPage(object e)
- {
- var btn = e as System.Windows.Controls.Button;
- if (btn != null)
- {
- if (btn.Name == "PrePageBtn")
- {
- if (TargetPaperIndex >= 2)
- {
- TargetPaperIndex--;
- }
- }
- else
- {
- if (TargetPaperIndex < PrintedPageCount)
- {
- TargetPaperIndex++;
- }
- }
- }
- }
- public async void RecvPrintQueue(PrintQueueWithUnicode printQueueWithUnicode)
- {
- if (printQueueWithUnicode.printQueue != null)
- {
- PrintIndex = 0;
- this.documentPrintQueue = printQueueWithUnicode.printQueue;
- var printDocument = new PrintDocument();
- var printTicket = new PrintTicket();
- printDocument.PrinterSettings.PrinterName = printQueueWithUnicode.printQueue.Name;
- //TODO 这里要改
- if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
- {
- printDocument.DefaultPageSettings.Landscape = false;
- }
- else if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusLandscape)
- {
- printDocument.DefaultPageSettings.Landscape = true;
- }
- if (!PrintSettingsInfo.IsGrayscale && printQueueWithUnicode.printQueue.GetPrintCapabilities().OutputColorCapability.Contains(OutputColor.Color))
- {
- printDocument.DefaultPageSettings.Color = true;
- }
- else
- {
- printDocument.DefaultPageSettings.Color = false;
- }
- if (PrintSettingsInfo.IsDuplex || PrintSettingsInfo.printModInfo.EnumPrintMod == EnumPrintMod.StatusBooklet)
- {
- if (printQueueWithUnicode.printQueue.GetPrintCapabilities().DuplexingCapability.Contains(Duplexing.TwoSidedLongEdge))
- {
- printDocument.PrinterSettings.Duplex = Duplex.Horizontal;
- }
- else if (printQueueWithUnicode.printQueue.GetPrintCapabilities().DuplexingCapability.Contains(Duplexing.TwoSidedShortEdge))
- {
- printDocument.PrinterSettings.Duplex = Duplex.Vertical;
- }
- else
- {
- printTicket.Duplexing = Duplexing.OneSided;
- //TODO 手动双面打印
- }
- }
- else
- {
- printTicket.Duplexing = Duplexing.OneSided;
- }
- printQueueWithUnicode.printQueue.DefaultPrintTicket = printTicket;
- printDocument.DefaultPageSettings.PaperSize = new PaperSize(PrintSettingsInfo.PageMediaSize.PageMediaSizeName.ToString(), (int)(PrintSettingsInfo.PageMediaSize.Width * 100 / 96), (int)(PrintSettingsInfo.PageMediaSize.Height * 100 / 96));
- posterPrintPaperIndex = 0;
- printDocument.PrintPage += PrintDocumentByCurrentSettings;
- await Task.Run(() =>
- {
- printDocument.Print();
- });
- }
- }
- private Bitmap Crop(Bitmap src, Rectangle rectangle)
- {
- Bitmap targetBitmap = new Bitmap(rectangle.Width, rectangle.Height);
- using (Graphics g = Graphics.FromImage(targetBitmap))
- {
- g.DrawImage(src, new Rectangle(0, 0, targetBitmap.Width, targetBitmap.Height), rectangle, GraphicsUnit.Pixel);
- }
- return targetBitmap;
- }
- private void PrintDocumentModSize(PrintPageEventArgs e)
- {
- SizeInfo sizeInfo = (SizeInfo)PrintSettingsInfo.printModInfo;
- CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(PrintSettingsInfo.PageRangeList[PrintIndex]);
- System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
- if (cPDFPage != null)
- {
- Bitmap printBitmap = new Bitmap((int)(float.Parse(PaperWidth)), (int)float.Parse(PaperHeight));
- Bitmap bitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)(cPDFPage.PageSize.Width * dpiRatio), (int)(cPDFPage.PageSize.Height * dpiRatio), PrintSettingsInfo.PageRangeList[PrintIndex], PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- if (PrintSettingsInfo.IsGrayscale)
- {
- bitmap = ToGray(bitmap, 0);
- }
- ///<summary>
- ///自适应模式
- /// </summary>
- if (sizeInfo.EnumSizeType == EnumSizeType.StatusAdaptive)
- {
- double mmToDpiRatio = 96 / 25.4;
- int resizedHeight = 0;
- int resizedWidth = 0;
- ///重设大小
- if ((float)bitmap.Height / (float)bitmap.Width >= (float.Parse(PaperHeight) / float.Parse(PaperWidth)))
- {
- resizedHeight = (int)(float.Parse(PaperHeight) * mmToDpiRatio);
- resizedWidth = (int)((float.Parse(PaperHeight) / bitmap.Height * bitmap.Width) * mmToDpiRatio);
- }
- else
- {
- resizedWidth = (int)(float.Parse(PaperWidth) * mmToDpiRatio);
- resizedHeight = (int)((float.Parse(PaperWidth) / bitmap.Width * bitmap.Height) * mmToDpiRatio);
- }
- bitmap = Resize(bitmap, resizedWidth, resizedHeight);
- startPoint.X = (blankPageBitmap.Width - resizedWidth) / 2;
- startPoint.Y = (blankPageBitmap.Height - resizedHeight) / 2;
- printBitmap = CombineBitmap(blankPageBitmap, bitmap, startPoint);
- e.Graphics.DrawImage(printBitmap, e.PageBounds);
- }
- else if (sizeInfo.EnumSizeType == EnumSizeType.StatusActural)
- {
- startPoint.X = (blankPageBitmap.Width - bitmap.Width) / 2;
- startPoint.Y = (blankPageBitmap.Height - bitmap.Height) / 2;
- printBitmap = CombineBitmap(blankPageBitmap, bitmap, startPoint);
- e.Graphics.DrawImage(printBitmap, e.PageBounds);
- }
- else if (sizeInfo.EnumSizeType == EnumSizeType.StatusCustomized)
- {
- bitmap = Resize(bitmap, (int)(bitmap.Width * (sizeInfo.DisplayRatio / 100.0)), (int)(bitmap.Height * (sizeInfo.DisplayRatio / 100.0)));
- startPoint.X = (blankPageBitmap.Width - bitmap.Width) / 2;
- startPoint.Y = (blankPageBitmap.Height - bitmap.Height) / 2;
- printBitmap = CombineBitmap(blankPageBitmap, bitmap, startPoint);
- e.Graphics.DrawImage(printBitmap, e.PageBounds);
- }
- bitmap.Dispose();
- printBitmap.Dispose();
- }
- if (PrintIndex < PrintedPageCount - 1)
- {
- PrintIndex++;
- e.HasMorePages = true;
- }
- else
- {
- e.HasMorePages = false;
- }
- }
- private void PrintDocumentModPoster(PrintPageEventArgs e)
- {
- PosterInfo posterInfo = (PosterInfo)PrintSettingsInfo.printModInfo;
- var pagebound = printDocument.DefaultPageSettings.Bounds;
- System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
- double effectiveHeight = (double)PrintSettingsInfo.PageMediaSize.Height;
- double effectiveWidth = (double)PrintSettingsInfo.PageMediaSize.Width;
- double PreviewHeight = 0;
- double PreviewWidth = 0;
- Bitmap bitmap = null;
- Bitmap displayBitmap = null;
- effectiveHeight = effectiveHeight - 2 * posterInfo.OverLap * 0.5 * 96 / 25.4;
- effectiveWidth = effectiveWidth - 2 * posterInfo.OverLap * 0.5 * 96 / 25.4;
- if (posterInfo.HasLabel || posterInfo.HasCutMarks)
- {
- effectiveHeight = effectiveHeight - 0.5 * 96 / 25.4;
- effectiveWidth = effectiveWidth - 0.5 * 96 / 25.4;
- }
- ///无论是拆分还是平铺,页面缩放比例都会在预览中完成
- ///先渲染整图
- ///得到effective域大小
- ///根据effective的量和重叠量计算开始的bitmap
- ///加字和标记
- ///字加在顶部,标记加在四角
- ///结束后翻页
- ///对于Tile页数不定,但是比例相等
- ///对于split页数固定,但是比例变化
- ///平铺模式:
- ///从第一页开始,先计算当前页总纸张数
- ///根据纸张数打印
- ///打印到位时页数进一
- ///页数到顶,纸数到顶打印结束
- if (posterInfo.EnumPosterMod == EnumPosterMod.StatusTile)
- {
- CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(PrintSettingsInfo.PageRangeList[posterPrintPaperIndex]);
- if (cPDFPage != null)
- {
- 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);
- HorizonPaperCount = (int)Math.Ceiling((double)bitmap.Width / effectiveWidth);
- VerticalPaperCount = (int)Math.Ceiling(((double)bitmap.Height / effectiveHeight));
- int wholeCountSinglePage = HorizonPaperCount * VerticalPaperCount;//每一轮的总页数
- PreviewWidth = (double)(effectiveWidth * HorizonPaperCount);
- PreviewHeight = (double)(effectiveHeight * VerticalPaperCount);
- displayBitmap = new Bitmap((int)PreviewWidth, (int)PreviewHeight);
- startPoint.X = (int)((PreviewWidth - bitmap.Width) / 2);
- startPoint.Y = (int)((PreviewHeight - bitmap.Height) / 2);
- if (PrintSettingsInfo.IsGrayscale)
- {
- bitmap = ToGray(bitmap, 0);
- }
- //取到和渲染一样的Bitmap
- displayBitmap = CombineBitmap(displayBitmap, bitmap, startPoint);
- ///当前页未打印完
- if (HorizonPaperIndex * VerticalPaperIndex < wholeCountSinglePage - 1)
- {
- ///取有效尺寸和重叠,边沿合并
- ///有效尺寸+重叠边
- Rectangle rectangle = new Rectangle((int)(HorizonPaperIndex * effectiveHeight), (int)(VerticalPaperIndex * effectiveHeight), (int)((HorizonPaperIndex + 1) * effectiveWidth), (int)((VerticalPaperIndex + 1) * effectiveHeight));
- ///左重叠
- if (HorizonPaperIndex != 0)
- {
- rectangle.Location = new System.Drawing.Point((int)(rectangle.Location.X - posterInfo.OverLap * 96 / 24.5), rectangle.Location.Y);
- }
- ///上重叠
- if (VerticalPaperIndex != 0)
- {
- rectangle.Size = new System.Drawing.Size(rectangle.Size.Width, (int)(rectangle.Size.Height + posterInfo.OverLap * 96 / 24.5));
- }
- ///右重叠
- if (HorizonPaperIndex != HorizonPaperCount)
- {
- rectangle.Size = new System.Drawing.Size((int)(rectangle.Size.Width + posterInfo.OverLap * 96 / 24.5), rectangle.Size.Height);
- }
- ///下重叠
- if (VerticalPaperIndex != VerticalPaperCount)
- {
- rectangle.Location = new System.Drawing.Point(rectangle.Location.X, (int)(rectangle.Location.Y - posterInfo.OverLap * 96 / 24.5));
- }
- Bitmap currentPrintbitmap = Crop(displayBitmap, rectangle);
- ///TODO暂时不管,先画
- e.Graphics.DrawImage(currentPrintbitmap, e.PageBounds);
- if (HorizonPaperIndex < HorizonPaperCount - 1)
- {
- HorizonPaperIndex++;
- e.HasMorePages = true;
- }
- else if (VerticalPaperIndex < VerticalPaperCount - 1)
- {
- HorizonPaperIndex = 0;
- VerticalPaperIndex++;
- e.HasMorePages = true;
- }
- else if (posterPrintPaperIndex < PrintedPageCount - 1)
- {
- ///当前总文件未打印完
- posterPrintPaperIndex++;
- HorizonPaperIndex = 0;
- VerticalPaperIndex = 0;
- e.HasMorePages = true;
- }
- else
- {
- e.HasMorePages = false;
- }
- }
- }
- }
- ///拆分模式
- ///该模式下
- ///渲染图片到指定大小
- ///直接计算所有需要打印的纸张数
- ///计算单页打印的纸张数
- ///进位
- ///根据当前水平垂直序号裁切图片
- ///继续打印表征:当前纸张序号小于总计纸张序号
- else
- {
- int wholeCountSinglePage = posterInfo.HorizontalSheetNumber * posterInfo.VerticalSheetNumber;//每一轮的总页数
- WholePaperCountModPoster = wholeCountSinglePage * PrintedPageCount;//总计打印纸数
- int PrintPageIndex = PrintIndex / wholeCountSinglePage;//当前打印纸张所在海报页 从0开始
- int PrintIndexInCurrentPage = PrintIndex % wholeCountSinglePage;//当前海报页中,打印纸的序号 从0开始
- HorizonPaperIndex = PrintIndexInCurrentPage % HorizonPaperCount;//行中的第几个
- VerticalPaperIndex = PrintIndexInCurrentPage / HorizonPaperCount;//第几行
- CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(PrintSettingsInfo.PageRangeList[PrintPageIndex]);//取得当前海报页
- PreviewWidth = (double)(effectiveWidth * HorizonPaperCount);
- PreviewHeight = (double)(effectiveHeight * VerticalPaperCount);
- displayBitmap = new Bitmap((int)PreviewWidth, (int)PreviewHeight);
- double resizedHeight = 0;
- double resizedWidth = 0;
- if ((cPDFPage.PageSize.Height / cPDFPage.PageSize.Width) >= PreviewHeight / PreviewWidth)
- {
- resizedHeight = PreviewHeight;
- resizedWidth = PreviewHeight / cPDFPage.PageSize.Height * cPDFPage.PageSize.Width;
- }
- else
- {
- resizedWidth = PreviewWidth;
- resizedHeight = PreviewWidth / cPDFPage.PageSize.Width * cPDFPage.PageSize.Height;
- }
- bitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, PrintSettingsInfo.PageRangeList[PrintPageIndex], PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- startPoint.X = (int)((PreviewWidth - bitmap.Width) / 2);
- startPoint.Y = (int)((PreviewHeight - bitmap.Height) / 2);
- if (PrintSettingsInfo.IsGrayscale)
- {
- bitmap = ToGray(bitmap, 0);
- }
- displayBitmap = CombineBitmap(displayBitmap, bitmap, startPoint);
- displayBitmap = CombineBitmap(displayBitmap, bitmap, startPoint);
- ///取有效尺寸和重叠,边沿合并
- ///有效尺寸+重叠边
- Rectangle rectangle = new Rectangle((int)(HorizonPaperIndex * effectiveHeight), (int)(VerticalPaperIndex * effectiveHeight), (int)((HorizonPaperIndex + 1) * effectiveWidth), (int)((VerticalPaperIndex + 1) * effectiveHeight));
- ///左重叠
- if (HorizonPaperIndex != 0)
- {
- rectangle.Location = new System.Drawing.Point((int)(rectangle.Location.X - posterInfo.OverLap * 96 / 24.5), rectangle.Location.Y);
- }
- ///上重叠
- if (VerticalPaperIndex != 0)
- {
- rectangle.Size = new System.Drawing.Size(rectangle.Size.Width, (int)(rectangle.Size.Height + posterInfo.OverLap * 96 / 24.5));
- }
- ///右重叠
- if (HorizonPaperIndex != HorizonPaperCount)
- {
- rectangle.Size = new System.Drawing.Size((int)(rectangle.Size.Width + posterInfo.OverLap * 96 / 24.5), rectangle.Size.Height);
- }
- ///下重叠
- if (VerticalPaperIndex != VerticalPaperCount)
- {
- rectangle.Location = new System.Drawing.Point(rectangle.Location.X, (int)(rectangle.Location.Y - posterInfo.OverLap * 96 / 24.5));
- }
- Bitmap currentPrintbitmap = Crop(displayBitmap, rectangle);
- ///TODO暂时不管,先画
- e.Graphics.DrawImage(currentPrintbitmap, e.PageBounds);
- if (PrintIndex < WholePaperCountModPoster - 1)
- {
- PrintIndex++;
- e.HasMorePages = true;
- }
- else
- {
- e.HasMorePages = false;
- }
- }
- }
- private void PrintDocumentModMultiple(PrintPageEventArgs e)
- {
- MultipleInfo multipleInfo = (MultipleInfo)PrintSettingsInfo.printModInfo;
- int singlePageHeight;
- int singlePageWidth;
- Bitmap printBitmap;
- int currentPage = 0;
- if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
- {
- singlePageHeight = (int)(PrintSettingsInfo.PageMediaSize.Height / multipleInfo.VerticalPageNumber);
- singlePageWidth = (int)(PrintSettingsInfo.PageMediaSize.Width / multipleInfo.HorizontalPageNumber);
- printBitmap = new Bitmap((int)PrintSettingsInfo.PageMediaSize.Width, (int)PrintSettingsInfo.PageMediaSize.Height);
- }
- else
- {
- singlePageHeight = (int)(PrintSettingsInfo.PageMediaSize.Width / multipleInfo.VerticalPageNumber);
- singlePageWidth = (int)(PrintSettingsInfo.PageMediaSize.Height / multipleInfo.HorizontalPageNumber);
- printBitmap = new Bitmap((int)PrintSettingsInfo.PageMediaSize.Height, (int)PrintSettingsInfo.PageMediaSize.Width);
- }
- System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
- int startPage = PrintSettingsInfo.PageRangeList[PrintIndex * (multipleInfo.VerticalPageNumber) * (multipleInfo.HorizontalPageNumber)];
- int resizedHeight = 0;
- int resizedWidth = 0;
- bool isNoSrcPage = false;
- Bitmap pageBitmap;
- ///水平:横向排列,先横后纵,纵轴不变横轴翻转
- if (multipleInfo.EnumPageOrder == EnumPageOrder.StatusHorizontalOrder || multipleInfo.EnumPageOrder == EnumPageOrder.StatusHorizontalReverseOrder)
- {
- for (int tempVerticalIndex = 0; tempVerticalIndex < multipleInfo.VerticalPageNumber; tempVerticalIndex++)
- {
- startPoint.Y = tempVerticalIndex * singlePageHeight;
- for (int tempHorizontalIndex = 0; tempHorizontalIndex < multipleInfo.HorizontalPageNumber; tempHorizontalIndex++)
- {
- try
- {
- currentPage =
- PrintSettingsInfo.PageRangeList[PrintIndex *
- (multipleInfo.VerticalPageNumber) *
- (multipleInfo.HorizontalPageNumber) +
- (tempVerticalIndex * multipleInfo.HorizontalPageNumber) +
- tempHorizontalIndex];
- }
- catch
- {
- break;
- }
- //横向
- if (multipleInfo.EnumPageOrder == EnumPageOrder.StatusHorizontalOrder)
- {
- startPoint.X = tempHorizontalIndex * singlePageWidth;
- }
- //横向倒序
- else
- {
- startPoint.X = (multipleInfo.HorizontalPageNumber - tempHorizontalIndex - 1) * singlePageWidth;
- }
- CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(currentPage);
- if (cPDFPage == null)
- {
- isNoSrcPage = true;
- break;
- }
- pageBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)(cPDFPage.PageSize.Width), (int)(cPDFPage.PageSize.Height), currentPage, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- ///重设大小
- if ((float)pageBitmap.Height / (float)pageBitmap.Width >= ((float)singlePageHeight / (float)singlePageWidth))
- {
- resizedHeight = (int)(singlePageHeight * 0.96);
- resizedWidth = (int)((float)singlePageHeight / pageBitmap.Height * pageBitmap.Width * 0.96);
- }
- else
- {
- resizedWidth = (int)(singlePageWidth * 0.96);
- resizedHeight = (int)((float)singlePageWidth / pageBitmap.Width * pageBitmap.Height * 0.96);
- }
- pageBitmap = Resize(pageBitmap, (int)resizedWidth, (int)resizedHeight);
- startPoint.X += (singlePageWidth - resizedWidth) / 2;
- startPoint.Y += (singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- pageBitmap = ToGray(pageBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, pageBitmap, startPoint);
- //绘制边界
- if (PrintSettingsInfo.IsPrintPageBorde)
- {
- using (Pen pen = new Pen(Color.Black, 4))
- {
- Graphics g = Graphics.FromImage(printBitmap);
- g.DrawRectangle(pen, new Rectangle(startPoint.X, startPoint.Y, resizedWidth, resizedHeight));
- g.Dispose();
- }
- }
- startPoint.Y -= (singlePageHeight - resizedHeight) / 2;
- }
- if (isNoSrcPage)
- {
- isNoSrcPage = false;
- break;
- }
- }
- }
- //垂直:纵向排列,先纵后横,纵轴不变横轴翻转
- else
- {
- for (int tempHorizontalIndex = 0; tempHorizontalIndex < multipleInfo.HorizontalPageNumber; tempHorizontalIndex++)
- {
- //纵向,垂直排列时先移位
- if (multipleInfo.EnumPageOrder == EnumPageOrder.StatusVerticalOrder)
- {
- startPoint.X = tempHorizontalIndex * singlePageWidth;
- }
- //纵向倒序
- else
- {
- startPoint.X = (multipleInfo.HorizontalPageNumber - tempHorizontalIndex - 1) * singlePageWidth;
- }
- for (int tempVerticalIndex = 0; tempVerticalIndex < multipleInfo.VerticalPageNumber; tempVerticalIndex++)
- {
- startPoint.Y = tempVerticalIndex * singlePageHeight;
- try
- {
- currentPage = PrintSettingsInfo.PageRangeList[PrintIndex *
- (multipleInfo.VerticalPageNumber) *
- (multipleInfo.HorizontalPageNumber) +
- (tempHorizontalIndex * multipleInfo.VerticalPageNumber) +
- tempVerticalIndex];
- }
- catch
- {
- break;
- }
- CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(currentPage);
- if (cPDFPage == null)
- {
- isNoSrcPage = true;
- break;
- }
- pageBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)(cPDFPage.PageSize.Width), (int)(cPDFPage.PageSize.Height), currentPage, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- Graphics graphics = Graphics.FromImage(pageBitmap);
- ///重设大小
- if ((float)pageBitmap.Height / (float)pageBitmap.Width >= ((float)singlePageHeight / (float)singlePageWidth))
- {
- resizedHeight = (int)(singlePageHeight * 0.96);
- resizedWidth = (int)((float)singlePageHeight / pageBitmap.Height * pageBitmap.Width * 0.96);
- }
- else
- {
- resizedWidth = (int)(singlePageWidth * 0.96);
- resizedHeight = (int)((float)singlePageWidth / pageBitmap.Width * pageBitmap.Height * 0.96);
- }
- pageBitmap = Resize(pageBitmap, (int)resizedWidth, (int)resizedHeight);
- graphics.Dispose();
- startPoint.X += (singlePageWidth - resizedWidth) / 2;
- startPoint.Y += (singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- pageBitmap = ToGray(pageBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, pageBitmap, startPoint);
- //绘制边界
- if (PrintSettingsInfo.IsPrintPageBorde)
- {
- using (Pen pen = new Pen(Color.Black, 4))
- {
- Graphics g = Graphics.FromImage(printBitmap);
- g.DrawRectangle(pen, new Rectangle(startPoint.X, startPoint.Y, resizedWidth, resizedHeight));
- g.Dispose();
- }
- }
- startPoint.X -= (singlePageWidth - resizedWidth) / 2;
- }
- if (isNoSrcPage)
- {
- isNoSrcPage = false;
- break;
- }
- }
- }
- e.Graphics.DrawImage(printBitmap, e.PageBounds);
- if (PrintIndex < PrintedPageCount - 1)
- {
- PrintIndex++;
- e.HasMorePages = true;
- }
- else
- {
- e.HasMorePages = false;
- }
- }
- private void PrintDocumentModBooklet(PrintPageEventArgs e)
- {
- BookletInfo bookletInfo = (BookletInfo)PrintSettingsInfo.printModInfo;
- ///空白画布
- Bitmap printBitmap;
- double singlePageHeight = 0;
- double singlePageWidth = 0;
- if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
- {
- printBitmap = new Bitmap((int)PrintSettingsInfo.PageMediaSize.Width, (int)PrintSettingsInfo.PageMediaSize.Height);
- singlePageHeight = (double)PrintSettingsInfo.PageMediaSize.Height;
- singlePageWidth = (double)(PrintSettingsInfo.PageMediaSize.Width / 2);
- }
- else
- {
- printBitmap = new Bitmap((int)PrintSettingsInfo.PageMediaSize.Height, (int)PrintSettingsInfo.PageMediaSize.Width);
- singlePageHeight = (double)PrintSettingsInfo.PageMediaSize.Width;
- singlePageWidth = (double)(PrintSettingsInfo.PageMediaSize.Height / 2);
- }
- System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
- int maxPaperNumber = (PDFViewer.Document.PageCount % 4 == 0) ? (PDFViewer.Document.PageCount / 4) : (PDFViewer.Document.PageCount / 4 + 1);
- Bitmap leftBitmap = null;
- Bitmap rightBitmap = null;
- double resizedHeight = 0;
- double resizedWidth = 0;
- CPDFPage leftPage = null;
- CPDFPage rightPage = null;
- int leftPageIndex = 0;
- int rightPageIndex = 0;
- ///<summary>
- ///左装订与右装订顺序相反
- /// </summary>
- if (bookletInfo.EnumBookletBinding == EnumBookletBinding.StatusLeft)
- {
- ///奇数页:
- ///右面为页面序号
- ///左面为[(总打印纸数*4)+1-打印页右侧页序号]
- if ((PrintIndex + 1) % 2 == 1)
- {
- try
- {
- leftPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - PrintIndex - 1];
- leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
- }
- catch
- {
- leftPage = null;
- }
- rightPageIndex = PrintSettingsInfo.PageRangeList[PrintIndex];
- //左面
- if (leftPage != null)
- {
- ///重设大小
- if ((leftPage.PageSize.Height / leftPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = singlePageHeight;
- resizedWidth = singlePageHeight / leftPage.PageSize.Height * leftPage.PageSize.Width;
- }
- else
- {
- resizedWidth = singlePageWidth;
- resizedHeight = singlePageWidth / leftPage.PageSize.Width * leftPage.PageSize.Height;
- }
- leftBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- leftBitmap = ToGray(leftBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, leftBitmap, startPoint);
- }
- //右面
- rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
- startPoint.X = (int)singlePageWidth;
- if (rightPage != null)
- {
- ///重设大小
- if ((rightPage.PageSize.Height / rightPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = singlePageHeight;
- resizedWidth = singlePageHeight / rightPage.PageSize.Height * rightPage.PageSize.Width;
- }
- else
- {
- resizedWidth = singlePageWidth;
- resizedHeight = singlePageWidth / rightPage.PageSize.Width * rightPage.PageSize.Height;
- }
- rightBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- rightBitmap = ToGray(rightBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, rightBitmap, startPoint);
- }
- }
- /// 偶数页:
- /// 左面为页面序号
- /// 右面为[(总打印纸数*4)+1-打印页右侧页序号]
- else
- {
- leftPageIndex = PrintSettingsInfo.PageRangeList[PrintIndex];
- //左面
- leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
- if (leftPage != null)
- {
- ///重设大小
- if ((leftPage.PageSize.Height / leftPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = singlePageHeight;
- resizedWidth = singlePageHeight / leftPage.PageSize.Height * leftPage.PageSize.Width;
- }
- else
- {
- resizedWidth = singlePageWidth;
- resizedHeight = singlePageWidth / leftPage.PageSize.Width * leftPage.PageSize.Height;
- }
- leftBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)(resizedWidth), (int)resizedHeight, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- leftBitmap = ToGray(leftBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, leftBitmap, startPoint);
- }
- //右面
- try
- {
- rightPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - 1 - PrintIndex];
- rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
- }
- catch
- {
- rightPage = null;
- }
- startPoint.X = (int)singlePageWidth;
- if (rightPage != null)
- {
- ///重设大小
- if ((rightPage.PageSize.Height / rightPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = singlePageHeight;
- resizedWidth = singlePageHeight / rightPage.PageSize.Height * rightPage.PageSize.Width;
- }
- else
- {
- resizedWidth = singlePageWidth;
- resizedHeight = singlePageWidth / rightPage.PageSize.Width * rightPage.PageSize.Height;
- }
- rightBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- rightBitmap = ToGray(rightBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, rightBitmap, startPoint);
- }
- }
- }
- ///<summary>
- ///右装订打印
- /// </summary>
- if (bookletInfo.EnumBookletBinding == EnumBookletBinding.StatusRight)
- {
- ///奇数页:
- ///右面为页面序号
- ///左面为[(总打印纸数*4)+1-打印页右侧页序号]
- if ((PrintIndex + 1) % 2 == 0)
- {
- try
- {
- leftPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - PrintIndex - 1];
- leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
- }
- catch
- {
- leftPage = null;
- }
- rightPageIndex = PrintSettingsInfo.PageRangeList[PrintIndex];
- //左面
- if (leftPage != null)
- {
- ///重设大小
- if ((leftPage.PageSize.Height / leftPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = singlePageHeight;
- resizedWidth = singlePageHeight / leftPage.PageSize.Height * leftPage.PageSize.Width;
- }
- else
- {
- resizedWidth = singlePageWidth;
- resizedHeight = singlePageWidth / leftPage.PageSize.Width * leftPage.PageSize.Height;
- }
- leftBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- leftBitmap = ToGray(leftBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, leftBitmap, startPoint);
- }
- //右面
- rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
- startPoint.X = (int)singlePageWidth;
- if (rightPage != null)
- {
- ///重设大小
- if ((rightPage.PageSize.Height / rightPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = singlePageHeight;
- resizedWidth = singlePageHeight / rightPage.PageSize.Height * rightPage.PageSize.Width;
- }
- else
- {
- resizedWidth = singlePageWidth;
- resizedHeight = singlePageWidth / rightPage.PageSize.Width * rightPage.PageSize.Height;
- }
- rightBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- rightBitmap = ToGray(rightBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, rightBitmap, startPoint);
- }
- }
- /// 偶数页:
- /// 左面为页面序号
- /// 右面为[(总打印纸数*4)+1-打印页右侧页序号]
- else
- {
- leftPageIndex = PrintSettingsInfo.PageRangeList[PrintIndex];
- //左面
- leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
- if (leftPage != null)
- {
- ///重设大小
- if ((leftPage.PageSize.Height / leftPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = singlePageHeight;
- resizedWidth = singlePageHeight / leftPage.PageSize.Height * leftPage.PageSize.Width;
- }
- else
- {
- resizedWidth = singlePageWidth;
- resizedHeight = singlePageWidth / leftPage.PageSize.Width * leftPage.PageSize.Height;
- }
- leftBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)(resizedWidth), (int)resizedHeight, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- leftBitmap = ToGray(leftBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, leftBitmap, startPoint);
- }
- //右面
- try
- {
- rightPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - 1 - PrintIndex];
- rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
- }
- catch
- {
- rightPage = null;
- }
- startPoint.X = (int)singlePageWidth;
- if (rightPage != null)
- {
- ///重设大小
- if ((rightPage.PageSize.Height / rightPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = singlePageHeight;
- resizedWidth = singlePageHeight / rightPage.PageSize.Height * rightPage.PageSize.Width;
- }
- else
- {
- resizedWidth = singlePageWidth;
- resizedHeight = singlePageWidth / rightPage.PageSize.Width * rightPage.PageSize.Height;
- }
- rightBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- rightBitmap = ToGray(rightBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, rightBitmap, startPoint);
- }
- }
- }
- e.Graphics.DrawImage(printBitmap, e.PageBounds);
- if (PrintIndex < PrintedPageCount - 1)
- {
- PrintIndex++;
- e.HasMorePages = true;
- }
- else
- {
- e.HasMorePages = false;
- }
- }
- private void PrintDocumentByCurrentSettings(object sender, PrintPageEventArgs e)
- {
- switch (PrintSettingsInfo.printModInfo.EnumPrintMod)
- {
- case EnumPrintMod.StatusPoster:
- PrintDocumentModPoster(e);
- break;
- case EnumPrintMod.StatusMultiple:
- PrintDocumentModMultiple(e);
- break;
- case EnumPrintMod.StatusBooklet:
- PrintDocumentModBooklet(e);
- break;
- case EnumPrintMod.StatusSize:
- default:
- PrintDocumentModSize(e);
- break;
- }
- }
- //跳转指定页
- public void JumpToSelectedPage()
- {
- if (TargetPaperIndex >= 1 && TargetPaperIndex <= PrintedPageCount)
- {
- PaintPageByCurrentPreviewIndex(TargetPaperIndex);
- }
- }
- //跳转指定页
- public void JumpPage(object e)
- {
- var txt = e as System.Windows.Controls.TextBox;
- if (txt.Text != null)
- {
- try
- {
- TargetPaperIndex = Convert.ToInt32(txt.Text);
- }
- catch
- {
- TargetPaperIndex = 1;
- }
- }
- if (TargetPaperIndex >= 1 && TargetPaperIndex <= PrintedPageCount)
- {
- PaintPageByCurrentPreviewIndex(TargetPaperIndex);
- }
- }
- public void RecvPrintSettingsInfo(PrintSettingsInfoWithUnicode printSettingsInfoWIthUnicode)
- {
- if (PDFViewer != null && printSettingsInfoWIthUnicode.printSettingsInfo != null)
- {
- CaculatePrintedPageCount();
- SetPaperCollection();
- TargetPaperIndex = 1;
- PaintPageByCurrentPreviewIndex(TargetPaperIndex);
- }
- }
- public void SetViewBox(double height, double width)
- {
- if (height / width >= (248.0 / 180.0))
- {
- ViewBoxHeight = "248.0";
- ViewBoxWidth = (width / height * 248.0).ToString();
- }
- else
- {
- ViewBoxWidth = "180.0";
- ViewBoxHeight = (height / width * 180.0).ToString();
- }
- }
- /// <summary>
- /// bitmap->bitmapsource转换器,用于显示到image控件
- /// </summary>
- /// <param name="bmp">
- /// Bitmap格式图片资源
- /// </param>
- /// <returns></returns>
- public static BitmapSource ToBitmapSource(System.Drawing.Bitmap bmp)
- {
- IntPtr ptr = bmp.GetHbitmap();//obtain the Hbitmap
- BitmapSource bmpsrc = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap
- (
- ptr,
- IntPtr.Zero,
- new Int32Rect(0, 0, bmp.Width, bmp.Height),
- System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()
- );
- return bmpsrc;
- }
- public Bitmap ResizeBitmap(Bitmap bitmap)
- {
- Resize(bitmap, (int)float.Parse(ViewBoxHeight), (int)float.Parse(ViewBoxWidth));
- return bitmap;
- }
- /// <summary>
- /// 创建空白底图
- /// </summary>
- private void CreateBlankBitmap()
- {
- if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
- {
- blankPageBitmap = new Bitmap((int)PrintSettingsInfo.PageMediaSize.Width, (int)PrintSettingsInfo.PageMediaSize.Height);
- }
- else
- {
- blankPageBitmap = new Bitmap((int)PrintSettingsInfo.PageMediaSize.Height, (int)PrintSettingsInfo.PageMediaSize.Width);
- }
- }
- /// <summary>
- /// 设置预览(除海报)
- /// </summary>
- public void SetPreviewExceptPoster()
- {
- if (PrintSettingsInfo.printModInfo.EnumPrintMod != EnumPrintMod.StatusPoster)
- {
- if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
- {
- PaperWidth = String.Format("{0:F1}", (PrintSettingsInfo.PageMediaSize.Width) / 96 * 25.4);
- PaperHeight = String.Format("{0:F1}", (PrintSettingsInfo.PageMediaSize.Height) / 96 * 25.4);
- }
- else
- {
- PaperHeight = String.Format("{0:F1}", (PrintSettingsInfo.PageMediaSize.Width) / 96 * 25.4);
- PaperWidth = String.Format("{0:F1}", (PrintSettingsInfo.PageMediaSize.Height) / 96 * 25.4);
- }
- SetViewBox(double.Parse(PaperHeight), double.Parse(PaperWidth));
- }
- else
- {
- }
- CreateBlankBitmap();
- }
- /// <summary>
- /// 直接Resize到给定大小
- /// </summary>
- /// <param name="input"></param>
- /// <param name="targetWidth"></param>
- /// <param name="targetHeight"></param>
- /// <returns></returns>
- public static Bitmap Resize(Bitmap input, int targetWidth, int targetHeight)
- {
- try
- {
- var actualBitmap = new Bitmap(targetWidth, targetHeight);
- var g = Graphics.FromImage(actualBitmap);
- g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default; //设定插值方式
- g.DrawImage(input,
- new Rectangle(0, 0, targetWidth, targetHeight),
- new Rectangle(0, 0, input.Width, input.Height),
- GraphicsUnit.Pixel);
- g.Dispose();
- return actualBitmap;
- }
- catch (Exception ex)
- {
- return null;
- }
- }
- /// <summary>
- /// 计算页数
- /// </summary>
- public void CaculatePrintedPageCount()
- {
- PrintedPageCount = PrintSettingsInfo.PageRangeList.Count;
- if (PrintSettingsInfo.printModInfo.EnumPrintMod == EnumPrintMod.StatusMultiple)
- {
- MultipleInfo multipleInfo = (MultipleInfo)PrintSettingsInfo.printModInfo;
- if (PrintedPageCount % (multipleInfo.HorizontalPageNumber * multipleInfo.VerticalPageNumber) != 0)
- {
- PrintedPageCount = PrintedPageCount / (multipleInfo.HorizontalPageNumber * multipleInfo.VerticalPageNumber) + 1;
- }
- else
- {
- PrintedPageCount = PrintedPageCount / (multipleInfo.HorizontalPageNumber * multipleInfo.VerticalPageNumber);
- }
- }
- else if (PrintSettingsInfo.printModInfo.EnumPrintMod == EnumPrintMod.StatusBooklet)
- {
- BookletInfo bookletInfo = (BookletInfo)PrintSettingsInfo.printModInfo;
- if (PrintedPageCount == 1)
- {
- PrintedPageCount = 1;
- }
- else
- {
- PrintedPageCount = (bookletInfo.EndPaperIndex - bookletInfo.BeginPaperIndex + 1) * 2;
- if (bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusFrontSideOnly ||
- bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusBackSideOnly)
- {
- PrintedPageCount /= 2;
- }
- }
- }
- }
- /// <summary>
- /// 转灰度
- /// </summary>
- /// <param name="bmp"></param>
- /// <param name="mode"></param>
- /// <returns></returns>
- private Bitmap ToGray(Bitmap bmp, int mode)
- {
- if (bmp == null)
- {
- return null;
- }
- int w = bmp.Width;
- int h = bmp.Height;
- try
- {
- byte newColor = 0;
- BitmapData srcData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
- unsafe
- {
- byte* p = (byte*)srcData.Scan0.ToPointer();
- for (int y = 0; y < h; y++)
- {
- for (int x = 0; x < w; x++)
- {
- if (mode == 0) // 加权平均
- {
- newColor = (byte)((float)p[0] * 0.114f + (float)p[1] * 0.587f + (float)p[2] * 0.299f);
- }
- else // 算数平均
- {
- newColor = (byte)((float)(p[0] + p[1] + p[2]) / 3.0f);
- }
- p[0] = newColor;
- p[1] = newColor;
- p[2] = newColor;
- p += 3;
- }
- p += srcData.Stride - w * 3;
- }
- bmp.UnlockBits(srcData);
- return bmp;
- }
- }
- catch
- {
- return null;
- }
- }
- /// <summary>
- /// 合并底图和前景
- /// </summary>
- /// <param name="background"></param>
- /// <param name="foreground"></param>
- /// <param name="point"></param>
- /// <returns></returns>
- public Bitmap CombineBitmap(Bitmap background, Bitmap foreground, System.Drawing.Point point)
- {
- if (background == null)
- {
- return null;
- }
- int bgWidth = background.Width;
- int bgHeight = background.Height;
- int fgWidth = foreground.Width;
- int fgHeight = foreground.Height;
- Bitmap newMap = new Bitmap(bgWidth, bgHeight);
- Graphics graphics = Graphics.FromImage(newMap);
- graphics.DrawImage(background, new System.Drawing.Point(0, 0));
- graphics.DrawImage(foreground, point);
- graphics.Dispose();
- return newMap;
- }
- /// <summary>
- /// size模式
- /// </summary>
- /// <param name="paperIndex"></param>
- private async void PreviewModSize(int paperIndex)
- {
- SizeInfo sizeInfo = (SizeInfo)PrintSettingsInfo.printModInfo;
- CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(PrintSettingsInfo.PageRangeList[paperIndex]);
- System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
- if (cPDFPage != null)
- {
- Bitmap printBitmap = new Bitmap((int)(float.Parse(PaperWidth)), (int)float.Parse(PaperHeight));
- Bitmap bitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)(cPDFPage.PageSize.Width * dpiRatio), (int)(cPDFPage.PageSize.Height * dpiRatio), PrintSettingsInfo.PageRangeList[paperIndex], PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- if (PrintSettingsInfo.IsGrayscale)
- {
- bitmap = ToGray(bitmap, 0);
- }
- ///<summary>
- ///自适应模式
- /// </summary>
- if (sizeInfo.EnumSizeType == EnumSizeType.StatusAdaptive)
- {
- double mmToDpiRatio = 96 / 25.4;
- int resizedHeight = 0;
- int resizedWidth = 0;
- ///重设大小
- if ((float)bitmap.Height / (float)bitmap.Width >= (float.Parse(PaperHeight) / float.Parse(PaperWidth)))
- {
- resizedHeight = (int)(float.Parse(PaperHeight) * mmToDpiRatio);
- resizedWidth = (int)((float.Parse(PaperHeight) / bitmap.Height * bitmap.Width) * mmToDpiRatio);
- }
- else
- {
- resizedWidth = (int)(float.Parse(PaperWidth) * mmToDpiRatio);
- resizedHeight = (int)((float.Parse(PaperWidth) / bitmap.Width * bitmap.Height) * mmToDpiRatio);
- }
- bitmap = Resize(bitmap, resizedWidth, resizedHeight);
- startPoint.X = (blankPageBitmap.Width - resizedWidth) / 2;
- startPoint.Y = (blankPageBitmap.Height - resizedHeight) / 2;
- printBitmap = CombineBitmap(blankPageBitmap, bitmap, startPoint);
- BitmapSource = ToBitmapSource(printBitmap);
- }
- else if (sizeInfo.EnumSizeType == EnumSizeType.StatusActural)
- {
- startPoint.X = (blankPageBitmap.Width - bitmap.Width) / 2;
- startPoint.Y = (blankPageBitmap.Height - bitmap.Height) / 2;
- printBitmap = CombineBitmap(blankPageBitmap, bitmap, startPoint);
- BitmapSource = ToBitmapSource(printBitmap);
- }
- else if (sizeInfo.EnumSizeType == EnumSizeType.StatusCustomized)
- {
- bitmap = Resize(bitmap, (int)(bitmap.Width * (sizeInfo.DisplayRatio / 100.0)), (int)(bitmap.Height * (sizeInfo.DisplayRatio / 100.0)));
- startPoint.X = (blankPageBitmap.Width - bitmap.Width) / 2;
- startPoint.Y = (blankPageBitmap.Height - bitmap.Height) / 2;
- printBitmap = CombineBitmap(blankPageBitmap, bitmap, startPoint);
- BitmapSource = ToBitmapSource(printBitmap);
- }
- bitmap.Dispose();
- printBitmap.Dispose();
- }
- }
- /// <summary>
- /// 海报模式
- /// </summary>
- /// <param name="paperIndex"></param>
- private async void PreviewModPoster(int paperIndex)
- {
- PosterInfo posterInfo = (PosterInfo)PrintSettingsInfo.printModInfo;
- CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(PrintSettingsInfo.PageRangeList[paperIndex]);
- double PreviewHeight = 0;
- double PreviewWidth = 0;
- System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
- Bitmap displayBitmap = null;
- //计算有效打印区域
- double effectiveHeight = (double)PrintSettingsInfo.PageMediaSize.Height;
- double effectiveWidth = (double)PrintSettingsInfo.PageMediaSize.Width;
- //重叠
- effectiveHeight = effectiveHeight - 2 * posterInfo.OverLap * 0.5 * 96 / 25.4;
- effectiveWidth = effectiveWidth - 2 * posterInfo.OverLap * 0.5 * 96 / 25.4;
- if (posterInfo.HasCutMarks)
- {
- effectiveHeight = effectiveHeight - 0.5 * 96 / 25.4 * 2;
- effectiveWidth = effectiveWidth - 0.5 * 96 / 25.4 * 2;
- }
- if (!posterInfo.HasCutMarks && posterInfo.HasLabel)
- {
- effectiveHeight = effectiveHeight - 0.5 * 96 / 25.4;
- }
- if (cPDFPage != null)
- {
- //拆分模式
- //根据页面大小,按最大有效打印区域划分为多块
- //最大有效区域
- //纸张大小-标记标签-重叠
- if (posterInfo.EnumPosterMod == EnumPosterMod.StatusTile)
- {
- Bitmap bitmap;
- //TODO 放大倍数过高引起int超限
- try
- {
- 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);
- }
- catch
- {
- bitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)(cPDFPage.PageSize.Width * dpiRatio), (int)(cPDFPage.PageSize.Height * dpiRatio), PrintSettingsInfo.PageRangeList[paperIndex], PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- }
- HorizonPaperCount = (int)Math.Ceiling((double)bitmap.Width / effectiveWidth);
- VerticalPaperCount = (int)Math.Ceiling(((double)bitmap.Height / effectiveHeight));
- PreviewWidth = (double)(PrintSettingsInfo.PageMediaSize.Width * HorizonPaperCount);
- PreviewHeight = (double)(PrintSettingsInfo.PageMediaSize.Height * VerticalPaperCount);
- //铺放纸张
- SetViewBox(PreviewHeight, PreviewWidth);
- //创建空白图像,大小等于纸张铺放后
- displayBitmap = new Bitmap((int)PreviewWidth, (int)PreviewHeight);
- //居中合并
- startPoint.X = (int)((PreviewWidth - bitmap.Width) / 2);
- startPoint.Y = (int)((PreviewHeight - bitmap.Height) / 2);
- if (PrintSettingsInfo.IsGrayscale)
- {
- bitmap = ToGray(bitmap, 0);
- }
- displayBitmap = CombineBitmap(displayBitmap, bitmap, startPoint);
- //画线
- Graphics g = Graphics.FromImage(displayBitmap);
- if (HorizonPaperCount > 1)
- {
- for (int tempX = 1; tempX < HorizonPaperCount; tempX++)
- {
- int horizonLocate = (int)(tempX * displayBitmap.Width / HorizonPaperCount);
- g.DrawLine(new Pen(Color.Black, HorizonPaperCount * 5), new System.Drawing.Point(horizonLocate, 0), new System.Drawing.Point(horizonLocate, (int)PreviewHeight));
- }
- }
- if (VerticalPaperCount > 1)
- {
- for (int tempY = 1; tempY < VerticalPaperCount; tempY++)
- {
- int verticalLocate = (int)(tempY * displayBitmap.Height / VerticalPaperCount);
- g.DrawLine(new Pen(Color.Black, VerticalPaperCount * 5), new System.Drawing.Point(0, verticalLocate), new System.Drawing.Point((int)PreviewWidth, verticalLocate));
- }
- }
- g.Dispose();
- bitmap.Dispose();
- PosterRatio = posterInfo.TileRatio / 100.0;
- }
- //平铺模式
- else if (posterInfo.EnumPosterMod == EnumPosterMod.StatusSplit)
- {
- HorizonPaperCount = posterInfo.HorizontalSheetNumber;
- VerticalPaperCount = posterInfo.VerticalSheetNumber;
- PreviewWidth = (double)(PrintSettingsInfo.PageMediaSize.Width * HorizonPaperCount);
- PreviewHeight = (double)(PrintSettingsInfo.PageMediaSize.Height * VerticalPaperCount);
- //铺放纸张
- SetViewBox(PreviewHeight, PreviewWidth);
- //创建空白图像,大小等于纸张铺放后
- displayBitmap = new Bitmap((int)PreviewWidth, (int)PreviewHeight);
- //文件根据有效范围缩放
- double wholeEffectiveHeight = effectiveHeight * VerticalPaperCount;
- double wholeEffectiveWidth = effectiveWidth * HorizonPaperCount;
- double resizedHeight = 0;
- double resizedWidth = 0;
- if ((cPDFPage.PageSize.Height / cPDFPage.PageSize.Width) >= wholeEffectiveHeight / wholeEffectiveWidth)
- {
- resizedHeight = wholeEffectiveHeight;
- resizedWidth = wholeEffectiveHeight / cPDFPage.PageSize.Height * cPDFPage.PageSize.Width;
- }
- else
- {
- resizedWidth = wholeEffectiveWidth;
- resizedHeight = wholeEffectiveWidth / cPDFPage.PageSize.Width * cPDFPage.PageSize.Height;
- }
- PosterRatio = resizedWidth / cPDFPage.PageSize.Width;
- Bitmap bitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, PrintSettingsInfo.PageRangeList[paperIndex], PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- startPoint.X = (int)((PreviewWidth - bitmap.Width) / 2);
- startPoint.Y = (int)((PreviewHeight - bitmap.Height) / 2);
- if (PrintSettingsInfo.IsGrayscale)
- {
- bitmap = ToGray(bitmap, 0);
- }
- displayBitmap = CombineBitmap(displayBitmap, bitmap, startPoint);
- //画线
- Graphics g = Graphics.FromImage(displayBitmap);
- if (HorizonPaperCount > 1)
- {
- for (int tempX = 1; tempX < HorizonPaperCount; tempX++)
- {
- int horizonLocate = (int)(tempX * displayBitmap.Width / HorizonPaperCount);
- g.DrawLine(new Pen(Color.Black, HorizonPaperCount * 5), new System.Drawing.Point(horizonLocate, 0), new System.Drawing.Point(horizonLocate, (int)PreviewHeight));
- }
- }
- if (VerticalPaperCount > 1)
- {
- for (int tempY = 1; tempY < VerticalPaperCount; tempY++)
- {
- int verticalLocate = (int)(tempY * displayBitmap.Height / VerticalPaperCount);
- g.DrawLine(new Pen(Color.Black, VerticalPaperCount * 5), new System.Drawing.Point(0, verticalLocate), new System.Drawing.Point((int)PreviewWidth, verticalLocate));
- }
- }
- g.Dispose();
- bitmap.Dispose();
- }
- }
- BitmapSource = ToBitmapSource(displayBitmap);
- displayBitmap.Dispose();
- }
- /// <summary>
- /// 多页
- /// </summary>
- /// <param name="paperIndex"></param>
- private async void PreviewModMultiple(int paperIndex)
- {
- MultipleInfo multipleInfo = (MultipleInfo)PrintSettingsInfo.printModInfo;
- int singlePageHeight;
- int singlePageWidth;
- Bitmap printBitmap;
- int currentPage = 0;
- if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
- {
- singlePageHeight = (int)(PrintSettingsInfo.PageMediaSize.Height / multipleInfo.VerticalPageNumber);
- singlePageWidth = (int)(PrintSettingsInfo.PageMediaSize.Width / multipleInfo.HorizontalPageNumber);
- printBitmap = new Bitmap((int)PrintSettingsInfo.PageMediaSize.Width, (int)PrintSettingsInfo.PageMediaSize.Height);
- }
- else
- {
- singlePageHeight = (int)(PrintSettingsInfo.PageMediaSize.Width / multipleInfo.VerticalPageNumber);
- singlePageWidth = (int)(PrintSettingsInfo.PageMediaSize.Height / multipleInfo.HorizontalPageNumber);
- printBitmap = new Bitmap((int)PrintSettingsInfo.PageMediaSize.Height, (int)PrintSettingsInfo.PageMediaSize.Width);
- }
- System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
- int startPage = PrintSettingsInfo.PageRangeList[paperIndex * (multipleInfo.VerticalPageNumber) * (multipleInfo.HorizontalPageNumber)];
- int resizedHeight = 0;
- int resizedWidth = 0;
- bool isNoSrcPage = false;
- Bitmap pageBitmap;
- ///水平:横向排列,先横后纵,纵轴不变横轴翻转
- if (multipleInfo.EnumPageOrder == EnumPageOrder.StatusHorizontalOrder || multipleInfo.EnumPageOrder == EnumPageOrder.StatusHorizontalReverseOrder)
- {
- for (int tempVerticalIndex = 0; tempVerticalIndex < multipleInfo.VerticalPageNumber; tempVerticalIndex++)
- {
- startPoint.Y = tempVerticalIndex * singlePageHeight;
- for (int tempHorizontalIndex = 0; tempHorizontalIndex < multipleInfo.HorizontalPageNumber; tempHorizontalIndex++)
- {
- try
- {
- currentPage =
- PrintSettingsInfo.PageRangeList[paperIndex *
- (multipleInfo.VerticalPageNumber) *
- (multipleInfo.HorizontalPageNumber) +
- (tempVerticalIndex * multipleInfo.HorizontalPageNumber) +
- tempHorizontalIndex];
- }
- catch
- {
- break;
- }
- //横向
- if (multipleInfo.EnumPageOrder == EnumPageOrder.StatusHorizontalOrder)
- {
- startPoint.X = tempHorizontalIndex * singlePageWidth;
- }
- //横向倒序
- else
- {
- startPoint.X = (multipleInfo.HorizontalPageNumber - tempHorizontalIndex - 1) * singlePageWidth;
- }
- CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(currentPage);
- if (cPDFPage == null)
- {
- isNoSrcPage = true;
- break;
- }
- pageBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)(cPDFPage.PageSize.Width), (int)(cPDFPage.PageSize.Height), currentPage, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- ///重设大小
- if ((float)pageBitmap.Height / (float)pageBitmap.Width >= ((float)singlePageHeight / (float)singlePageWidth))
- {
- resizedHeight = (int)(singlePageHeight * 0.96);
- resizedWidth = (int)((float)singlePageHeight / pageBitmap.Height * pageBitmap.Width * 0.96);
- }
- else
- {
- resizedWidth = (int)(singlePageWidth * 0.96);
- resizedHeight = (int)((float)singlePageWidth / pageBitmap.Width * pageBitmap.Height * 0.96);
- }
- pageBitmap = Resize(pageBitmap, (int)resizedWidth, (int)resizedHeight);
- startPoint.X += (singlePageWidth - resizedWidth) / 2;
- startPoint.Y += (singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- pageBitmap = ToGray(pageBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, pageBitmap, startPoint);
- //绘制边界
- if (PrintSettingsInfo.IsPrintPageBorde)
- {
- using (Pen pen = new Pen(Color.Black, 4))
- {
- Graphics g = Graphics.FromImage(printBitmap);
- g.DrawRectangle(pen, new Rectangle(startPoint.X, startPoint.Y, resizedWidth, resizedHeight));
- g.Dispose();
- }
- }
- startPoint.Y -= (singlePageHeight - resizedHeight) / 2;
- }
- if (isNoSrcPage)
- {
- isNoSrcPage = false;
- break;
- }
- }
- }
- //垂直:纵向排列,先纵后横,纵轴不变横轴翻转
- else
- {
- for (int tempHorizontalIndex = 0; tempHorizontalIndex < multipleInfo.HorizontalPageNumber; tempHorizontalIndex++)
- {
- //纵向,垂直排列时先移位
- if (multipleInfo.EnumPageOrder == EnumPageOrder.StatusVerticalOrder)
- {
- startPoint.X = tempHorizontalIndex * singlePageWidth;
- }
- //纵向倒序
- else
- {
- startPoint.X = (multipleInfo.HorizontalPageNumber - tempHorizontalIndex - 1) * singlePageWidth;
- }
- for (int tempVerticalIndex = 0; tempVerticalIndex < multipleInfo.VerticalPageNumber; tempVerticalIndex++)
- {
- startPoint.Y = tempVerticalIndex * singlePageHeight;
- try
- {
- currentPage = PrintSettingsInfo.PageRangeList[paperIndex *
- (multipleInfo.VerticalPageNumber) *
- (multipleInfo.HorizontalPageNumber) +
- (tempHorizontalIndex * multipleInfo.VerticalPageNumber) +
- tempVerticalIndex];
- }
- catch
- {
- break;
- }
- CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(currentPage);
- if (cPDFPage == null)
- {
- isNoSrcPage = true;
- break;
- }
- pageBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)(cPDFPage.PageSize.Width), (int)(cPDFPage.PageSize.Height), currentPage, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- Graphics graphics = Graphics.FromImage(pageBitmap);
- ///重设大小
- if ((float)pageBitmap.Height / (float)pageBitmap.Width >= ((float)singlePageHeight / (float)singlePageWidth))
- {
- resizedHeight = (int)(singlePageHeight * 0.96);
- resizedWidth = (int)((float)singlePageHeight / pageBitmap.Height * pageBitmap.Width * 0.96);
- }
- else
- {
- resizedWidth = (int)(singlePageWidth * 0.96);
- resizedHeight = (int)((float)singlePageWidth / pageBitmap.Width * pageBitmap.Height * 0.96);
- }
- pageBitmap = Resize(pageBitmap, (int)resizedWidth, (int)resizedHeight);
- graphics.Dispose();
- startPoint.X += (singlePageWidth - resizedWidth) / 2;
- startPoint.Y += (singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- pageBitmap = ToGray(pageBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, pageBitmap, startPoint);
- //绘制边界
- if (PrintSettingsInfo.IsPrintPageBorde)
- {
- using (Pen pen = new Pen(Color.Black, 4))
- {
- Graphics g = Graphics.FromImage(printBitmap);
- g.DrawRectangle(pen, new Rectangle(startPoint.X, startPoint.Y, resizedWidth, resizedHeight));
- g.Dispose();
- }
- }
- startPoint.X -= (singlePageWidth - resizedWidth) / 2;
- }
- if (isNoSrcPage)
- {
- isNoSrcPage = false;
- break;
- }
- }
- }
- WholePaperCountModPoster = PrintedPageCount * HorizonPaperCount * VerticalPaperCount;
- BitmapSource = ToBitmapSource(printBitmap);
- }
- /// <summary>
- /// 小册子模式
- /// </summary>
- /// <param name="paperIndex"></param>
- private async void PreviewModBooklet(int paperIndex)
- {
- BookletInfo bookletInfo = (BookletInfo)PrintSettingsInfo.printModInfo;
- ///空白画布
- Bitmap printBitmap;
- double singlePageHeight = 0;
- double singlePageWidth = 0;
- if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
- {
- printBitmap = new Bitmap((int)PrintSettingsInfo.PageMediaSize.Width, (int)PrintSettingsInfo.PageMediaSize.Height);
- singlePageHeight = (double)PrintSettingsInfo.PageMediaSize.Height;
- singlePageWidth = (double)(PrintSettingsInfo.PageMediaSize.Width / 2);
- }
- else
- {
- printBitmap = new Bitmap((int)PrintSettingsInfo.PageMediaSize.Height, (int)PrintSettingsInfo.PageMediaSize.Width);
- singlePageHeight = (double)PrintSettingsInfo.PageMediaSize.Width;
- singlePageWidth = (double)(PrintSettingsInfo.PageMediaSize.Height / 2);
- }
- System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
- int maxPaperNumber = (PDFViewer.Document.PageCount % 4 == 0) ? (PDFViewer.Document.PageCount / 4) : (PDFViewer.Document.PageCount / 4 + 1);
- Bitmap leftBitmap = null;
- Bitmap rightBitmap = null;
- double resizedHeight = 0;
- double resizedWidth = 0;
- CPDFPage leftPage = null;
- CPDFPage rightPage = null;
- int leftPageIndex = 0;
- int rightPageIndex = 0;
- ///<summary>
- ///左装订与右装订顺序相反
- /// </summary>
- if (bookletInfo.EnumBookletBinding == EnumBookletBinding.StatusLeft)
- {
- ///奇数页:
- ///右面为页面序号
- ///左面为[(总打印纸数*4)+1-打印页右侧页序号]
- if ((paperIndex + 1) % 2 == 1)
- {
- try
- {
- leftPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - paperIndex - 1];
- leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
- }
- catch
- {
- leftPage = null;
- }
- rightPageIndex = PrintSettingsInfo.PageRangeList[paperIndex];
- //左面
- if (leftPage != null)
- {
- ///重设大小
- if ((leftPage.PageSize.Height / leftPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = singlePageHeight;
- resizedWidth = singlePageHeight / leftPage.PageSize.Height * leftPage.PageSize.Width;
- }
- else
- {
- resizedWidth = singlePageWidth;
- resizedHeight = singlePageWidth / leftPage.PageSize.Width * leftPage.PageSize.Height;
- }
- leftBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- leftBitmap = ToGray(leftBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, leftBitmap, startPoint);
- }
- //右面
- rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
- startPoint.X = (int)singlePageWidth;
- if (rightPage != null)
- {
- ///重设大小
- if ((rightPage.PageSize.Height / rightPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = singlePageHeight;
- resizedWidth = singlePageHeight / rightPage.PageSize.Height * rightPage.PageSize.Width;
- }
- else
- {
- resizedWidth = singlePageWidth;
- resizedHeight = singlePageWidth / rightPage.PageSize.Width * rightPage.PageSize.Height;
- }
- rightBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- rightBitmap = ToGray(rightBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, rightBitmap, startPoint);
- }
- }
- /// 偶数页:
- /// 左面为页面序号
- /// 右面为[(总打印纸数*4)+1-打印页右侧页序号]
- else
- {
- try
- {
- leftPageIndex = PrintSettingsInfo.PageRangeList[paperIndex];
- leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
- }
- catch
- {
- leftPage = null;
- }
- //左面
- if (leftPage != null)
- {
- ///重设大小
- if ((leftPage.PageSize.Height / leftPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = singlePageHeight;
- resizedWidth = singlePageHeight / leftPage.PageSize.Height * leftPage.PageSize.Width;
- }
- else
- {
- resizedWidth = singlePageWidth;
- resizedHeight = singlePageWidth / leftPage.PageSize.Width * leftPage.PageSize.Height;
- }
- leftBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)(resizedWidth), (int)resizedHeight, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- leftBitmap = ToGray(leftBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, leftBitmap, startPoint);
- }
- //右面
- try
- {
- rightPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - 1 - paperIndex];
- rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
- }
- catch
- {
- rightPage = null;
- }
- startPoint.X = (int)singlePageWidth;
- if (rightPage != null)
- {
- ///重设大小
- if ((rightPage.PageSize.Height / rightPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = singlePageHeight;
- resizedWidth = singlePageHeight / rightPage.PageSize.Height * rightPage.PageSize.Width;
- }
- else
- {
- resizedWidth = singlePageWidth;
- resizedHeight = singlePageWidth / rightPage.PageSize.Width * rightPage.PageSize.Height;
- }
- rightBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- rightBitmap = ToGray(rightBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, rightBitmap, startPoint);
- }
- }
- }
- ///<summary>
- ///右装订打印
- /// </summary>
- if (bookletInfo.EnumBookletBinding == EnumBookletBinding.StatusRight)
- {
- ///奇数页:
- ///右面为页面序号
- ///左面为[(总打印纸数*4)+1-打印页右侧页序号]
- if ((paperIndex + 1) % 2 == 0)
- {
- try
- {
- leftPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - paperIndex - 1];
- leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
- }
- catch
- {
- leftPage = null;
- }
- rightPageIndex = PrintSettingsInfo.PageRangeList[paperIndex];
- //左面
- if (leftPage != null)
- {
- ///重设大小
- if ((leftPage.PageSize.Height / leftPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = singlePageHeight;
- resizedWidth = singlePageHeight / leftPage.PageSize.Height * leftPage.PageSize.Width;
- }
- else
- {
- resizedWidth = singlePageWidth;
- resizedHeight = singlePageWidth / leftPage.PageSize.Width * leftPage.PageSize.Height;
- }
- leftBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- leftBitmap = ToGray(leftBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, leftBitmap, startPoint);
- }
- //右面
- rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
- startPoint.X = (int)singlePageWidth;
- if (rightPage != null)
- {
- ///重设大小
- if ((rightPage.PageSize.Height / rightPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = singlePageHeight;
- resizedWidth = singlePageHeight / rightPage.PageSize.Height * rightPage.PageSize.Width;
- }
- else
- {
- resizedWidth = singlePageWidth;
- resizedHeight = singlePageWidth / rightPage.PageSize.Width * rightPage.PageSize.Height;
- }
- rightBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- rightBitmap = ToGray(rightBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, rightBitmap, startPoint);
- }
- }
- /// 偶数页:
- /// 左面为页面序号
- /// 右面为[(总打印纸数*4)+1-打印页右侧页序号]
- else
- {
- leftPageIndex = PrintSettingsInfo.PageRangeList[paperIndex];
- //左面
- leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
- if (leftPage != null)
- {
- ///重设大小
- if ((leftPage.PageSize.Height / leftPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = singlePageHeight;
- resizedWidth = singlePageHeight / leftPage.PageSize.Height * leftPage.PageSize.Width;
- }
- else
- {
- resizedWidth = singlePageWidth;
- resizedHeight = singlePageWidth / leftPage.PageSize.Width * leftPage.PageSize.Height;
- }
- leftBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)(resizedWidth), (int)resizedHeight, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- leftBitmap = ToGray(leftBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, leftBitmap, startPoint);
- }
- //右面
- try
- {
- rightPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - 1 - paperIndex];
- rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
- }
- catch
- {
- rightPage = null;
- }
- startPoint.X = (int)singlePageWidth;
- if (rightPage != null)
- {
- ///重设大小
- if ((rightPage.PageSize.Height / rightPage.PageSize.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = singlePageHeight;
- resizedWidth = singlePageHeight / rightPage.PageSize.Height * rightPage.PageSize.Width;
- }
- else
- {
- resizedWidth = singlePageWidth;
- resizedHeight = singlePageWidth / rightPage.PageSize.Width * rightPage.PageSize.Height;
- }
- rightBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- rightBitmap = ToGray(rightBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, rightBitmap, startPoint);
- }
- }
- }
- BitmapSource = ToBitmapSource(printBitmap);
- }
- /// <summary>
- /// 按照模式绘制指定纸张
- /// </summary>
- /// <param name="TargetPageIndex"></param>
- private void PaintPageByCurrentPreviewIndex(int TargetPaperIndex)
- {
- if (PrintSettingsInfo.printModInfo.EnumPrintMod == EnumPrintMod.StatusSize)
- {
- SetPreviewExceptPoster();
- PreviewModSize(TargetPaperList[TargetPaperIndex]);
- }
- else if (PrintSettingsInfo.printModInfo.EnumPrintMod == EnumPrintMod.StatusPoster)
- {
- PreviewModPoster(TargetPaperList[TargetPaperIndex]);
- }
- else if (PrintSettingsInfo.printModInfo.EnumPrintMod == EnumPrintMod.StatusMultiple)
- {
- SetPreviewExceptPoster();
- PreviewModMultiple(TargetPaperList[TargetPaperIndex]);
- }
- else if (PrintSettingsInfo.printModInfo.EnumPrintMod == EnumPrintMod.StatusBooklet)
- {
- SetPreviewExceptPoster();
- PreviewModBooklet(TargetPaperList[TargetPaperIndex]);
- }
- }
- /// <summary>
- /// 设置纸张集合
- /// 大小模式:目标纸张和目标纸张页一致
- /// 海报模式:一致
- /// 多页模式: 一致
- /// 小册子模式:不一致
- /// 第a页-第b页:从第1页开始数,a页=(a-1)*2, b页=(b-1)*2
- /// 面向:奇数页:paper%2 = 1
- /// 背向:偶数页:Paper%2 = 0;
- /// </summary>
- public void SetPaperCollection()
- {
- if (PrintSettingsInfo.printModInfo.EnumPrintMod == EnumPrintMod.StatusSize ||
- PrintSettingsInfo.printModInfo.EnumPrintMod == EnumPrintMod.StatusPoster ||
- PrintSettingsInfo.printModInfo.EnumPrintMod == EnumPrintMod.StatusMultiple)
- {
- TargetPaperList.Clear();
- TargetPaperList.Add(0);
- for (int temp = 0; temp < PrintedPageCount; temp++)
- {
- TargetPaperList.Add(temp);
- }
- }
- else
- {
- BookletInfo bookletInfo = (BookletInfo)PrintSettingsInfo.printModInfo;
- if (bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusBothSides)
- {
- TargetPaperList.Clear();
- TargetPaperList.Add(0);
- for (int temp = bookletInfo.BeginPaperIndex * 2 - 2; temp < bookletInfo.EndPaperIndex * 2; temp++)
- {
- TargetPaperList.Add(temp);
- }
- }
- else if (bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusFrontSideOnly)
- {
- TargetPaperList.Clear();
- TargetPaperList.Add(0);
- for (int temp = bookletInfo.BeginPaperIndex * 2 - 2; temp < bookletInfo.EndPaperIndex * 2; temp += 2)
- {
- TargetPaperList.Add(temp);
- }
- }
- else
- {
- TargetPaperList.Clear();
- TargetPaperList.Add(0);
- for (int temp = bookletInfo.BeginPaperIndex * 2 - 1; temp < bookletInfo.EndPaperIndex * 2; temp += 2)
- {
- TargetPaperList.Add(temp);
- }
- }
- }
- }
- public bool IsNavigationTarget(NavigationContext navigationContext)
- {
- return true;
- }
- public void OnNavigatedFrom(NavigationContext navigationContext)
- {
- this.printDocument.Dispose();
- }
- public void OnNavigatedTo(NavigationContext navigationContext)
- {
- navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
- navigationContext.Parameters.TryGetValue<PrintSettingsInfo>(ParameterNames.PrintSettingsInfo, out PrintSettingsInfo);
- navigationContext.Parameters.TryGetValue<string>("Unicode", out Unicode);
- if (PDFViewer != null && PrintSettingsInfo != null)
- {
- SetPreviewExceptPoster();
- CaculatePrintedPageCount();
- SetPaperCollection();
- PaintPageByCurrentPreviewIndex(1);
- }
- }
- }
- }
|