WatermarkListDialog.xaml.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFWatermark;
  3. using ComPDFKit.Controls.Helper;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Data;
  14. using System.Windows.Documents;
  15. using System.Windows.Forms;
  16. using System.Windows.Input;
  17. using System.Windows.Media;
  18. using System.Windows.Media.Imaging;
  19. using System.Windows.Shapes;
  20. using System.Xml.Linq;
  21. using MessageBox = System.Windows.MessageBox;
  22. using Path = System.IO.Path;
  23. namespace ComPDFKit.Controls.PDFControl
  24. {
  25. /// <summary>
  26. /// Interaction logic for WatermarkListDialog.xaml
  27. /// </summary>
  28. public partial class WatermarkListDialog : Window
  29. {
  30. private WatermarkData watermarkData;
  31. public WatermarkListDialog()
  32. {
  33. InitializeComponent();
  34. Title = LanguageHelper.SecurityManager.GetString("Title_AddWatermark");
  35. }
  36. private void NextBtn_Click(object sender, RoutedEventArgs e)
  37. {
  38. WatermarkDialog watermarkDialog = new WatermarkDialog
  39. {
  40. Owner = this
  41. };
  42. watermarkDialog.WindowClosed += WatermarkDialog_WindowClosed;
  43. if (watermarkDialog.InitWithFileInfo(FileGridListWithPageRangeControl.FileForDisplay))
  44. {
  45. watermarkDialog.ShowDialog();
  46. }
  47. else
  48. {
  49. MessageBox.Show("No file selected.");
  50. }
  51. }
  52. private void WatermarkDialog_WindowClosed(object sender, WatermarkData watermarkData)
  53. {
  54. if (watermarkData != null)
  55. {
  56. this.watermarkData = watermarkData;
  57. var dialog = new FolderBrowserDialog();
  58. if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
  59. {
  60. return;
  61. }
  62. else
  63. {
  64. var path = dialog.SelectedPath;
  65. foreach (var fileInfo in FileGridListWithPageRangeControl.FileInfoDataList)
  66. {
  67. if (watermarkData.Type == C_Watermark_Type.WATERMARK_TYPE_TEXT)
  68. {
  69. UpdateByTextWatermarkData(fileInfo.Document, fileInfo.PageRangeList);
  70. }
  71. else if (watermarkData.Type == C_Watermark_Type.WATERMARK_TYPE_IMG)
  72. {
  73. UpdateByImageWatermarkData(fileInfo.Document, fileInfo.PageRangeList);
  74. }
  75. else
  76. {
  77. return;
  78. }
  79. string savePath = Path.Combine(path, Path.GetFileNameWithoutExtension(fileInfo.Document.FileName) + "_"
  80. + LanguageHelper.SecurityManager.GetString("FileName_Watermark")) + ".pdf";
  81. fileInfo.Document.WriteToFilePath(savePath);
  82. }
  83. System.Diagnostics.Process.Start("explorer.exe", "/select," + path + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(FileGridListWithPageRangeControl.FileInfoDataList[0].Document.FileName) + "_Watermark.pdf");
  84. }
  85. }
  86. Close();
  87. }
  88. private void UpdateByImageWatermarkData(CPDFDocument document, List<int> pageRangeList)
  89. {
  90. CPDFWatermark watermark = document.InitWatermark(watermarkData.Type);
  91. Bitmap bitmap = new Bitmap(watermarkData.ImagePath);
  92. bitmap = CommonHelper.ConvertTo32bppArgb(bitmap);
  93. byte[] byteArray = CommonHelper.ConvertBitmapToByteArray(bitmap);
  94. watermark.SetImage(byteArray, bitmap.Width, bitmap.Height);
  95. watermark.SetScale((float)(watermarkData.ImageScale / 100.0));
  96. UpdateByCommonWatermarkData(watermark, pageRangeList);
  97. }
  98. private void UpdateByTextWatermarkData(CPDFDocument document, List<int> pageRangeList)
  99. {
  100. CPDFWatermark watermark = document.InitWatermark(watermarkData.Type);
  101. watermark.SetText(watermarkData.Text);
  102. watermark.SetFontName(watermarkData.FontName);
  103. watermark.SetTextRGBColor(watermarkData.Color);
  104. watermark.SetFontSize(watermarkData.FontSize);
  105. watermark.SetScale(1);
  106. UpdateByCommonWatermarkData(watermark, pageRangeList);
  107. }
  108. private void UpdateByCommonWatermarkData(CPDFWatermark watermark, List<int> pageRangeList)
  109. {
  110. for (int i = 0; i < pageRangeList.Count; i++)
  111. {
  112. pageRangeList[i]--;
  113. }
  114. watermark.SetPages(CommonHelper.GetPageParmFromList(pageRangeList));
  115. watermark.SetRotation((float)(watermarkData.Rotation * Math.PI / 180.0));
  116. watermark.SetOpacity(watermarkData.Opacity);
  117. watermark.SetVertalign((C_Watermark_Vertalign)(watermarkData.Align / 3));
  118. watermark.SetHorizalign((C_Watermark_Horizalign)(watermarkData.Align % 3));
  119. watermark.SetVertOffset(watermarkData.VertOffset);
  120. watermark.SetHorizOffset(watermarkData.HorizOffset);
  121. watermark.SetFront(watermarkData.IsFront);
  122. watermark.SetFullScreen(watermarkData.IsFullScreen);
  123. watermark.SetVerticalSpacing(watermarkData.VerticalSpacing);
  124. watermark.SetHorizontalSpacing(watermarkData.HorizontalSpacing);
  125. watermark.CreateWatermark();
  126. }
  127. private void CancelBtn_Click(object sender, RoutedEventArgs e)
  128. {
  129. Close();
  130. }
  131. protected override void OnClosed(EventArgs e)
  132. {
  133. foreach (var fileInfo in FileGridListWithPageRangeControl.FileInfoDataList)
  134. {
  135. fileInfo.Document.Release();
  136. }
  137. base.OnClosed(e);
  138. }
  139. }
  140. }