FrmPDFView.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using DevExpress.XtraEditors;
  11. using System.IO;
  12. using ProjectBase.Data.Logs;
  13. namespace ProjectBase.Controls.DocViewer
  14. {
  15. /// <summary>
  16. /// PDF测试显示窗体
  17. /// </summary>
  18. public partial class FrmPDFView : DevExpress.XtraEditors.XtraForm
  19. {
  20. /// <summary>
  21. /// 加载流数据
  22. /// </summary>
  23. public Stream Stream { get; set; }
  24. /// <summary>
  25. /// 文件后缀名,如.pdf
  26. /// </summary>
  27. public string Extension { get; set; }
  28. /// <summary>
  29. /// 文档文件路径。如果指定了该属性,可以不用设置Stream和Extension属性。
  30. /// </summary>
  31. public string FilePath { get; set; }
  32. /// <summary>
  33. /// 是否显示打开文件按钮
  34. /// </summary>
  35. public bool ShowBtnOpenFile
  36. {
  37. get { return m_ShowBtnOpenFile; }
  38. set
  39. {
  40. m_ShowBtnOpenFile = value;
  41. this.btnOpenFile.Visible = this.ShowBtnOpenFile;
  42. }
  43. }
  44. //记录窗体的名称
  45. readonly string mainFormText;
  46. private bool m_ShowBtnOpenFile = false;//是否显示打开文件按钮
  47. public FrmPDFView()
  48. {
  49. InitializeComponent();
  50. //记录窗体的名称,并实现文档变化事件的处理,方便显示新的文件名称
  51. mainFormText = this.Text;
  52. pdfViewer1.DocumentChanged += new DevExpress.XtraPdfViewer.PdfDocumentChangedEventHandler(pdfViewer1_DocumentChanged);
  53. }
  54. /// <summary>
  55. /// PDF文档变化后,实现对新文件名称的显示
  56. /// </summary>
  57. void pdfViewer1_DocumentChanged(object sender, DevExpress.XtraPdfViewer.PdfDocumentChangedEventArgs e)
  58. {
  59. string fileName = Path.GetFileName(e.DocumentFilePath);
  60. if (String.IsNullOrEmpty(fileName))
  61. {
  62. Text = mainFormText;
  63. }
  64. else
  65. {
  66. Text = fileName + " - " + mainFormText;
  67. }
  68. }
  69. private void FrmPDFView_Load(object sender, EventArgs e)
  70. {
  71. //如果文件流不为空,首先根据Stream对象加载文档,否则根据文件路径进行加载
  72. if (!this.DesignMode)
  73. {
  74. if (this.Stream != null)
  75. {
  76. #region MyRegion
  77. try
  78. {
  79. this.pdfViewer1.LoadDocument(this.Stream);
  80. }
  81. catch (Exception ex)
  82. {
  83. DevExpress.XtraEditors.XtraMessageBox.Show("加载PDF文件失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  84. LogHelper.log.Error(string.Format("加载PDF文件失败,错误为{0}", ex.ToString()));
  85. }
  86. #endregion
  87. }
  88. else if (!string.IsNullOrEmpty(FilePath))
  89. {
  90. this.pdfViewer1.LoadDocument(this.FilePath);
  91. }
  92. }
  93. }
  94. /// <summary>
  95. /// 打开PDF文件
  96. /// </summary>
  97. private void btnOpenFile_Click(object sender, EventArgs e)
  98. {
  99. string filePath;
  100. OpenFileDialog dialog = new OpenFileDialog();
  101. dialog.Filter = "PDF(*.pdf)|*.pdf|All File(*.*)|*.*";
  102. dialog.Title = "PDF选择";
  103. dialog.RestoreDirectory = true;
  104. dialog.FileName = null;
  105. if (dialog.ShowDialog() == DialogResult.OK)
  106. {
  107. filePath = dialog.FileName;
  108. }
  109. else
  110. {
  111. filePath = string.Empty;
  112. }
  113. if (!string.IsNullOrEmpty(filePath))
  114. {
  115. this.pdfViewer1.LoadDocument(filePath);
  116. }
  117. }
  118. /// <summary>
  119. /// 另存为PDF文件
  120. /// </summary>
  121. private void btnSaveAs_Click(object sender, EventArgs e)
  122. {
  123. string dir = System.Environment.CurrentDirectory;
  124. string filePath = null;
  125. SaveFileDialog dialog = new SaveFileDialog();
  126. dialog.Filter = "PDF(*.pdf)|*.pdf|All File(*.*)|*.*";
  127. dialog.Title = "保存Pdf";
  128. dialog.FileName = "";
  129. dialog.RestoreDirectory = true;
  130. if (!string.IsNullOrEmpty(dir))
  131. {
  132. dialog.InitialDirectory = dir;
  133. }
  134. if (dialog.ShowDialog() == DialogResult.OK)
  135. {
  136. filePath = dialog.FileName;
  137. }
  138. if (!string.IsNullOrEmpty(filePath))
  139. {
  140. try
  141. {
  142. this.pdfViewer1.SaveDocument(filePath);
  143. DevExpress.XtraEditors.XtraMessageBox.Show("保存PDF文件成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  144. this.Close();
  145. }
  146. catch (Exception ex)
  147. {
  148. DevExpress.XtraEditors.XtraMessageBox.Show("保存PDF文件失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  149. LogHelper.log.Error(string.Format("保存PDF文件失败,错误为{0}", ex.ToString()));
  150. }
  151. }
  152. }
  153. /// <summary>
  154. /// PDF文件打印
  155. /// </summary>
  156. private void btnPreview_Click(object sender, EventArgs e)
  157. {
  158. this.pdfViewer1.Print();
  159. }
  160. private void btnClose_Click(object sender, EventArgs e)
  161. {
  162. this.Close();
  163. }
  164. }
  165. }