123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- using ComPDFKitViewer.PdfViewer;
- using PDF_Office.EventAggregators;
- using PDF_Office.Model;
- using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePagePrinter;
- using Prism.Commands;
- using Prism.Events;
- using Prism.Mvvm;
- using Prism.Regions;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter
- {
- public class HomePagePrinterDocumentContentViewModel : BindableBase, INavigationAware
- {
- private CPDFViewer PDFViewer;
- public PrintSettingsInfo PrintSettingsInfo;
- public IEventAggregator printDocumentEvent;
- private string _paperWidth;
- public string PaperWidth
- {
- get => _paperWidth;
- set => SetProperty(ref _paperWidth, value);
- }
- private string _paperHeight;
- public string PaperHeight
- {
- get => _paperHeight;
- set => SetProperty(ref _paperHeight, value);
- }
- private string _viewBoxWidth;
- public string ViewBoxWidth
- {
- get => _viewBoxWidth;
- set => SetProperty(ref _viewBoxWidth, value);
- }
- private string _viewBoxHeight;
- public string ViewBoxHeight
- {
- get => _viewBoxHeight;
- set => SetProperty(ref _viewBoxHeight, value);
- }
- /// <summary>
- /// 打印的总页数
- /// </summary>
- private int _printedPageCount;
- public int PrintedPageCount
- {
- get => _printedPageCount;
- set => SetProperty(ref _printedPageCount, value);
- }
- public HomePagePrinterDocumentContentViewModel(IEventAggregator eventAggregator)
- {
- this.printDocumentEvent = eventAggregator;
- printDocumentEvent.GetEvent<SendPrintSettingsInfoEvent>().Subscribe(RecvPrintSettingsInfo);
- }
- public void RecvPrintSettingsInfo(PrintSettingsInfo printSettingsInfo)
- {
- if (PDFViewer != null && printSettingsInfo != null)
- {
- SetPreview();
- CaculatePrintedPageCount();
- }
- }
- public void SetViewBox()
- {
- if (float.Parse(PaperHeight) / float.Parse(PaperWidth) >= (306.0 / 224.0))
- {
- ViewBoxHeight = "306";
- ViewBoxWidth = (float.Parse(PaperWidth) / float.Parse(PaperHeight) * 306).ToString();
- }
- else
- {
- ViewBoxWidth = "224";
- ViewBoxHeight = (float.Parse(PaperHeight) / float.Parse(PaperWidth) * 224).ToString();
- }
- }
- public void SetPreview()
- {
- if (PrintSettingsInfo.printModInfo.EnumPrintMod != EnumPrintMod.StatusPoster)
- {
- if (PrintSettingsInfo.EnumPrintOrientation == EnumPrintOrientation.StatusPortrait)
- {
- PaperWidth = String.Format("{0:F1}", (PrintSettingsInfo.PageMediaSize.Width) / 96 * 25.4);
- PaperHeight = String.Format("{0:F1}", (PrintSettingsInfo.PageMediaSize.Height) / 96 * 25.4);
- }
- else
- {
- PaperHeight = String.Format("{0:F1}", (PrintSettingsInfo.PageMediaSize.Width) / 96 * 25.4);
- PaperWidth = String.Format("{0:F1}", (PrintSettingsInfo.PageMediaSize.Height) / 96 * 25.4);
- }
- SetViewBox();
- }
- else
- {
- }
- }
- public void CaculatePrintedPageCount()
- {
- PrintedPageCount = PDFViewer.Document.PageCount;
- if (PrintSettingsInfo.EnumPageRange == EnumPageRange.StatusOddRange)
- {
- if (PrintedPageCount % 2 == 0)
- {
- PrintedPageCount = PrintedPageCount / 2;
- }
- else
- {
- PrintedPageCount = PrintedPageCount / 2 + 1;
- }
- }
- else if (PrintSettingsInfo.EnumPageRange == EnumPageRange.StatusEvenRange)
- {
- if (PrintedPageCount % 2 == 0)
- {
- PrintedPageCount = PrintedPageCount / 2;
- }
- else
- {
- PrintedPageCount = PrintedPageCount / 2;
- }
- }
- else if (PrintSettingsInfo.EnumPageRange == EnumPageRange.StatusCustomizedRange)
- {
- PrintedPageCount = PrintSettingsInfo.CustomRange.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) * 2;
- //PrintedPageCount = (PrintedPageCount%4 == 0)?(PrintedPageCount/2):((PrintedPageCount / 4) + 1) * 2;
- if (bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusFrontSideOnly ||
- bookletInfo.EnumBookletSubset == EnumBookletSubset.StatusBackSideOnly)
- {
- PrintedPageCount /= 2;
- }
- }
- }
- }
- public bool IsNavigationTarget(NavigationContext navigationContext)
- {
- return true;
- }
- public void OnNavigatedFrom(NavigationContext navigationContext)
- {
- }
- public void OnNavigatedTo(NavigationContext navigationContext)
- {
- navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
- navigationContext.Parameters.TryGetValue<PrintSettingsInfo>(ParameterNames.PrintSettingsInfo, out PrintSettingsInfo);
- if (PDFViewer != null && PrintSettingsInfo != null)
- {
- SetPreview();
- CaculatePrintedPageCount();
- }
- }
- }
- }
|