Esempio n. 1
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;
                }
            }
        }
Esempio n. 2
0
        public static void CreateListWatermark(string watermarkText, List <TAILIEUDINHKEM> ListTaiLieu, string Signature, string personalSign)
        {
            Application app = new Application();

            try
            {
                foreach (var item in ListTaiLieu)
                {
                    var doc = app.Documents.Open(FolderPath + item.DUONGDAN_FILE);
                    try
                    {
                        if (doc.Bookmarks != null && !string.IsNullOrEmpty(personalSign))
                        {
                            if (doc.Bookmarks["chuky"] != null)
                            {
                                var shape = doc.Bookmarks["chuky"].Range.InlineShapes
                                            .AddPicture(personalSign, false, true);
                            }
                        }

                        //var shape = doc.Bookmarks["PicHere"].Range.InlineShapes.AddPicture(Signature, false, true);
                        //shape.Width = 345;
                        //shape.Height = 150;
                        //doc.Close();
                        object Missing     = System.Reflection.Missing.Value;
                        object oMissing    = System.Reflection.Missing.Value;
                        object saveChanges = null;
                        Microsoft.Office.Interop.Word.Shape nShape = null;
                        string wmText = watermarkText;
                        foreach (Microsoft.Office.Interop.Word.Section section in doc.Sections)
                        {
                            nShape =
                                section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Shapes.AddTextEffect(
                                    MsoPresetTextEffect.msoTextEffect1,
                                    wmText, "Calibri", (float)65, MsoTriState.msoTrue,
                                    MsoTriState.msoFalse, 0, 350, ref oMissing);
                            nShape.Fill.Visible =
                                MsoTriState.msoTrue;
                            nShape.Line.Visible =
                                MsoTriState.msoFalse;
                            nShape.Fill.Solid();
                            nShape.Fill.ForeColor.RGB = (Int32)WdColor.wdColorGray35;
                            nShape.Left     = (float)WdShapePosition.wdShapeLeft;
                            nShape.Top      = 250;
                            nShape.Rotation = -45;
                        }

                        object newFile = FolderPath + item.DUONGDAN_FILE;
                        object a       = 11;
                        doc.SaveAs(ref newFile, ref a, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing,
                                   ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing,
                                   ref Missing, ref Missing);
                        doc.Close(ref saveChanges, ref oMissing, ref oMissing);
                        app.Quit(ref saveChanges, ref Missing, ref Missing);
                        doc.Close();
                    }
                    catch (Exception ex)
                    {
                        doc.Close();
                    }

                    #region ap dung chu ky so
                    if (ENABLECHUKYSO.ToIntOrZero() == 1)
                    {
                        FileUltilities file       = new FileUltilities();
                        string         fileinput  = FolderPath + item.DUONGDAN_FILE;
                        string         fileoutput = Path.GetDirectoryName(FolderPath + item.DUONGDAN_FILE) + Path.GetFileNameWithoutExtension(FolderPath + item.DUONGDAN_FILE) + "_1.docx";
                        if (System.IO.File.Exists(fileoutput))
                        {
                            System.IO.File.Delete(fileoutput);
                        }
                        var signer = DocumentSigner.For(fileinput);
                        var cert   = CertUtil.GetFromFile(CHUKYSO, PASSCHUKYSO);
                        var serial = cert.SerialNumber;
                        signer.Sign(fileinput, fileoutput, cert);
                        file.MoveFile(fileoutput, fileinput);
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }