コード例 #1
0
        public ActionResult ImageExtraction(string Browser, string submit1)
        {
            if (submit1 == "View Template")
            {
                string dataPath = ResolveApplicationDataPath("Brochure.pdf");
                Stream file2    = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                //Load the template document
                PdfLoadedDocument ldoc = new PdfLoadedDocument(file2);
                return(ldoc.ExportAsActionResult("InputTemplate.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Open));
            }

            //Load an existing PDF.
            PdfLoadedDocument loadedDocument = new PdfLoadedDocument(ResolveApplicationDataPath("Brochure.pdf"));

            //Load first page.
            PdfPageBase pageBase = loadedDocument.Pages[0];

            //Extract images from first page.
            System.Drawing.Image[] extractedImages = pageBase.ExtractImages();

            //Stream the output to the browser.
            if (Browser == "Browser")
            {
                ExportAsImage(extractedImages[0], "sample.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg, HttpContext.ApplicationInstance.Response, HttpReadType.Open);
            }
            else
            {
                ExportAsImage(extractedImages[0], "sample.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg, HttpContext.ApplicationInstance.Response, HttpReadType.Save);
            }
            return(View());
        }
コード例 #2
0
        static void Main(string[] args)
        {
            //Instantiate an object of Spire.Pdf.PdfDocument
            PdfDocument doc = new PdfDocument();

            //Load a PDF file
            doc.LoadFromFile("sample.pdf");
            List <Image> ListImage = new List <Image>();

            for (int i = 0; i < doc.Pages.Count; i++)
            {
                // Get an object of Spire.Pdf.PdfPageBase
                PdfPageBase page = doc.Pages[i];
                // Extract images from Spire.Pdf.PdfPageBase
                Image[] images = page.ExtractImages();
                if (images != null && images.Length > 0)
                {
                    ListImage.AddRange(images);
                }
            }
            if (ListImage.Count > 0)
            {
                for (int i = 0; i < ListImage.Count; i++)
                {
                    Image image = ListImage[i];
                    image.Save("image" + (i + 1).ToString() + ".png", System.Drawing.Imaging.ImageFormat.Png);
                }
                System.Diagnostics.Process.Start("image1.png");
            }
        }
コード例 #3
0
ファイル: SpireHelper.cs プロジェクト: hudean/itextsharpDemo
        /// <summary>
        /// Spire插件读取PDF中的二维码并保存二维码图片
        /// </summary>
        /// <param name="file">pdf文件路径</param>
        public static void QrCodeToPdf(string file, string fileImg)
        {
            //加载PDF文档
            PdfDocument doc = new PdfDocument();

            doc.LoadFromFile(file);

            List <Image> listImages = new List <Image>();

            for (int i = 0; i < doc.Pages.Count; i++)
            {
                // 实例化一个Spire.Pdf.PdfPageBase对象
                PdfPageBase page = doc.Pages[i];

                // 获取所有pages里面的图片
                Image[] images = page.ExtractImages(true); //page.ExtractImages();
                if (images != null && images.Length > 0)
                {
                    listImages.AddRange(images);
                }
            }

            // 将提取到的图片保存到本地路径
            if (listImages.Count > 0)
            {
                for (int i = 0; i < listImages.Count; i++)
                {
                    Image image = listImages[i];
                    //image.Save(@"C:\Users\Administrator\Desktop\ABC\image" + (i + 3).ToString() + ".png", System.Drawing.Imaging.ImageFormat.Png);
                    image.Save(fileImg, System.Drawing.Imaging.ImageFormat.Png);
                }
            }
        }
コード例 #4
0
ファイル: SpireParser.cs プロジェクト: leoparddne/PDFParser
 private static void SaveImage(this PdfPageBase page, string saveImagePath)
 {
     foreach (var item in page.ExtractImages())
     {
         if (!Directory.Exists(saveImagePath))
         {
             Directory.CreateDirectory(saveImagePath);
         }
         String imageFileName = Path.Combine(saveImagePath, Guid.NewGuid().ToString() + ".png");
         item.Save(imageFileName, ImageFormat.Png);
     }
 }
コード例 #5
0
ファイル: SpireHelper.cs プロジェクト: hudean/itextsharpDemo
        /// <summary>
        /// Spire插件读取PDF中的二维码,Zxing识别二维码
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static string QrCodeToPdftwo(string file)
        {
            string str = "";
            //加载PDF文档
            PdfDocument doc = new PdfDocument();

            doc.LoadFromFile(file);

            List <Image> listImages = new List <Image>();

            for (int i = 0; i < doc.Pages.Count; i++)
            {
                // 实例化一个Spire.Pdf.PdfPageBase对象
                PdfPageBase page = doc.Pages[i];

                // 获取所有pages里面的图片
                Image[] images = page.ExtractImages();
                if (images != null && images.Length > 0)
                {
                    listImages.AddRange(images);
                }
            }

            if (listImages.Count > 0)
            {
                //QRCodeDecoder decoder = new QRCodeDecoder();
                //var image = listImages[0];
                //str = decoder.decode(new ThoughtWorks.QRCode.Codec.Data.QRCodeBitmapImage((Bitmap)image));

                var image = listImages[0];
                ZXing.BarcodeReader reader = new ZXing.BarcodeReader();
                reader.Options.CharacterSet = "UTF-8";
                Bitmap map    = new Bitmap(image);
                Result result = reader.Decode(map);
                return(result == null ? "" : result.Text);
            }

            return(str);
        }
コード例 #6
0
        public static StandardFlowDocument DoExport(PDFDocument pdf_document)
        {
            StandardFlowDocument flow_document = new StandardFlowDocument();

            flow_document.Background = Brushes.White;

            using (AugmentedPdfLoadedDocument doc = new AugmentedPdfLoadedDocument(pdf_document.DocumentPath))
            {
                for (int page = 1; page <= pdf_document.PageCount; ++page)
                {
                    string msg = $"Exporting page {page}/{pdf_document.PageCountAsString}";

                    StatusManager.Instance.UpdateStatus("ExportToWord", msg, page, Math.Max(1, pdf_document.PageCount));

                    try
                    {
                        Logging.Info(msg);

                        // Add the header for the page
                        {
                            Bold bold = new Bold();
                            bold.Inlines.Add(String.Format("--- Page {0} ---", page));
                            flow_document.Blocks.Add(new Paragraph(bold));
                        }

                        StringBuilder sb    = new StringBuilder();
                        WordList      words = pdf_document.GetOCRText(page);
                        if (null != words)
                        {
                            Word last_word = null;

                            foreach (Word word in words)
                            {
                                // Add a newline if we seem to have a linebreak
                                if (
                                    (
                                        (null != last_word && last_word.Left > word.Left) &&
                                        (word.Top > last_word.Top + 2 * last_word.Height) &&
                                        (word.Text.Length > 0 && !Char.IsLower(word.Text[0]))
                                    )
                                    ||
                                    (
                                        (null != last_word && last_word.Left > word.Left) &&
                                        (word.Top > last_word.Top + 2.5 * last_word.Height)
                                    )
                                    )
                                {
                                    sb.Append("\n\n");
                                }

                                sb.Append(word.Text);
                                sb.Append(' ');

                                last_word = word;
                            }
                        }
                        else
                        {
                            sb.AppendLine(String.Format("OCR text is not yet ready for page {0}.  Please try again in a few minutes.", page));
                        }

                        // Add the text from the page
                        {
                            Paragraph paragraph = new Paragraph();
                            paragraph.Inlines.Add(sb.ToString());
                            flow_document.Blocks.Add(paragraph);
                        }

                        // Add the images from the page
                        PdfPageBase pdf_page = doc.Pages[page - 1];
                        Image[]     images   = pdf_page.ExtractImages();

                        if (null != images)
                        {
                            foreach (Image image in images)
                            {
                                BitmapSource bitmap_source = BitmapImageTools.FromImage(image);

                                System.Windows.Controls.Image image_to_render = new System.Windows.Controls.Image();
                                image_to_render.Source = bitmap_source;
                                BlockUIContainer image_container = new BlockUIContainer(image_to_render);
                                Figure           floater         = new Figure(image_container);
                                floater.HorizontalAnchor = FigureHorizontalAnchor.PageLeft;
                                floater.WrapDirection    = WrapDirection.None;
                                floater.Width            = new FigureLength(image.Width);

                                {
                                    Paragraph paragraph = new Paragraph();
                                    paragraph.Inlines.Add(floater);
                                    flow_document.Blocks.Add(paragraph);
                                }
                            }
                        }

                        // Add the annotations from the page
#if false
                        {
                            Logging.Debug("This PDF has {0} annotations", pdf_page.Annotations.Count);
                            Logging.Debug("The size of the page is {0}", pdf_page.Size);
                            foreach (var annotation in pdf_page.Annotations)
                            {
                                PdfLoadedUriAnnotation uri_annotation = annotation as PdfLoadedUriAnnotation;
                                if (null != uri_annotation)
                                {
                                    Logging.Debug("There is a URL to '{0}' with text '{1}' with bounds '{2}'",
                                                  uri_annotation.Uri,
                                                  uri_annotation.Bounds,
                                                  uri_annotation.Text,
                                                  null);
                                }
                            }
                        }
#endif
                    }
                    catch (Exception ex)
                    {
                        Logging.Error(ex, "There was a problem exporting page {0}", page);
                    }
                }
            }

            StatusManager.Instance.UpdateStatus("ExportToWord", "Finished exporting");

            return(flow_document);
        }