Esempio n. 1
0
        //配列srcで指定されたPDFファイルを結合し、destで指定されたパスに保存する
        public void Run(string[] src, string dest)
        {
            // srcで渡されたファイルが存在するか?
            foreach(var item in src){
                if(!File.Exists(item)){
                    throw new FileNotFoundException();
                }
            }

            var reader = new List<iTextSharp.text.pdf.PdfReader>();
            foreach (string path in src)
            {
                reader.Add(new iTextSharp.text.pdf.PdfReader(path));
            }

            // 一時ファイルを作成してdestにコピー
            string tmp = Path.GetTempPath() + Path.GetFileName(dest);
            using (FileStream fs = new FileStream(tmp, FileMode.Create))
            {
                iTextSharp.text.pdf.PdfCopyFields copy = new iTextSharp.text.pdf.PdfCopyFields(fs);
                foreach (var source in reader)
                {
                    copy.AddDocument(source);
                }
                copy.Close();
            }
            FileSystem.CopyFile(tmp, dest, UIOption.OnlyErrorDialogs);
            File.Delete(tmp);

            foreach (var source in reader)
            {
                source.Close();
            }
        }
Esempio n. 2
0
 private Stream MergedPDF(List <Stream> ListPdf)
 {
     try
     {
         using (MemoryStream newStream = new MemoryStream())
         {
             iTextSharp.text.pdf.PdfCopyFields pdf = new iTextSharp.text.pdf.PdfCopyFields(newStream);
             try
             {
                 foreach (Stream filePdf in ListPdf)
                 {
                     pdf.AddDocument(new iTextSharp.text.pdf.PdfReader(filePdf));
                 }
                 pdf.Close();
                 return(new MemoryStream(newStream.ToArray()));
             }
             catch (Exception ex)
             {
                 throw ex;
             }
             finally
             {
                 pdf.Close();
             }
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Esempio n. 3
0
        /* ----------------------------------------------------------------- */
        ///
        /// Merge
        ///
        /// <summary>
        /// 2 つの PDF ファイルを結合します。
        /// </summary>
        ///
        /* ----------------------------------------------------------------- */
        private bool Merge(UserSetting setting, string escaped)
        {
            // Nothing to do.
            if (string.IsNullOrEmpty(escaped) ||
                (setting.ExistedFile != Parameter.ExistedFiles.MergeHead &&
                setting.ExistedFile != Parameter.ExistedFiles.MergeTail)) return true;

            string tmp = Utility.WorkingDirectory + '\\' + System.IO.Path.GetRandomFileName();
            string head = (setting.ExistedFile == Parameter.ExistedFiles.MergeHead) ? setting.OutputPath : escaped;
            string tail = (setting.ExistedFile == Parameter.ExistedFiles.MergeTail) ? setting.OutputPath : escaped;

            bool status = true;
            try
            {
                iTextSharp.text.pdf.PdfReader reader_head = Open(head, setting.Permission.Password);
                iTextSharp.text.pdf.PdfReader reader_tail = Open(tail, setting.Permission.Password);

                using (var fs = new System.IO.FileStream(tmp, System.IO.FileMode.Create))
                {
                    iTextSharp.text.pdf.PdfCopyFields copy = new iTextSharp.text.pdf.PdfCopyFields(fs);
                    copy.AddDocument(reader_head);
                    copy.AddDocument(reader_tail);
                    copy.Close();
                }
                reader_head.Close();
                reader_tail.Close();
            }
            catch (Exception err)
            {
                _messages.Add(new Message(Message.Levels.Error, err));
                _messages.Add(new Message(Message.Levels.Debug, err));
                status = false;
            }
            finally
            {
                if (CubePdf.Misc.File.Exists(setting.OutputPath)) CubePdf.Misc.File.Delete(setting.OutputPath, true);
                if (!status || !CubePdf.Misc.File.Exists(tmp)) CubePdf.Misc.File.Move(escaped, setting.OutputPath, true);
                else CubePdf.Misc.File.Move(tmp, setting.OutputPath, true);

                if (CubePdf.Misc.File.Exists(tmp)) CubePdf.Misc.File.Delete(tmp, false);
                if (CubePdf.Misc.File.Exists(escaped)) CubePdf.Misc.File.Delete(escaped, false);
            }

            return status;
        }
Esempio n. 4
0
        /* ----------------------------------------------------------------- */
        ///
        /// Merge
        ///
        /// <summary>
        /// 2 つの PDF ファイルを結合します。
        /// </summary>
        ///
        /* ----------------------------------------------------------------- */
        private bool Merge(UserSetting setting, string escaped)
        {
            // Nothing to do.
            if (string.IsNullOrEmpty(escaped) ||
                (setting.ExistedFile != Parameter.ExistedFiles.MergeHead &&
                 setting.ExistedFile != Parameter.ExistedFiles.MergeTail))
            {
                return(true);
            }

            string tmp  = Utility.WorkingDirectory + '\\' + System.IO.Path.GetRandomFileName();
            string head = (setting.ExistedFile == Parameter.ExistedFiles.MergeHead) ? setting.OutputPath : escaped;
            string tail = (setting.ExistedFile == Parameter.ExistedFiles.MergeTail) ? setting.OutputPath : escaped;

            bool status = true;

            try
            {
                iTextSharp.text.pdf.PdfReader reader_head = Open(head, setting.Permission.Password);
                iTextSharp.text.pdf.PdfReader reader_tail = Open(tail, setting.Permission.Password);

                using (var fs = new System.IO.FileStream(tmp, System.IO.FileMode.Create))
                {
                    iTextSharp.text.pdf.PdfCopyFields copy = new iTextSharp.text.pdf.PdfCopyFields(fs);
                    copy.AddDocument(reader_head);
                    copy.AddDocument(reader_tail);
                    copy.Close();
                }
                reader_head.Close();
                reader_tail.Close();
            }
            catch (Exception err)
            {
                _messages.Add(new Message(Message.Levels.Error, err));
                _messages.Add(new Message(Message.Levels.Debug, err));
                status = false;
            }
            finally
            {
                if (CubePdf.Misc.File.Exists(setting.OutputPath))
                {
                    CubePdf.Misc.File.Delete(setting.OutputPath, true);
                }
                if (!status || !CubePdf.Misc.File.Exists(tmp))
                {
                    CubePdf.Misc.File.Move(escaped, setting.OutputPath, true);
                }
                else
                {
                    CubePdf.Misc.File.Move(tmp, setting.OutputPath, true);
                }

                if (CubePdf.Misc.File.Exists(tmp))
                {
                    CubePdf.Misc.File.Delete(tmp, false);
                }
                if (CubePdf.Misc.File.Exists(escaped))
                {
                    CubePdf.Misc.File.Delete(escaped, false);
                }
            }

            return(status);
        }