示例#1
0
文件: PSPDF.cs 项目: nethomtz/DGCCH
        private void Button11_Click(object sender, EventArgs e)
        {
            var document = new Document();
            var ms       = new MemoryStream();

            string[] Lista = { "Guia_CV_2016.pdf", "Guia_CV_2016.pdf" };

            var pdfCopy = new PdfCopy(document, ms);

            document.Open();

            foreach (var item in Lista)
            {
                var pdfReader = new PdfReader(item);
                var n         = pdfReader.NumberOfPages;
                for (var page = 0; page < n;)
                {
                    pdfCopy.AddPage(pdfCopy.GetImportedPage(pdfReader, ++page));
                }
                pdfCopy.FreeReader(pdfReader);
            }
            pdfCopy.Flush();

            document.Close();
            Document doc = new Document();

            PdfWriter.GetInstance(doc, new FileStream("COMPLETO2.pdf", FileMode.Create));
            //ms.WriteTo(new FileStream("documentoJutar.pdf", FileMode.OpenOrCreate));
            ms.WriteTo(new FileStream("Prueba.pdf", FileMode.OpenOrCreate));
        }
示例#2
0
        /* private void Button2_Click(object sender, EventArgs e)
         * {
         *   if (/*digital.Rows.Count > 0)
         *   {
         *       int id = int.Parse(/**digital .Rows[*digital .currentRow.Index].Cell[0].Value.ToString());
         *       using (Model.datosUsuario db = new Model.datosUsuario())
         *       {
         *           var oDocument = db.documen.Find(id);
         *           string path = AppDomain.CurrentDomain.BaseDirectory;
         *           string folder = path + "/temp/";
         *           string fullFilePath = folder + oDocument.realName;
         *
         *           if (!Directory.Exists(folder))
         *               Directory.CreateDirectory(folder);
         *
         *           if (File.Exists(fullFilePath))
         *               Directory.Delete(fullFilePath);
         *
         *           File.WriteAllBytes(fullFilePath, oDocument.doc);
         *
         *           Process.Start(fullFilePath);
         *
         *       }
         *
         *   }
         * }
         *
         * private void Button3_Click(object sender, EventArgs e)
         * {
         *   try
         *   {
         *       conexion.Close();
         *       SqlDataAdapter adapter = new SqlDataAdapter("Select Archivo from Scanner where Nombre = '" + Nombre + "'", conexion);
         *       DataSet set = new DataSet();
         *       conecta();
         *       adapter.Fill(set, "Scanner");
         *       byte[] datos = new byte[0];
         *       DataRow row = set.Tables["Scanner"].Rows[0];
         *       datos = (byte[])row["Archivo"];
         *       datos = (byte[])row["Archivo"];
         *       datos = (byte[])row["Archivo"];
         *       datos = (byte[])row["Archivo"];
         *       System.IO.MemoryStream ms = new System.IO.MemoryStream(datos);
         *
         *       PDF.src = openFileDialog.FileName;
         *
         *   }
         *   catch (SqlException)
         *   {
         *       MessageBox.Show("Error Al Cargar La Imagen", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         *   }
         *
         * }
         * public void verdocumentos(string Nombre, AxAcroPDF PDF, OpenFileDialog openFileDialog)
         * {
         *   try
         *   {
         *       string query = "Select Archivo from Scanner where Nombre = @nombre";
         *       SqlCommand cmd = new SqlCommand(query, conexion)
         *
         * cmd.Parameters.AddWithValue("@nombre", Nombre);
         *
         *       SqlDataAdapter da = new SqlDataAdapter(cmd);
         *       DataTable dt = new DataTable();
         *       adapter.Fill(dt);
         *
         *       DataRow row = dt.Rows[0];
         *       byte[] datos = (byte[])row["Archivo"];
         *
         *       File.WriteAllBytes("ruta", datos);
         *       File.WriteAllBytes(fileDialog.FileName, datos);
         *       PDF.src = "ruta";
         *
         *
         *   }
         *   catch (SqlException)
         *   {
         *       MessageBox.Show("Error Al Cargar La Imagen", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         *   }
         * }*/

        private void Button4_Click(object sender, EventArgs e)
        {
            var document = new Document();
            var ms       = new MemoryStream();

            string[] Lista = { "PRUEBA5.pdf", "PRUEBA7.pdf" };

            var pdfCopy = new PdfCopy(document, ms);

            document.Open();

            foreach (var item in Lista)
            {
                var pdfReader = new PdfReader(item);
                var n         = pdfReader.NumberOfPages;
                for (var page = 0; page < n;)
                {
                    pdfCopy.AddPage(pdfCopy.GetImportedPage(pdfReader, ++page));
                }
                pdfCopy.FreeReader(pdfReader);
            }
            pdfCopy.Flush();

            document.Close();
            ms.WriteTo(new FileStream("documentoJutar.pdf", FileMode.OpenOrCreate));
        }
示例#3
0
        private void AggregatePdfs()
        {
            /*    Console.WriteLine("Aggregate PDF raised >> " + DateTime.Now.ToShortTimeString());
             *  var srcPDF = CurrentSrc + @"\src.pdf";
             *  PdfReader.unethicalreading = true;
             *
             *  if(Directory.Exists(CurrentSrc))
             *  {
             *      try
             *      {
             *
             *          using (MemoryStream memoryStream = new MemoryStream())
             *          using (Document doc = new Document())
             *          using (PdfCopy pdf = new PdfCopy(doc, memoryStream))
             *          {
             *              doc.Open();
             *              if(srcPDFFiles.Length > 0)
             *              {
             *                  foreach(var file in srcPDFFiles)
             *                  {
             *                      if (IsValidPdf(file))
             *                      {
             *                          using (MemoryStream _ReadOnlyStream = new MemoryStream())
             *                          {
             *                              using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
             *                              {
             *                                  _ReadOnlyStream.SetLength(fileStream.Length);
             *                                  fileStream.Read(_ReadOnlyStream.GetBuffer(), 0, (int)_ReadOnlyStream.Length);
             *                                  fileStream.Flush();
             *                                  fileStream.Close();
             *                              }
             *
             *                              using (PdfReader reader = new PdfReader(_ReadOnlyStream))
             *                              {
             *                                  for (var i = 0; i < reader.NumberOfPages; i++)
             *                                  {
             *                                      PdfImportedPage page = pdf.GetImportedPage(reader, i + 1);
             *                                      pdf.AddPage(page);
             *                                  }
             *                                  pdf.FreeReader(reader);
             *                                  reader.Close();
             *                              }
             *
             *                              _ReadOnlyStream.Flush();
             *                              _ReadOnlyStream.Close();
             *                          }
             *                      }
             *                  }
             *              }
             *              pdf.Flush();
             *              pdf.Close();
             *              doc.Close();
             *
             *              using (var streamX = new FileStream(srcPDF, FileMode.Create, FileAccess.ReadWrite))
             *              {
             *                  memoryStream.WriteTo(streamX);
             *                  streamX.Flush();
             *                  streamX.Close();
             *              }
             *              memoryStream.Flush();
             *              memoryStream.Close();
             *          }
             *
             *      }
             *      catch
             *      {
             *          throw;
             *      }
             *  }
             *
             *  Wait = false;
             *  Console.WriteLine("Aggregare PDF dropped >> " + DateTime.Now.ToShortTimeString());*/

            Console.WriteLine("Aggregate PDF raised >> " + DateTime.Now.ToShortTimeString());


            var srcPdf = SourceFolder + @"\src.pdf";

            PdfReader.unethicalreading = true;

            var filesTemp = new List <string>();

            filesTemp.Clear();

            try
            {
                if (Directory.Exists(SourceFolder))
                {
                    foreach (var files in Directory.GetFiles(SourceFolder).Where(File =>
                                                                                 !File.Contains("src.pdf") && File.Contains(".pdf")))
                    {
                        filesTemp.Add(files);
                    }

                    if (filesTemp.Count > 0)
                    {
                        if (File.Exists(srcPdf))
                        {
                            MoveSrc();
                        }

                        using (var stream = new MemoryStream())
                        {
                            using (var doc = new Document())
                            {
                                using (var pdf = new PdfCopy(doc, stream)
                                {
                                    CloseStream = false
                                })
                                {
                                    doc.Open();

                                    try
                                    {
                                        if (filesTemp.Count > 0)
                                        {
                                            foreach (var file in filesTemp)
                                            {
                                                using (MemoryStream memoryStream = new MemoryStream())
                                                {
                                                    using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
                                                    {
                                                        memoryStream.SetLength(fs.Length);
                                                        fs.Read(memoryStream.GetBuffer(), 0, (int)fs.Length);
                                                        fs.Flush();
                                                    }

                                                    using (PdfReader reader = new PdfReader(memoryStream))
                                                    {
                                                        for (var i = 0; i < reader.NumberOfPages; i++)
                                                        {
                                                            PdfImportedPage page = null;
                                                            page = pdf.GetImportedPage(reader, i + 1);
                                                            pdf.AddPage(page);
                                                        }
                                                        pdf.FreeReader(reader);
                                                    }
                                                    memoryStream.Flush();
                                                }
                                            }
                                        }
                                    }
                                    catch (Exception ee)
                                    {
                                        throw new Exception("Not PDFs in source", ee);
                                    }
                                    pdf.Flush();
                                }
                            }

                            using (var streamX = new FileStream(srcPdf, FileMode.Create))
                            {
                                stream.WriteTo(streamX);
                                streamX.Flush();
                            }
                            stream.Flush();
                        }
                    }
                }
            }
            catch (Exception ee)
            {
                Console.WriteLine(ee);
            }
            Wait = false;

            Console.WriteLine("Aggregare PDF dropped >> " + DateTime.Now.ToShortTimeString());
        }