예제 #1
0
        public void MergePdfs(string[] inputs)
        {
            var outPdfDocument = new PdfDocument();

            foreach (var pdf in inputs)
            {
                var inputDocument = PdfReader.Open(pdf, PdfDocumentOpenMode.Import);
                outPdfDocument.Version = inputDocument.Version;

                foreach (var page in inputDocument.Pages)
                {
                    outPdfDocument.AddPage(page);
                }
            }

            var name = "WTF.pdf";

            outPdfDocument.Info.Creator = string.Empty;
            outPdfDocument.Save($"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}/lens.pdf");

            Process.Start(name);
        }
예제 #2
0
        /// <summary>
        /// permet de séparer les pages du document source
        /// vers le répertoire temporaire
        /// <param name="file">fichier source</param>
        /// <param name="copy">true:copie false:déplace</param>
        /// </summary>
        public static void SplitPdf(string file, bool copy)
        {
            try
            {
                var srcDoc = file;

                //ouverture du fichier
                PdfSharpDocument inputDocument = PdfSharpReader.Open(srcDoc, PdfDocumentOpenMode.Import);

                string name = Path.GetFileNameWithoutExtension(file);
                if (inputDocument.PageCount > 1)
                {
                    for (int idx = 0; idx < inputDocument.PageCount; idx++)
                    {
                        //nouveau document
                        PdfSharpDocument outputDocument = new PdfSharpDocument();
                        outputDocument.Version      = inputDocument.Version;
                        outputDocument.Info.Title   = $"Page {idx + 1} of {inputDocument.Info.Title}";
                        outputDocument.Info.Creator = inputDocument.Info.Creator;

                        //ajout de la page et sauvegarde
                        outputDocument.AddPage(inputDocument.Pages[idx]);
                        var temp = Path.Combine(ServiceCfg.TempFolder, Path.GetFileNameWithoutExtension(file));
                        CheckFolder(temp, true);
                        outputDocument.Save(Path.Combine(temp, $"{name}__p{idx + 1}.pdf"));
                        outputDocument.Close();
                    }
                }
                else
                {
                    //nouveau document
                    PdfSharpDocument outputDocument = new PdfSharpDocument();
                    outputDocument.Version      = inputDocument.Version;
                    outputDocument.Info.Title   = inputDocument.Info.Title;
                    outputDocument.Info.Creator = inputDocument.Info.Creator;

                    //ajout de la page et sauvegarde
                    outputDocument.AddPage(inputDocument.Pages[0]);
                    var temp = Path.Combine(ServiceCfg.TempFolder, Path.GetFileNameWithoutExtension(file));
                    CheckFolder(temp, true);
                    outputDocument.Save(Path.Combine(temp, $"{name}.pdf"));
                    outputDocument.Close();
                }

                if (CheckFolder(ServiceCfg.OutputFolderPath, false))
                {
                    var pathbckup = Path.Combine(ServiceCfg.OutputFolderPath, "original\\");
                    var filebck   = Path.Combine(pathbckup, Path.GetFileName(file));
                    if (CheckFolder(pathbckup, true))
                    {
                        if (File.Exists(filebck))
                        {
                            filebck = Path.Combine(pathbckup, $"{Path.GetFileNameWithoutExtension(file)}_[ALT-{DateTime.Now.ToString("yyyyMMddHHmmss")}]{Path.GetExtension(file)}");
                        }
                        if (copy)
                        {
                            File.Copy(srcDoc, filebck);
                        }
                        else
                        {
                            File.Move(srcDoc, filebck);
                        }
                        //on garde le chemin du fichier d'origine
                        DataManager.SetDicoValue(LogTableParam.Source, filebck);
                    }
                }
            }
            catch (Exception e)
            {
                ServiceCfg.Log.Error($"PdfManager.SplitPdf : {file}{Environment.NewLine}", e);
                throw new Exception($"PdfManager.SplitPdf : {file}{Environment.NewLine}", e);
            }
        }
예제 #3
0
 /// <summary>
 /// Opens an existing PDF document.
 /// </summary>
 public static PdfDocument Open(Stream stream)
 {
     return(PdfReader.Open(stream, PdfDocumentOpenMode.Modify));
 }