sql二进制图片存取,word签字,转pdf

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;
using System.IO;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;
using Microsoft.Office.Interop.Word;
using iTextSharp.text;
using iTextSharp.text.pdf; 

namespace PartyOrganization
{
    public partial class Sign2 : System.Web.UI.Page
    {
        MsgBox msg = new MsgBox();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["i"] = 0;
            }          
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            BLL.ManageSys.FileOperate file = new BLL.ManageSys.FileOperate();
            if (File1.HasFile)
            {
                //获取文件全路径
                string fullFileName = this.File1.PostedFile.FileName;
                //获取文件名
                string fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);
                this.File1.PostedFile.SaveAs(Server.MapPath("UpFileIMage") + "\\" + fileName);
                string FileName = Server.MapPath("UpFileIMage").ToString().Trim() + "\\" + fileName;
                msg.AjaxResponeSrcipt(this.UpdatePanel1, this.GetType(), "上传成功!");
                FileInfo TpFile = new FileInfo(FileName);
                byte[] ImageBuffer = new byte[TpFile.Length];
                FileStream stream = TpFile.OpenRead();//创建只读的FileStream对象
                stream.Read(ImageBuffer, 0, ImageBuffer.Length);
                stream.Close();
                string strCon = "server=(local);database=PartyOrganization;integrated security=sspi";
                SqlConnection conn = new SqlConnection(strCon);
                conn.Open();
                string strCom = "insert into book(bookName,bookImg,imageUrl) values(@bookName,@bookImage,@imageUrl)";
                SqlCommand com = new SqlCommand(strCom, conn);
                com.Parameters.Add("@bookName", SqlDbType.VarChar, 50).Value = "测试";
                com.Parameters.Add("@bookImage", SqlDbType.Image).Value = ImageBuffer;
                com.Parameters.Add("@imageUrl", SqlDbType.VarChar, 100).Value = FileName;
                com.ExecuteNonQuery();
            }
            else
            {
                msg.AjaxResponeSrcipt(this.UpdatePanel1, this.GetType(), "上传失败!");
            }

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            byte[] imagebytes = null;
            //打开数据库
            SqlConnection con = new SqlConnection("server=(local);database=PartyOrganization;integrated security=sspi");
            con.Open();
            SqlCommand com = new SqlCommand("select * from book", con);
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            da.Fill(ds);
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                imagebytes = (byte[])row["bookImg"];
            }
            com.Clone();
            con.Close();
            //以上 换成三层取得图片即可
            MemoryStream ms = new MemoryStream(imagebytes);
            Bitmap bmpt = new Bitmap(ms);        
            bmpt.Save("c:\\111.jpg");
            bmpt.Dispose();            

            //word.Selection.Text = ViewState["i"].ToString();
            //int s = int.Parse(ViewState["i"].ToString()); s++;
            //ViewState["i"] = s;

            //WordSign("c:\\111.jpg", @"c:\b.doc", s);
            WordConvertToPdf("c:\\b2.doc", "c:\\new.pdf");
            //AddWaterMark();
            //System.IO.File.Delete("c:\\111.jpg");  //删掉临时签名图片
        }
        /// <summary>
        /// word 签字
        /// </summary>
        /// <param name="SignImagePath">签字图片路径</param>
        /// <param name="WordPath">word文档路径</param>
        protected void WordSign(string SignImagePath,string WordPath,int s)
        {
            object Nothing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Word.ApplicationClass word = new Word.ApplicationClass();
            word.Visible = true;
            Word.Document doc = null;
            try
            {
                doc = word.Documents.Open(WordPath);  //你的附件word的位置

                //插入图片
                string FileName = SignImagePath;//自己在服务器设置一个零时文件夹,取出签字图片放在此
                //移动光标到文档末尾
                object count = doc.Paragraphs.Count;
                object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdParagraph;
                word.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点          
                if (s == 1)
                {
                    word.Selection.TypeParagraph();//插入段落 自己控制 如果是第一个签字的人就让插入段落。
                }
                word.Selection.InsertAfter("\x20\x20");//图片间隔两个空格
                object LinkToFile = false;
                object SaveWithDocument = true;
                object Anchor = word.Application.Selection.Range;
                InlineShape InlineShapesNew = word.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
                //签名图片大小设置
                InlineShapesNew.Width = 30f;
                InlineShapesNew.Height = 40f;

                doc.SaveAs(WordPath, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                doc.Close(ref Nothing, ref Nothing, ref Nothing);
                word.Application.Quit(ref Nothing, ref Nothing, ref Nothing);     
            }
            catch
            {
                //打开失败              
            }
              
        }
      
        /// <summary>
        /// word转pdf 
        /// </summary>
        /// <param name="WordPath">word文档路径</param>
        /// <param name="PdfPath">生成的pdf保存路径</param>
        protected void WordConvertToPdf(string WordPath,string PdfPath)
        {
            Microsoft.Office.Interop.Word.ApplicationClass oWord = new Word.ApplicationClass();        
            Type wordType = oWord.GetType();

            Microsoft.Office.Interop.Word.Documents docs = oWord.Documents;
            Type docsType = docs.GetType();
            object objDocName = WordPath;
            Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { objDocName, true, true });

            //打印输出到指定文件
            Type docType = doc.GetType();
            object printFileName = @"c:\tmp\test.ps";//改成自己项目的目录 防止覆盖
            docType.InvokeMember("PrintOut", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { false, false, Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintAllDocument, printFileName });
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, oWord, null);

            string o1 = "c:\\tmp\\test.ps";
            string o2 = PdfPath;
            string o3 = "";

            //引用将PS转换成PDF的对象
            try
            {
                ACRODISTXLib.PdfDistillerClass pdf = new ACRODISTXLib.PdfDistillerClass();
                pdf.FileToPDF(o1, o2, o3);
            }
            catch { }
            System.IO.File.Delete("c:\\tmp\\test.ps");  //删掉临时文件

            //为防止本方法调用多次时发生错误,必须停止acrodist.exe进程
            foreach (System.Diagnostics.Process proc in System.Diagnostics.Process.GetProcesses())
            {
                int begpos;
                int endpos;

                string sProcName = proc.ToString();
                begpos = sProcName.IndexOf("(") + 1;
                endpos = sProcName.IndexOf(")");

                sProcName = sProcName.Substring(begpos, endpos - begpos);

                if (sProcName.ToLower().CompareTo("acrodist") == 0)
                {
                    try
                    {
                        proc.Kill();
                    }
                    catch { }
                    break;
                }
            }
        }
        /// <summary>
        /// 添加水印 处理过的水印图片浮在内容上方
        /// </summary>
        protected void AddWaterMark()
        {
            PdfReader reader = new PdfReader("C:/tmp/5.pdf");
            FileStream stream =new FileStream("C:/tmp/New.pdf", FileMode.Create,FileAccess.ReadWrite);
            PdfStamper stamp = new PdfStamper(reader, stream);
            #region           
            int n = reader.NumberOfPages;
            int i = 0;
            PdfContentByte under;
            iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance("C:/tmp/123.png");
            image.SetAbsolutePosition(200, 200); //SetAbsolutePosition()方法接受两个float类型的参数,第一个参数是X轴坐标,是从文档最左边开始算起,第二个参数是Y轴坐标,是从文档下方开始算起。A4纸的的默认参数是595像素宽,842像素高,四周的页边距都为36像素.
            image.ScaleAbsolute(80, 80);//水印图片大小设置         
            image.Rotation=1;//旋转
            image.RotationDegrees = 45;//旋转角度
            //水印的位置 
            while (i < n)
            {
                i++;
                under = stamp.GetOverContent(i);//位置 每页
                under.AddImage(image);
            }
            #endregion          
            stamp.Close();
            reader.Close();   
            stream.Close();
        }




    }
}
原文地址:https://www.cnblogs.com/ymecho/p/3034007.html