ab56a9bcf7
SVN-Revision: r240
139 lines
5.2 KiB
C#
139 lines
5.2 KiB
C#
/***********************
|
|
* 说明:PDF签名合成
|
|
* 创建人:龚宇超
|
|
* 创建日期:2018-06-07
|
|
* 修改人:
|
|
* 修改日期:
|
|
* 修改备注:
|
|
* 版本:1.0.0.0
|
|
***********************/
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Data;
|
|
using Lskj.Business.Impl;
|
|
using Lskj.Util;
|
|
using System.IO;
|
|
using iTextSharp.text.pdf;
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
using System.Drawing;
|
|
using System.Diagnostics;
|
|
using System.Threading;
|
|
|
|
namespace Lskj.Control.Model
|
|
{
|
|
public class PdfViewerHelper
|
|
{
|
|
|
|
/// <summary>
|
|
/// <para>说明:</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-06-11 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="pdfFileId">PDF主键.</param>
|
|
/// <param name="pdfFileName">PDF文件名.</param>
|
|
/// <param name="pdfFilePath">PDF路径</param>
|
|
public static void CreateNewPdf(int pdfFileId,string pdfFileName, string pdfFilePath)
|
|
{
|
|
DataTable dt = AttachImpl.GetSignSources(pdfFileId);
|
|
if (dt.Rows.Count > 0)
|
|
{
|
|
//复制一个作为读取模板,源文件作为操作流
|
|
string newFilePath =PubUtil.MenuExcelPath + Guid.NewGuid() + ".pdf";
|
|
File.Copy(pdfFilePath, newFilePath);
|
|
|
|
using (Stream outputPdfStream = new FileStream(pdfFilePath, FileMode.Open, FileAccess.Write, FileShare.None))
|
|
{
|
|
using (PdfReader reader = new PdfReader(newFilePath))
|
|
{
|
|
PdfStamper stamper = new PdfStamper(reader, outputPdfStream);
|
|
foreach (DataRow rowItem in dt.Rows)
|
|
{
|
|
//iTextSharp.text.Image 获取图片的方法直接传流会出错,先转图片
|
|
MemoryStream ms = new MemoryStream((byte[])rowItem["Pic"], 0, ((byte[])rowItem["Pic"]).Length);
|
|
BinaryFormatter bf = new BinaryFormatter();
|
|
object obj = bf.Deserialize(ms);
|
|
ms.Close();
|
|
Image pic = obj as Image;
|
|
|
|
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(pic, pic.RawFormat);
|
|
PointF point = new PointF(float.Parse(rowItem["ImageX"] + ""), float.Parse(rowItem["ImageY"] + ""));
|
|
PdfContentByte pdfContentByte = stamper.GetOverContent(Convert.ToInt32(rowItem["Page"]) + 1);
|
|
float percent = image.Height / image.Width * 100;
|
|
image.ScalePercent(Convert.ToInt32(Math.Round(percent)));
|
|
image.SetAbsolutePosition(point.X, point.Y);
|
|
pdfContentByte.AddImage(image);
|
|
}
|
|
stamper.Close();
|
|
}
|
|
}
|
|
File.Delete(newFilePath);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:验证下载完成(应对不能获取下载完成消息)</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-06-11 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
public static void VerifyDownLoadFinish(string filePath,string fileName)
|
|
{
|
|
int i = 0;
|
|
//进行20次查找
|
|
while (i < 20)
|
|
{
|
|
try
|
|
{
|
|
if (File.Exists(filePath) && !SearchProc("LSTest"))
|
|
{
|
|
//是PDF文件就进行签名合成
|
|
if (fileName.Contains(".pdf"))
|
|
{
|
|
CreateNewPdf(0,fileName, filePath);
|
|
}
|
|
return;
|
|
}
|
|
Thread.Sleep(500);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw;
|
|
}
|
|
i++;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:查找进程里面是否还在下载</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-06-11 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="strProcName">Name of the string proc.</param>
|
|
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
|
|
public static bool SearchProc(string strProcName)
|
|
{
|
|
try
|
|
{
|
|
//精确进程名 用GetProcessesByName
|
|
Process[] ps = Process.GetProcessesByName(strProcName);
|
|
return ps.Length > 0;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|