/// <summary> /// Will only be triggered if the AllowDrop-Property is set to true /// </summary> /// <param name="e"></param> protected override void OnDrop(DragEventArgs e) { base.OnDrop(e); if (e.Data.GetDataPresent(DataFormats.FileDrop)) { var filenames = (string[])e.Data.GetData(DataFormats.FileDrop); var filename = filenames.FirstOrDefault(); if (filename != null && File.Exists(filename)) { string pw = null; if (MuPdfWrapper.NeedsPassword(new FileSource(filename))) { if (this.PasswordRequired == null) { return; } var args = new PasswordRequiredEventArgs(); this.PasswordRequired(this, args); if (args.Cancel) { return; } pw = args.Password; } this.OpenFile(filename, pw); } } }
public CoverImage GenerateCoverImageFromPdf(Book book) { // If Book not found then return null; if (!File.Exists(book.BookFile.FullPathAndFileNameWithExtension)) { return(CoverImageFactory.CreateEmpty()); } var coverImage = CoverImageFactory.CreateNew(); using (var img = MuPdfWrapper.ExtractPage(new FileSource(book.BookFile.FullPathAndFileNameWithExtension), 1) ) { img.Save(coverImage.FullPathAndFileNameWithExtension); } if (book.Id == 0) { coverImage.EntityState = EntityState.Added; } else { coverImage.Id = book.CoverImage.Id; coverImage.EntityState = EntityState.Modified; } return(coverImage); }
public void Open(IPdfSource source, string password = null) { var pw = password; if (this.PasswordRequired != null && MuPdfWrapper.NeedsPassword(source) && pw == null) { var e = new PasswordRequiredEventArgs(); this.PasswordRequired(this, e); if (e.Cancel) { return; } pw = e.Password; } this.LoadPdf(source, pw); this.CurrentSource = source; this.CurrentPassword = pw; if (this.PdfLoaded != null) { this.PdfLoaded(this, EventArgs.Empty); } }
private static void convertPdfToImg() { for (CurPage = 1; CurPage <= PagesCount; CurPage++) { System.Drawing.Bitmap pageImage = MuPdfWrapper.ExtractPage(pdfSource, CurPage, Precision); pageImage.Save(outputDir + "\\" + CurPage + ".png"); } }
private void LoadPdf(IPdfSource source, string password) { var pageBounds = MuPdfWrapper.GetPageBounds(source, this.Rotation, password); this.pageRowBounds = CalculatePageRowBounds(pageBounds, this.ViewType); this.TotalPages = pageBounds.Length; this.innerPanel.Load(source, password); }
private void LoadPdfFile(string pdfFilename) { var pageBounds = MuPdfWrapper.GetPageBounds(pdfFilename, this.Rotation); this.pageRowBounds = CalculatePageRowBounds(pageBounds, this.ViewType); this.TotalPages = pageBounds.Length; this.innerPanel.Load(pdfFilename); }
public int FetchCount() { if (count == -1) { count = MuPdfWrapper.CountPages(pdfSource, this.password); } return(count); }
public int FetchCount() { if (count == -1) { count = MuPdfWrapper.CountPages(pdfFilename); } return(count); }
public static byte[] TryCreatePdfThumb(string pdfPathName) { var src = new FileSource(pdfPathName); using (var ms = new MemoryStream()) { using (Bitmap bmp = MuPdfWrapper.ExtractPage(src, 0)) { bmp.Save(ms, ImageFormat.Jpeg); } return(ms.ToArray()); } }
public IList <IEnumerable <PdfImage> > FetchRange(int startIndex, int count) { var imagesPerRow = this.Settings.ImagesPerRow; var viewType = this.Settings.ViewType; startIndex = (startIndex * imagesPerRow) + 1; if (preFetch) { count = count * imagesPerRow; } if (viewType == ViewType.BookView) { if (startIndex == 1) { count = Math.Min(this.totalPages, preFetch ? (1 /*first page*/ + imagesPerRow) : 0); } else { startIndex--; } } var end = Math.Min(FetchCount(), startIndex + count - 1); var list = new List <IEnumerable <PdfImage> >(); var rowList = new List <PdfImage>(imagesPerRow); var offset = viewType == ViewType.BookView ? 1 : 0; for (int i = Math.Min(FetchCount(), startIndex); i <= Math.Min(FetchCount(), Math.Max(startIndex, end)); i++) { var margin = new Thickness(0, 0, this.Settings.HorizontalOffsetBetweenPages, 0); using (var bmp = MuPdfWrapper.ExtractPage(pdfFilename, i, this.Settings.ZoomFactor)) { if (Settings.Rotation != ImageRotation.None) { var flipType = System.Drawing.RotateFlipType.Rotate90FlipNone; if (Settings.Rotation != ImageRotation.Rotate90) { flipType = Settings.Rotation == ImageRotation.Rotate180 ? System.Drawing.RotateFlipType.Rotate180FlipNone : System.Drawing.RotateFlipType.Rotate270FlipNone; } bmp.RotateFlip(flipType); } var bms = bmp.ToBitmapSource(); // Freeze bitmap to avoid threading problems when using AsyncVirtualizingCollection, // because FetchRange is NOT called from the UI thread bms.Freeze();
/// <summary> /// Will only be triggered if the AllowDrop-Property is set to true /// </summary> /// <param name="e"></param> protected override void OnDrop(DragEventArgs e) { base.OnDrop(e); if (!e.Data.GetDataPresent(DataFormats.FileDrop)) { return; } var filenames = (string[])e.Data.GetData(DataFormats.FileDrop); if (filenames == null) { return; } var filename = filenames.FirstOrDefault(); if (filename != null && File.Exists(filename)) { string pw = null; if (MuPdfWrapper.NeedsPassword(new FileSource(filename))) { if (this.PasswordRequired == null) { return; } var args = new PasswordRequiredEventArgs(); this.PasswordRequired(this, args); if (args.Cancel) { return; } pw = args.Password; } try { this.OpenFile(filename, pw); } catch (Exception ex) { MessageBox.Show(string.Format("An error occured: " + ex.Message)); } } }
public void AddPdf(FileSource fs) { isPdfPresent = true; var mainPanel = new MoonPdfPanel(); mainPanel.InitializeComponent(); mainPanel.Open(fs); var newSlidesNumber = MuPdfWrapper.CountPages(fs); for (int i = 0; i < newSlidesNumber; i++) { if (i < slidesList.Count) { // the slide was already created slidesList[i].AddPdfSlide(fs); } else { // we have to create new, empty slide slidesList.Add(new Slide(slidesList.Count, this)); slidesList.Last().AddPdfSlide(fs); panel.Children.Add(slidesList.Last().Grid); } } // we have to delete images from previous PDF and empty slides at the end of note bool stillDeleting = true; // have we met only empty slide for (int i = slidesList.Count - 1; i >= newSlidesNumber; i--) { // removing old PDF slidesList[i].RemovePdfSlide(); if (slidesList[i].Text == "") { if (stillDeleting) { // we can delete it from StackPanel RemoveLastSlide(); } } else { stillDeleting = false; } } isSaved = false; }
private void LoadPdf(IPdfSource source, string password) { var pageBounds = MuPdfWrapper.GetPageBounds(source, this.Rotation, password); this.pageRowBounds = CalculatePageRowBounds(pageBounds, this.ViewType); this.TotalPages = pageBounds.Length; var ue = this.innerPanel as FrameworkElement; if (ue != null && !ue.IsLoaded) { ue.UpdateLayout(); } this.innerPanel.Load(source, password); }
public static void Convert() { if (!File.Exists(inputFile)) { MessageBox.Show("File not found!"); return; } pdfSource = new FileSource(inputFile); PagesCount = MuPdfWrapper.CountPages(pdfSource); Thread thread = new Thread(convertPdfToImg); thread.Start(); }
public IList <IEnumerable <PdfImage> > FetchRange(int startIndex, int count) { var imagesPerRow = this.Settings.ImagesPerRow; var viewType = this.Settings.ViewType; startIndex = (startIndex * imagesPerRow) + 1; if (preFetch) { count = count * imagesPerRow; } if (viewType == ViewType.BookView) { if (startIndex == 1) { count = Math.Min(this.totalPages, preFetch ? (1 /*first page*/ + imagesPerRow) : 0); } else { startIndex--; } } var end = Math.Min(FetchCount(), startIndex + count - 1); var list = new List <IEnumerable <PdfImage> >(); var rowList = new List <PdfImage>(imagesPerRow); var offset = viewType == ViewType.BookView ? 1 : 0; for (int i = Math.Min(FetchCount(), startIndex); i <= Math.Min(FetchCount(), Math.Max(startIndex, end)); i++) { var margin = new Thickness(0, 0, this.Settings.HorizontalOffsetBetweenPages, 0); using (var bmp = MuPdfWrapper.ExtractPage(pdfSource, i, this.Settings.ZoomFactor, this.password)) { if (Settings.Rotation != ImageRotation.None) { var flipType = System.Drawing.RotateFlipType.Rotate90FlipNone; if (Settings.Rotation != ImageRotation.Rotate90) { flipType = Settings.Rotation == ImageRotation.Rotate180 ? System.Drawing.RotateFlipType.Rotate180FlipNone : System.Drawing.RotateFlipType.Rotate270FlipNone; } bmp.RotateFlip(flipType); } var bms = bmp.ToBitmapSource(); // Freeze bitmap to avoid threading problems when using AsyncVirtualizingCollection, // because FetchRange is NOT called from the UI thread bms.Freeze(); if ((i == 1 && viewType == ViewType.BookView) || (i + offset) % 2 == 0) { margin.Right = 0; // set right margin to zero for first page and for all pages that are on the right side } var img = new PdfImage { ImageSource = bms, Margin = margin }; // if first page and viewtype bookview, add the first page and continue with next if (viewType == ViewType.BookView && i == 1) { list.Add(new[] { img }); continue; } rowList.Add(img); } // if all images per row were added or the end of the pdf is reached, add the remaining PdfImages from rowList to the final list if (rowList.Count % imagesPerRow == 0 || i == end) { list.Add(rowList); if (i == end && rowList.Count % imagesPerRow != 0) { var last = rowList.Last(); last.Margin = new Thickness(0); } if (i < end) { rowList = new List <PdfImage>(imagesPerRow); } } } return(list); }
public void AddPdfSlide(FileSource fs) { image = BitmapHelper.ResizeBitmapToWidth(MuPdfWrapper.ExtractPage(fs, nr + 1), 400); AddPdfImageControl(); }
internal async Task ShowProgressDialogAddLessonShare(string title, string message, string lessonName, byte[] bytePDF, bool justSave) { var mainWindow = (Application.Current.MainWindow as MainWindow); var controller = await mainWindow.ShowProgressAsync(title, message); controller.SetCancelable(true); var tokenSource = new CancellationTokenSource(); var token = tokenSource.Token; await Task.Run(async() => //Task.Run automatically unwraps nested Task types! { // Control the name var baseName = lessonName; var i = 1; while (Directory.Exists(Data.GetLessonFolder(lessonName))) { lessonName = baseName + " " + i++; } // Initial model data var lessonFolder = Data.GetLessonFolder(lessonName); var thumbsFolder = Data.GetThumbsFolder(lessonName); var pagesFolder = Data.GetPagesFolder(lessonName); var pdfPath = Data.GetPdfFile(lessonName); Directory.CreateDirectory(lessonFolder); Directory.CreateDirectory(thumbsFolder); Directory.CreateDirectory(pagesFolder); File.WriteAllBytes(pdfPath, bytePDF); var pdf = new FileSource(pdfPath); MuPdfWrapper.GeneratePagesAtHeight(pdf, thumbsFolder, 100); MuPdfWrapper.GeneratePagesAtScale(pdf, pagesFolder, 1.0f); var pageCount = MuPdfWrapper.CountPages(pdf); if (!justSave) { Data.Instance.Lesson = new Lesson(lessonName, pageCount); await Application.Current.Dispatcher.BeginInvoke( new Action( () => { if (!controller.IsCanceled) { Catalog.Instance.ReplaceAndGoTo("MainPage"); } } ), DispatcherPriority.Normal ); } else { await Application.Current.Dispatcher.BeginInvoke( new Action( () => { Data.Instance.Lesson = new Lesson(lessonName, pageCount); Save(); if (!controller.IsCanceled && Catalog.Instance.CurrentPageName != "MainPage") { Catalog.Instance.ReplaceAndGoTo("HomePage"); } } ), DispatcherPriority.Normal ); } await controller.CloseAsync(); }); if (controller.IsCanceled) { try { var directory = Data.GetLessonFolder(lessonName); Directory.Delete(directory, true); } catch (IOException e) { MessageBox.Show(e.Message); } } else { // Save lesson just after its creation (Application.Current.MainWindow.DataContext as NineManagement).SaveFile.Execute(null); } CloseFlyout(); }