//This method changes the Header text when necessary and loading the other pdf in the pdfviewer //TODO: this method should unload the documents from memory, will be fixed in the future private void Change_Frame(object sender, RoutedEventArgs e) { if (orologio.Text.Equals("Ωρολόγιο Πρόγραμμα")) { orologio.Text = "Πρόγραμμα Εξεταστικής"; combost.Visibility = Visibility.Collapsed; datepick.Visibility = Visibility.Visible; cb1.SelectedIndex = 0; cb2.SelectedIndex = 0; pdfViewer.GotoPage(1); // Load the Byte array PdfLoadedDocument loadedDocument = new PdfLoadedDocument(contentBytesx2); // Display the PDF document in PdfViewer pdfViewer.LoadDocument(loadedDocument); } else { //closing orologio.Text = "Ωρολόγιο Πρόγραμμα"; datepick.Visibility = Visibility.Collapsed; combost.Visibility = Visibility.Visible; exetastiki.Date = null; pdfViewer.GotoPage(1); // Load the Byte array PdfLoadedDocument loadedDocument = new PdfLoadedDocument(contentBytesx1); // Display the PDF document in PdfViewer pdfViewer.LoadDocument(loadedDocument); loadedDocument.Dispose(); } }
private void CreateTxtFromPDF(string filename) { //ScanPageStatus scanPageStatus = new ScanPageStatus(); string tesseractPath = Path.Combine(AssemblyDirectory(), GetConstants.TesseractBinaries()); string tesseractData = Path.Combine(AssemblyDirectory(), GetConstants.TesseractData()); try { using (OCRProcessor processor = new OCRProcessor(tesseractPath)) { //Stream pdfStream2 = filename; // FileUpload1.PostedFile.InputStream; // Read in PDF image file, and convert to searchable TXT pdf file PdfLoadedDocument IDoc = new PdfLoadedDocument(filename); processor.Settings.Language = Languages.English; processor.Settings.Performance = Performance.Slow; // var zz = processor.Settings.Performance; //string tessdata = tesseractPath + @"\\Tessdata\\"; processor.PerformOCR(IDoc, tesseractData); string outFileName = Path.GetFileName(filename) + "_OCR" + Path.GetExtension(filename); string homePath = Path.GetDirectoryName(Path.GetDirectoryName(filename)); string savePath = Path.Combine(homePath, GetConstants.Directory("out"), outFileName); // If file exists - delete it first. if (File.Exists(savePath)) { File.SetAttributes(savePath, FileAttributes.Normal); File.Delete(savePath); } IDoc.Save(savePath); IDoc.Close(true); IDoc.Dispose(); scanPageStatus.scannedFileName = savePath; scanPageStatus.rc = 0; scanPageStatus.statusMessage = String.Format("File {0} scanned and saved to {1}", filename, scanPageStatus.scannedFileName); } } catch (Exception ex) { scanPageStatus.scannedFileName = ""; scanPageStatus.statusMessage = String.Format("Error {0} when running OCR on source file {1}", ex, filename); scanPageStatus.rc = -1; } // return scanPageStatus; }
public static async void SaveChangesFile(string text) { PdfLoadedDocument loadedDocument = new PdfLoadedDocument(); await loadedDocument.OpenAsync(openFile).ConfigureAwait(true); loadedDocument.Pages.RemoveAt(0); Syncfusion.Pdf.PdfPage page = (Syncfusion.Pdf.PdfPage)loadedDocument.Pages.Add(); PdfGraphics graphics = page.Graphics; Syncfusion.Pdf.Graphics.PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20); graphics.DrawString(text, font, PdfBrushes.Black, new PointF(0, 0)); await loadedDocument.Save().ConfigureAwait(true); loadedDocument.Dispose(); }
public static async Task <string> Read(StorageFile openFile) { SaveFilePdf.openFile = openFile; string extractedText = ""; if (openFile != null) { PdfLoadedDocument loadedDocument = new PdfLoadedDocument(); await loadedDocument.OpenAsync(openFile).ConfigureAwait(true); PdfPageBase page = loadedDocument.Pages[0]; extractedText = page.ExtractText(); loadedDocument.Close(true); loadedDocument.Dispose(); var mru = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList; string mruToken = mru.Add(openFile, "Pdf file"); } return(extractedText); }
public string SaveRightMovePdf(RightMoveModel rightMoveModel) { try { PdfLoadedDocument loadedDocument = new PdfLoadedDocument(new FileStream(templatePath, FileMode.Open)); if (!Directory.Exists(archiveFolder)) { Directory.CreateDirectory(archiveFolder); } if (loadedDocument.PageCount > 0) { PdfLoadedPage pdfLoadedPage = loadedDocument.Pages[1] as PdfLoadedPage; PdfTemplate pdfTemplate = new PdfTemplate(900, 600); PdfFont pdfFont = new PdfStandardFont(PdfFontFamily.Helvetica, 15); PdfBrush brush = new PdfSolidBrush(SfDrawing.Color.Black); byte[] imageBytes = new WebClient().DownloadData(rightMoveModel.PropertyMainPicture); Stream imageStream = new MemoryStream(imageBytes); pdfTemplate.Graphics.DrawString($"Property Address: {rightMoveModel.PropertyAddress}", pdfFont, brush, 100, 30); pdfTemplate.Graphics.DrawString($"Property Type: {rightMoveModel.PropertyType}", pdfFont, brush, 100, 50); pdfTemplate.Graphics.DrawString($"PropertyPrice: {rightMoveModel.PropertyPrice} ", pdfFont, brush, 100, 70); pdfTemplate.Graphics.DrawImage(PdfImage.FromStream(imageStream), new SfDrawing.PointF(100, 100), new SfDrawing.SizeF(400, 400)); pdfLoadedPage.Graphics.DrawPdfTemplate(pdfTemplate, SfDrawing.PointF.Empty); string rawName = rightMoveModel .PropertyUrl .Replace("/", "") .Replace("-", "") .Replace(".", "") .Replace(":", "") .Replace("//", ""); string fileName = Regex.Match(rawName, @"(\d+(?:\.\d{1,2})?)").Value; PdfDocument propertyHeatMapPdfDocument = htmlConverter.Convert(rightMoveModel.PropertyHeatHtmlString, string.Empty); PdfDocument homeCoUKHtmlPdfDocument = htmlConverter.Convert(rightMoveModel.HomeCoUKHtmlString, string.Empty); string tempPropertyHeatMap = Path.Combine(tempFolder, $"propertyHeatMap{fileName}.pdf"); using (FileStream propertyHeatMapStream = new FileStream(tempPropertyHeatMap, FileMode.Create)) { propertyHeatMapPdfDocument.Save(propertyHeatMapStream); propertyHeatMapPdfDocument.Close(true); propertyHeatMapPdfDocument.Dispose(); propertyHeatMapStream.Close(); propertyHeatMapStream.Dispose(); } string tempHomeCoUK = Path.Combine(tempFolder, $"homeCoUK{fileName}.pdf"); using (FileStream homeCoUKHtmlPdfStream = new FileStream(tempHomeCoUK, FileMode.Create)) { homeCoUKHtmlPdfDocument.Save(homeCoUKHtmlPdfStream); homeCoUKHtmlPdfDocument.Close(true); homeCoUKHtmlPdfDocument.Dispose(); homeCoUKHtmlPdfStream.Close(); homeCoUKHtmlPdfStream.Dispose(); } using (FileStream propertyHeatMapReadStream = new FileStream(tempPropertyHeatMap, FileMode.Open)) { PdfLoadedDocument tempPropertyHeatMapDocument = new PdfLoadedDocument(propertyHeatMapReadStream); loadedDocument.ImportPage(tempPropertyHeatMapDocument, 0); propertyHeatMapReadStream.Close(); propertyHeatMapReadStream.Dispose(); } using (FileStream homeCoUKReadStream = new FileStream(tempHomeCoUK, FileMode.Open)) { PdfLoadedDocument tempHomeCoUKDocument = new PdfLoadedDocument(homeCoUKReadStream); loadedDocument.ImportPage(tempHomeCoUKDocument, 0); homeCoUKReadStream.Close(); homeCoUKReadStream.Dispose(); } string savePath = Path.Combine(archiveFolder, $"{fileName}.pdf"); using (FileStream saveStream = new FileStream(savePath, FileMode.Create)) { loadedDocument.Save(saveStream); loadedDocument.Close(true); loadedDocument.Dispose(); saveStream.Close(); saveStream.Dispose(); } return($"file successfully saved at: {savePath}"); } else { Console.WriteLine("Invalid PDF file"); return($"Invalid PDF file"); } } catch (Exception ex) { Console.WriteLine($"Unable to save the file. {ex.Message}"); return($"Unable to save the file"); } }
private async void Extract_Click(object sender, RoutedEventArgs e) { if (_cts != null) { _cts.Cancel(); return; } try { _cts = new CancellationTokenSource(); ChangeEnability(false); var source = SourceTextBox.Text; var destination = DestinationTextBox.Text; if (string.IsNullOrWhiteSpace(source) || !Directory.Exists(source)) { MessageBox.Show("Please specify a valid source folder!"); return; } else if (string.IsNullOrWhiteSpace(destination)) { MessageBox.Show("Please specify destination folder!"); return; } var skipDuplicateFiles = SkipRadioButton.IsChecked == true; if (!Directory.Exists(source)) { MessageBox.Show("This folder doesn't exist!"); return; } var pdfs = Directory.EnumerateFiles(source, "*.pdf", SearchOption.AllDirectories).ToList(); progressBar.Value = 0; int processed = 0; statusTextBlock.Text = "Starting..."; await Task.Run(async() => { try { foreach (var pdf in pdfs) { _cts.Token.ThrowIfCancellationRequested(); var name = pdf.Split('\\', '/').Last(); var destinationFile = Path.Combine(destination, name + ".txt"); if (skipDuplicateFiles && File.Exists(destinationFile)) { continue; } var builder = new StringBuilder(); PdfLoadedDocument loadedDocument = new PdfLoadedDocument(pdf); foreach (PdfLoadedPage page in loadedDocument.Pages) { builder.AppendLine(page.ExtractText()); } loadedDocument.Dispose(); var document = builder.ToString(); File.WriteAllText(Path.Combine(destination, name + ".txt"), document); Interlocked.Increment(ref processed); await Dispatcher.InvokeAsync(() => { progressBar.Value = (double)processed / pdfs.Count; statusTextBlock.Text = $"{processed} / {pdfs.Count} ({progressBar.Value * 100:N2}%)"; }); } } catch (OperationCanceledException) { } }); } catch (OperationCanceledException) { } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { ChangeEnability(true); statusTextBlock.Text = ""; progressBar.Value = 0; _cts = null; } }