123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785 |
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFPage;
- using ComPDFKit.PDFWatermark;
- using ComPDFKitViewer.PdfViewer;
- using PDF_Office.CustomControl;
- using PDF_Office.EventAggregators;
- using PDF_Office.Helper;
- using PDF_Office.Model;
- using PDF_Office.Model.EditTools.Background;
- using PDF_Office.Model.EditTools.Watermark;
- using PDF_Office.Model.PageEdit;
- using PDF_Office.Properties;
- using PDF_Office.ViewModels.Tools;
- using PDFSettings;
- using Prism.Commands;
- using Prism.Events;
- using Prism.Mvvm;
- using Prism.Regions;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows;
- namespace PDF_Office.ViewModels.EditTools.Watermark
- {
- public class WatermarkCreateFileContentViewModel : BindableBase, INavigationAware
- {
- public WatermarkInfo WatermarkInfo = new WatermarkInfo();
- private WatermarkItem TemplateWatermarkItem;
- private CPDFViewer PDFViewer;
- IEventAggregator eventAggregator;
- private List<string> _opacityList = new List<string>();
- public List<string> OpacityList
- {
- get { return _opacityList; }
- set
- {
- SetProperty(ref _opacityList, value);
- }
- }
- private void InitOpacityList()
- {
- OpacityList.Clear();
- for (int temp = 0; temp <= 100; temp += 10)
- {
- OpacityList.Add(temp.ToString() + " %");
- }
- }
- private List<string> _rotationList = new List<string>();
- public List<string> RotationList
- {
- get { return _rotationList; }
- set
- {
- SetProperty(ref _rotationList, value);
- }
- }
- private void InitRotationList()
- {
- RotationList.Clear();
- for (int temp = -45; temp <= 45; temp += 15)
- {
- RotationList.Add(temp.ToString());
- }
- }
- private List<string> _scaleList = new List<string>();
- public List<string> ScaleList
- {
- get { return _scaleList; }
- set
- {
- SetProperty(ref _scaleList, value);
- }
- }
- private void InitScaleList()
- {
- ScaleList.Clear();
- for (int temp = 0; temp <= 100; temp += 10)
- {
- ScaleList.Add(temp.ToString() + " %");
- }
- }
- private List<string> _isFrontList = new List<string>();
- public List<string> IsFrontList
- {
- get { return _isFrontList; }
- set
- {
- SetProperty(ref _isFrontList, value);
- }
- }
- private void InitIsFrontList()
- {
- IsFrontList.Clear();
- IsFrontList.Add("位于页面上方");
- IsFrontList.Add("位于页面下方");
- }
- private int _rotationValue = 0;
- public int RotationValue
- {
- get { return _rotationValue; }
- set
- {
- SetProperty(ref _rotationValue, value);
- WatermarkInfo.Rotation = RotationValue;
- eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
- {
- Unicode = Unicode,
- Status = WatermarkInfo
- });
- }
- }
- private int _opacityValue = 100;
- public int OpacityValue
- {
- get { return _opacityValue; }
- set
- {
- SetProperty(ref _opacityValue, value);
- WatermarkInfo.Opacity = (byte)OpacityValue;
- eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
- {
- Unicode = Unicode,
- Status = WatermarkInfo
- });
- }
- }
- private string _rotationText = "0";
- public string RotationText
- {
- get { return _rotationText; }
- set
- {
- SetProperty(ref _rotationText, value);
- }
- }
- private string _opacityText = "100 %";
- public string OpacityText
- {
- get { return _opacityText; }
- set
- {
- SetProperty(ref _opacityText, value);
- }
- }
- private string _relativeScaleText = "50 %";
- public string RelativeScaleText
- {
- get { return _relativeScaleText; }
- set
- {
- SetProperty(ref _relativeScaleText, value);
- }
- }
- private int _relativeScaleValue = 50;
- public int RelativeScaleValue
- {
- get { return _relativeScaleValue; }
- set
- {
- SetProperty(ref _relativeScaleValue, value);
- WatermarkInfo.ImageSize=RelativeScaleValue;
- eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
- {
- Unicode = Unicode,
- Status = WatermarkInfo
- });
- }
- }
- private string _vertOffsetValue = "0";
- public string VertOffsetValue
- {
- get { return _vertOffsetValue; }
- set
- {
- SetProperty(ref _vertOffsetValue, value);
- WatermarkInfo.VertOffset = float.Parse(VertOffsetValue);
- eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
- {
- Unicode = Unicode,
- Status = WatermarkInfo
- });
- }
- }
- private string _horizOffsetValue = "0";
- public string HorizOffsetValue
- {
- get { return _horizOffsetValue; }
- set
- {
- SetProperty(ref _horizOffsetValue, value);
- WatermarkInfo.HorizOffset = float.Parse(HorizOffsetValue);
- eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
- {
- Unicode = Unicode,
- Status = WatermarkInfo
- });
- }
- }
- private string _verticalSpacingValue = "6";
- public string VerticalSpacingValue
- {
- get { return _verticalSpacingValue; }
- set
- {
- SetProperty(ref _verticalSpacingValue, value);
- WatermarkInfo.VerticalSpacing = float.Parse(VerticalSpacingValue);
- eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
- {
- Unicode = Unicode,
- Status = WatermarkInfo
- });
- }
- }
- private string _horizontalSpacingValue = "6";
- public string HorizontalSpacingValue
- {
- get { return _horizontalSpacingValue; }
- set
- {
- SetProperty(ref _horizontalSpacingValue, value);
- WatermarkInfo.HorizontalSpacing = float.Parse(HorizontalSpacingValue);
- eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
- {
- Unicode = Unicode,
- Status = WatermarkInfo
- });
- }
- }
- public string _pageRangeText = "0";
- public string PageRangeText
- {
- get { return _pageRangeText; }
- set
- {
- _pageRangeText = value;
- EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref WatermarkInfo.PageRange, PageRangeText);
- eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
- {
- Unicode = Unicode,
- Status = WatermarkInfo
- });
- }
- }
- private bool _isFullValue = false;
- public bool IsFullValue
- {
- get { return _isFullValue; }
- set
- {
- SetProperty(ref _isFullValue, value);
- WatermarkInfo.Isfull = IsFullValue;
- eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
- {
- Unicode = Unicode,
- Status = WatermarkInfo
- });
- }
- }
- private bool _isRelativeScaleValue = false;
- public bool IsRelativeScaleValue
- {
- get { return _isRelativeScaleValue; }
- set
- {
- SetProperty(ref _isRelativeScaleValue, value);
- WatermarkInfo.IsRelativeScale = IsRelativeScaleValue;
- eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
- {
- Unicode = Unicode,
- Status = WatermarkInfo
- });
- }
- }
- private int _pageRangeSelectIndex = 0;
- public int PageRangeSelectIndex
- {
- get { return _pageRangeSelectIndex; }
- set
- {
- SetProperty(ref _pageRangeSelectIndex, value);
- WatermarkInfo.PageRangeIndex = PageRangeSelectIndex;
- EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref WatermarkInfo.PageRange, PageRangeText);
- eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
- {
- Unicode = Unicode,
- Status = WatermarkInfo
- });
- }
- }
- private int _isFrontSelectedIndex = 0;
- public int IsFrontSelectedIndex
- {
- get { return _isFrontSelectedIndex; }
- set
- {
- SetProperty(ref _isFrontSelectedIndex, value);
- SetIsFront(IsFrontSelectedIndex);
- eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
- {
- Unicode = Unicode,
- Status = WatermarkInfo
- });
- }
- }
- private string _fileNameText = "";
- public string FileNameText
- {
- get { return _fileNameText; }
- set
- {
- SetProperty(ref _fileNameText, value);
- }
- }
- private Visibility _createFileVisible = Visibility.Collapsed;
- public Visibility CreateFileVisible
- {
- get { return _createFileVisible; }
- set { SetProperty(ref _createFileVisible, value); }
- }
- private ObservableDictionary<string, bool> _getLocationFromNumber = new ObservableDictionary<string, bool>();
- public ObservableDictionary<string, bool> GetLocationFromNumber
- {
- get { return _getLocationFromNumber; }
- set { _getLocationFromNumber = value; }
- }
- public DelegateCommand<object> ChangeLocationCommand { get; set; }
- public DelegateCommand OpenFileCommand { get; set; }
- public string Unicode = null;
- public WatermarkCreateFileContentViewModel(IEventAggregator eventAggregator)
- {
- this.eventAggregator = eventAggregator;
- Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
- WatermarkInfo.WatermarkType = C_Watermark_Type.WATERMARK_TYPE_IMG;
- ChangeLocationCommand = new DelegateCommand<object>(ChangeLocation);
- OpenFileCommand = new DelegateCommand(OpenFile);
- InitList();
- InitWatermarkInfo();
- InitLocationButtonMatrix();
- eventAggregator.GetEvent<SaveWatermarkTemplateEvent>().Subscribe(SaveWatermarkTemplate, e => e.Unicode == Unicode);
- eventAggregator.GetEvent<ConfirmEditWatermarkTemplateItemEvent>().Subscribe(ConfirmEditWatermarkTemplateItem, e => e.Unicode == Unicode);
- }
- public void SaveWatermarkTemplate(EnumTextOrFileUnicode enumTextOrFileunicode)
- {
- EnumTextOrFile enumTextOrFile = enumTextOrFileunicode.Status;
- if (enumTextOrFile == EnumTextOrFile.StatusFile && FileNameText != "")
- {
- SaveCurrentTemplate();
- }
- }
- public void ConvertInfoToItem(ref WatermarkItem watermarkItem, WatermarkInfo watermarkInfo)
- {
- if (watermarkInfo != null)
- {
- watermarkItem.type = watermarkInfo.WatermarkType;
- //watermarkItem.imageArray = watermarkInfo.ImageArray;
- watermarkItem.imagepath = watermarkInfo.ImagePath;
- watermarkItem.imageSize = watermarkInfo.ImageSize;
- watermarkItem.imageWidth = watermarkInfo.ImageWidth;
- watermarkItem.imageHeight = watermarkInfo.ImageHeight;
- watermarkItem.horizalign = watermarkInfo.WatermarkHorizalign;
- watermarkItem.vertalign = watermarkInfo.WatermarkVertalign;
- watermarkItem.isRelativeScale = watermarkInfo.IsRelativeScale;
- watermarkItem.isFullScreen = watermarkInfo.Isfull;
- watermarkItem.isfront = watermarkInfo.IsFront;
- watermarkItem.opacity = watermarkInfo.Opacity;
- watermarkItem.rotation = watermarkInfo.Rotation;
- watermarkItem.vertOffset = watermarkInfo.VertOffset;
- watermarkItem.horizOffset = watermarkInfo.HorizOffset;
- watermarkItem.verticalSpacing = watermarkInfo.VerticalSpacing;
- watermarkItem.horizontalSpacing = watermarkInfo.HorizontalSpacing;
- watermarkItem.pageRangeIndex = watermarkInfo.PageRangeIndex;
- watermarkItem.pageRange = watermarkInfo.PageRange;
- }
- }
- public void ConvertItemToInfo(WatermarkItem watermarkItem, ref WatermarkInfo watermarkInfo)
- {
- if (watermarkItem != null)
- {
- watermarkInfo.WatermarkType=C_Watermark_Type.WATERMARK_TYPE_IMG;
- if (watermarkItem.imagepath != "")
- {
- FileInfo file = new FileInfo(watermarkItem.imagepath);
- if (file.Exists)
- {
- if (file.Extension == ".pdf")
- {
- GetBitmapFromDocment(watermarkItem.imagepath);
- }
- else
- {
- EditToolsHelper.ChooseFile(watermarkItem.imagepath, ref WatermarkInfo);
- }
- }
- else
- {
- watermarkInfo.WatermarkType = ComPDFKit.PDFWatermark.C_Watermark_Type.WATERMARK_TYPE_UNKWON;
- MessageBoxEx.Show("原路径为:" + watermarkItem.imagepath + ",该文件不存在");
- }
- }
- watermarkInfo.WatermarkHorizalign = watermarkItem.horizalign;
- watermarkInfo.WatermarkVertalign = watermarkItem.vertalign;
- watermarkInfo.IsRelativeScale = watermarkItem.isRelativeScale;
- watermarkInfo.Isfull = watermarkItem.isFullScreen;
- watermarkInfo.IsFront = watermarkItem.isfront;
- watermarkInfo.ImageSize = watermarkItem.imageSize;
- watermarkInfo.Opacity = watermarkItem.opacity;
- watermarkInfo.Rotation = watermarkItem.rotation;
- watermarkInfo.VertOffset = watermarkItem.vertOffset;
- watermarkInfo.HorizOffset = watermarkItem.horizOffset;
- watermarkInfo.VerticalSpacing = watermarkItem.verticalSpacing;
- watermarkInfo.HorizontalSpacing = watermarkItem.horizontalSpacing;
- watermarkInfo.PageRangeIndex = watermarkItem.pageRangeIndex;
- EditToolsHelper.GetPageRange(watermarkItem.pageRangeIndex, PDFViewer.Document, ref WatermarkInfo.PageRange, watermarkItem.pageRange);
- }
- }
- public void SaveCurrentTemplate()
- {
- var watermarkItem = new WatermarkItem();
- ConvertInfoToItem(ref watermarkItem, WatermarkInfo);
- watermarkItem.templateName += Settings.Default.WatermarkIndex.ToString();
- try
- {
- //创建缓存文件夹
- string folderPath = Path.Combine(App.CurrentPath, "Watermark");
- //有可能因为其他原因存在同名文件,导致创建文件夹失败,需要先删除同名文件
- //保险措施(猜测)
- if (File.Exists(folderPath))
- {
- File.Delete(folderPath);
- }
- DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
- if (!tempfolder.Exists)
- {
- tempfolder.Create();
- }
- //预览图缓存
- string saveName = Guid.NewGuid().ToString();
- string savePath = Path.Combine(folderPath, saveName);
- System.Windows.Size pageSize = new Size { Width = 128, Height = 160 };
- CPDFDocument tempDocument = CPDFDocument.CreateDocument();
- if (tempDocument != null)
- {
- tempDocument.InsertPage(0, pageSize.Width, pageSize.Height, null);
- CreateWatermark(tempDocument, WatermarkInfo);
- //获取透明背景的图片
- var bitmap = ToolMethod.RenderPageBitmapNoWait(tempDocument, (int)pageSize.Width, (int)pageSize.Height, 0, false, false, 0x00FFFFFF);
- using (FileStream stream = new FileStream(savePath, FileMode.Create))
- {
- bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
- }
- watermarkItem.previewImagePath = savePath;
- tempDocument.Release();
- Settings.Default.WatermarkTemplateList.Add(watermarkItem);
- Settings.Default.Save();
- }
- }
- catch
- {
- }
-
- this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode
- {
- Unicode = Unicode,
- Status = EnumTemplateListOrCreate.StatusTemplate
- });
- }
- private void SetIsFront(int Index)
- {
- if (Index == 0)
- {
- WatermarkInfo.IsFront = true;
- }
- if (Index == 1)
- {
- WatermarkInfo.IsFront = false;
- }
- }
- private void InitList()
- {
- InitOpacityList();
- InitIsFrontList();
- InitRotationList();
- InitScaleList();
- }
- private void InitWatermarkInfo()
- {
- WatermarkInfo.Rotation = RotationValue;
- WatermarkInfo.ImageSize=RelativeScaleValue;
- WatermarkInfo.Opacity = (byte)OpacityValue ;
- WatermarkInfo.VertOffset = float.Parse(VertOffsetValue);
- WatermarkInfo.HorizOffset = float.Parse(HorizOffsetValue);
- WatermarkInfo.VerticalSpacing = float.Parse(VerticalSpacingValue);
- WatermarkInfo.HorizontalSpacing = float.Parse(HorizontalSpacingValue);
- WatermarkInfo.WatermarkHorizalign = C_Watermark_Horizalign.WATERMARK_HORIZALIGN_CENTER;
- WatermarkInfo.WatermarkVertalign = C_Watermark_Vertalign.WATERMARK_VERTALIGN_CENTER;
- WatermarkInfo.IsRelativeScale = IsRelativeScaleValue;
- WatermarkInfo.Isfull = IsFullValue;
- SetIsFront(IsFrontSelectedIndex);
- }
- private void InitLocationButtonMatrix()
- {
- GetLocationFromNumber.Clear();
- for (var temp = 0; temp <= 22; temp++)
- {
- GetLocationFromNumber.Add(temp.ToString(), true);
- if (temp % 10 == 2)
- {
- temp += 7;
- }
- }
- int Num = (int)WatermarkInfo.WatermarkVertalign * 10 + (int)WatermarkInfo.WatermarkHorizalign;
- GetLocationFromNumber[Num.ToString()] = false;
- }
- public void ChangeLocation(object e)
- {
- string args = e as string;
- if (args != null)
- {
- WatermarkInfo.WatermarkVertalign = (C_Watermark_Vertalign)(int.Parse(args) / 10);
- WatermarkInfo.WatermarkHorizalign = (C_Watermark_Horizalign)(int.Parse(args) % 10);
- eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
- {
- Unicode = Unicode,
- Status = WatermarkInfo
- });
- InitLocationButtonMatrix();
- }
- }
- public void ConfirmEditToolsWatermark()
- {
- eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
- {
- Unicode = Unicode,
- Status = WatermarkInfo
- });
- }
- public void CreateWatermark(CPDFDocument document, WatermarkInfo watermarkInfo)
- {
- if (watermarkInfo != null)
- {
- document.DeleteWatermarks();
- CPDFWatermark watermark;
- if (watermarkInfo.WatermarkType == C_Watermark_Type.WATERMARK_TYPE_TEXT)
- {
- watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_TEXT);
- watermark.SetText(watermarkInfo.Text);
- watermark.SetFontName(watermarkInfo.FontName);
- watermark.SetFontSize(watermarkInfo.TextSize);
- //watermark.SetScale(watermarkInfo.TextSize);
- watermark.SetTextRGBColor(watermarkInfo.TextColor);
- }
- else
- {
- watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_IMG);
- if (watermarkInfo.ImageArray != null)
- {
- watermark.SetImage(watermarkInfo.ImageArray, watermarkInfo.ImageWidth, watermarkInfo.ImageHeight);
- }
- watermark.SetScale(watermarkInfo.ImageSize/100);
- }
- watermark.SetRotation((float)((watermarkInfo.Rotation / 180) * Math.PI));
- watermark.SetOpacity((byte)(((float)watermarkInfo.Opacity / 100) * 255));
- watermark.SetFront(watermarkInfo.IsFront);
- watermark.SetVertalign(watermarkInfo.WatermarkVertalign);
- watermark.SetHorizalign(watermarkInfo.WatermarkHorizalign);
- watermark.SetFullScreen(watermarkInfo.Isfull);
- watermark.SetVertOffset(watermarkInfo.VertOffset);
- watermark.SetHorizOffset(watermarkInfo.HorizOffset);
- watermark.SetHorizontalSpacing(watermarkInfo.HorizontalSpacing);
- watermark.SetVerticalSpacing(watermarkInfo.VerticalSpacing);
- watermark.SetPages("0");
- Trace.WriteLine("IsCreateWatermark: " + watermark.CreateWatermark());
- Trace.WriteLine("IsUpdateWatermark: " + watermark.UpdateWatermark());
- }
- }
- public void ConfirmEditWatermarkTemplateItem(EnumTextOrFileUnicode enumTextOrFileunicode)
- {
- EnumTextOrFile enumTextOrFile = enumTextOrFileunicode.Status;
- if (enumTextOrFile == EnumTextOrFile.StatusFile && FileNameText != "")
- {
- var watermarkItem = new WatermarkItem();
- ConvertInfoToItem(ref watermarkItem, WatermarkInfo);
- try
- {
- //创建缓存文件夹
- string folderPath = Path.Combine(App.CurrentPath, "Watermark");
- //有可能因为其他原因存在同名文件,导致创建文件夹失败,需要先删除同名文件
- //保险措施(猜测)
- if (File.Exists(folderPath))
- {
- File.Delete(folderPath);
- }
- DirectoryInfo tempfolder = new DirectoryInfo(folderPath);
- if (!tempfolder.Exists)
- {
- tempfolder.Create();
- }
- //预览图缓存
- string saveName = Guid.NewGuid().ToString();
- string savePath = Path.Combine(folderPath, saveName);
- System.Windows.Size pageSize = new Size { Width = 128, Height = 160 };
- CPDFDocument tempDocument = CPDFDocument.CreateDocument();
- if (tempDocument != null)
- {
- tempDocument.InsertPage(0, pageSize.Width/4, pageSize.Height, null);
- CreateWatermark(tempDocument, WatermarkInfo);
- //获取透明背景的图片
- var bitmap = ToolMethod.RenderPageBitmapNoWait(tempDocument, (int)pageSize.Width, (int)pageSize.Height, 0, false, false, 0x00FFFFFF);
- using (FileStream stream = new FileStream(savePath, FileMode.Create))
- {
- bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
- }
- watermarkItem.previewImagePath = savePath;
- tempDocument.Release();
- Settings.Default.WatermarkTemplateList[TemplateWatermarkItem.listIndex] = watermarkItem;
- Settings.Default.Save();
- }
- }
- catch
- {
- }
- this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode
- {
- Unicode = Unicode,
- Status = EnumTemplateListOrCreate.StatusTemplate
- });
- }
- }
- /// <summary>
- /// 根据已知Info初始化
- /// </summary>
- public void InitComponentBySelectedInfo()
- {
- ConvertItemToInfo(TemplateWatermarkItem, ref WatermarkInfo);
- VertOffsetValue = WatermarkInfo.VertOffset.ToString();
- HorizOffsetValue = WatermarkInfo.HorizOffset.ToString();
- VerticalSpacingValue = WatermarkInfo.VerticalSpacing.ToString();
- HorizontalSpacingValue = WatermarkInfo.HorizontalSpacing.ToString();
- PageRangeSelectIndex = WatermarkInfo.PageRangeIndex;
- IsFrontSelectedIndex = Convert.ToInt32(!WatermarkInfo.IsFront);
- RotationText = WatermarkInfo.Rotation.ToString();
- OpacityText = WatermarkInfo.Opacity.ToString() + " %";
- RelativeScaleText = WatermarkInfo.TextSize.ToString() + " %";
- InitLocationButtonMatrix();
- CreateFileVisible=Visibility.Visible;
- IsRelativeScaleValue = WatermarkInfo.IsRelativeScale;
- IsFullValue = WatermarkInfo.Isfull;
- RelativeScaleText = WatermarkInfo.ImageSize.ToString() + " %";
- }
- public void OpenFile()
- {
- System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
- dlg.Multiselect = false;
- dlg.Filter = "PDF|*.png;*.jpg;*.pdf";
- if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- FileNameText = dlg.SafeFileName;
- FileInfo file = new FileInfo(dlg.FileName);
- if (file.Extension == ".pdf")
- {
- GetBitmapFromDocment(dlg.FileName);
- }
- else
- {
- EditToolsHelper.ChooseFile(dlg.FileName, ref WatermarkInfo);
- }
- WatermarkInfo.ImagePath = dlg.FileName;
- CreateFileVisible = Visibility.Visible;
- eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
- {
- Unicode = Unicode,
- Status = WatermarkInfo
- });
- }
- }
- public async void GetBitmapFromDocment(string filePath)
- {
- CPDFDocument document = CPDFDocument.InitWithFilePath(filePath);
- CPDFPage page = document.PageAtIndex(0);
- byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
- await Task.Run(delegate
- {
- page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1);
- });
- WatermarkInfo.ImageArray = bmp_data;
- WatermarkInfo.ImageWidth = (int)page.PageSize.Width;
- WatermarkInfo.ImageHeight = (int)page.PageSize.Height;
- document.ReleasePages();
- document.Release();
- }
- 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);
- EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref WatermarkInfo.PageRange, PageRangeText);
- if (navigationContext.Parameters.TryGetValue<WatermarkItem>("WatermarkItem", out TemplateWatermarkItem))
- {
- InitComponentBySelectedInfo();
- }
- }
- }
- }
|