Esempio n. 1
0
        public bool Decrypt(string infile, string outfile)
        {
            if (!File.Exists(infile))
            {
                mErrorMessage = "iTextFilters.Decrypt Could not find infile: " + infile;
                return(false);
            }

            if (File.Exists(outfile))
            {
                mErrorMessage = "iTextFilters.Decrypt Outfile already exists bailing: " + outfile;
                return(false);
            }

            try{
                FileStream FileOutputStream = new FileStream(outfile, System.IO.FileMode.OpenOrCreate);
                PdfReader.unethicalreading = true;
                PdfReader     reader1 = new PdfReader(infile);
                PdfCopyFields copy    = new PdfCopyFields(FileOutputStream);
                copy.AddDocument(reader1);
                copy.Close();
                return(true);
            }
            catch (Exception e) {
                mErrorMessage = "iTextFilters.Decrypt Caught Exception: \n\n" + e.ToString();
                return(false);
            }
        }
        private void BtnGenPdf_Click(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            string    filename;
            ArrayList selected = new ArrayList();



            MemoryStream[] streams = null;
            byte[]         bytes;
            using (MemoryStream finalStream = new MemoryStream())
            {
                PdfCopyFields copy = new PdfCopyFields(finalStream);

                foreach (MemoryStream ms in streams)
                {
                    ms.Position = 0;
                    //Add it to the copy object
                    copy.AddDocument(new PdfReader(ms));
                    //Clean up
                    ms.Dispose();
                }

                copy.Close();
                bytes = finalStream.ToArray();
            }
            string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Combined.pdf");

            using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                fs.Write(bytes, 0, bytes.Length);
            }
        }
Esempio n. 3
0
        public MemoryStream MemoryStreamMerger(List <MemoryStream> streams)
        {
            MemoryStream OurFinalReturnedMemoryStream;

            using (OurFinalReturnedMemoryStream = new MemoryStream())
            {
                //Create our copy object
                PdfCopyFields copy = new PdfCopyFields(OurFinalReturnedMemoryStream);

                //Loop through each MemoryStream
                foreach (MemoryStream ms in streams)
                {
                    //Reset the position back to zero
                    ms.Position = 0;
                    //Add it to the copy object
                    copy.AddDocument(new PdfReader(ms));
                    //Clean up
                    ms.Dispose();
                }
                //Close the copy object
                copy.Close();

                //Get the raw bytes to save to disk
                //bytes = finalStream.ToArray();
            }
            return(new MemoryStream(OurFinalReturnedMemoryStream.ToArray()));
        }
Esempio n. 4
0
    public static byte[] concatmemory(MemoryStream[] streams)
    {
        //I don't have a web server handy so I'm going to write my final MemoryStream to a byte array and then to disk
        byte[] bytes;
        //Create our final combined MemoryStream
        using (MemoryStream finalStream = new MemoryStream())
        {
            //Create our copy object
            PdfCopyFields copy = new PdfCopyFields(finalStream);

            //Loop through each MemoryStream
            foreach (MemoryStream memory in streams)
            {
                //Reset the position back to zero
                memory.Position = 0;
                //Add it to the copy object
                copy.AddDocument(new PdfReader(memory));
                //Clean up
                memory.Dispose();
            }
            //Close the copy object
            copy.Close();
            //Get the raw bytes to save to disk
            bytes = finalStream.ToArray();
        }
        return(bytes);
    }
 //Copy a set of form fields from an existing PDF template/doc
 //and keep appending to a brand new PDF file.
 //The copied set of fields will have different values.
 private void AppendSetOfFormFields()
 {
     PdfCopyFields _copy = new PdfCopyFields(new FileStream(Server.MapPath(P_OutputStream), FileMode.Create));
     _copy.AddDocument(new PdfReader(a1("1")));
     _copy.AddDocument(new PdfReader(a1("2")));
     _copy.AddDocument(new PdfReader(new FileStream(Server.MapPath("~/MyPDFTemplates/Myaspx.pdf"), FileMode.Open)));
     _copy.Close();
 }
Esempio n. 6
0
        /// <summary>
        /// Merge multiple pdf files into one.
        /// </summary>
        public static void MergePdfs(string outPdf, string masterPart, params string[] parts)
        {
            if (String.IsNullOrEmpty(outPdf))
            {
                throw new ArgumentNullException("outPdf");
            }
            if (String.IsNullOrEmpty(masterPart))
            {
                throw new ArgumentNullException("masterPart");
            }
            if (outPdf.Equals(masterPart, StringComparison.CurrentCultureIgnoreCase))
            {
                throw new ArgumentException("the output file must be different from the master file");
            }
            if (parts == null || parts.Length == 0)
            {
                //No need to do a merge here, just copy the only one
                File.Copy(masterPart, outPdf);
                return;
            }

            PdfCopyFields output = null;

            try
            {
                output = new PdfCopyFields(File.OpenWrite(outPdf));
                foreach (var part in new[] { masterPart }.Concat(parts))
                {
                    output.AddDocument(new PdfReader(part));
                }
            }
            finally
            {
                if (output != null)
                {
                    try
                    {
                        output.Close();
                    }
                    catch (Exception exception)
                    {
                        throw new Exception("Error closing merged file: " + exception.Message, exception);
                    }
                }
            }
        }
Esempio n. 7
0
// ---------------------------------------------------------------------------
        public void Write(Stream stream)
        {
            using (ZipFile zip = new ZipFile()) {
                using (MemoryStream ms = new MemoryStream()) {
                    // Create a PdfCopyFields object
                    PdfCopyFields copy = new PdfCopyFields(ms);
                    // add a document
                    copy.AddDocument(new PdfReader(DATASHEET));
                    // add a document
                    copy.AddDocument(new PdfReader(DATASHEET));
                    // close the PdfCopyFields object
                    copy.Close();
                    zip.AddEntry(RESULT, ms.ToArray());
                }
                zip.AddFile(DATASHEET, "");
                zip.Save(stream);
            }
        }
Esempio n. 8
0
        public void addAdditionalPages()
        {
            if (additionalHealthAnswers.Count > 0)
            {
                ByteBuffer    tmp  = new ByteBuffer();
                PdfCopyFields copy = new PdfCopyFields(tmp);

                //Original App
                PdfReader reader = new PdfReader(myPDF.Buffer);
                copy.AddDocument(reader);
                reader.Close();

                //Add Aditional Pages
                foreach (ByteBuffer b in additionalHealthAnswers)
                {
                    reader = new PdfReader(b.Buffer);
                    copy.AddDocument(reader);
                    reader.Close();
                }
                copy.Close();
                myPDF = tmp;
            }
        }
Esempio n. 9
0
        private void btnPDF_Create_Click(object sender, EventArgs e)
        {


            if (txtInput.Text == "")
            {
                MessageBox.Show("参照元PDFを選択して下さい。");
                btnInput.Focus();
                return;
            }

            if (txtOutput.Text == "")
            {
                MessageBox.Show("保存先を選択して下さい。");
                btnOutput.Focus();
                return;
            }

            DialogResult dr = MessageBox.Show("処理を開始します。よろしいですか?", "確認", MessageBoxButtons.YesNo);

            if (dr == System.Windows.Forms.DialogResult.Yes)
            {
            }
            else if (dr == System.Windows.Forms.DialogResult.No)
            {
                return;
            }
            else
            {
                return;
            }



            //コントロールを初期化する
            ProgressBar1.Minimum = 0;
            ProgressBar1.Value = 0;
            Label1.Text = "";
            //Label1を再描画する
            Label1.Update();


            // 参照元PDFファイルのオープン
            PdfReader rd = new PdfReader(txtInput.Text);

            //出力先pdf用
            PdfCopyFields oPdfcopy = null;
            Document oDocument = null;

            // ページ数を取得
            int iEND = rd.NumberOfPages;


            // しおりの情報を取得
            IList<Dictionary<string, object>> obookmarkList = SimpleBookmark.GetBookmark(rd);


            // PDFを閉じる
            rd.Close();


            int i; // カウント用変数
            string[] sTITLE = new string[obookmarkList.Count]; // しおりの件数をセット(しおり名)
            int[] iSectionStart = new int[obookmarkList.Count]; // しおりの件数をセット(しおりの開始位置)
            int[] iSectionEnd = new int[obookmarkList.Count]; // しおりの件数をセット(しおりの終了位置)
            ProgressBar1.Maximum =   obookmarkList.Count; // プログレスバーの最大値設定
            string[] sMONTH = new string[13] { "","Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; //英字変換用



            try
            {


                for (i = 0; i < obookmarkList.Count; ++i) // iをしおりの件数分、1ずつ増やして繰り返し
                {

                    // しおりの名前
                    string[] sSHIORI = ((obookmarkList[i]["Title"]).ToString()).Split('@');
                    sTITLE[i] = sSHIORI[2].ToString().Replace("CSTCNTR:", "");


                    // 開始ページ数
                    string[] sStartPage = ((obookmarkList[i]["Page"]).ToString()).Split(' ');
                    iSectionStart[i] = Convert.ToInt32(sStartPage[0]);



                    // 終了ページ数
                    int w_sEndPage;


                    int j = i;

                    do
                    {
                        if (j + 1 == obookmarkList.Count)
                        {
                            w_sEndPage = iEND;
                        }
                        else
                        {
                            string[] sEndPage = ((obookmarkList[j + 1]["Page"]).ToString()).Split(' ');  //次のしおりの開始ページを取得
                            w_sEndPage = Convert.ToInt32(sEndPage[0]);
                            j = j + 1;
                        }


                    }
                    while (iSectionStart[i] == w_sEndPage && i < obookmarkList.Count - 1 - 1); //現在のしおりの開始ページと次のしおりの開始ページが同じ時(重複時)、
                                                                                               //その次のしおりの開始ページを読み込む(ループ処理を続ける)
                                                                                               //※しかし、最終の重複時は処理を抜ける。

                    if (i + 1 == obookmarkList.Count) //最終データ

                    { iSectionEnd[i] = iEND; }

                    else if (iSectionStart[i] == w_sEndPage && i == obookmarkList.Count - 1 - 1) //最終の一つ前のデータ(しおり重複時)
                                                                                                 //※しおりカウント(obookmarkList.Count)は1から始まっているので、処理変数iの位置と合わせるために-1、
                                                                                                 //  更に最終の一つ前のデータなので、更に-1

                    { iSectionEnd[i] = iEND; }

                    else  //そのほかのデータ

                    { iSectionEnd[i] = w_sEndPage - 1; }


                }


                oDocument = new Document(rd.GetPageSizeWithRotation(1));
                oDocument.Open();

                string SHIORI_NAME = "";
                int w_SectionEnd = 0;
                int w_COUNT = 1;
                int k = 0;

                ProgressBar1.Visible = true;

                for (i = 0; i < obookmarkList.Count; ++i) // iをしおりの件数分、1ずつ増やして繰り返し
                {

                    ProgressBar1.Value = i;


                    if (sTITLE[i] == SHIORI_NAME)


                    { } //前回処理した時としおり名が一致しているとき、処理を行わない。

                    else
 
                    {
                        oPdfcopy = new PdfCopyFields(new FileStream(txtOutput.Text + "\\" + "MAN " + sMONTH[cmbMONTH.SelectedIndex + 1] + " " + cmbYEAR.SelectedItem + "_" + w_COUNT  + ".pdf", FileMode.Create));


                        k = 0;


                        //同しおり名の最後のページを記憶
                        do
                        {
                            w_SectionEnd = iSectionEnd[i + k];
                            k = k + 1;
                            if (i + k >= obookmarkList.Count - 1) { break;}
                        }
                        while (sTITLE[i] == sTITLE[i + k] );

                        
                        rd = new PdfReader(txtInput.Text);

                        rd.SelectPages(iSectionStart[i].ToString() + "-" + w_SectionEnd.ToString()); //現在のしおりの該当ページ部分を選択

                        oPdfcopy.AddDocument(rd); //PDFアウトプット
                        oPdfcopy.Close(); //pdf保存
                        rd.Close();

                        w_COUNT = w_COUNT + 1;


                        SHIORI_NAME = sTITLE[i]; //処理したしおり名を記憶しておく


                        Label1.Text = "PDF分割完了:" + "MAN " + sMONTH[cmbMONTH.SelectedIndex + 1] + " " + cmbYEAR.SelectedItem + "_" + w_COUNT + ".pdf";
                        Label1.Update();
                    }
                }

                ProgressBar1.Value = 0;
                ProgressBar1.Visible = false;
                Label1.Text = "処理完了:" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");


            }


            catch (Exception ex)
            {
                MessageBox.Show("エラー発生: " + ex.ToString());
            }


            finally
            {
                if (rd        != null) { rd.Close(); }
                if (oPdfcopy  != null) { oPdfcopy.Close(); }
                if (oDocument != null) { oDocument.Close(); }
            }

        
    }