Пример #1
0
        private void PDF()
        {
            Spire.Pdf.PdfDocument document = new Spire.Pdf.PdfDocument();
            document.LoadFromFile(sciezkaDoPliku);
            Image wynik = document.SaveAsImage(0, Spire.Pdf.Graphics.PdfImageType.Metafile);

            for (int i = 1; i < document.Pages.Count; i++)
            {
                Image pom = document.SaveAsImage(i, Spire.Pdf.Graphics.PdfImageType.Metafile);
                wynik = MergeTwoImages(wynik, pom);
                pom.Dispose();
            }
            wynik.Save(sciezgaDoZapisaniaPliku + /*"_" + document.Pages.Count + */ ".jpg");
            wynik.Dispose();
            document.Dispose();
        }
Пример #2
0
        public ActionResult GetDocumentThumbnail(int id)
        {
            string doctype   = "image/jpg";
            var    imageData = DMLObj.GetDocDataById(id, out doctype);

            if (doctype.ToLower() == "application/pdf")
            {
                Spire.Pdf.PdfDocument document = new Spire.Pdf.PdfDocument();
                document.LoadFromStream(new MemoryStream(imageData));
                Image ImgDoc = document.SaveAsImage(0, Spire.Pdf.Graphics.PdfImageType.Bitmap, 120, 120);
                return(File(imageToByteArray(ImgDoc.ScaleImage(150, 150)), "image/jpeg"));
            }

            //if (doctype.ToLower() == "application/msword")
            //{
            //    Spire.Doc.Document document = new Spire.Doc.Document();
            //    document.LoadFromStream(new MemoryStream(imageData),Spire.Doc.FileFormat.Doc);
            //    Image ImgDoc = document.SaveToImages(0, Spire.Doc.Documents.ImageType.Metafile);
            //    return File(imageToByteArray(ImgDoc.ScaleImage(150, 150)), "image/jpeg");
            //}

            //if (doctype.ToLower() == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
            //{
            //    Spire.Doc.Document document = new Spire.Doc.Document();
            //    document.LoadFromStream(new MemoryStream(imageData), Spire.Doc.FileFormat.Docx2010);
            //    Image ImgDoc = document.SaveToImages(0, Spire.Doc.Documents.ImageType.Metafile);
            //    return File(imageToByteArray(ImgDoc.ScaleImage(150, 150)), "image/jpeg");
            //}
            return(File(imageData, doctype));
        }
Пример #3
0
 public System.Drawing.Image ConvertPdfToImage()
 {
     try
     {
         Spire.Pdf.PdfDocument pdfDocument = new Spire.Pdf.PdfDocument();
         pdfDocument.LoadFromFile(this.FilePath);
         System.Drawing.Image image1 = pdfDocument.SaveAsImage(0, PdfImageType.Metafile);
         Size size  = image1.Size;
         int  width = size.Width * 5;
         size = image1.Size;
         int height = size.Height * 5;
         System.Drawing.Image image2 = (System.Drawing.Image) new Bitmap(width, height);
         using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(image2))
         {
             graphics.ScaleTransform(5f, 5f);
             graphics.DrawImage(image1, new System.Drawing.Rectangle(new Point(0, 0), image1.Size), new System.Drawing.Rectangle(new Point(0, 0), image1.Size), GraphicsUnit.Pixel);
         }
         MembercardService.logger.Debug("PDF conversion to image succeeded.");
         return(image2);
     }
     catch (Exception ex)
     {
         MembercardService.logger.Error("PDF conversion to image failed:" + ex.Message);
         return((System.Drawing.Image)null);
     }
 }
Пример #4
0
        public Image FirstPage(byte[] source)
        {
            var doc = new Spire.Pdf.PdfDocument();

            doc.LoadFromBytes(source);
            return(doc.SaveAsImage(0));
        }
Пример #5
0
        private void ConvertPdfToPng(byte[] pdfImage)
        {
            var ms = new MemoryStream();

            new MemoryStream(pdfImage).CopyTo(ms);
            Spire.Pdf.PdfDocument d = new Spire.Pdf.PdfDocument(ms);
            pngImage = d.SaveAsImage(0, 300, 300);
            d.Close();
        }
Пример #6
0
 public Image FirstPage(string sourcePath)
 {
     if (File.Exists(sourcePath))
     {
         var doc = new Spire.Pdf.PdfDocument();
         doc.LoadFromFile(sourcePath);
         return(doc.SaveAsImage(0));
     }
     return(null);
 }
Пример #7
0
 void PdftoIMG(string pdfpath)
 {
     Spire.Pdf.PdfDocument pdfdocument = new Spire.Pdf.PdfDocument();
     pdfdocument.LoadFromFile(pdfpath);
     //for (int i = 0; i < pdfdocument.Pages.Count; i++)
     //{
     System.Drawing.Image image = pdfdocument.SaveAsImage(0, 96, 96);
     image.Save(string.Format(Server.MapPath("~/Images/UIDimage/New.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg));
     //}
 }
Пример #8
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.DefaultExt = ".pdf";
            dlg.Filter     = "PDF Files (*.pdf)|*.pdf";

            bool?result = dlg.ShowDialog();

            if (result == true)
            {
                Spire.Pdf.PdfDocument pdf = new Spire.Pdf.PdfDocument();
                pathToBackground = dlg.FileName;
                pdf.LoadFromFile(pathToBackground);

                try
                {
                    //zapisanie strony pdf jako image
                    Image bmp = pdf.SaveAsImage(0);

                    //konwersja z Image na BitmapImage
                    MemoryStream ms = new MemoryStream();
                    bmp.Save(ms, ImageFormat.Bmp);

                    BitmapImage image = new BitmapImage();
                    image.BeginInit();
                    image.StreamSource = ms;
                    image.EndInit();

                    //ustawienie tła
                    DiplomaBackground.Source = image;

                    //w przypadku poprawnego wczytania pliku i poprawnego wykonania działań na nim
                    Description.Visibility          = Visibility.Hidden;
                    ParticipantNameLabel.Visibility = Visibility.Visible;
                    TextPathToPDF.Text           = pathToBackground;
                    LabelPathToPDF.Visibility    = Visibility.Visible;
                    TextPathToPDF.Visibility     = Visibility.Visible;
                    checkBoxBackground.IsChecked = true;
                    isPDFloaded = true;
                    if (isPathChosen && Globals.database != null && Globals.database.isConnected())
                    {
                        GeneratePDFs.IsEnabled = true;
                    }
                    MessageBox.Show("Poprawnie wczytano obraz.", "Informacja", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                catch (OutOfMemoryException ex)
                {
                    MessageBox.Show("Za mało pamięci, aby kontynuować. \nKomunikat błędu: " + ex, "Błąd", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
                catch (FileNotFoundException ex)
                {
                    MessageBox.Show("Nie znaleziono pliku. \nKomunikat błędu: " + ex, "Błąd", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
        }
Пример #9
0
        public List <Image> SingleFile(byte[] source)
        {
            var result = new List <Image>();
            var doc    = new Spire.Pdf.PdfDocument();

            doc.LoadFromBytes(source);
            for (int it = 0; it < doc.Pages.Count; it++)
            {
                result.Add(doc.SaveAsImage(it));
            }

            return(result);
        }
Пример #10
0
        public static void SpireAPI()
        {
            try
            {
                Stopwatch watch = new Stopwatch();
                watch.Start();
                string url = @"C:\Users\BuzzBuzzUser\Pictures\PDFSImages.pdf";
                //HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                //HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse();

                //MemoryStream memStream;
                //using (var stream = httpWebReponse.GetResponseStream())
                //{
                //    memStream = new MemoryStream();

                //    byte[] buffer = new byte[1024];
                //    int byteCount;
                //    do
                //    {
                //        byteCount = stream.Read(buffer, 0, buffer.Length);
                //        memStream.Write(buffer, 0, byteCount);
                //    } while (byteCount > 0);
                //}
                //memStream.Seek(0, SeekOrigin.Begin);

                watch.Stop();
                Console.WriteLine("It took {0} milliseconds to download the file", watch.ElapsedMilliseconds);
                watch.Restart();

                Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument();
                doc.LoadFromFile(url);
                //doc.LoadFromStream(memStream);
                //string fileName = url.Substring(url.LastIndexOf("/"));

                for (int i = 0; i < doc.Pages.Count; i++)
                {
                    //Console.WriteLine(doc.Pages[i].IsBlank());
                    var pdfImg = doc.SaveAsImage(i, Spire.Pdf.Graphics.PdfImageType.Bitmap, 300, 300);

                    pdfImg.Save(string.Format(@"C:\Users\BuzzBuzzUser\Pictures\Saved Pictures\{0}.bmp", i));
                    //pdfImg.Save(string.Format(@"C:\Users\BuzzBuzzUser\Pictures\Saved Pictures\{0}.png", i));
                }

                watch.Stop();
                Console.WriteLine("PDF conversion took {0} milliseconds", watch.ElapsedMilliseconds);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }
        }
Пример #11
0
        public async Task ConvertToImage(string fileLink)
        {
            try
            {
                string      tortf = ReadPdfFile(fileLink);
                RichTextBox rtf   = new RichTextBox();
                rtf.Document.Blocks.Clear();
                rtf.Document.Blocks.Add(new Paragraph(new Run(tortf)));
                string path = System.IO.Path.GetDirectoryName(fileLink);

                if (!Directory.Exists(path))
                {
                    DirectoryInfo di = Directory.CreateDirectory(path);
                }
                Spire.Pdf.PdfDocument pdf = new Spire.Pdf.PdfDocument();

                await Task.Run(() =>
                {
                    pdf.LoadFromFile(fileLink);
                    System.Drawing.Size size = new System.Drawing.Size(650, 1080);
                    for (int i = 0; i <= pdf.Pages.Count; i++)
                    {
                        try
                        {
                            var source         = pdf.SaveAsImage(i);
                            Bitmap bitmapImage = (Bitmap)source;
                            var result         = new Bitmap(Resize(bitmapImage, size, ImageFormat.Png));
                            result.Save(path + @"\page" + i.ToString() + ".png", ImageFormat.Png);
                        }
                        catch
                        {
                        }
                    }
                });
            }
            catch (Exception r)
            {
                MessageBox.Show(r.Message);
            }
        }
Пример #12
0
        public void SaveAsImage(string sourcePath, string targetPath)
        {
            // Get some file names
            string[] files = GetPDfs(sourcePath);

            // Iterate files
            foreach (string file in files)
            {
                var name = Path.GetFileNameWithoutExtension(file);
                var path = targetPath.Contains(".") ? Path.GetDirectoryName(targetPath) : targetPath;
                if (File.Exists(Path.Combine(path, name + "_1".ToString() + ".jpg")))
                {
                    continue;
                }
                var doc = new Spire.Pdf.PdfDocument();
                doc.LoadFromFile(file);
                for (int it = 0; it < doc.Pages.Count; it++)
                {
                    var bmp = doc.SaveAsImage(it);
                    bmp.Save(Path.Combine(path, name + "_" + (it + 1).ToString() + ".jpg"), ImageFormat.Jpeg);
                }
            }
        }
Пример #13
0
        public ActionResult GetDocument(string DocId, string PageNumber)
        {
            int AmDocId = 0;
            int AmPn    = 0;

            Int32.TryParse(DocId, out AmDocId);
            Int32.TryParse(PageNumber, out AmPn);
            string doctype   = "image/jpg";
            var    imageData = DMLObj.GetDocDataById(AmDocId, out doctype);

            if (imageData == null)
            {
                return(null);
            }
            if (doctype.ToLower() == "application/pdf")
            {
                Spire.Pdf.PdfDocument document = new Spire.Pdf.PdfDocument();
                document.LoadFromStream(new MemoryStream(imageData));
                Image ImgDoc = document.SaveAsImage(AmPn, Spire.Pdf.Graphics.PdfImageType.Bitmap, 400, 400);
                ImgDoc = ImgDoc.cropImage(new Rectangle(0, 57, ImgDoc.Width, ImgDoc.Height - 57));
                return(File(imageToByteArray(ImgDoc), "image/jpeg"));
            }
            return(File(imageData, doctype));
        }
Пример #14
0
        public string GenerarCredencial()
        {
            string fileName = string.Empty;

            #region iniciandoDocumento
            doc.AddAuthor("CODESUR");
            doc.AddKeywords("pdf, PdfWriter; Documento; iTextSharp");
            fileName = string.Format("CredencialAcreditacion_{0}_{1:yyyyMMddHHss}.pdf", Usuario, DateTime.Now);
            string path = PathResources;
            path = string.Format("{0}Reportes\\{1}", path, fileName);
            PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(path, FileMode.Create));
            doc.Open();
            #endregion
            try
            {
                AgregarFondoAnverso();
                AgregarFoto();
                AgregarQR();
                AgregarCodigoRol();
                AgregarNombre();
                AgregarDatosDelegacion();
                AgregarPrivilegios();
                AgregarFondoReverso();
                AgregarCodigoBarras(wri);
                AgregarTextoFoto();
                AgregarNombreReverso();
                AgregarPrivilegiosReverso();
                PdfContentByte cb = wri.DirectContent;
                //PdfPTable table = new PdfPTable(1);
                ////PdfPCell[] cells = new PdfPCell[] { new PdfPCell(new Phrase("HOLAAA"))};
                ////PdfPRow p = new PdfPRow(cells);
                ////p.
                //table.TotalWidth = 400f;
                //table.AddCell("Test");
                //table.WriteSelectedRows(0, -1, 200, 50, cb);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                doc.Close();
            }
            Spire.License.LicenseProvider.SetLicenseFileFullPath(@"C:\Hammer\ExternalReferences\license.lic");
            Spire.Pdf.PdfDocument pdfdocument = new Spire.Pdf.PdfDocument();
            try
            {
                pdfdocument.LoadFromFile(path);
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                Document  realDoc = new Document(new Rectangle(566f, 454f), 0f, 0f, 0f, 0f);
                PdfPTable Reporte = new PdfPTable(new float[] { 283f, 283f });
                Reporte.WidthPercentage = 100;
                for (int i = 0; i < pdfdocument.Pages.Count; i++)
                {
                    System.Drawing.Image image = pdfdocument.SaveAsImage(i, 480, 500);
                    MemoryStream         ms    = new MemoryStream();
                    image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    iTextSharp.text.Image imgpdf = iTextSharp.text.Image.GetInstance(ms.ToArray());
                    PdfPCell imgCell             = new PdfPCell(imgpdf, true);
                    Reporte.AddCell(imgCell);
                }
                PdfWriter realWri = PdfWriter.GetInstance(realDoc, new FileStream(path, FileMode.Create));
                realDoc.Open();
                realDoc.Add(Reporte);


                realDoc.Close();
            }
            catch
            {
            }
            return(path);
        }
Пример #15
0
        public string GenerarCredencial()
        {
            string fileName = string.Empty;

            #region iniciandoDocumento
            doc.AddAuthor("CODESUR");
            doc.AddKeywords("pdf, PdfWriter; Documento; iTextSharp");
            fileName = string.Format("CredencialAcreditacion_{0}_{1:yyyyMMddHHss}.pdf", Usuario, DateTime.Now);
            string path = PathResources;
            path = string.Format("{0}Reportes\\{1}", path, fileName);
            PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(path, FileMode.Create));
            doc.Open();
            #endregion
            try
            {
                AgregarFondoAnverso();
                AgregarFoto();
                AgregarQR();
                AgregarCodigoRol();
                AgregarNombre();
                AgregarDatosDelegacion();
                AgregarPrivilegios();
                AgregarFondoReverso();
                AgregarCodigoBarras(wri);
                AgregarTextoFoto();
                AgregarNombreReverso();
                AgregarPrivilegiosReverso();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                doc.Close();
            }

            Spire.Pdf.PdfDocument pdfdocument = new Spire.Pdf.PdfDocument();
            try
            {
                pdfdocument.LoadFromFile(path);
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                Document  realDoc = new Document(new Rectangle(566f, 454f), 0f, 0f, 0f, 0f);
                PdfPTable Reporte = new PdfPTable(new float[] { 283f, 283f });
                for (int i = 0; i < pdfdocument.Pages.Count; i++)
                {
                    System.Drawing.Image image = pdfdocument.SaveAsImage(i);
                    MemoryStream         ms    = new MemoryStream();
                    image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    iTextSharp.text.Image imgpdf = iTextSharp.text.Image.GetInstance(ms.ToArray());
                    PdfPCell imgCell             = new PdfPCell(imgpdf, true);
                    imgCell.Border          = PdfPCell.NO_BORDER;
                    imgCell.BackgroundColor = BaseColor.BLACK;
                    Reporte.AddCell(imgCell);
                }
                PdfWriter realWri = PdfWriter.GetInstance(realDoc, new FileStream(path, FileMode.Create));
                realDoc.Open();
                realDoc.Add(Reporte);
                realDoc.Close();
            }
            catch
            {
            }

            return(path);
        }
Пример #16
0
        public static void SaveImage(Spire.Pdf.PdfDocument doc, string fileName, int index)
        {
            var pdfImg = doc.SaveAsImage(index, Spire.Pdf.Graphics.PdfImageType.Bitmap, 300, 300);

            pdfImg.Save(string.Format(@"C:\Users\BuzzBuzzUser\Pictures\Saved Pictures\ThreadedImg{0}.bmp", index));
        }