12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972 |
- using ComPDFKit.PDFPage;
- using ComPDFKitViewer.PdfViewer;
- using ImageMagick.Formats;
- using ImTools;
- using Microsoft.Office.Core;
- using PDF_Master.CustomControl;
- using PDF_Master.EventAggregators;
- using PDF_Master.Helper;
- using PDF_Master.Model;
- using PDF_Master.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.Drawing2D;
- using System.Drawing.Imaging;
- using System.Drawing.Printing;
- using System.IO;
- 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 System.Windows.Threading;
- using static Dropbox.Api.TeamLog.PaperDownloadFormat;
- using static PDF_Master.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter.HomePagePrinterDocumentContentViewModel;
- namespace PDF_Master.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;
- #region 打印过程中一些参数需要在函数外定义
- /// <summary>
- /// 海报模式:打印纸
- /// </summary>
- int posterPrintPaperIndex = 0;
- /// <summary>
- /// 海报模式,水平纸张数
- /// </summary>
- public int HorizonPaperCount = 0;
- /// <summary>
- /// 海报模式,垂直纸张数
- /// </summary>
- public int VerticalPaperCount = 0;
- /// <summary>
- /// 海报模式,单页总纸张数
- /// </summary>
- public int WholeCountSinglePage = 0;
- ///<summary>
- ///海报模式当前水平纸
- /// </summary>
- public int HorizonPaperIndex = 0;
- ///<summary>
- ///海报模式当前垂直纸
- /// </summary>
- public int VerticalPaperIndex = 0;
- /// <summary>
- /// 海报模式,总打印纸张数
- /// </summary>
- public int WholePaperCountModPoster = 0;
- /// <summary>
- /// 海报模式渲染出来的纸张,只有第一次进入和每次翻页才重新渲染,减少渲染次数
- /// </summary>
- public Bitmap PosterCurrentBitmap = null;
- /// <summary>
- /// 同理,海报模式实际被拆分的纸张,更改过比例,添加了白边
- /// </summary>
- public Bitmap DisplayBitmap = null;
- /// <summary>
- /// 海报模式,由预览决定的比例
- /// </summary>
- public double PosterRatio = 0;
- #endregion
- public class DocumentSettingsInfo
- {
- public int DocumentWidth = 0;
- public int DocumentHeight = 0;
- public Margins margins = new Margins();
- public Rectangle DocumentBounds = new Rectangle();
- public bool DocumentLandscape = false;
- public bool DocumentColor = false;
- public bool IsSettingsChanged = true;
- }
- /// <summary>
- /// 转存防卡顿
- /// </summary>
- public DocumentSettingsInfo documentSettingsInfo = new DocumentSettingsInfo();
- /// <summary>
- /// 记录打印设置改变前的总页数,用于判断是否需要跳转第一页,仅当总页数改变时跳转
- /// </summary>
- int PreviousPrintedPageCount = 0;
- public int PrintIndex;
- ///<summary>
- ///打印纸dpi100, PDF dpi 72,需要缩放
- /// </summary>
- public double PDFToMediaDpiRatio = 100.0 / 72.0;
- public double mmToDpiRatio = 100 / 25.4;
- Bitmap blankPageBitmap;
- Bitmap blankPageBitmapForPrint;
- 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 double _viewBoxWidth;
- public double ViewBoxWidth
- {
- get => _viewBoxWidth;
- set => SetProperty(ref _viewBoxWidth, value);
- }
- private double _viewBoxHeight;
- public double 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);
- }
- private Visibility _errorPageTxtVisibility = Visibility.Collapsed;
- public Visibility ErrorPageTxtVisibility
- {
- get => _errorPageTxtVisibility;
- set => SetProperty(ref _errorPageTxtVisibility, value);
- }
- /// <summary>
- /// 手动双面打印
- /// </summary>
- public bool IsManualDuplex = false;
- 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);
- printDocumentEvent.GetEvent<SendLabelEvent>().Subscribe(RecvLabel, 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 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;
- 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.PrintModInfo.EnumDuplexPrintMod == EnumDuplexPrintMod.StatusFlipLongEdge || PrintSettingsInfo.PrintModInfo.EnumDuplexPrintMod == EnumDuplexPrintMod.StatusFlipShortEdge || PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusBooklet)
- {
- ///无阶段,自动双面打印
- ///已在起始时验证
- if (printQueueWithUnicode.EnumBothSidesStage == EnumBothSidesStage.StatusNone)
- {
- if (PrintSettingsInfo.PrintModInfo.EnumDuplexPrintMod == EnumDuplexPrintMod.StatusFlipLongEdge)
- {
- ///Fix 13346: Duplex.Vertical, 这个是长边翻页
- printDocument.PrinterSettings.Duplex = Duplex.Vertical;
- }
- else
- {
- ///Fix 13346: Duplex.Horizontal, 这个是短边翻页
- printDocument.PrinterSettings.Duplex = Duplex.Horizontal;
- }
- }
- else
- {
- printDocument.PrinterSettings.Duplex = Duplex.Simplex;
- IsManualDuplex = true;
- //TODO 手动双面打印,先印单数页再印双数页
- if (printQueueWithUnicode.EnumBothSidesStage == EnumBothSidesStage.StatusFrontSide)
- {
- PrintIndex = 0;
- }
- else
- {
- PrintIndex = 1;
- }
- }
- }
- //海报模式,预设单面打印
- else
- {
- printDocument.PrinterSettings.Duplex = Duplex.Simplex;
- }
- printQueueWithUnicode.printQueue.DefaultPrintTicket = printTicket;
- printDocument.DefaultPageSettings.PaperSize = PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize;
- PaperSource pkSource;
- List<PaperSource> paperSources = new List<PaperSource>();
- paperSources.Clear();
- for (int i = 0; i < printDocument.PrinterSettings.PaperSources.Count; i++)
- {
- pkSource = printDocument.PrinterSettings.PaperSources[i];
- paperSources.Add(pkSource);
- }
- ///纸盒不为空,实体打印机默认设置纸盒序号0(主纸盒)
- if (paperSources.Count != 0)
- {
- printDocument.DefaultPageSettings.PaperSource = paperSources[0];
- }
- //虚拟打印机:仅有一份的情况下
- if (printQueueWithUnicode.printQueue.GetPrintCapabilities().MaxCopyCount == 1)
- {
- printDocument.PrinterSettings.Copies = (short)PrintSettingsInfo.Copies;
- }
- else if (PrintSettingsInfo.Copies <= printQueueWithUnicode.printQueue.GetPrintCapabilities().MaxCopyCount && PrintSettingsInfo.Copies > 0)
- {
- printDocument.PrinterSettings.Copies = (short)PrintSettingsInfo.Copies;
- }
- else if (PrintSettingsInfo.Copies > printQueueWithUnicode.printQueue.GetPrintCapabilities().MaxCopyCount)
- {
- PrintSettingsInfo.Copies = (int)printQueueWithUnicode.printQueue.GetPrintCapabilities().MaxCopyCount;
- }
- else
- {
- PrintSettingsInfo.Copies = 1;
- }
- posterPrintPaperIndex = 0;
- printDocument.PrintPage += PrintDocumentByCurrentSettings;
- printDocument.PrinterSettings.PrintFileName = PDFViewer.Document.FileName;
- try
- {
- printDocument.Print();
- }
- catch
- {
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("", App.MainPageLoader.GetString("NoPrinter_Warning"), App.ServiceLoader.GetString("Text_ok"));
- }
- finally
- {
- printDocument.Dispose();
- }
- App.Current.MainWindow.Activate();
- }
- }
- private void RecvLabel(LabelWithUnicode labelWithUnicode)
- {
- if (PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusPoster)
- {
- var posterInfo = (PosterInfo)PrintSettingsInfo.PrintModInfo;
- posterInfo.Label = labelWithUnicode.Label.ToString();
- PrintSettingsInfo.PrintModInfo = posterInfo;
- }
- }
- 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;
- }
- #region 打印各个模式
- private void PrintDocumentModSize(PrintPageEventArgs e)
- {
- ///Fix 13337 :
- ///打印的宽度和高度是1/100英寸
- ///图片的宽度和高度是像素
- ///假如打印机DPI宽高600,图片宽高600px
- ///那么用DrawImage打印图片的时候, 就会把图片600像素的宽,理解为了600百分之一英寸(6英寸),数值没变,只是单位的理解不一样
- ///于是打印6英寸宽,需要跨度6*600DPI=3600像素, 但是图片本身只有600像素,于是就把图片的宽放大到3600像素,同理高也是这样,需要放大.
- ///放大以后的位图就会出现锯齿
- ///所以如果要打印6英寸的一条线,DPI为600的情况下, 需要准备6 * 600 = 3600像素的线条,然后告诉打印机要打印的宽度是600(百分之一英寸) , 打印图像的3600像素, 元素图像的单位是像素.
- ///关键代码 e.Graphics.DrawImage(printBitmap, realBound, new Rectangle(0, 0, printBitmap.Width, printBitmap.Height), GraphicsUnit.Pixel);
- ///其他三个模式也一样
- int widthDpiRatio;
- int heightDpiRatio;
- ///TODO 可扩展:高低分辨率打印
- if (true)
- {
- widthDpiRatio = (int)e.Graphics.DpiX / 100;
- heightDpiRatio = (int)e.Graphics.DpiY / 100;
- }
- SizeInfo sizeInfo = (SizeInfo)PrintSettingsInfo.PrintModInfo;
- CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(PrintSettingsInfo.PageRangeList[PrintIndex]);
- Rectangle realBound = new Rectangle(documentSettingsInfo.margins.Left * widthDpiRatio,
- documentSettingsInfo.margins.Top * heightDpiRatio,
- e.PageBounds.Width - documentSettingsInfo.margins.Left - documentSettingsInfo.margins.Right,
- e.PageBounds.Height - documentSettingsInfo.margins.Top - documentSettingsInfo.margins.Bottom);
- System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
- blankPageBitmapForPrint = new Bitmap((int)documentSettingsInfo.DocumentWidth * widthDpiRatio, (int)documentSettingsInfo.DocumentHeight * heightDpiRatio);
- /////听说有效
- ////e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
- ////e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
- ////e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
- if (cPDFPage != null)
- {
- Bitmap printBitmap = new Bitmap((int)(float.Parse(PaperWidth)) * widthDpiRatio, (int)float.Parse(PaperHeight) * heightDpiRatio);
- Bitmap bitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)(cPDFPage.PageSize.Width * PDFToMediaDpiRatio * widthDpiRatio), (int)(cPDFPage.PageSize.Height * PDFToMediaDpiRatio * heightDpiRatio), PrintSettingsInfo.PageRangeList[PrintIndex], PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- if (PrintSettingsInfo.IsGrayscale)
- {
- bitmap = ToGray(bitmap, 0);
- }
- ///< summary >
- /// 自适应模式
- ///</ summary >
- if (sizeInfo.EnumSizeType == EnumSizeType.StatusAdaptive)
- {
- 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) * widthDpiRatio;
- resizedWidth = (int)((float.Parse(PaperHeight) / bitmap.Height * bitmap.Width) * mmToDpiRatio) * heightDpiRatio;
- }
- else
- {
- resizedWidth = (int)(float.Parse(PaperWidth) * mmToDpiRatio) * widthDpiRatio;
- resizedHeight = (int)((float.Parse(PaperWidth) / bitmap.Width * bitmap.Height) * mmToDpiRatio) * heightDpiRatio;
- }
- /// 直接调整bitmap会引起内存问题,所以先这样处理....
- Bitmap resizedBitmap = new Bitmap(bitmap, resizedWidth, resizedHeight);
- startPoint.X = (blankPageBitmapForPrint.Width - resizedWidth) / 2;
- startPoint.Y = (blankPageBitmapForPrint.Height - resizedHeight) / 2;
- printBitmap = CombineBitmap(blankPageBitmapForPrint, resizedBitmap, startPoint);
- resizedBitmap.Dispose();
- if (IsManualDuplex && PrintIndex % 2 == 1 && sizeInfo.EnumDuplexPrintMod == EnumDuplexPrintMod.StatusFlipShortEdge)
- {
- printBitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
- }
- e.Graphics.DrawImage(printBitmap, realBound, new Rectangle(0, 0, printBitmap.Width, printBitmap.Height), GraphicsUnit.Pixel);
- }
- else if (sizeInfo.EnumSizeType == EnumSizeType.StatusActural)
- {
- startPoint.X = (blankPageBitmapForPrint.Width - bitmap.Width) / 2;
- startPoint.Y = (blankPageBitmapForPrint.Height - bitmap.Height) / 2;
- printBitmap = CombineBitmap(blankPageBitmapForPrint, bitmap, startPoint);
- if (IsManualDuplex && PrintIndex % 2 == 1 && sizeInfo.EnumDuplexPrintMod == EnumDuplexPrintMod.StatusFlipShortEdge)
- {
- printBitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
- }
- e.Graphics.DrawImage(printBitmap, realBound, new Rectangle(0, 0, printBitmap.Width, printBitmap.Height), GraphicsUnit.Pixel);
- }
- 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 = (blankPageBitmapForPrint.Width - bitmap.Width) / 2;
- startPoint.Y = (blankPageBitmapForPrint.Height - bitmap.Height) / 2;
- printBitmap = CombineBitmap(blankPageBitmapForPrint, bitmap, startPoint);
- if (IsManualDuplex && PrintIndex % 2 == 1 && sizeInfo.EnumDuplexPrintMod == EnumDuplexPrintMod.StatusFlipShortEdge)
- {
- printBitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
- }
- e.Graphics.DrawImage(printBitmap, realBound, new Rectangle(0, 0, printBitmap.Width, printBitmap.Height), GraphicsUnit.Pixel);
- }
- bitmap.Dispose();
- printBitmap.Dispose();
- blankPageBitmapForPrint.Dispose();
- GC.Collect();
- //手动双面打印,且页码不为1
- if (IsManualDuplex && PrintSettingsInfo.PageRangeList.Count != 1)
- {
- if (PrintIndex < PrintedPageCount - 2)
- {
- PrintIndex += 2;
- e.HasMorePages = true;
- }
- else
- {
- e.HasMorePages = false;
- if (PrintIndex % 2 == 0)
- {
- this.printDocumentEvent.GetEvent<SendFinishedFrontSideEvent>().Publish(new FinishedFrontSide { Unicode = this.Unicode });
- }
- }
- }
- //单面打印
- else
- {
- if (PrintIndex < PrintedPageCount - 1)
- {
- PrintIndex++;
- e.HasMorePages = true;
- }
- else
- {
- e.HasMorePages = false;
- }
- }
- }
- }
- ///无论是拆分还是平铺,页面缩放比例都会在预览中完成
- ///先渲染整图
- ///得到effective域大小
- ///根据effective的量和重叠量计算开始的bitmap
- ///加字和标记
- ///字加在顶部,标记加在四角
- ///结束后翻页
- ///对于Tile页数不定,但是比例相等
- ///对于split页数固定,但是比例变化
- ///平铺模式:
- ///从第一页开始,先计算当前页总纸张数
- ///根据纸张数打印
- ///打印到位时页数进一
- ///页数到顶,纸数到顶打印结束
- ///当一页只有一张纸的时候,注意特殊处理
- ///
- ///内存问题:
- ///1、只有每次翻页才重新取整张图。
- ///取到的图片先不急着放大。
- ///根据原图大小取得图片,并进行处理后,进行放大
- ///再绘制到打印纸上
- private void PrintDocumentModPoster(PrintPageEventArgs e)
- {
- PosterInfo posterInfo = (PosterInfo)PrintSettingsInfo.PrintModInfo;
- var pageBound = documentSettingsInfo.DocumentBounds;
- int widthDpiRatio;
- int heightDpiRatio;
- ///TODO 可扩展:高低分辨率打印
- if (true)
- {
- //当前模式放大程度过高引发异常,暂时采用低质量打印
- widthDpiRatio = (int)e.Graphics.DpiX / 100;
- heightDpiRatio = (int)e.Graphics.DpiY / 100;
- if (widthDpiRatio > 2)
- {
- widthDpiRatio = 2;
- heightDpiRatio = 2;
- }
- }
- double effectiveHeight;
- double effectiveWidth;
- double PreviewWidth;
- double PreviewHeight;
- System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
- ///计算有效打印区域
- if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
- {
- effectiveHeight = documentSettingsInfo.DocumentHeight;
- effectiveWidth = documentSettingsInfo.DocumentWidth;
- }
- else
- {
- effectiveHeight = documentSettingsInfo.DocumentWidth;
- effectiveWidth = documentSettingsInfo.DocumentHeight;
- }
- effectiveHeight -= 2 * posterInfo.OverLap * mmToDpiRatio;
- effectiveWidth -= 2 * posterInfo.OverLap * mmToDpiRatio;
- if (posterInfo.HasLabel || posterInfo.HasCutMarks)
- {
- effectiveHeight -= 40;
- effectiveWidth -= 40;
- }
- effectiveHeight *= heightDpiRatio;
- effectiveWidth *= widthDpiRatio;
- ///平铺模式下:
- if (posterInfo.EnumPosterMod == EnumPosterMod.StatusTile)
- {
- CPDFPage cPDFPage = PDFViewer.Document.PageAtIndex(PrintSettingsInfo.PageRangeList[posterPrintPaperIndex]);
- if (cPDFPage != null)
- {
- ///仅当每一页第一张纸打印时,进行一次渲染,,更换打印底图
- if (HorizonPaperIndex == 0 && VerticalPaperIndex == 0)
- {
- PosterCurrentBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)(cPDFPage.PageSize.Width * PDFToMediaDpiRatio * (posterInfo.TileRatio / 100.0) * widthDpiRatio), (int)(cPDFPage.PageSize.Height * PDFToMediaDpiRatio * (posterInfo.TileRatio / 100.0) * heightDpiRatio), PrintSettingsInfo.PageRangeList[posterPrintPaperIndex], PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- if (PrintSettingsInfo.IsGrayscale)
- {
- PosterCurrentBitmap = ToGray(PosterCurrentBitmap, 0);
- }
- HorizonPaperCount = (int)Math.Ceiling((double)PosterCurrentBitmap.Width / effectiveWidth);
- VerticalPaperCount = (int)Math.Ceiling(((double)PosterCurrentBitmap.Height / effectiveHeight));
- WholeCountSinglePage = HorizonPaperCount * VerticalPaperCount;//每一轮的总页数
- if (WholeCountSinglePage != 0)
- {
- PreviewWidth = (double)(effectiveWidth * HorizonPaperCount);
- PreviewHeight = (double)(effectiveHeight * VerticalPaperCount);
- DisplayBitmap = new Bitmap((int)PreviewWidth, (int)PreviewHeight);
- }
- else
- {
- PreviewWidth = effectiveWidth;
- PreviewHeight = effectiveHeight;
- DisplayBitmap = new Bitmap((int)effectiveWidth, (int)effectiveHeight);
- }
- //取到和渲染一样的Bitmap
- startPoint.X = (int)((PreviewWidth - PosterCurrentBitmap.Width) / 2);
- startPoint.Y = (int)((PreviewHeight - PosterCurrentBitmap.Height) / 2);
- DisplayBitmap = CombineBitmap(DisplayBitmap, PosterCurrentBitmap, startPoint);
- }
- if (WholeCountSinglePage > 1)
- {
- /////当前页未打印完
- if (HorizonPaperIndex * VerticalPaperIndex < WholeCountSinglePage - 1)
- {
- ///取有效尺寸和重叠,边沿合并
- ///有效尺寸+重叠边
- Rectangle rectangle = new Rectangle((int)(HorizonPaperIndex * effectiveWidth), (int)(VerticalPaperIndex * effectiveHeight), (int)effectiveWidth, (int)effectiveHeight);
- ///左重叠
- if (HorizonPaperIndex != 0)
- {
- rectangle.Location = new System.Drawing.Point((int)(rectangle.Location.X - posterInfo.OverLap * mmToDpiRatio * widthDpiRatio), rectangle.Location.Y);
- }
- ///上重叠
- if (VerticalPaperIndex != 0)
- {
- rectangle.Size = new System.Drawing.Size(rectangle.Size.Width, (int)(rectangle.Size.Height + posterInfo.OverLap * mmToDpiRatio * heightDpiRatio));
- }
- ///右重叠
- if (HorizonPaperIndex != HorizonPaperCount)
- {
- rectangle.Size = new System.Drawing.Size((int)(rectangle.Size.Width + posterInfo.OverLap * mmToDpiRatio * widthDpiRatio), rectangle.Size.Height);
- }
- ///下重叠
- if (VerticalPaperIndex != VerticalPaperCount)
- {
- rectangle.Location = new System.Drawing.Point(rectangle.Location.X, (int)(rectangle.Location.Y - posterInfo.OverLap * mmToDpiRatio * heightDpiRatio));
- }
- ///当存在边沿时
- int margin = 0;
- ///Label和CutMarks
- ///
- if (posterInfo.HasLabel || posterInfo.HasCutMarks)
- {
- margin = 20;
- }
- if (posterInfo.HasLabel)
- {
- string sign = null;
- if (posterInfo.Label == null || posterInfo.Label == "")
- {
- sign = string.Format("({0},{1}) -{2} -{3}.pdf {4} {5}", HorizonPaperIndex + 1, VerticalPaperIndex + 1, PrintIndex + 1, PDFViewer.Document.FileName, System.DateTime.Today.ToString("yyyy/MM/dd"), System.DateTime.Now.ToString("HH:mm:ss"));
- }
- else
- {
- sign = posterInfo.Label;
- }
- e.Graphics.DrawString(sign, new Font("Helvetica", 10.0f), System.Drawing.Brushes.Black, margin + 5, 0);
- }
- if (posterInfo.HasCutMarks)
- {
- System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
- //除了四角之外都要添加裁切标记
- if (!(HorizonPaperIndex == 0 && VerticalPaperIndex == 0))
- {
- e.Graphics.DrawRectangle(pen, new Rectangle(-margin, -margin, 2 * margin, 2 * margin));
- }
- if (!(HorizonPaperIndex == HorizonPaperCount - 1 && VerticalPaperIndex == 0))
- {
- e.Graphics.DrawRectangle(pen, new Rectangle(pageBound.Width - margin, -margin, margin * 2, margin * 2));
- }
- if (!(HorizonPaperIndex == 0 && VerticalPaperIndex == VerticalPaperCount - 1))
- {
- e.Graphics.DrawRectangle(pen, new Rectangle(-margin, pageBound.Height - margin, margin * 2, margin * 2));
- }
- if (!(HorizonPaperIndex == HorizonPaperCount - 1 && VerticalPaperIndex == VerticalPaperCount - 1))
- {
- e.Graphics.DrawRectangle(pen, new Rectangle(pageBound.Width - margin, (int)pageBound.Height - margin, margin * 2, margin * 2));
- }
- pen.Dispose();
- }
- Bitmap currentPrintBitmap = Crop(DisplayBitmap, rectangle);
- e.Graphics.DrawImage(currentPrintBitmap, new Rectangle(margin, margin, pageBound.Width - 2 * margin, pageBound.Height - 2 * margin), new Rectangle(0, 0, currentPrintBitmap.Width, currentPrintBitmap.Height), GraphicsUnit.Pixel);
- currentPrintBitmap.Dispose();
- }
- ///三种进位:当前列,当前行,当前页
- 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++;
- PosterCurrentBitmap.Dispose();
- DisplayBitmap.Dispose();
- GC.Collect();
- GC.WaitForPendingFinalizers();
- HorizonPaperIndex = 0;
- VerticalPaperIndex = 0;
- e.HasMorePages = true;
- }
- else
- {
- HorizonPaperIndex = 0;
- VerticalPaperIndex = 0;
- e.HasMorePages = false;
- }
- } //当一页只有一张打印纸的时候
- else
- {
- e.Graphics.DrawImage(DisplayBitmap, new Rectangle(0, 0, pageBound.Width, pageBound.Height), new Rectangle(0, 0, DisplayBitmap.Width, DisplayBitmap.Height), GraphicsUnit.Pixel);
- if (posterPrintPaperIndex < PrintedPageCount - 1)
- {
- ///当前总文件未打印完
- posterPrintPaperIndex++;
- PosterCurrentBitmap.Dispose();
- DisplayBitmap.Dispose();
- GC.Collect();
- GC.WaitForPendingFinalizers();
- 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;
- }
- if ((HorizonPaperIndex == 0 && VerticalPaperIndex == 0) || HorizonPaperCount * VerticalPaperCount == 1)
- {
- PosterCurrentBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)resizedWidth, (int)resizedHeight, PrintSettingsInfo.PageRangeList[PrintPageIndex], PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- }
- startPoint.X = (int)((PreviewWidth - PosterCurrentBitmap.Width) / 2);
- startPoint.Y = (int)((PreviewHeight - PosterCurrentBitmap.Height) / 2);
- if (PrintSettingsInfo.IsGrayscale)
- {
- PosterCurrentBitmap = ToGray(PosterCurrentBitmap, 0);
- }
- DisplayBitmap = CombineBitmap(DisplayBitmap, PosterCurrentBitmap, startPoint);
- ///取有效尺寸和重叠,边沿合并
- ///有效尺寸+重叠边
- Rectangle rectangle = new Rectangle((int)(HorizonPaperIndex * effectiveWidth), (int)(VerticalPaperIndex * effectiveHeight), (int)effectiveWidth, (int)effectiveHeight);
- ///左重叠
- if (HorizonPaperIndex != 0)
- {
- rectangle.Location = new System.Drawing.Point((int)(rectangle.Location.X - posterInfo.OverLap * mmToDpiRatio * widthDpiRatio), rectangle.Location.Y);
- }
- ///上重叠
- if (VerticalPaperIndex != 0)
- {
- rectangle.Size = new System.Drawing.Size(rectangle.Size.Width, (int)(rectangle.Size.Height + posterInfo.OverLap * mmToDpiRatio * heightDpiRatio));
- }
- ///右重叠
- if (HorizonPaperIndex != HorizonPaperCount)
- {
- rectangle.Size = new System.Drawing.Size((int)(rectangle.Size.Width + posterInfo.OverLap * mmToDpiRatio * widthDpiRatio), rectangle.Size.Height);
- }
- ///下重叠
- if (VerticalPaperIndex != VerticalPaperCount)
- {
- rectangle.Location = new System.Drawing.Point(rectangle.Location.X, (int)(rectangle.Location.Y - posterInfo.OverLap * mmToDpiRatio * heightDpiRatio));
- }
- ///当存在边沿时
- int margin = 0;
- ///Label和CutMarks
- ///
- if (posterInfo.HasLabel || posterInfo.HasCutMarks)
- {
- margin = 20;
- }
- if (posterInfo.HasLabel)
- {
- string sign = null;
- if (posterInfo.Label == null || posterInfo.Label == "")
- {
- ///Fix 13366: (行、列)文件名 时间 日期)
- sign = string.Format("({0},{1}) -{2} -{3}.pdf {4} {5}", HorizonPaperIndex + 1, VerticalPaperIndex + 1, PrintIndex + 1, PDFViewer.Document.FileName, System.DateTime.Today.ToString("yyyy/MM/dd"), System.DateTime.Now.ToString("HH:mm:ss"));
- }
- else
- {
- sign = posterInfo.Label;
- }
- e.Graphics.DrawString(sign, new Font("Helvetica", 10.0f), System.Drawing.Brushes.Black, margin + 5, 0);
- }
- if (posterInfo.HasCutMarks)
- {
- System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
- //除了四角之外都要添加裁切标记
- if (!(HorizonPaperIndex == 0 && VerticalPaperIndex == 0))
- {
- e.Graphics.DrawRectangle(pen, new Rectangle(-margin, -margin, 2 * margin, 2 * margin));
- }
- if (!(HorizonPaperIndex == HorizonPaperCount - 1 && VerticalPaperIndex == 0))
- {
- e.Graphics.DrawRectangle(pen, new Rectangle(pageBound.Width - margin, -margin, margin * 2, margin * 2));
- }
- if (!(HorizonPaperIndex == 0 && VerticalPaperIndex == VerticalPaperCount - 1))
- {
- e.Graphics.DrawRectangle(pen, new Rectangle(-margin, (int)pageBound.Height - margin, margin * 2, margin * 2));
- }
- if (!(HorizonPaperIndex == HorizonPaperCount - 1 && VerticalPaperIndex == VerticalPaperCount - 1))
- {
- e.Graphics.DrawRectangle(pen, new Rectangle(pageBound.Width - margin, (int)pageBound.Height - margin, margin * 2, margin * 2));
- }
- pen.Dispose();
- }
- Bitmap currentPrintBitmap = Crop(DisplayBitmap, rectangle);
- e.Graphics.DrawImage(currentPrintBitmap, new Rectangle(margin, margin, pageBound.Width - 2 * margin, pageBound.Height - 2 * margin), new Rectangle(0, 0, currentPrintBitmap.Width, currentPrintBitmap.Height), GraphicsUnit.Pixel);
- currentPrintBitmap.Dispose();
- if (PrintIndex < WholePaperCountModPoster - 1)
- {
- PrintIndex++;
- if (PrintIndex % wholeCountSinglePage == 0)
- {
- PosterCurrentBitmap.Dispose();
- DisplayBitmap.Dispose();
- GC.Collect();
- GC.WaitForPendingFinalizers();
- }
- e.HasMorePages = true;
- }
- else
- {
- e.HasMorePages = false;
- }
- }
- }
- /// <summary>
- /// 优化内存的小册子模式
- /// </summary>
- /// <param name="e"></param>
- private void PrintDocumentModMultiple(PrintPageEventArgs e)
- {
- MultipleInfo multipleInfo = (MultipleInfo)PrintSettingsInfo.PrintModInfo;
- int widthDpiRatio;
- int heightDpiRatio;
- ///TODO 可扩展:高低分辨率打印
- if (true)
- {
- widthDpiRatio = (int)e.Graphics.DpiX / 100;
- heightDpiRatio = (int)e.Graphics.DpiY / 100;
- }
- int singlePageHeight;
- int singlePageWidth;
- ///打印纸实际上可画范围
- Rectangle realBound = new Rectangle(documentSettingsInfo.margins.Left,
- documentSettingsInfo.margins.Top,
- e.PageBounds.Width - documentSettingsInfo.margins.Left - documentSettingsInfo.margins.Right,
- e.PageBounds.Height - documentSettingsInfo.margins.Top - documentSettingsInfo.margins.Bottom);
- ///每一个PDF页实际可用的范围
- singlePageHeight = realBound.Height / multipleInfo.VerticalPageNumber;
- singlePageWidth = realBound.Width / multipleInfo.HorizontalPageNumber;
- ///起画点
- System.Drawing.Point startPoint = new System.Drawing.Point(0, 0);
- int currentPage = 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;
- }
- ///这些渲染都使用WidthDpi
- ///防止横竖Dpi不同引发图片拉伸
- pageBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)(cPDFPage.PageSize.Width * widthDpiRatio), (int)(cPDFPage.PageSize.Height * widthDpiRatio), currentPage, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- if (multipleInfo.IsAutoRotate)
- {
- if ((cPDFPage.PageSize.Height / cPDFPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
- (cPDFPage.PageSize.Height / cPDFPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
- {
- pageBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
- }
- }
- ///重设大小
- ///计算出应有的大小,但是不马上应用
- 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);
- }
- startPoint.X += (singlePageWidth - resizedWidth) / 2;
- startPoint.Y += (singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- pageBitmap = ToGray(pageBitmap, 0);
- }
- e.Graphics.DrawImage(pageBitmap, new Rectangle(realBound.X + startPoint.X, realBound.Y + startPoint.Y, resizedWidth, resizedHeight), new Rectangle(0, 0, pageBitmap.Width, pageBitmap.Height), GraphicsUnit.Pixel);
- //绘制边界
- if (PrintSettingsInfo.IsPrintPageBorde)
- {
- System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
- e.Graphics.DrawRectangle(pen, new Rectangle(realBound.X + startPoint.X - 2, realBound.Y + startPoint.Y - 2, resizedWidth + 4, resizedHeight + 4));
- pen.Dispose();
- }
- pageBitmap.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 * widthDpiRatio), (int)(cPDFPage.PageSize.Height * widthDpiRatio), currentPage, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- if (multipleInfo.IsAutoRotate)
- {
- if ((cPDFPage.PageSize.Height / cPDFPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
- (cPDFPage.PageSize.Height / cPDFPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
- {
- pageBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
- }
- }
- ///重设大小
- ///计算出应有的大小,但是不马上应用
- 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);
- }
- startPoint.X += (singlePageWidth - resizedWidth) / 2;
- startPoint.Y += (singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- pageBitmap = ToGray(pageBitmap, 0);
- }
- e.Graphics.DrawImage(pageBitmap, new Rectangle(realBound.X + startPoint.X, realBound.Y + startPoint.Y, resizedWidth, resizedHeight), new Rectangle(0, 0, pageBitmap.Width, pageBitmap.Height), GraphicsUnit.Pixel);
- //绘制边界
- if (PrintSettingsInfo.IsPrintPageBorde)
- {
- System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
- e.Graphics.DrawRectangle(pen, new Rectangle(realBound.X + startPoint.X - 2, realBound.Y + startPoint.Y - 2, resizedWidth + 4, resizedHeight + 4));
- pen.Dispose();
- }
- pageBitmap.Dispose();
- startPoint.X -= (singlePageWidth - resizedWidth) / 2;
- }
- if (isNoSrcPage)
- {
- isNoSrcPage = false;
- break;
- }
- }
- if (IsManualDuplex && PrintIndex % 2 == 1 && multipleInfo.EnumDuplexPrintMod == EnumDuplexPrintMod.StatusFlipShortEdge)
- {
- }
- if (IsManualDuplex)
- {
- if (PrintIndex < PrintedPageCount - 2)
- {
- PrintIndex += 2;
- e.HasMorePages = true;
- }
- else
- {
- e.HasMorePages = false;
- if (PrintIndex % 2 == 0)
- {
- this.printDocumentEvent.GetEvent<SendFinishedFrontSideEvent>().Publish(new FinishedFrontSide { Unicode = this.Unicode });
- }
- }
- }
- //单面打印
- else
- {
- if (PrintIndex < PrintedPageCount - 1)
- {
- PrintIndex++;
- e.HasMorePages = true;
- }
- else
- {
- e.HasMorePages = false;
- }
- }
- }
- }
- private void PrintDocumentModBooklet(PrintPageEventArgs e)
- {
- BookletInfo bookletInfo = (BookletInfo)PrintSettingsInfo.PrintModInfo;
- int widthDpiRatio;
- int heightDpiRatio;
- ///TODO 可扩展:高低分辨率打印
- if (true)
- {
- widthDpiRatio = (int)e.Graphics.DpiX / 100;
- heightDpiRatio = (int)e.Graphics.DpiY / 100;
- }
- Rectangle realBound = new Rectangle(documentSettingsInfo.margins.Left, documentSettingsInfo.margins.Top, e.PageBounds.Width - documentSettingsInfo.margins.Left - documentSettingsInfo.margins.Right, e.PageBounds.Height - documentSettingsInfo.margins.Top - documentSettingsInfo.margins.Bottom);
- double singlePageHeight;
- double singlePageWidth;
- if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
- {
- singlePageHeight = (double)documentSettingsInfo.DocumentHeight;
- singlePageWidth = (double)(documentSettingsInfo.DocumentWidth / 2);
- }
- else
- {
- singlePageHeight = (double)documentSettingsInfo.DocumentWidth;
- singlePageWidth = (double)(documentSettingsInfo.DocumentHeight / 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;
- int resizedHeight = 0;
- int resizedWidth = 0;
- CPDFPage leftPage = null;
- CPDFPage rightPage = null;
- int leftPageIndex = 0;
- int rightPageIndex = 0;
- ///<summary>
- ///左装订与右装订顺序相反
- ///左装订:
- /// </summary>
- if (bookletInfo.EnumBookletBinding == EnumBookletBinding.StatusLeft)
- {
- ///奇数页:
- ///右面为页面序号
- ///左面为[(总打印纸数*4)+1-打印页右侧页序号]
- if ((TargetPaperList[PrintIndex + 1] + 1) % 2 == 1)
- {
- try
- {
- leftPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - TargetPaperList[PrintIndex + 1] - 1];
- leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
- }
- catch
- {
- leftPage = null;
- }
- //左面
- if (leftPage != null)
- {
- leftBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)leftPage.PageSize.Width, (int)leftPage.PageSize.Height, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- if (bookletInfo.IsAutoRotate)
- {
- if ((leftPage.PageSize.Height / leftPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
- (leftPage.PageSize.Height / leftPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
- {
- leftBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
- }
- }
- ///重设大小
- if (((float)leftBitmap.Height / leftBitmap.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = (int)(singlePageHeight * 0.96);
- resizedWidth = (int)(singlePageHeight / leftBitmap.Height * leftBitmap.Width * 0.96);
- }
- else
- {
- resizedWidth = (int)(singlePageWidth * 0.96);
- resizedHeight = (int)(singlePageWidth / leftBitmap.Width * leftBitmap.Height * 0.96);
- }
- startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- leftBitmap = ToGray(leftBitmap, 0);
- }
- e.Graphics.DrawImage(leftBitmap, new Rectangle(realBound.X + startPoint.X, realBound.Y + startPoint.Y, resizedWidth, resizedHeight), new Rectangle(0, 0, leftBitmap.Width, leftBitmap.Height), GraphicsUnit.Pixel);
- if (PrintSettingsInfo.IsPrintPageBorde)
- {
- System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
- e.Graphics.DrawRectangle(pen, new Rectangle(realBound.X + startPoint.X - 2, realBound.Y + startPoint.Y - 2, resizedWidth + 4, resizedHeight + 4));
- pen.Dispose();
- }
- }
- ///右面
- ///
- try
- {
- rightPageIndex = PrintSettingsInfo.PageRangeList[TargetPaperList[PrintIndex + 1]];
- rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
- }
- catch
- {
- rightPage = null;
- rightPage = null;
- }
- if (rightPage != null)
- {
- startPoint.X = (int)singlePageWidth;
- rightBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)rightPage.PageSize.Width * widthDpiRatio, (int)rightPage.PageSize.Height * heightDpiRatio, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- if (bookletInfo.IsAutoRotate)
- {
- if ((rightPage.PageSize.Height / rightPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
- (rightPage.PageSize.Height / rightPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
- {
- rightBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
- }
- }
- ///重设大小
- if (((float)rightBitmap.Height / rightBitmap.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = (int)(singlePageHeight * 0.96);
- resizedWidth = (int)(singlePageHeight / rightBitmap.Height * rightBitmap.Width * 0.96);
- }
- else
- {
- resizedWidth = (int)(singlePageWidth * 0.96);
- resizedHeight = (int)(singlePageWidth / rightBitmap.Width * rightBitmap.Height * 0.96);
- }
- startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- rightBitmap = ToGray(leftBitmap, 0);
- }
- e.Graphics.DrawImage(rightBitmap, new Rectangle(realBound.X + startPoint.X, realBound.Y + startPoint.Y, resizedWidth, resizedHeight), new Rectangle(0, 0, rightBitmap.Width, rightBitmap.Height), GraphicsUnit.Pixel);
- if (PrintSettingsInfo.IsPrintPageBorde)
- {
- System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
- e.Graphics.DrawRectangle(pen, new Rectangle(realBound.X + startPoint.X - 2, realBound.Y + startPoint.Y - 2, resizedWidth + 4, resizedHeight + 4));
- pen.Dispose();
- }
- }
- }
- ///偶数页
- else
- {
- ///左侧
- try
- {
- leftPageIndex = PrintSettingsInfo.PageRangeList[TargetPaperList[PrintIndex + 1]];
- leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
- }
- catch
- {
- leftPage = null;
- }
- if (leftPage != null)
- {
- leftBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)leftPage.PageSize.Width * widthDpiRatio, (int)leftPage.PageSize.Height * heightDpiRatio, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- if (bookletInfo.IsAutoRotate)
- {
- if ((leftPage.PageSize.Height / leftPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
- (leftPage.PageSize.Height / leftPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
- {
- leftBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
- }
- }
- ///重设大小
- if (((float)leftBitmap.Height / leftBitmap.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = (int)(singlePageHeight * 0.96);
- resizedWidth = (int)(singlePageHeight / leftBitmap.Height * leftBitmap.Width * 0.96);
- }
- else
- {
- resizedWidth = (int)(singlePageWidth * 0.96);
- resizedHeight = (int)(singlePageWidth / leftBitmap.Width * leftBitmap.Height * 0.96);
- }
- startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- leftBitmap = ToGray(leftBitmap, 0);
- }
- e.Graphics.DrawImage(leftBitmap, new Rectangle(realBound.X + startPoint.X, realBound.Y + startPoint.Y, resizedWidth, resizedHeight), new Rectangle(0, 0, leftBitmap.Width, leftBitmap.Height), GraphicsUnit.Pixel);
- if (PrintSettingsInfo.IsPrintPageBorde)
- {
- System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
- e.Graphics.DrawRectangle(pen, new Rectangle(realBound.X + startPoint.X - 2, realBound.Y + startPoint.Y - 2, resizedWidth + 4, resizedHeight + 4));
- pen.Dispose();
- }
- }
- //右面
- try
- {
- rightPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - TargetPaperList[PrintIndex + 1] - 1];
- rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
- }
- catch
- {
- rightPage = null;
- }
- if (rightPage != null)
- {
- startPoint.X = (int)singlePageWidth;
- rightBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)rightPage.PageSize.Width, (int)rightPage.PageSize.Height, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- if (bookletInfo.IsAutoRotate)
- {
- if ((rightPage.PageSize.Height / rightPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
- (rightPage.PageSize.Height / rightPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
- {
- rightBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
- }
- }
- ///重设大小
- if (((float)rightBitmap.Height / rightBitmap.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = (int)(singlePageHeight * 0.96);
- resizedWidth = (int)(singlePageHeight / rightBitmap.Height * rightBitmap.Width * 0.96);
- }
- else
- {
- resizedWidth = (int)(singlePageWidth * 0.96);
- resizedHeight = (int)(singlePageWidth / rightBitmap.Width * rightBitmap.Height * 0.96);
- }
- startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- rightBitmap = ToGray(rightBitmap, 0);
- }
- e.Graphics.DrawImage(rightBitmap, new Rectangle(realBound.X + startPoint.X, realBound.Y + startPoint.Y, resizedWidth, resizedHeight), new Rectangle(0, 0, rightBitmap.Width, rightBitmap.Height), GraphicsUnit.Pixel);
- if (PrintSettingsInfo.IsPrintPageBorde)
- {
- System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
- e.Graphics.DrawRectangle(pen, new Rectangle(realBound.X + startPoint.X - 2, realBound.Y + startPoint.Y - 2, resizedWidth + 4, resizedHeight + 4));
- pen.Dispose();
- }
- }
- }
- }
- ///右装订
- else
- {
- if ((TargetPaperList[PrintIndex + 1] + 1) % 2 == 1)
- {
- try
- {
- rightPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - TargetPaperList[PrintIndex + 1] - 1];
- rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
- }
- catch
- {
- rightPage = null;
- }
- //左面
- if (rightPage != null)
- {
- startPoint.X = (int)singlePageWidth;
- rightBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)rightPage.PageSize.Width, (int)rightPage.PageSize.Height, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- if (bookletInfo.IsAutoRotate)
- {
- if ((rightPage.PageSize.Height / rightPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
- (rightPage.PageSize.Height / rightPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
- {
- rightBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
- }
- }
- ///重设大小
- if (((float)rightBitmap.Height / rightBitmap.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = (int)(singlePageHeight * 0.96);
- resizedWidth = (int)(singlePageHeight / rightBitmap.Height * rightBitmap.Width * 0.96);
- }
- else
- {
- resizedWidth = (int)(singlePageWidth * 0.96);
- resizedHeight = (int)(singlePageWidth / rightBitmap.Width * rightBitmap.Height * 0.96);
- }
- startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- rightBitmap = ToGray(rightBitmap, 0);
- }
- e.Graphics.DrawImage(rightBitmap, new Rectangle(realBound.X + startPoint.X, realBound.Y + startPoint.Y, resizedWidth, resizedHeight), new Rectangle(0, 0, rightBitmap.Width, rightBitmap.Height), GraphicsUnit.Pixel);
- if (PrintSettingsInfo.IsPrintPageBorde)
- {
- System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
- e.Graphics.DrawRectangle(pen, new Rectangle(realBound.X + startPoint.X - 2, realBound.Y + startPoint.Y - 2, resizedWidth + 4, resizedHeight + 4));
- pen.Dispose();
- }
- }
- ///右面
- ///
- try
- {
- leftPageIndex = PrintSettingsInfo.PageRangeList[TargetPaperList[PrintIndex + 1]];
- leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
- }
- catch
- {
- leftPage = null;
- leftPage = null;
- }
- if (leftPage != null)
- {
- leftBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)leftPage.PageSize.Width * widthDpiRatio, (int)leftPage.PageSize.Height * heightDpiRatio, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- if (bookletInfo.IsAutoRotate)
- {
- if ((leftPage.PageSize.Height / leftPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
- (leftPage.PageSize.Height / leftPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
- {
- leftBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
- }
- }
- ///重设大小
- if (((float)leftBitmap.Height / leftBitmap.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = (int)(singlePageHeight * 0.96);
- resizedWidth = (int)(singlePageHeight / leftBitmap.Height * leftBitmap.Width * 0.96);
- }
- else
- {
- resizedWidth = (int)(singlePageWidth * 0.96);
- resizedHeight = (int)(singlePageWidth / leftBitmap.Width * leftBitmap.Height * 0.96);
- }
- startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- leftBitmap = ToGray(leftBitmap, 0);
- }
- e.Graphics.DrawImage(leftBitmap, new Rectangle(realBound.X + startPoint.X, realBound.Y + startPoint.Y, resizedWidth, resizedHeight), new Rectangle(0, 0, leftBitmap.Width, leftBitmap.Height), GraphicsUnit.Pixel);
- if (PrintSettingsInfo.IsPrintPageBorde)
- {
- System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
- e.Graphics.DrawRectangle(pen, new Rectangle(realBound.X + startPoint.X - 2, realBound.Y + startPoint.Y - 2, resizedWidth + 4, resizedHeight + 4));
- pen.Dispose();
- }
- }
- }
- ///偶数页
- else
- {
- try
- {
- rightPageIndex = PrintSettingsInfo.PageRangeList[TargetPaperList[PrintIndex + 1]];
- rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
- }
- catch
- {
- rightPage = null;
- }
- if (rightPage != null)
- {
- startPoint.X = (int)singlePageWidth;
- rightBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)rightPage.PageSize.Width * widthDpiRatio, (int)rightPage.PageSize.Height * heightDpiRatio, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- if (bookletInfo.IsAutoRotate)
- {
- if ((rightPage.PageSize.Height / rightPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
- (rightPage.PageSize.Height / rightPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
- {
- rightBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
- }
- }
- ///重设大小
- if (((float)rightBitmap.Height / rightBitmap.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = (int)(singlePageHeight * 0.96);
- resizedWidth = (int)(singlePageHeight / rightBitmap.Height * rightBitmap.Width * 0.96);
- }
- else
- {
- resizedWidth = (int)(singlePageWidth * 0.96);
- resizedHeight = (int)(singlePageWidth / rightBitmap.Width * rightBitmap.Height * 0.96);
- }
- startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- rightBitmap = ToGray(rightBitmap, 0);
- }
- e.Graphics.DrawImage(rightBitmap, new Rectangle(realBound.X + startPoint.X, realBound.Y + startPoint.Y, resizedWidth, resizedHeight), new Rectangle(0, 0, rightBitmap.Width, rightBitmap.Height), GraphicsUnit.Pixel);
- if (PrintSettingsInfo.IsPrintPageBorde)
- {
- System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
- e.Graphics.DrawRectangle(pen, new Rectangle(realBound.X + startPoint.X - 2, realBound.Y + startPoint.Y - 2, resizedWidth + 4, resizedHeight + 4));
- pen.Dispose();
- }
- }
- try
- {
- leftPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - TargetPaperList[PrintIndex + 1] - 1];
- leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
- }
- catch
- {
- leftPage = null;
- }
- if (leftPage != null)
- {
- leftBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)leftPage.PageSize.Width, (int)leftPage.PageSize.Height, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- if (bookletInfo.IsAutoRotate)
- {
- if ((leftPage.PageSize.Height / leftPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
- (leftPage.PageSize.Height / leftPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
- {
- leftBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
- }
- }
- ///重设大小
- if (((float)leftBitmap.Height / leftBitmap.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = (int)(singlePageHeight * 0.96);
- resizedWidth = (int)(singlePageHeight / leftBitmap.Height * leftBitmap.Width * 0.96);
- }
- else
- {
- resizedWidth = (int)(singlePageWidth * 0.96);
- resizedHeight = (int)(singlePageWidth / leftBitmap.Width * leftBitmap.Height * 0.96);
- }
- startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- leftBitmap = ToGray(leftBitmap, 0);
- }
- e.Graphics.DrawImage(leftBitmap, new Rectangle(realBound.X + startPoint.X, realBound.Y + startPoint.Y, resizedWidth, resizedHeight), new Rectangle(0, 0, leftBitmap.Width, leftBitmap.Height), GraphicsUnit.Pixel);
- if (PrintSettingsInfo.IsPrintPageBorde)
- {
- System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2);
- e.Graphics.DrawRectangle(pen, new Rectangle(realBound.X + startPoint.X - 2, realBound.Y + startPoint.Y - 2, resizedWidth + 4, resizedHeight + 4));
- pen.Dispose();
- }
- }
- }
- }
- if (IsManualDuplex)
- {
- if (PrintIndex < PrintedPageCount - 2)
- {
- PrintIndex += 2;
- e.HasMorePages = true;
- }
- else
- {
- e.HasMorePages = false;
- if (PrintIndex % 2 == 0)
- {
- this.printDocumentEvent.GetEvent<SendFinishedFrontSideEvent>().Publish(new FinishedFrontSide { Unicode = this.Unicode });
- }
- }
- }
- //单面打印或自动双面
- else
- {
- if (PrintIndex < PrintedPageCount - 1)
- {
- PrintIndex++;
- e.HasMorePages = true;
- }
- else
- {
- e.HasMorePages = false;
- }
- }
- }
- #endregion
- 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);
- ErrorPageTxtVisibility = Visibility.Collapsed;
- }
- else
- {
- ErrorPageTxtVisibility = Visibility.Visible;
- }
- }
- //跳转指定页
- 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)
- {
- if (PrintSettingsInfo.IsPaperSizeChanged)
- {
- documentSettingsInfo.IsSettingsChanged = true;
- }
- if (documentSettingsInfo.IsSettingsChanged)
- {
- documentSettingsInfo.DocumentWidth = PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize.Width;
- documentSettingsInfo.DocumentHeight = PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize.Height;
- documentSettingsInfo.margins = PrintSettingsInfo.PrintDocument.DefaultPageSettings.Margins;
- documentSettingsInfo.DocumentBounds = PrintSettingsInfo.PrintDocument.DefaultPageSettings.Bounds;
- documentSettingsInfo.IsSettingsChanged = false;
- }
- CaculatePrintedPageCount();
- SetPaperCollection();
- if (PrintedPageCount != PreviousPrintedPageCount)
- {
- TargetPaperIndex = 1;
- PreviousPrintedPageCount = PrintedPageCount;
- }
- PaintPageByCurrentPreviewIndex(TargetPaperIndex);
- }
- }
- public void SetViewBox(double height, double width)
- {
- if (height / width >= (248.0 / 180.0))
- {
- ViewBoxHeight = 248.0;
- ViewBoxWidth = (width / height * 248.0);
- }
- else
- {
- ViewBoxWidth = 180.0;
- ViewBoxHeight = (height / width * 180.0);
- }
- }
- #region 渲染具体函数
- [System.Runtime.InteropServices.DllImport("gdi32.dll")]
- public static extern bool DeleteObject(IntPtr hObject);
- /// <summary>
- /// bitmap->bitmapsource转换器,用于显示到image控件
- ///
- /// </summary>
- /// <param name="bmp">
- /// Bitmap格式图片资源
- /// </param>
- /// <returns></returns>
- public BitmapSource ToBitmapSource(System.Drawing.Bitmap bmp)
- {
- IntPtr ptr = bmp.GetHbitmap();//obtain the Hbitmap
- try
- {
- 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;
- }
- finally
- {
- DeleteObject(ptr);
- }
- }
- /// <summary>
- /// 创建空白底图
- /// </summary>
- private Bitmap CreateBlankBitmap()
- {
- if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
- {
- blankPageBitmap = new Bitmap((int)documentSettingsInfo.DocumentWidth, (int)documentSettingsInfo.DocumentHeight);
- }
- else
- {
- blankPageBitmap = new Bitmap((int)documentSettingsInfo.DocumentHeight, (int)documentSettingsInfo.DocumentWidth);
- }
- return blankPageBitmap;
- }
- /// <summary>
- /// 设置预览(除海报)
- /// </summary>
- public void SetPreview()
- {
- if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
- {
- PaperWidth = String.Format("{0:F1}", (documentSettingsInfo.DocumentWidth) / 100.0 * 25.4);
- PaperHeight = String.Format("{0:F1}", (documentSettingsInfo.DocumentHeight) / 100.0 * 25.4);
- }
- else
- {
- PaperHeight = String.Format("{0:F1}", (documentSettingsInfo.DocumentWidth) / 100.0 * 25.4);
- PaperWidth = String.Format("{0:F1}", (documentSettingsInfo.DocumentHeight) / 100.0 * 25.4);
- }
- if (PrintSettingsInfo.PrintModInfo.EnumPrintMod != EnumPrintMod.StatusPoster)
- {
- SetViewBox(double.Parse(PaperHeight), double.Parse(PaperWidth));
- blankPageBitmap = CreateBlankBitmap();
- }
- }
- public System.Drawing.Image ResizeImage(System.Drawing.Image image, System.Drawing.Size size)
- {
- //获取图片宽度
- int sourceWidth = image.Width;
- //获取图片高度
- int sourceHeight = image.Height;
- float nPercent = 0;
- float nPercentW = 0;
- float nPercentH = 0;
- //计算宽度的缩放比例
- //nPercentW = ((float)size.Width * (float)GetDpiByGraphics()/120/ (float)sourceWidth);
- ////计算高度的缩放比例
- //nPercentH = ((float)size.Height*(float)GetDpiByGraphics() / 120 / (float)sourceHeight);
- nPercentW = ((float)size.Width / (float)sourceWidth);
- //计算高度的缩放比例
- nPercentH = ((float)size.Height / (float)sourceHeight);
- if (nPercentH < nPercentW)
- nPercent = nPercentH;
- else
- nPercent = nPercentW;
- //期望的宽度
- int destWidth = (int)(sourceWidth * nPercent);
- //期望的高度
- int destHeight = (int)(sourceHeight * nPercent);
- System.Drawing.Bitmap b = new System.Drawing.Bitmap(destWidth, destHeight);
- Graphics g = Graphics.FromImage((System.Drawing.Image)b);
- //g.InterpolationMode = InterpolationMode.HighQualityBicubic;
- g.SmoothingMode = SmoothingMode.None; //抗锯齿
- g.InterpolationMode = InterpolationMode.HighQualityBicubic; //高质量的双三次插值
- g.CompositingQuality = CompositingQuality.HighQuality; //高质量、低速度复合
- //绘制图像
- g.DrawImage(image, 0, 0, destWidth, destHeight);
- g.Dispose();
- return (System.Drawing.Image)b;
- }
- /// <summary>
- /// 直接Resize到给定大小
- /// </summary>
- /// <param name="input"></param>
- /// <param name="targetWidth"></param>
- /// <param name="targetHeight"></param>
- /// <returns></returns>
- public 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.HighQualityBicubic; //设定插值方式
- g.DrawImage(input,
- new Rectangle(0, 0, targetWidth, targetHeight),
- new Rectangle(0, 0, input.Width, input.Height),
- GraphicsUnit.Pixel);
- g.Dispose();
- return actualBitmap;
- }
- catch
- {
- 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 * PDFToMediaDpiRatio), (int)(cPDFPage.PageSize.Height * PDFToMediaDpiRatio), PrintSettingsInfo.PageRangeList[paperIndex], PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- if (PrintSettingsInfo.IsGrayscale)
- {
- bitmap = ToGray(bitmap, 0);
- }
- ///<summary>
- ///自适应模式
- /// </summary>
- if (sizeInfo.EnumSizeType == EnumSizeType.StatusAdaptive)
- {
- 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 effectiveWidth;
- //计算有效打印区域
- if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
- {
- effectiveHeight = documentSettingsInfo.DocumentHeight;
- effectiveWidth = documentSettingsInfo.DocumentWidth;
- }
- else
- {
- effectiveHeight = documentSettingsInfo.DocumentWidth;
- effectiveWidth = documentSettingsInfo.DocumentHeight;
- }
- //重叠
- effectiveHeight = effectiveHeight - 2 * posterInfo.OverLap * 100 / 25.4;
- effectiveWidth = effectiveWidth - 2 * posterInfo.OverLap * 100 / 25.4;
- if (posterInfo.HasCutMarks || posterInfo.HasLabel)
- {
- ///如果边距小于预留尺寸
- effectiveHeight = effectiveHeight - 40;
- effectiveWidth = effectiveWidth - 40;
- }
- if (cPDFPage != null)
- {
- //平铺模式
- //根据页面大小,按最大有效打印区域划分为多块
- //最大有效区域
- //纸张大小-标记标签-重叠
- if (posterInfo.EnumPosterMod == EnumPosterMod.StatusTile)
- {
- Bitmap bitmap;
- //TODO 放大倍数过高引起超限
- try
- {
- bitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)(cPDFPage.PageSize.Width * PDFToMediaDpiRatio * (posterInfo.TileRatio / 100.0)), (int)(cPDFPage.PageSize.Height * PDFToMediaDpiRatio * (posterInfo.TileRatio / 100.0)), PrintSettingsInfo.PageRangeList[paperIndex], PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- }
- catch
- {
- bitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)(cPDFPage.PageSize.Width * PDFToMediaDpiRatio), (int)(cPDFPage.PageSize.Height * PDFToMediaDpiRatio), 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)(effectiveWidth * HorizonPaperCount);
- PreviewHeight = (double)(effectiveHeight * 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.Gray, HorizonPaperCount * 2), 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.Gray, VerticalPaperCount * 2), 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)(effectiveWidth * HorizonPaperCount);
- PreviewHeight = (double)(effectiveHeight * 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.Gray, HorizonPaperCount * 2), 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.Gray, VerticalPaperCount * 2), 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)(documentSettingsInfo.DocumentHeight / multipleInfo.VerticalPageNumber);
- singlePageWidth = (int)(documentSettingsInfo.DocumentWidth / multipleInfo.HorizontalPageNumber);
- printBitmap = new Bitmap((int)documentSettingsInfo.DocumentWidth, (int)documentSettingsInfo.DocumentHeight);
- }
- else
- {
- singlePageHeight = (int)(documentSettingsInfo.DocumentWidth / multipleInfo.VerticalPageNumber);
- singlePageWidth = (int)(documentSettingsInfo.DocumentHeight / multipleInfo.HorizontalPageNumber);
- printBitmap = new Bitmap((int)documentSettingsInfo.DocumentHeight, (int)documentSettingsInfo.DocumentWidth);
- }
- 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 (multipleInfo.IsAutoRotate)
- {
- if ((cPDFPage.PageSize.Height / cPDFPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
- (cPDFPage.PageSize.Height / cPDFPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
- {
- pageBitmap = ToolMethod.RenderPageBitmapNoWait(PDFViewer.Document, (int)(cPDFPage.PageSize.Width), (int)(cPDFPage.PageSize.Height), currentPage, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- pageBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
- }
- }
- ///重设大小
- 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);
- if (multipleInfo.IsAutoRotate)
- {
- if ((cPDFPage.PageSize.Height / cPDFPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
- (cPDFPage.PageSize.Height / cPDFPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
- {
- pageBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
- }
- }
- 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)documentSettingsInfo.DocumentWidth, (int)documentSettingsInfo.DocumentHeight);
- singlePageHeight = (double)documentSettingsInfo.DocumentHeight;
- singlePageWidth = (double)(documentSettingsInfo.DocumentWidth / 2);
- }
- else
- {
- printBitmap = new Bitmap((int)documentSettingsInfo.DocumentHeight, (int)documentSettingsInfo.DocumentWidth);
- singlePageHeight = (double)documentSettingsInfo.DocumentWidth;
- singlePageWidth = (double)(documentSettingsInfo.DocumentHeight / 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)
- {
- leftBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)leftPage.PageSize.Width, (int)leftPage.PageSize.Height, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- if (bookletInfo.IsAutoRotate)
- {
- if ((leftPage.PageSize.Height / leftPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
- (leftPage.PageSize.Height / leftPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
- {
- leftBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
- }
- }
- ///重设大小
- if ((leftBitmap.Height / leftBitmap.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = singlePageHeight * 0.96;
- resizedWidth = singlePageHeight / leftBitmap.Height * leftBitmap.Width * 0.96;
- }
- else
- {
- resizedWidth = singlePageWidth * 0.96;
- resizedHeight = singlePageWidth / leftBitmap.Width * leftBitmap.Height * 0.96;
- }
- leftBitmap = Resize(leftBitmap, (int)(resizedWidth), (int)(resizedHeight));
- 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)
- {
- rightBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)rightPage.PageSize.Width, (int)rightPage.PageSize.Height, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- if (bookletInfo.IsAutoRotate)
- {
- if ((rightPage.PageSize.Height / rightPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
- (rightPage.PageSize.Height / rightPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
- {
- rightBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
- }
- }
- ///重设大小
- if ((rightBitmap.Height / rightBitmap.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = singlePageHeight * 0.96;
- resizedWidth = singlePageHeight / rightBitmap.Height * rightBitmap.Width * 0.96;
- }
- else
- {
- resizedWidth = singlePageWidth * 0.96;
- resizedHeight = singlePageWidth / rightBitmap.Width * rightBitmap.Height * 0.96;
- }
- rightBitmap = Resize(rightBitmap, (int)(resizedWidth), (int)(resizedHeight));
- startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- rightBitmap = ToGray(rightBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, rightBitmap, 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, (int)resizedWidth, (int)resizedHeight));
- g.Dispose();
- }
- }
- }
- }
- /// 偶数页:
- /// 左面为页面序号
- /// 右面为[(总打印纸数*4)+1-打印页右侧页序号]
- else
- {
- try
- {
- leftPageIndex = PrintSettingsInfo.PageRangeList[paperIndex];
- leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
- }
- catch
- {
- leftPage = null;
- }
- //左面
- if (leftPage != null)
- {
- leftBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)leftPage.PageSize.Width, (int)leftPage.PageSize.Height, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- if (bookletInfo.IsAutoRotate)
- {
- if ((leftPage.PageSize.Height / leftPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
- (leftPage.PageSize.Height / leftPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
- {
- leftBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
- }
- }
- ///重设大小
- if ((leftBitmap.Height / leftBitmap.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = singlePageHeight * 0.96;
- resizedWidth = singlePageHeight / leftBitmap.Height * leftBitmap.Width * 0.96;
- }
- else
- {
- resizedWidth = singlePageWidth * 0.96;
- resizedHeight = singlePageWidth / leftBitmap.Width * leftBitmap.Height * 0.96;
- }
- leftBitmap = Resize(leftBitmap, (int)(resizedWidth), (int)(resizedHeight));
- startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- leftBitmap = ToGray(leftBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, leftBitmap, 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, (int)resizedWidth, (int)resizedHeight));
- g.Dispose();
- }
- }
- }
- //右面
- try
- {
- rightPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - 1 - paperIndex];
- rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
- }
- catch
- {
- rightPage = null;
- }
- startPoint.X = (int)singlePageWidth;
- if (rightPage != null)
- {
- ///重设大小
- rightBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)rightPage.PageSize.Width, (int)rightPage.PageSize.Height, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- if (bookletInfo.IsAutoRotate)
- {
- if ((rightPage.PageSize.Height / rightPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
- (rightPage.PageSize.Height / rightPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
- {
- rightBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
- }
- }
- ///重设大小
- if ((rightBitmap.Height / rightBitmap.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = singlePageHeight * 0.96;
- resizedWidth = singlePageHeight / rightBitmap.Height * rightBitmap.Width * 0.96;
- }
- else
- {
- resizedWidth = singlePageWidth * 0.96;
- resizedHeight = singlePageWidth / rightBitmap.Width * rightBitmap.Height * 0.96;
- }
- rightBitmap = Resize(rightBitmap, (int)(resizedWidth), (int)(resizedHeight));
- startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- rightBitmap = ToGray(rightBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, rightBitmap, 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, (int)resizedWidth, (int)resizedHeight));
- g.Dispose();
- }
- }
- }
- }
- }
- ///<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)
- {
- leftBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)leftPage.PageSize.Width, (int)leftPage.PageSize.Height, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- if (bookletInfo.IsAutoRotate)
- {
- if ((leftPage.PageSize.Height / leftPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
- (leftPage.PageSize.Height / leftPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
- {
- leftBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
- }
- }
- ///重设大小
- if ((leftBitmap.Height / leftBitmap.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = singlePageHeight * 0.96;
- resizedWidth = singlePageHeight / leftBitmap.Height * leftBitmap.Width * 0.96;
- }
- else
- {
- resizedWidth = singlePageWidth;
- resizedHeight = singlePageWidth / leftBitmap.Width * leftBitmap.Height;
- }
- leftBitmap = Resize(leftBitmap, (int)(resizedWidth), (int)(resizedHeight));
- startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- leftBitmap = ToGray(leftBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, leftBitmap, 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, (int)resizedWidth, (int)resizedHeight));
- g.Dispose();
- }
- }
- }
- //右面
- rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
- startPoint.X = (int)singlePageWidth;
- if (rightPage != null)
- {
- ///重设大小
- rightBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)rightPage.PageSize.Width, (int)rightPage.PageSize.Height, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- if (bookletInfo.IsAutoRotate)
- {
- if ((rightPage.PageSize.Height / rightPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
- (rightPage.PageSize.Height / rightPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
- {
- rightBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
- }
- }
- ///重设大小
- if ((rightBitmap.Height / rightBitmap.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = singlePageHeight * 0.96;
- resizedWidth = singlePageHeight / rightBitmap.Height * rightBitmap.Width * 0.96;
- }
- else
- {
- resizedWidth = singlePageWidth * 0.96;
- resizedHeight = singlePageWidth / rightBitmap.Width * rightBitmap.Height * 0.96;
- }
- rightBitmap = Resize(rightBitmap, (int)(resizedWidth), (int)(resizedHeight));
- startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- rightBitmap = ToGray(rightBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, rightBitmap, 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, (int)resizedWidth, (int)resizedHeight));
- g.Dispose();
- }
- }
- }
- }
- /// 偶数页:
- /// 左面为页面序号
- /// 右面为[(总打印纸数*4)+1-打印页右侧页序号]
- else
- {
- leftPageIndex = PrintSettingsInfo.PageRangeList[paperIndex];
- //左面
- leftPage = PDFViewer.Document.PageAtIndex(leftPageIndex);
- if (leftPage != null)
- {
- ///重设大小
- leftBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)leftPage.PageSize.Width, (int)leftPage.PageSize.Height, leftPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- if (bookletInfo.IsAutoRotate)
- {
- if ((leftPage.PageSize.Height / leftPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
- (leftPage.PageSize.Height / leftPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
- {
- leftBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
- }
- }
- ///重设大小
- if ((leftBitmap.Height / leftBitmap.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = singlePageHeight * 0.96;
- resizedWidth = singlePageHeight / leftBitmap.Height * leftBitmap.Width * 0.96;
- }
- else
- {
- resizedWidth = singlePageWidth * 0.96;
- resizedHeight = singlePageWidth / leftBitmap.Width * leftBitmap.Height * 0.96;
- }
- leftBitmap = Resize(leftBitmap, (int)(resizedWidth), (int)(resizedHeight)); startPoint.X = (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- leftBitmap = ToGray(leftBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, leftBitmap, 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, (int)resizedWidth, (int)resizedHeight));
- g.Dispose();
- }
- }
- }
- //右面
- try
- {
- rightPageIndex = PrintSettingsInfo.PageRangeList[maxPaperNumber * 4 - 1 - paperIndex];
- rightPage = PDFViewer.Document.PageAtIndex(rightPageIndex);
- }
- catch
- {
- rightPage = null;
- }
- startPoint.X = (int)singlePageWidth;
- if (rightPage != null)
- {
- ///重设大小
- rightBitmap = await ToolMethod.RenderPageBitmap(PDFViewer.Document, (int)rightPage.PageSize.Width, (int)rightPage.PageSize.Height, rightPageIndex, PrintSettingsInfo.IsPrintWithAnnot, PrintSettingsInfo.IsPrintWithForm);
- if (bookletInfo.IsAutoRotate)
- {
- if ((rightPage.PageSize.Height / rightPage.PageSize.Width > 1 && (double)singlePageHeight / singlePageWidth < 1) ||
- (rightPage.PageSize.Height / rightPage.PageSize.Width < 1 && (double)singlePageHeight / singlePageWidth > 1))
- {
- rightBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
- }
- }
- ///重设大小
- if ((rightBitmap.Height / rightBitmap.Width) >= singlePageHeight / singlePageWidth)
- {
- resizedHeight = singlePageHeight * 0.96;
- resizedWidth = singlePageHeight / rightBitmap.Height * rightBitmap.Width * 0.96;
- }
- else
- {
- resizedWidth = singlePageWidth * 0.96;
- resizedHeight = singlePageWidth / rightBitmap.Width * rightBitmap.Height * 0.96;
- }
- rightBitmap = Resize(rightBitmap, (int)(resizedWidth), (int)(resizedHeight)); startPoint.X += (int)(singlePageWidth - resizedWidth) / 2;
- startPoint.Y = (int)(singlePageHeight - resizedHeight) / 2;
- if (PrintSettingsInfo.IsGrayscale)
- {
- rightBitmap = ToGray(rightBitmap, 0);
- }
- printBitmap = CombineBitmap(printBitmap, rightBitmap, 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, (int)resizedWidth, (int)resizedHeight));
- g.Dispose();
- }
- }
- }
- }
- }
- BitmapSource = ToBitmapSource(printBitmap);
- }
- #endregion
- /// <summary>
- /// 按照模式绘制指定纸张
- /// </summary>
- /// <param name="TargetPageIndex"></param>
- private void PaintPageByCurrentPreviewIndex(int TargetPaperIndex)
- {
- lock (this)
- {
- if (PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusSize)
- {
- SetPreview();
- PreviewModSize(TargetPaperList[TargetPaperIndex]);
- }
- else if (PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusPoster)
- {
- SetPreview();
- PreviewModPoster(TargetPaperList[TargetPaperIndex]);
- }
- else if (PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusMultiple)
- {
- SetPreview();
- PreviewModMultiple(TargetPaperList[TargetPaperIndex]);
- }
- else if (PrintSettingsInfo.PrintModInfo.EnumPrintMod == EnumPrintMod.StatusBooklet)
- {
- SetPreview();
- 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 < PrintSettingsInfo.PageRangeList.Count; 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)
- {
- documentSettingsInfo.DocumentWidth = PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize.Width;
- documentSettingsInfo.DocumentHeight = PrintSettingsInfo.PrintDocument.DefaultPageSettings.PaperSize.Height;
- documentSettingsInfo.margins = PrintSettingsInfo.PrintDocument.DefaultPageSettings.Margins;
- documentSettingsInfo.DocumentBounds = PrintSettingsInfo.PrintDocument.DefaultPageSettings.Bounds;
- SetPreview();
- CaculatePrintedPageCount();
- SetPaperCollection();
- PaintPageByCurrentPreviewIndex(1);
- }
- }
- }
- }
|