private void Navigate_OnClick(object sender, RoutedEventArgs e) { OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.Filter = "Pdf file| *.pdf"; bool? dialogOk = fileDialog.ShowDialog(); string filename = ""; if (dialogOk == true) { filename = fileDialog.FileName; var path = Path.GetDirectoryName(filename); try { var images = PdfImageExtractor.ExtractImages(filename); foreach (var name in images.Keys) { if (path != null) { images[name].Save(Path.Combine(path, name)); } } MessageBox.Show("Done"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } else { return; } }
public ActionResult ExtactImagesFromFile(EmpModel model) { try { string rootFolder = Directory.GetCurrentDirectory(); var imagePath = new List <FileDetails>(); Stream fileStream = model.files.OpenReadStream(); string outPutFilePath = rootFolder + "\\wwwroot\\ExtractedImages\\"; var images = PdfImageExtractor.ExtractImages(fileStream, model.files.FileName); var directory = System.IO.Path.GetDirectoryName(outPutFilePath); foreach (var name in images.Keys) { var obj = new FileDetails(); var imgPath = System.IO.Path.Combine(directory, name); images[name].Save(imgPath); obj.FilePath = Utility.ConvertImageURLToBase64(imgPath); obj.FileName = imgPath; imagePath.Add(obj); } ViewBag.ImageExtracted = "Image Extracted Succesfully in this path :- " + outPutFilePath; ViewBag.ImagePath = imagePath; } catch (Exception) { } return(View()); }
private static void RunApp() { if (!Directory.Exists(saveFolder)) { Directory.CreateDirectory(saveFolder); } var reader = new PdfReader(pdfPath); var raf = new RandomAccessFileOrArray(pdfPath); Console.WriteLine("PDF Found, getting text."); for (var i = 1; i < reader.NumberOfPages; i++) { PDFText.Add(PdfTextExtractor.GetTextFromPage(reader, i)); if (PdfImageExtractor.PageContainsImages(pdfPath, i)) { PDFImages.Add(PdfImageExtractor.ExtractImages(pdfPath, i)); } } Console.WriteLine(string.Format("Found {0} pages of text, from {1} pages in PDF", PDFText.Count, reader.NumberOfPages)); var saveFile = System.IO.Path.Combine(saveFolder, string.Format("{0}_Text.txt", pdfName)); Console.WriteLine(string.Format("Saving text to {0}", saveFile)); File.AppendAllLines(saveFile, PDFText); Console.WriteLine("Saving Images"); foreach (var dict in PDFImages) { foreach (var key in dict.Keys) { System.Drawing.Image img; if (dict.TryGetValue(key, out img)) { imageCount += dict.Count - 1; try { img.Save(System.IO.Path.Combine(saveFolder, key)); } catch (Exception e) { Console.WriteLine(e.Message); } } } } Console.WriteLine(string.Format("Saved {0} images in {1}", imageCount, saveFolder)); Console.WriteLine("...any key to continue..."); Console.ReadKey(); }
public void loadSignInfo(string pdf_filename) { PdfReader reader = new PdfReader(pdf_filename); Dictionary <string, System.Drawing.Image> dict = PdfImageExtractor.ExtractImages(pdf_filename); int sigIndex = 1; foreach (string key in dict.Keys) { System.Drawing.Image img = dict[key]; MemoryStream ms = new MemoryStream(); if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp)) { img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); } else if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Tiff)) { img.Save(ms, System.Drawing.Imaging.ImageFormat.Tiff); } else if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png)) { img.Save(ms, System.Drawing.Imaging.ImageFormat.Png); } SigObj sig = new SigObj(); ReadEncodedBitmapResult result = sig.ReadEncodedBitmap(ms.ToArray()); if (result == ReadEncodedBitmapResult.ReadEncodedBitmapOK) { //MessageBox.Show(sig.Who + " " + sig.Why + " " + sig.When); treeView1.BeginUpdate(); treeView1.Nodes.Add("Signature " + sigIndex); treeView1.Nodes[sigIndex - 1].Nodes.Add("Name: " + sig.Who); treeView1.Nodes[sigIndex - 1].Nodes.Add("Reason: " + sig.Why); treeView1.Nodes[sigIndex - 1].Nodes.Add("Timestamp: " + sig.When); treeView1.Nodes[sigIndex - 1].Nodes.Add("Digitizer: " + sig.get_AdditionalData(CaptData.CaptDigitizer)); treeView1.Nodes[sigIndex - 1].Nodes.Add("Digitizer Driver: " + sig.get_AdditionalData(CaptData.CaptDigitizerDriver)); treeView1.Nodes[sigIndex - 1].Nodes.Add("Machine OS: " + sig.get_AdditionalData(CaptData.CaptMachineOS)); treeView1.Nodes[sigIndex - 1].Nodes.Add("Network Card: " + sig.get_AdditionalData(CaptData.CaptNetworkCard)); treeView1.EndUpdate(); sigIndex = sigIndex + 1; } treeView1.ExpandAll(); } }
private void getSignatureFromPdf(string pdf_filename) { bool imagesFound = PdfImageExtractor.PageContainsImages(pdf_filename, 1); if (imagesFound) { Dictionary <string, System.Drawing.Image> dict = PdfImageExtractor.ExtractImages(pdf_filename, 1); foreach (string key in dict.Keys) { System.Drawing.Image img = dict[key]; img.Save("sign.png"); SigObj sig = new SigObj(); ReadEncodedBitmapResult result = sig.ReadEncodedBitmap("sign.png"); if (result == ReadEncodedBitmapResult.ReadEncodedBitmapOK) { MessageBox.Show(sig.Who + " " + sig.Why + " " + sig.When); } } } }
public async static void Run(Stream myBlob, string blobName, TraceWriter log) { log.Info($"C# Blob trigger function Processed blob\n Name:{blobName} \n Size: {myBlob.Length} Bytes"); log.Info($"Extracting images from the PDF"); List <Image> images = PdfImageExtractor.ExtractImages(myBlob, blobName, log); StringBuilder extractedText = new StringBuilder(); try { log.Info($"Extracting text from images"); VisionServiceClient visionServiceClient = new VisionServiceClient(VisionServiceSubscriptionKey); foreach (Image image in images) { using (var stream = new MemoryStream()) { image.Save(stream, System.Drawing.Imaging.ImageFormat.Png); stream.Seek(0, SeekOrigin.Begin); await ExtractTextAsync(visionServiceClient, stream, extractedText, log); } } if (extractedText.Length != 0) { SearchServiceClient serviceClient = new SearchServiceClient(SearchServiceName, new SearchCredentials(SearchServiceAPIKey)); ISearchIndexClient indexClient = serviceClient.Indexes.GetClient(IndexName); string documentId = HttpServerUtility.UrlTokenEncode(Encoding.UTF8.GetBytes(blobName)); log.Info($"Uploading document to Azure Search using ID: {documentId}"); UploadToAzureSeearch(indexClient, documentId, log, extractedText.ToString()); } } catch (Exception ex) { log.Info($"Error: {ex.Message}"); } }
static void Main() { var args = Environment.GetCommandLineArgs(); var searchPath = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(args[0]))), "Data"); foreach (var filename in Directory.GetFiles(searchPath, "*.pdf", SearchOption.TopDirectoryOnly)) { Console.WriteLine("Extracting images from {0}", Path.GetFileName(filename)); var images = PdfImageExtractor.ExtractImages(filename); Console.WriteLine("{0} images found.", images.Count); Console.WriteLine(); var directory = Path.GetDirectoryName(filename); foreach (var name in images.Keys) { images[name].Save(Path.Combine(directory, name)); } } Console.WriteLine("Done."); Console.ReadLine(); }