Ejemplo n.º 1
0
        /// <summary>
        /// uses itextsharp 4.1.6 to convert any pdf to 1.4 compatible pdf, called instead of PdfReader.open
        /// </summary>
        static public PdfDocument Open(MemoryStream sourceStream, PdfDocumentOpenMode openmode)
        {
            PdfDocument outDoc   = null;
            bool        cantOpen = false;

            sourceStream.Position = 0;

            try
            {
                outDoc = PdfReader.Open(sourceStream, openmode);
                int count = outDoc.PageCount;
            }
            catch (Exception)
            {
                cantOpen = true;
            }

            if (cantOpen || outDoc == null)
            {
                try
                {
                    //workaround if pdfsharp doesn't support this pdf
                    sourceStream.Position = 0;
                    MemoryStream outputStream                 = new MemoryStream();
                    iTextSharp.text.pdf.PdfReader  reader     = new iTextSharp.text.pdf.PdfReader(sourceStream);
                    iTextSharp.text.pdf.PdfStamper pdfStamper = new iTextSharp.text.pdf.PdfStamper(reader, outputStream);
                    pdfStamper.FormFlattening = true;
                    pdfStamper.Writer.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_4);
                    pdfStamper.Writer.CloseStream = false;
                    pdfStamper.Close();

                    outDoc = PdfReader.Open(outputStream, openmode);
                    int count = outDoc.PageCount;
                }
                catch (Exception)
                {
                    return(null);
                }
            }

            return(outDoc);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Opens an existing PDF document.
        /// </summary>
        public static PdfDocument Open(string path, string password, PdfDocumentOpenMode openmode, PdfPasswordProvider provider)
        {
            PdfDocument document;
            Stream      stream = new FileStream(path, FileMode.Open, FileAccess.Read);

            try
            {
                document = PdfReader.Open(stream, password, openmode, provider);
                if (document != null)
                {
                    document.fullPath = Path.GetFullPath(path);
                }
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
            return(document);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Opens an existing PDF document.
 /// </summary>
 public static PdfDocument Open(Stream stream)
 {
     return(PdfReader.Open(stream, PdfDocumentOpenMode.Modify));
 }