예제 #1
0
        public void SignPdf()
        {
            Cert myCert = null;

            try
            {
                if (_certificateData != null)
                {
                    byte[] bytes = _certificateData.Export(X509ContentType.Pfx, String.Empty);
                    myCert = new Cert(bytes);
                }
                else
                {
                    myCert = new Cert(cbxCertificates.Text, passwordBox.Text, String.Empty, String.Empty, String.Empty);
                }
            }
            catch
            {
                return;
            }

            var pdfSigFileName = String.Format("{0}-Sig.pdf", DateTime.Now.ToString("s").Replace(":", String.Empty));

            PDFSigner      pdfs  = new PDFSigner(outputPdfFile, Path.Combine(dirName, pdfSigFileName), myCert);
            PDFSignatureAP sigAp = new PDFSignatureAP();

            sigAp.SigReason   = Reasontext.Text;
            sigAp.SigContact  = Contacttext.Text;
            sigAp.SigLocation = Locationtext.Text;
            sigAp.Visible     = chkSigVisible.Checked;
            sigAp.Multi       = multiSigChkBx.Checked;
            sigAp.Page        = sigLocationUCtrl1.numberOfPages;
            sigAp.CustomText  = custSigText.Text;

            if (sigImgBox.Image != null)
            {
                MemoryStream ms = new MemoryStream();
                sigImgBox.Image.Save(ms, ImageFormat.Bmp);
                sigAp.RawData = ms.ToArray();
                ms.Close();
            }

            sigAp.SigX = (float)sigLocationUCtrl1.SigPosX;
            sigAp.SigY = (float)sigLocationUCtrl1.SigPosY;
            sigAp.SigW = (float)sigLocationUCtrl1.SigWidth;
            sigAp.SigH = (float)sigLocationUCtrl1.SigHeight;

            pdfs.Sign(sigAp, false, PDFEnc);
        }
예제 #2
0
 private bool AddFileToDataBase(FileBase file)
 {
     try
     {
         Directory.CreateDirectory(SystemSingleton.Configuration.FilesPath + file.FileID);
         if (!PDFSigner.PDFSign(file.Path,
                                SystemSingleton.Configuration.FilesPath + file.FileID + "\\" + file.Name,
                                this))
         {
             return(false);
         }
         using (var con = new SqlConnection(SystemSingleton.Configuration.ConnectionString))
         {
             SystemSingleton.Configuration.SqlConnections.Add(con);
             using (var command = new SqlCommand(SqlCommands.AddFileToDataBaseCommand, con))
             {
                 command.Parameters.Add("@TaskID", SqlDbType.UniqueIdentifier);
                 command.Parameters["@TaskID"].Value = Card.Task.ID.Value;
                 command.Parameters.Add("@FileID", SqlDbType.UniqueIdentifier);
                 command.Parameters["@FileID"].Value = file.FileID;
                 command.Parameters.Add("@FileName", SqlDbType.NVarChar);
                 command.Parameters["@FileName"].Value = file.Name;
                 EnvironmentHelper.SendLogSQL(command.CommandText);
                 con.Open();
                 int colms = command.ExecuteNonQuery();
                 con.Close();
                 if (colms != 0)
                 {
                     return(true);
                 }
                 else
                 {
                     EnvironmentHelper.SendDialogBox(
                         (string)SystemSingleton.Configuration.mainWindow.FindResource("m_CantSaveFile") + "\n\n" + file.Name,
                         "File Error"
                         );
                     return(false);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         EnvironmentHelper.SendErrorDialogBox(ex.Message, "SQL Error", ex.StackTrace);
         return(false);
     }
 }
예제 #3
0
    /// <summary>
    /// 对PDF采购合同进行电子签名
    /// </summary>
    /// <param name="inputName">源文件名称</param>
    /// <param name="outputName">签名后输出的文件名称</param>
    public void PDFSign(string inputName, string outputName)
    {
        string inputPath  = PDFPath + inputName + ".pdf";
        string outputPath = PDFPath + outputName + ".pdf";

        Cert myCert = new Cert(PDF_CertPath, PDF_CertPassword);

        MetaData MyMD = new MetaData();

        MyMD.Author   = tools.NullStr(Application["Site_Name"]);
        MyMD.Title    = tools.NullStr(Application["Site_Name"]) + "电子交易合同";
        MyMD.Subject  = "";
        MyMD.Keywords = "";
        MyMD.Creator  = tools.NullStr(Application["Site_Name"]);
        MyMD.Producer = tools.NullStr(Application["Site_Name"]);

        PDFSigner pdfs = new PDFSigner(inputPath, outputPath, myCert, MyMD);

        pdfs.Sign("签订交易合同", tools.NullStr(Application["Site_Tel"]), tools.NullStr(Application["Site_Name"]), true);
    }
예제 #4
0
        public HttpResponseMessage Post([FromBody] SignatureTable signature)
        {
            try
            {
                Cert myCert = null;
                try
                {
                    myCert = new Cert(pathToFiles + "sertifikat/" + signature.certName, signature.password);
                }
                catch (Exception ex)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
                }

                //Adding Meta Datas
                MetaData MyMD = new MetaData();
                MyMD.Author   = signature.author;
                MyMD.Title    = signature.title;
                MyMD.Subject  = signature.subject;
                MyMD.Keywords = signature.keyword;

                PDFSigner pdfs = new PDFSigner(pathToFiles + "input/" + signature.pdfName, pathToFiles + "output/sign_" + signature.pdfName, myCert, MyMD);
                pdfs.Sign(signature.reason, signature.email, signature.location, true);
                using (SignatureDBEntities entities = new SignatureDBEntities())
                {
                    entities.SignatureTables.Add(signature);
                    entities.SaveChanges();

                    var message = Request.CreateResponse(HttpStatusCode.Created, signature);
                    message.Headers.Location = new Uri(Request.RequestUri + "/" + signature.id.ToString());
                    return(message);
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
예제 #5
0
        public static void CreateWaterMarkPdf(List <TAILIEUDINHKEM> ListTaiLieu, string waterMark)
        {
            FileUltilities file   = new FileUltilities();
            BaseFont       bfFont = BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);

            iTextSharp.text.pdf.PdfGState gstate = null;
            gstate               = new iTextSharp.text.pdf.PdfGState();
            gstate.FillOpacity   = 0.3f;
            gstate.StrokeOpacity = 0.3f;
            foreach (var item in ListTaiLieu)
            {
                try
                {
                    #region gan water mark
                    string     DestinationFile = Path.GetDirectoryName(FolderPath + item.DUONGDAN_FILE) + Path.GetFileNameWithoutExtension(FolderPath + item.DUONGDAN_FILE) + "_1.pdf";
                    PdfReader  reader          = new PdfReader(FolderPath + item.DUONGDAN_FILE);
                    PdfStamper stamper         = new PdfStamper(reader,
                                                                new FileStream(DestinationFile, FileMode.Create, FileAccess.Write));
                    for (int iCount = 0; iCount < reader.NumberOfPages; iCount++)
                    {
                        iTextSharp.text.Rectangle PageSize = reader.GetCropBox(iCount + 1);
                        PdfContentByte            PDFData  = stamper.GetOverContent(iCount + 1);
                        BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
                        int      altitude = Math.Max(bfFont.GetAscent(waterMark), bfFont.GetDescent(waterMark));
                        int      width    = bfFont.GetWidth(waterMark);
                        PDFData.SaveState();
                        PDFData.SetGState(gstate);
                        PDFData.BeginText();
                        PDFData.SetColorFill(CMYKColor.LIGHT_GRAY);
                        PDFData.SetFontAndSize(bfFont, (float)80);
                        PDFData.ShowTextAligned(PdfContentByte.ALIGN_CENTER, waterMark, (PageSize.Right + PageSize.Left) / 2, (PageSize.Top + PageSize.Bottom) / 2, 45);
                        PDFData.EndText();
                    }
                    stamper.Close();
                    file.MoveFile(DestinationFile, FolderPath + item.DUONGDAN_FILE);
                    #endregion

                    #region ap dung chu ky so
                    if (ENABLECHUKYSO.ToIntOrZero() == 1)
                    {
                        string TmpDestinationFile = Path.GetDirectoryName(FolderPath + item.DUONGDAN_FILE) + "\\" + Path.GetFileNameWithoutExtension(FolderPath + item.DUONGDAN_FILE) + "_111.pdf";
                        Cert   myCert             = null;
                        myCert = new Cert(CHUKYSO, PASSCHUKYSO);
                        MetaData metaDT = new MetaData();
                        metaDT.Author   = "Duynt";
                        metaDT.Title    = "Demo title";
                        metaDT.Subject  = "Demo subject";
                        metaDT.Creator  = "Duynt create";
                        metaDT.Producer = "HiNet product";

                        PDFSigner pdf = new PDFSigner(FolderPath + item.DUONGDAN_FILE, TmpDestinationFile, myCert, metaDT);
                        pdf.Sign("Reason text", "Contact text", "Location text", true);
                        file.MoveFile(TmpDestinationFile, FolderPath + item.DUONGDAN_FILE);
                    }

                    #endregion
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
예제 #6
0
        public HttpResponseMessage Put(int id, [FromBody] m_signature signature)
        {
            try
            {
                /*section processing certificate*/
                Cert myCert = null;
                try
                {
                    myCert = new Cert(
                        pathToFiles + "sertifikat/" + signature.certName,
                        signature.password
                        );
                }
                catch (Exception ex)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
                }

                /*section processing Signature*/
                MetaData MyMD = new MetaData();
                MyMD.Author   = signature.author;
                MyMD.Title    = signature.title;
                MyMD.Subject  = signature.subject;
                MyMD.Keywords = signature.keyword;

                PDFSigner pdfs = new PDFSigner(
                    pathToFiles + "input/" + signature.pdfName,
                    pathToFiles + "output/sign_" + signature.pdfName,
                    myCert,
                    MyMD
                    );
                pdfs.Sign(signature.reason, signature.email, signature.location,
                          pathToFiles + "pic/" + signature.picName, signature.visible,
                          signature.posX, signature.posY
                          );

                /*section save data to DB*/
                using (SignatureDBEntities entities = new SignatureDBEntities())
                {
                    var entity = entities.SignatureTables
                                 .FirstOrDefault(e => e.id == id);
                    if (entity == null)
                    {
                        System.Diagnostics.Debug.WriteLine("Id not found");
                        return(Request.CreateErrorResponse(
                                   HttpStatusCode.BadRequest, "Id not found"));
                    }
                    else
                    {
                        entity.author      = signature.author;
                        entity.title       = signature.title;
                        entity.subject     = signature.subject;
                        entity.keyword     = signature.keyword;
                        entity.reason      = signature.reason;
                        entity.email       = signature.email;
                        entity.location    = signature.location;
                        entity.certName    = signature.certName;
                        entity.issuerId    = signature.issuerId;
                        entity.requestorId = signature.requestorId;
                        entity.status      = "sign";
                        entity.approval    = 1;
                        entities.SaveChanges();
                        System.Diagnostics.Debug.WriteLine("Sukses Sign File");
                    }

                    var message = Request.CreateResponse(
                        HttpStatusCode.Created, signature);
                    message.Headers.Location = new Uri(
                        Request.RequestUri + "/" + signature.id.ToString()
                        );
                    return(message);
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(
                           HttpStatusCode.BadRequest, ex));
            }
        }