Beispiel #1
0
        public static string getMergePdfwithSig(string pathPdf)
        {
            string StartupPath = Path.GetDirectoryName(pathPdf);
            string FileName    = Path.GetFileNameWithoutExtension(pathPdf);
            string FileExt     = Path.GetExtension(pathPdf);

            //			string pathPdf = StartupPath + @"\dogovor_magomed.pdf";
            string filePdfNew = "";

            try
            {
                string fileName = StartupPath + @"\" + FileName + "_withSign" + FileExt;
                using (var fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
                {
                    filePdfNew = StartupPath + @"\" + FileName + "_withSign" + FileExt;
                }
            }
            catch (IOException ioex)
            {
                filePdfNew = StartupPath + @"\" + FileName + "_withSign" + DateTime.Now.Millisecond + FileExt;
            }



            string tmpPdfSig = StartupPath + @"\sigToPdf.pdf";

            clSignature sig     = new clSignature();
            string      htmlSig = sig.getHtmlSig(pathPdf);


            //			using (FileStream htmlSource = File.Open(StartupPath + @"\USTUPKA.html", FileMode.Open))
            using (FileStream htmlSource = File.Open(htmlSig, FileMode.Open))
                using (FileStream pdfDest = File.Open(tmpPdfSig, FileMode.OpenOrCreate))
                {
                    ConverterProperties converterProperties = new ConverterProperties();
                    converterProperties.SetCharset("windows-1251");
                    HtmlConverter.ConvertToPdf(htmlSource, pdfDest, converterProperties);
                }

            PdfDocument pdfDocument  = new PdfDocument(new PdfReader(pathPdf), new PdfWriter(filePdfNew));
            PdfDocument pdfDocument2 = new PdfDocument(new PdfReader(tmpPdfSig));

            PdfMerger merger = new PdfMerger(pdfDocument);

            merger.Merge(pdfDocument2, 1, pdfDocument2.GetNumberOfPages());

            pdfDocument2.Close();
            pdfDocument.Close();


            return(filePdfNew);
        }
Beispiel #2
0
        static int Main(string[] args)
        {
//			args[0] = @"d:\temp_egron\20191225\доверенность копия.pdf";
            if (args.Length == 0)
            {
                System.Console.WriteLine("Не указаны параметры вызова");
                Console.ReadKey();
                return(1);
            }
            else
            {
                int result = 1;


                //если нет вторго параметра, то объединяем только указанный файл
                if (!string.IsNullOrEmpty(args[0]) && !string.IsNullOrEmpty(args[1]) && args[1] == "file")
                {
                    clSignature sig = new clSignature();
                    //			string pathPdf = clMerge.getMergePdfwithSig(@"d:\work_temp\паке_ЕГРОН\report-0fda88b2-d87b-44b1-9967-335035bb6625-BC-2020-05-28-141452-05-03[2].pdf");
                    string pathPdf = clMerge.getMergePdfwithSig(args[0]);

                    Process.Start(pathPdf);
                    sig.clearFile(args[0]);
                    //			Environment.Exit(0);
                    result = 0;
                }


                //второй параметр "path"/ указывает на то что работаем с дирректорией, и объединять необходимо все найденные pdf
                if (!string.IsNullOrEmpty(args[0]) && !string.IsNullOrEmpty(args[1]) && args[1] == "path")
                {
                    var fileList = Directory.GetFiles(args[0]);
                    foreach (var filePath in fileList)
                    {
                        FileInfo fileInf = new FileInfo(filePath);
//						Console.WriteLine("Имя файла: {0}", fileInf.Name);
//						Console.WriteLine("Время создания: {0}", fileInf.CreationTime);
//						Console.WriteLine("Размер: {0}", fileInf.Length);
//						Console.WriteLine("sign: {0}", fileInf.Name.Contains("_withSign"));

                        if (fileInf.Exists)
                        {
                            if (fileInf.Extension.ToUpper() == ".PDF" && !fileInf.Name.Contains("_withSign") && !fileInf.Name.Contains("sigToPdf.pdf"))
                            {
                                clSignature sig     = new clSignature();
                                string      pathPdf = clMerge.getMergePdfwithSig(filePath);

                                Process.Start(pathPdf);
                                sig.clearFile(args[0]);
                            }
                        }
                    }

                    result = 0;
                }


                //если нет вторго параметра, то объединяем только указанный файл
                if (!string.IsNullOrEmpty(args[0]) && !string.IsNullOrEmpty(args[1]) && args[1] == "htmlToPdf")
                {
//					clSignature sig = new clSignature();

                    string pathPdf = clMerge.getConvertHtmlToPdf(args[0]);

                    Process.Start(pathPdf);

                    result = 0;
                }
                return(result);
            }
        }