ab56a9bcf7
SVN-Revision: r240
1001 lines
38 KiB
C#
1001 lines
38 KiB
C#
/***********************
|
|
* 说明:PDF签名控件
|
|
* 创建人:龚宇超
|
|
* 创建日期:2018-05-29
|
|
* 修改人:
|
|
* 修改日期:
|
|
* 修改备注:
|
|
* 版本:1.0.0.0
|
|
***********************/
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Drawing;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
using iTextSharp.text.pdf;
|
|
using System.IO;
|
|
using O2S.Components.PDFView4NET;
|
|
using Lskj.Util;
|
|
using Lskj.Model;
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
using Lskj.Core;
|
|
using System.Diagnostics;
|
|
using Lskj.Business;
|
|
using System.Text.RegularExpressions;
|
|
using Lskj.Business.Impl;
|
|
using DevExpress.XtraGrid.Columns;
|
|
using DevExpress.Utils;
|
|
using DevExpress.XtraGrid.Views.Grid;
|
|
|
|
namespace Lskj.Control
|
|
{
|
|
public partial class PdfEditorEx : UserControl
|
|
{
|
|
#region 公开变量
|
|
/// <summary>
|
|
/// 当前保存签名的ID
|
|
/// </summary>
|
|
public int CurrentId;
|
|
/// <summary>
|
|
/// 当前页码
|
|
/// </summary>
|
|
public int CurrentPageNo;
|
|
/// <summary>
|
|
/// 源文件(该文件不进行编辑)
|
|
/// </summary>
|
|
public string TempletFilePath = string.Empty;
|
|
/// <summary>
|
|
/// 签名图片
|
|
/// </summary>
|
|
public Image SignImage;
|
|
/// <summary>
|
|
/// 记录签名预制体pictureBox坐标
|
|
/// </summary>
|
|
public PointF SignBoxPoint;
|
|
/// <summary>
|
|
/// 签名载体
|
|
/// </summary>
|
|
public PictureBox PicBox { get { return this.pictureBox; } }
|
|
/// <summary>
|
|
/// 保存回调
|
|
/// </summary>
|
|
public event EventHandler OnSaveCallBack;
|
|
/// <summary>
|
|
/// 删除回调
|
|
/// </summary>
|
|
public event EventHandler OnDeleteCallBack;
|
|
/// <summary>
|
|
/// 另存为回调
|
|
/// </summary>
|
|
public event EventHandler OnSaveAsCallBack;
|
|
#endregion
|
|
|
|
#region 私有变量
|
|
/// <summary>
|
|
/// 当前操作文件
|
|
/// </summary>
|
|
private string _currentPdf;
|
|
/// <summary>
|
|
/// 上一次编辑的文件地址,保存失误后可以返回
|
|
/// </summary>
|
|
private string _pdfHistory;
|
|
/// <summary>
|
|
/// 后台合成的PDf
|
|
/// </summary>
|
|
private string _pdfNew;
|
|
/// <summary>
|
|
/// 签名预制体集
|
|
/// </summary>
|
|
private List<SignBox> _signBoxList;
|
|
/// <summary>
|
|
/// pdf模板
|
|
/// </summary>
|
|
public Dictionary<string, FileInfo> pdfTempDic = new Dictionary<string, FileInfo>();
|
|
#endregion
|
|
|
|
public PdfEditorEx()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
/// <summary>
|
|
/// 窗体初始化时
|
|
/// </summary>
|
|
public void OnLoad()
|
|
{
|
|
InitLeftSignGrid();
|
|
InitTempComboBox();
|
|
}
|
|
/// <summary>
|
|
/// 初始化左侧签名表
|
|
/// </summary>
|
|
private void InitLeftSignGrid()
|
|
{
|
|
try
|
|
{
|
|
GridColumn signColumn = new GridColumn();
|
|
signColumn.FieldName = "signName";
|
|
signColumn.Caption = "签名";
|
|
signColumn.VisibleIndex = 0;
|
|
signColumn.OptionsColumn.AllowEdit = false;
|
|
this.leftSignGrid.GridView.Columns.Add(signColumn);
|
|
|
|
DataTable LeftSignDataSource = new DataTable();
|
|
LeftSignDataSource.Columns.Add("signName");
|
|
LeftSignDataSource.Columns.Add("signPath");
|
|
if (Directory.Exists(PubUtil.PdfSignImagePath))
|
|
{
|
|
DirectoryInfo dirInfo = new DirectoryInfo(PubUtil.PdfSignImagePath);
|
|
FileInfo[] fileInfos = dirInfo.GetFiles();
|
|
foreach (FileInfo fileInfo in fileInfos)
|
|
{
|
|
DataRow dataRow = LeftSignDataSource.NewRow();
|
|
dataRow["signName"] = Path.GetFileNameWithoutExtension(fileInfo.FullName);
|
|
dataRow["signPath"] = fileInfo.FullName;
|
|
LeftSignDataSource.Rows.Add(dataRow);
|
|
}
|
|
}
|
|
this.leftSignGrid.GridView.OptionsBehavior.EditorShowMode = EditorShowMode.MouseDownFocused;
|
|
this.leftSignGrid.GridControl.DataSource = LeftSignDataSource;
|
|
this.leftSignGrid.GridView.DoubleClick += new EventHandler(OnGridView_DoubleClick);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageUtil.Show(ex.Message);
|
|
}
|
|
}
|
|
private void OnGridView_DoubleClick(object sender, EventArgs e)
|
|
{
|
|
if (!string.IsNullOrEmpty(this._currentPdf))
|
|
{
|
|
GridView gridView = (GridView)sender;
|
|
DataRow dataRow = gridView.GetFocusedDataRow();
|
|
if (dataRow != null)
|
|
{
|
|
AddSignPicControl(dataRow);
|
|
}
|
|
}
|
|
}
|
|
private void InitTempComboBox()
|
|
{
|
|
try
|
|
{
|
|
this.checkTemp.Properties.AllowNullInput = DefaultBoolean.True;
|
|
this.checkTemp.Properties.NullText = "请选择模板";
|
|
if (Directory.Exists(PubUtil.PdfSignTempPath))
|
|
{
|
|
DirectoryInfo dirInfo = new DirectoryInfo(PubUtil.PdfSignTempPath);
|
|
FileInfo[] fileInfos = dirInfo.GetFiles();
|
|
foreach (FileInfo fileInfo in fileInfos)
|
|
{
|
|
string tempKey = Path.GetFileNameWithoutExtension(fileInfo.FullName);
|
|
this.pdfTempDic.Add(tempKey, fileInfo);
|
|
this.checkTemp.Properties.Items.Add(tempKey);
|
|
}
|
|
}
|
|
this.checkTemp.SelectedIndexChanged += new EventHandler(OnCheckTemp_SelectedIndexChanged);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageUtil.Show(ex.Message);
|
|
}
|
|
}
|
|
private void OnCheckTemp_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
string tempKey = this.checkTemp.SelectedText;
|
|
TempletFilePath = this.pdfTempDic[tempKey].FullName;
|
|
if (_signBoxList != null)
|
|
{
|
|
foreach (SignBox item in _signBoxList)
|
|
{
|
|
item.ParentPicBox.Dispose();
|
|
}
|
|
}
|
|
_signBoxList = new List<SignBox>();
|
|
InitPdfPage(null);
|
|
}
|
|
/// <summary>
|
|
/// 添加签名控件
|
|
/// </summary>
|
|
public void AddSignPicControl(DataRow dataRow)
|
|
{
|
|
string imgPath = dataRow["signPath"] + "";
|
|
PictureBox pictureBox = new PictureBox();
|
|
pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
|
|
SignBox signBox = new SignBox();
|
|
_signBoxList.Add(signBox);
|
|
try
|
|
{
|
|
Image image = Image.FromFile(imgPath);
|
|
signBox.ParentPicBox = pictureBox;
|
|
signBox.PageNumber = this.CurrentPageNo + 1;
|
|
signBox.ParentPicBoxImagePath = imgPath;
|
|
signBox.SignHeight = image.Height;
|
|
signBox.SignWidth = image.Width;
|
|
signBox.SignTop = 0;
|
|
signBox.SignLeft = 0;
|
|
pictureBox.BorderStyle = BorderStyle.None;
|
|
pictureBox.Image = image;
|
|
//pictureBox.Height = signHeight;
|
|
//pictureBox.Width = signWidth;
|
|
pictureBox.Tag = signBox;
|
|
pictureBox.Parent = this.pdfPageView;
|
|
ContextMenuStrip cms = new ContextMenuStrip();
|
|
cms.Items.Add("删除");
|
|
cms.Items[0].Click += new EventHandler(OnItemClick);
|
|
cms.Items[0].Tag = pictureBox;
|
|
pictureBox.ContextMenuStrip = cms;
|
|
pictureBox.BringToFront();
|
|
//pictureBox.Location = new Point(signLeft, signTop);
|
|
pictureBox.MouseDown += new MouseEventHandler(OnPictureBox_MouseDown);
|
|
pictureBox.MouseMove += new MouseEventHandler(OnPictureBox_MouseMove);
|
|
pictureBox.MouseUp += new MouseEventHandler(OnPictureBox_MouseUp);
|
|
pictureBox.MouseClick += new MouseEventHandler(OnPictureBox_MouseClick);
|
|
pictureBox.MouseLeave += new EventHandler(OnPictureBox_MouseLeave);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
_signBoxList.Remove(signBox);
|
|
pictureBox.Dispose();
|
|
}
|
|
}
|
|
private void OnPictureBox_MouseLeave(object sender, EventArgs e)
|
|
{
|
|
PictureBox pictureBox = (PictureBox)sender;
|
|
pictureBox.Cursor = Cursors.Default;
|
|
}
|
|
private void OnPictureBox_MouseClick(object sender, MouseEventArgs e)
|
|
{
|
|
PictureBox pictureBox = (PictureBox)sender;
|
|
SignBox signBox = (SignBox)pictureBox.Tag;
|
|
if (e.Button == MouseButtons.Left)
|
|
{
|
|
pictureBox.Focus();
|
|
this.propertyGrid.SelectedObject = signBox;
|
|
pictureBox.Cursor = Cursors.SizeAll;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 签名删除
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void OnItemClick(object sender, EventArgs e)
|
|
{
|
|
ToolStripMenuItem toolStripMenuItem = (ToolStripMenuItem)sender;
|
|
PictureBox pictureBox = (PictureBox)toolStripMenuItem.Tag;
|
|
SignBox signBox = (SignBox)pictureBox.Tag;
|
|
this._signBoxList.Remove(signBox);
|
|
pictureBox.Dispose();
|
|
}
|
|
private void OnPictureBox_MouseUp(object sender, MouseEventArgs e)
|
|
{
|
|
PictureBox pictureBox = (PictureBox)sender;
|
|
SignBox signBox = (SignBox)pictureBox.Tag;
|
|
signBox.IsMove = false;
|
|
}
|
|
private void OnPictureBox_MouseDown(object sender, MouseEventArgs e)
|
|
{
|
|
PictureBox pictureBox = (PictureBox)sender;
|
|
SignBox signBox = (SignBox)pictureBox.Tag;
|
|
signBox.IsMove = true;
|
|
signBox.MouseDownPoint = pictureBox.PointToClient(System.Windows.Forms.Control.MousePosition);
|
|
}
|
|
private void OnPictureBox_MouseMove(object sender, MouseEventArgs e)
|
|
{
|
|
PictureBox pictureBox = (PictureBox)sender;
|
|
SignBox signBox = (SignBox)pictureBox.Tag;
|
|
if (pictureBox.Focused)
|
|
{
|
|
pictureBox.Cursor = Cursors.SizeAll;
|
|
}
|
|
else
|
|
{
|
|
pictureBox.Cursor = Cursors.Default;
|
|
}
|
|
if (signBox.IsMove && e.Button == MouseButtons.Left && pictureBox.Focused)
|
|
{
|
|
signBox.SignLeft = pictureBox.Location.X + (e.X - signBox.MouseDownPoint.X);
|
|
signBox.SignTop = pictureBox.Location.Y + (e.Y - signBox.MouseDownPoint.Y);
|
|
//pictureBox.Location = new Point(pictureBox.Location.X + (e.X - signBox.MouseDownPoint.X), pictureBox.Location.Y + (e.Y - signBox.MouseDownPoint.Y));
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:添加图片到Pdf</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-05-30 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="imagePath">图片地址</param>
|
|
/// <param name="pageNo">页码</param>
|
|
/// <param name="point">坐标点</param>
|
|
/// <param name="stamper">stamper</param>
|
|
/// <param name="isCalc">是否计算,由于初始绘制点是计算好的不用计算.</param>
|
|
private void AddImageToPdf(Image pic, int pageNo, PointF point, PdfStamper stamper, bool isCalc = false)
|
|
{
|
|
//插入图片
|
|
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(pic, pic.RawFormat);
|
|
PdfContentByte pdfContentByte = stamper.GetOverContent(Convert.ToInt32(pageNo + 1));
|
|
int percent = GetPercentSize(image.Height, image.Width);
|
|
image.ScalePercent(percent);
|
|
if (isCalc)
|
|
{
|
|
//手动画点时,进行图片缩放
|
|
this.SignBoxPoint = point = new PointF(point.X, float.Parse(point.Y - image.Height * percent * 0.01 / 2 + ""));
|
|
}
|
|
image.SetAbsolutePosition(point.X, point.Y);
|
|
pdfContentByte.AddImage(image);
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:关闭PDf进程</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-06-08 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
public void Close()
|
|
{
|
|
try
|
|
{
|
|
this.pdfDocument.Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
string Message = ErrorMessage.PromptErrorMessage(ex);
|
|
MessageUtil.Show(Message, ex.Message);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:画签名</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-05-30 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
private void DrawSignBox(Point point)
|
|
{
|
|
pictureBox.Visible = true;
|
|
pictureBox.Image = this.SignImage;
|
|
pictureBox.Width = this.SignImage.Width;
|
|
pictureBox.Height = this.SignImage.Height;
|
|
//设置pictureBox 透明
|
|
pictureBox.Parent = this.pdfPageView;
|
|
pictureBox.BackColor = Color.Transparent;
|
|
pictureBox.Location = new Point(point.X, point.Y - this.pictureBox.Height / 2);
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:删除</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-05-30 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
public void Delete()
|
|
{
|
|
_signBoxList = _signBoxList.Where(o => o.PageNumber != this.pdfPageView.PageNumber).ToList();
|
|
foreach (SignBox item in _signBoxList)
|
|
{
|
|
item.ParentPicBox.Dispose();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:找到记录的sign坐标并画</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-05-30 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
private void FindSignBox()
|
|
{
|
|
//翻页切换时候进行画点操作
|
|
//List<SignBox> signBoxs = _signBoxList.Where(o => o.PageNumber == this.pdfPageView.PageNumber).ToList();
|
|
foreach (SignBox item in this._signBoxList)
|
|
{
|
|
if (item.PageNumber != this.CurrentPageNo + 1)
|
|
{
|
|
item.ParentPicBox.Parent = null;
|
|
}
|
|
else
|
|
{
|
|
item.ParentPicBox.Parent = this.pdfPageView;
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:初始化pdf控件</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-05-29 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
public void InitPdfEditor()
|
|
{
|
|
try
|
|
{
|
|
//存储记录picBox在页面打的点
|
|
this._signBoxList = new List<SignBox>();
|
|
this.pdfPageView.MouseWheel += new MouseEventHandler(OnMouseWheelClick);
|
|
DirectoryInfo dir = new DirectoryInfo(PubUtil.MenuExcelPath);
|
|
//初始化时清除掉文件
|
|
foreach (FileInfo f in dir.GetFiles("*.pdf"))
|
|
{
|
|
f.Delete();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
string Message = ErrorMessage.PromptErrorMessage(ex);
|
|
MessageUtil.Show(Message, ex.Message);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:初始在源文件上描点</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-05-30 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
public void InitPdfPage(DataTable dt)
|
|
{
|
|
this.CurrentId = 0;
|
|
if (dt == null || dt.Rows.Count == 0)
|
|
{
|
|
this._pdfHistory = this._currentPdf = this.TempletFilePath;
|
|
}
|
|
else
|
|
{
|
|
this._pdfHistory = this._currentPdf = PubUtil.MenuExcelPath + Guid.NewGuid() + ".pdf";
|
|
using (Stream outputPdfStream = new FileStream(this._currentPdf, FileMode.Create, FileAccess.Write, FileShare.None))
|
|
{
|
|
PdfReader reader = new PdfReader(this.TempletFilePath);
|
|
PdfStamper stamper = new PdfStamper(reader, outputPdfStream);
|
|
foreach (DataRow row in dt.Rows)
|
|
{
|
|
PdfSignModel model = new PdfSignModel(row);
|
|
PointF point = new PointF(model.ImageX, model.ImageY);
|
|
Image pic = Image.FromStream(new MemoryStream(model.PicStream), true); ;
|
|
AddImageToPdf(pic, model.PageNo, point, stamper);
|
|
}
|
|
stamper.Close();
|
|
}
|
|
}
|
|
this.pdfDocument.Load(this._currentPdf);
|
|
this.CurrentPageNo = 0;
|
|
this.pdfPageView.PageNumber = this.CurrentPageNo;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:记录保存的签名</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-05-30 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
private void SaveSignBox(Point point)
|
|
{
|
|
//先移除本页原有的点
|
|
this._signBoxList.Remove(_signBoxList.FirstOrDefault(o => o.PageNumber == this.CurrentPageNo));
|
|
SignBox signBox = new SignBox();
|
|
signBox.PageNumber = this.CurrentPageNo;
|
|
signBox.PointObj = point;
|
|
this._signBoxList.Add(signBox);
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:保存</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-05-30 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
public void Save(string savePath)
|
|
{
|
|
using (FileStream fileStream = new FileStream(savePath, FileMode.Create, FileAccess.ReadWrite))
|
|
{
|
|
using (iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(this._currentPdf))
|
|
{
|
|
PdfStamper stamper = new PdfStamper(reader, fileStream);
|
|
int readPageNum = reader.NumberOfPages;
|
|
for (int j = 0; j < readPageNum; j++)
|
|
{
|
|
List<SignBox> signBoxes = this._signBoxList.Where(n => n.PageNumber == j + 1).ToList();
|
|
foreach (SignBox item in signBoxes)
|
|
{
|
|
PdfContentByte pdfContentByte = stamper.GetOverContent(j + 1);//获取内容
|
|
using (Stream inputImageStream = new FileStream(item.ParentPicBoxImagePath, FileMode.Open, FileAccess.Read, FileShare.Read))
|
|
{
|
|
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(inputImageStream);//获取图片
|
|
PDFPoint startPoint = this.pdfPageView.ConvertScreenPointToPDFPoint(new Point(item.SignLeft, item.SignTop));
|
|
PDFPoint endPoint = this.pdfPageView.ConvertScreenPointToPDFPoint(new Point(item.SignLeft + item.SignWidth, item.SignTop + item.SignHeight));
|
|
image.ScaleAbsolute((float)(endPoint.X - startPoint.X), (float)(startPoint.Y - endPoint.Y));//设置图片比例
|
|
image.SetAbsolutePosition((float)startPoint.X, (float)startPoint.Y - (float)(startPoint.Y - endPoint.Y));//设置图片的绝对位置
|
|
pdfContentByte.AddImage(image);
|
|
}
|
|
}
|
|
}
|
|
stamper.Close();
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:根据比例缩放图片</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-05-30 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="h">高度</param>
|
|
/// <param name="w">宽度</param>
|
|
/// <returns>System.Int32.</returns>
|
|
private int GetPercentSize(float height, float width)
|
|
{
|
|
float percent = height / width * 100;
|
|
return Convert.ToInt32(Math.Round(percent));
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:另存为</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-05-30 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
public bool SaveAs()
|
|
{
|
|
if (string.IsNullOrEmpty(this._currentPdf)) return false;
|
|
string defaultSavePath = string.Format("{0}{1}", PubUtil.FileDownLoadTempPath, "signTempFile.pdf");
|
|
string tempSavePath = string.Format("{0}{1}", PubUtil.PdfSignTempSavePath, "signTempFile.pdf");
|
|
this.Save(defaultSavePath);
|
|
this.pdfDocument.Close();
|
|
this.pdfDocument.Load(this._currentPdf);
|
|
//加载到保存前的页码位置
|
|
this.pdfPageView.PageNumber = this.CurrentPageNo;
|
|
string fileName = string.Empty;
|
|
bool saveState = false;
|
|
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
|
|
{
|
|
saveFileDialog.Title = "另存为";
|
|
saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
|
|
saveFileDialog.Filter = "pdf(*.pdf)|*.pdf";
|
|
saveFileDialog.AddExtension = true;
|
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
fileName = saveFileDialog.FileName;
|
|
File.Copy(defaultSavePath, fileName, true);
|
|
saveState = true;
|
|
}
|
|
else
|
|
{
|
|
//另存操作失误,取消另存时候,加载到保存前状态
|
|
this._currentPdf = this._pdfHistory;
|
|
this.pdfDocument.Load(this._currentPdf);
|
|
this.pdfPageView.PageNumber = this.CurrentPageNo;
|
|
}
|
|
return saveState;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:打开本地文件</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-05-28 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <returns>System.String.</returns>
|
|
private string OpenLocalFile()
|
|
{
|
|
string fileName = string.Empty;
|
|
OpenFileDialog openFileDialog = new OpenFileDialog();
|
|
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
|
|
openFileDialog.Filter = "(*.pdf)|*.pdf";
|
|
openFileDialog.RestoreDirectory = true;
|
|
openFileDialog.FilterIndex = 1;
|
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
|
{
|
|
TempletFilePath = fileName = openFileDialog.FileName;
|
|
InitPdfPage(null);
|
|
}
|
|
return fileName;
|
|
}
|
|
#region 事件部分
|
|
/// <summary>
|
|
/// <para>说明:清除签名点</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-05-30 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="sender">The source of the event.</param>
|
|
/// <param name="e">The <see cref="DevExpress.XtraBars.ItemClickEventArgs"/> instance containing the event data.</param>
|
|
void OnDeleteClick(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (OnDeleteCallBack != null)
|
|
{
|
|
OnDeleteCallBack(null, null);
|
|
}
|
|
else
|
|
{
|
|
this.Delete();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
string Message = ErrorMessage.PromptErrorMessage(ex);
|
|
MessageUtil.Show(Message, ex.Message);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:鼠标滑轮滚动事件</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-05-29 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="sender">The sender.</param>
|
|
/// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
|
|
void OnMouseWheelClick(object sender, MouseEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
//向前滑
|
|
if (e.Delta > 0)
|
|
{
|
|
OnPageUpClick(null, null);
|
|
}
|
|
else
|
|
{
|
|
OnPageNextClick(null, null);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
string Message = ErrorMessage.PromptErrorMessage(ex);
|
|
MessageUtil.Show(Message, ex.Message);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:打开文件</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-05-30 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="sender">The sender.</param>
|
|
/// <param name="e">The <see cref="DevExpress.XtraBars.ItemClickEventArgs"/> instance containing the event data.</param>
|
|
void OnOpenClick(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
OpenLocalFile();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
string Message = ErrorMessage.PromptErrorMessage(ex);
|
|
MessageUtil.Show(Message, ex.Message);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-05-30 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="sender">The source of the event.</param>
|
|
/// <param name="e">The <see cref="O2S.Components.PDFView4NET.PageClickEventArgs" /> instance containing the event data.</param>
|
|
void OnPageClick(object sender, O2S.Components.PDFView4NET.PageClickEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
//初始化签名键值
|
|
this.CurrentId = 0;
|
|
this.CurrentPageNo = this.pdfPageView.PageNumber;
|
|
PdfReader reader = new PdfReader(this._currentPdf);
|
|
//记录上次操作文件,操作失误能返回
|
|
this._pdfHistory = this._currentPdf;
|
|
//合成的文件地址
|
|
this._pdfNew = PubUtil.MenuExcelPath + Guid.NewGuid() + ".pdf";
|
|
using (Stream outPutPdfStream = new FileStream(_pdfNew, FileMode.Create, FileAccess.Write, FileShare.None))
|
|
{
|
|
|
|
PdfStamper stamper = new PdfStamper(reader, outPutPdfStream);
|
|
PointF point = new PointF(float.Parse(e.PDFPoint.X + ""), float.Parse(e.PDFPoint.Y + ""));
|
|
this.AddImageToPdf(this.SignImage, this.CurrentPageNo, point, stamper, true);
|
|
stamper.Close();
|
|
}
|
|
reader.Close();
|
|
DrawSignBox(e.ControlPoint);
|
|
//将打的点记录下来
|
|
SaveSignBox(e.ControlPoint);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
string Message = ErrorMessage.PromptErrorMessage(ex);
|
|
MessageUtil.Show(Message, ex.Message);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:上一页</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-05-30 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="sender">The source of the event.</param>
|
|
/// <param name="e">The <see cref="DevExpress.XtraBars.ItemClickEventArgs"/> instance containing the event data.</param>
|
|
void OnPageUpClick(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (this.pdfPageView.PageNumber > 0)
|
|
{
|
|
this.pdfPageView.PageNumber--;
|
|
this.CurrentPageNo = this.pdfPageView.PageNumber;
|
|
this.FindSignBox();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
string Message = ErrorMessage.PromptErrorMessage(ex);
|
|
MessageUtil.Show(Message, ex.Message);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:下一页</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-05-30 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="sender">The source of the event.</param>
|
|
/// <param name="e">The <see cref="DevExpress.XtraBars.ItemClickEventArgs"/> instance containing the event data.</param>
|
|
void OnPageNextClick(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (this.pdfPageView.PageNumber < this.pdfDocument.PageCount - 1)
|
|
{
|
|
this.pdfPageView.PageNumber++;
|
|
this.CurrentPageNo = this.pdfPageView.PageNumber;
|
|
this.FindSignBox();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
string Message = ErrorMessage.PromptErrorMessage(ex);
|
|
MessageUtil.Show(Message, ex.Message);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:保存事件</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-05-30 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="sender">The source of the event.</param>
|
|
/// <param name="e">The <see cref="DevExpress.XtraBars.ItemClickEventArgs"/> instance containing the event data.</param>
|
|
void OnSaveClick(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (OnSaveCallBack != null)
|
|
{
|
|
OnSaveCallBack(null, null);
|
|
}
|
|
else
|
|
{
|
|
if (string.IsNullOrEmpty(this._currentPdf)) return;
|
|
string defaultSavePath = string.Format("{0}{1}", PubUtil.FileDownLoadTempPath, "signTempFile.pdf");
|
|
string tempSavePath = string.Format("{0}{1}", PubUtil.PdfSignTempSavePath, "signTempFile.pdf");
|
|
this.Save(defaultSavePath);
|
|
this.pdfDocument.Close();
|
|
File.Copy(defaultSavePath, tempSavePath, true);
|
|
foreach (SignBox item in this._signBoxList)
|
|
{
|
|
item.ParentPicBox.Dispose();
|
|
}
|
|
this._signBoxList = new List<SignBox>();
|
|
this._currentPdf = tempSavePath;
|
|
this.pdfDocument.Load(this._currentPdf);
|
|
//加载到保存前的页码位置
|
|
this.pdfPageView.PageNumber = this.CurrentPageNo;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
string Message = ErrorMessage.PromptErrorMessage(ex);
|
|
MessageUtil.Show(Message, ex.Message);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:另存事件</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-05-30 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="sender">The source of the event.</param>
|
|
/// <param name="e">The <see cref="DevExpress.XtraBars.ItemClickEventArgs"/> instance containing the event data.</param>
|
|
void OnSaveAsClick(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (OnSaveAsCallBack != null)
|
|
{
|
|
OnSaveAsCallBack(null, null);
|
|
}
|
|
else
|
|
{
|
|
this.SaveAs();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
string Message = ErrorMessage.PromptErrorMessage(ex);
|
|
MessageUtil.Show(Message, ex.Message);
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
/// <summary>
|
|
/// 签名box对象
|
|
/// </summary>
|
|
public class SignBox
|
|
{
|
|
/// <summary>
|
|
/// 父图片控件
|
|
/// </summary>
|
|
[BrowsableAttribute(false)]
|
|
public PictureBox ParentPicBox { get; set; }
|
|
/// <summary>
|
|
/// 图片地址
|
|
/// </summary>
|
|
[BrowsableAttribute(false)]
|
|
public string ParentPicBoxImagePath { get; set; }
|
|
/// <summary>
|
|
/// 页码
|
|
/// </summary>
|
|
/// <value>The page number.</value>
|
|
[BrowsableAttribute(false)]
|
|
public int PageNumber { get; set; }
|
|
/// <summary>
|
|
/// 坐标
|
|
/// </summary>
|
|
/// <value>The point.</value>
|
|
[BrowsableAttribute(false)]
|
|
public Point PointObj { get; set; }
|
|
private int _signWidth;
|
|
/// <summary>
|
|
/// 宽度
|
|
/// </summary>
|
|
[Category("基本属性")]
|
|
[DisplayName("宽度")]
|
|
[Description("宽度")]
|
|
public int SignWidth
|
|
{
|
|
get
|
|
{
|
|
return _signWidth;
|
|
}
|
|
set
|
|
{
|
|
_signWidth = value;
|
|
ParentPicBox.Width = _signWidth;
|
|
}
|
|
}
|
|
private int _signHeight;
|
|
/// <summary>
|
|
/// 高度
|
|
/// </summary>
|
|
[Category("基本属性")]
|
|
[DisplayName("高度")]
|
|
[Description("高度")]
|
|
public int SignHeight
|
|
{
|
|
get
|
|
{
|
|
return _signHeight;
|
|
}
|
|
set
|
|
{
|
|
_signHeight = value;
|
|
ParentPicBox.Height = _signHeight;
|
|
|
|
}
|
|
}
|
|
private int _signLeft;
|
|
/// <summary>
|
|
/// 左边距
|
|
/// </summary>
|
|
[Category("基本属性")]
|
|
[DisplayName("左边距")]
|
|
[Description("左边距")]
|
|
public int SignLeft
|
|
{
|
|
get
|
|
{
|
|
return _signLeft;
|
|
}
|
|
set
|
|
{
|
|
_signLeft = value;
|
|
ParentPicBox.Left = _signLeft;
|
|
}
|
|
}
|
|
private int _signTop;
|
|
/// <summary>
|
|
/// 上边距
|
|
/// </summary>
|
|
[Category("基本属性")]
|
|
[DisplayName("上边距")]
|
|
[Description("上边距")]
|
|
public int SignTop
|
|
{
|
|
get
|
|
{
|
|
return _signTop;
|
|
}
|
|
set
|
|
{
|
|
_signTop = value;
|
|
ParentPicBox.Top = _signTop;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 是否移动状态
|
|
/// </summary>
|
|
[BrowsableAttribute(false)]
|
|
public bool IsMove { get; set; }
|
|
/// <summary>
|
|
/// 鼠标点击位置
|
|
/// </summary>
|
|
[BrowsableAttribute(false)]
|
|
public Point MouseDownPoint { get; set; }
|
|
}
|
|
}
|