public void AddAnnotations() { try { for (nint i = 0; i < (nint)this.Document.PageCount; i++) { Page page = BooksOnDeviceAccessor.GetPage(book.ID, i + 1); if (page != null) { Annotation annotation = BooksOnDeviceAccessor.GetAnnotation(book.ID, page.ID); if (annotation != null) { Dictionary <String, PSPDFInkAnnotation> dictionary = AnnotationsDataAccessor.GenerateAnnDictionary((nuint)i, annotation); if (dictionary != null) { List <PSPDFAnnotation> annList = new List <PSPDFAnnotation>(); foreach (KeyValuePair <String, PSPDFInkAnnotation> item in dictionary) { annList.Add(item.Value); } this.Document.AddAnnotations(annList.ToArray()); } } } } } catch (Exception ex) { Logger.WriteLineDebugging("CustomPSPDFViewController - AddAnnotations: {0}", ex.ToString()); } }
bool GenerateImage(Dictionary <Page, eBriefingMobile.Annotation> dict1, Dictionary <Page, List <eBriefingMobile.Note> > dict2) { try { if (pageList != null) { nuint totalImageSize = 0; List <Note> notes = new List <eBriefingMobile.Note> (); foreach (var page in pageList) { String localPath = DownloadedFilesCache.BuildCachedFilePath(page.URL); var printItemDict = new Dictionary <UIImage, List <Note> >(); if (!String.IsNullOrEmpty(localPath)) { CGPDFDocument pdfDoc = CGPDFDocument.FromFile(localPath); if (pdfDoc != null) { CGPDFPage pdfPage = pdfDoc.GetPage(1); if (pdfPage != null) { CGRect pageRect = pdfPage.GetBoxRect(CGPDFBox.Media); UIImage pdfImg = ImageHelper.PDF2Image(pdfPage, pageRect.Width, scale); // Add annotation if option selected if (dict1.ContainsKey(page)) { Annotation annotation = dict1 [page]; if (annotation != null) { Dictionary <String, PSPDFInkAnnotation> coordinateDict = AnnotationsDataAccessor.GenerateAnnDictionary((UInt32)page.PageNumber - 1, annotation); if (coordinateDict != null) { foreach (KeyValuePair <String, PSPDFInkAnnotation> item in coordinateDict) { // Create full size annotation UIImage annImg = ImageHelper.DrawPSPDFAnnotation(item.Key, item.Value); if (annImg != null) { // Scale down the annotation image annImg = annImg.Scale(new CGSize(pdfImg.Size.Width, pdfImg.Size.Height)); // Overlap pdfImg and annImg pdfImg = ImageHelper.Overlap(pdfImg, annImg, CGPoint.Empty, CGPoint.Empty, scale); } } } } } // Create image from text bool printNote = false; UIImage noteImg = null; if (dict2.ContainsKey(page) && dict2 [page] != null) { printNote = true; notes = dict2 [page]; // Create image from text //noteImg = ImageHelper.Text2Image(_notesText, pdfImg.Size); } else { notes = null; } // Scale down and add to canvas // Used 900 and 1200 because couldn't control the paper margin // if (Orientation == ORIENTATION.PORTRAIT) // { // //if (printNote) // { // //pdfImg = ImageHelper.Scale(pdfImg, 500); // //pdfImg=ImageHelper.MaxResizeImage(pdfImg,1000,scale); // //pdfImg = ImageHelper.Add2Canvas(pdfImg, new CGPoint(0, (1024 / 2) - (pdfImg.Size.Height / 2)), scale); // // // Overlap pdfImg and noteImg // //pdfImg = ImageHelper.Overlap(pdfImg, noteImg, CGPoint.Empty, new CGPoint(500, 0), scale); // } // //else // { // //pdfImg=ImageHelper.MaxResizeImage(pdfImg,1000,scale); // //pdfImg = ImageHelper.Scale(pdfImg, 900); // //pdfImg = ImageHelper.Add2Canvas(pdfImg, new CGPoint((768 / 2) - (pdfImg.Size.Width / 2), (1024 / 2) - (pdfImg.Size.Height / 2)), scale); // } // } // else // { // //if (printNote) // { // //pdfImg=ImageHelper.MaxResizeImage(pdfImg,500,scale); // //pdfImg = ImageHelper.Scale(pdfImg, 500); // // pdfImg = ImageHelper.Add2Canvas(pdfImg, new CGPoint(0,0), scale*2, UIInterfaceOrientation.LandscapeLeft); // // Overlap pdfImg and noteImg // //pdfImg = ImageHelper.Overlap(pdfImg, noteImg, CGPoint.Empty, new CGPoint(756, 0), scale); // } // //else // { // //pdfImg=ImageHelper.MaxResizeImage(pdfImg,1000,scale); // //pdfImg = ImageHelper.Scale(pdfImg, 500); // ///pdfImg = ImageHelper.Add2Canvas(pdfImg, new CGPoint((1024 / 2) - (pdfImg.Size.Width / 2), (768 / 2) - (pdfImg.Size.Height / 2)), scale*2, UIInterfaceOrientation.LandscapeLeft); // } // // // Rotate canvas // //pdfImg = ImageHelper.Rotate(pdfImg); // } // Save // if (printItems == null) // { // printItems = new List<UIImage>(); // } // printItems.Add(pdfImg); if (dict == null) { dict = new Dictionary <int, Dictionary <UIImage, List <Note> > > (); } if (pdfImg != null) { printItemDict.Add(pdfImg, notes); dict.Add(page.PageNumber, printItemDict); var pngImage = pdfImg.AsPNG(); totalImageSize = pngImage.Length + totalImageSize; Console.WriteLine("Img : " + totalImageSize.ToString()); //image dispose pdfImg = null; if (CheckReachMemoryLimit(totalImageSize)) { PagesNum = dict.Count - 1; dict.Clear(); dict = null; return(false); } } } } } } PagesNum = dict.Count; return(true); } return(false); } catch (Exception ex) { return(false); } }
private void GenerateThumbnail(String bookID, String pageID, String text, bool bookmark = false) { try { // activityIndicator UIActivityIndicatorView activityIndicator = new UIActivityIndicatorView(); activityIndicator.ActivityIndicatorViewStyle = UIActivityIndicatorViewStyle.White; activityIndicator.StartAnimating(); activityIndicator.HidesWhenStopped = true; activityIndicator.Frame = new RectangleF((this.Frame.Width / 2) - 10, (this.Frame.Height / 2) - 10, 20, 20); this.AddSubview(activityIndicator); // Generate pdf thumbnail Page page = null; Annotation annotation = null; BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += delegate { page = BooksOnDeviceAccessor.GetPage(bookID, pageID); if (String.IsNullOrEmpty(text)) { annotation = BooksOnDeviceAccessor.GetAnnotation(bookID, pageID); } }; worker.RunWorkerCompleted += delegate { this.InvokeOnMainThread(delegate { activityIndicator.StopAnimating(); if (page != null) { String localPath = DownloadedFilesCache.BuildCachedFilePath(page.URL); if (!String.IsNullOrEmpty(localPath)) { CGPDFDocument pdfDoc = CGPDFDocument.FromFile(localPath); if (pdfDoc != null) { CGPDFPage pdfPage = pdfDoc.GetPage(1); if (pdfPage != null) { UIImage pdfImg = PDFConverter.Transform2Image(pdfPage, this.Frame.Width); // pageView UIImageView pageView = new UIImageView(); pageView.Frame = new RectangleF(0, (this.Frame.Height / 2) - (pdfImg.Size.Height / 2), pdfImg.Size.Width, pdfImg.Size.Height); // If this is annotation thumbnail, draw annotation overlay on top of pdf if (annotation != null) { Dictionary <String, PSPDFInkAnnotation> dictionary = AnnotationsDataAccessor.GenerateAnnDictionary((UInt32)page.PageNumber - 1, annotation); if (dictionary != null) { foreach (KeyValuePair <String, PSPDFInkAnnotation> item in dictionary) { // Create full size annotation UIImage annImg = DrawAnnotation(item.Key, item.Value); if (annImg != null) { // Scale down the annotation image annImg = annImg.Scale(new SizeF(pdfImg.Size.Width, pdfImg.Size.Height)); // Overlap pdfImg and annImg pdfImg = Overlap(pdfImg, annImg); } } } } pageView.Image = pdfImg; this.AddSubview(pageView); // THIS IS REQUIRED TO SKIP iCLOUD BACKUP SkipBackup2iCloud.SetAttribute(localPath); // Add ribbon if this is bookmark thumbnail if (bookmark) { UIImageView ribbon = new UIImageView(); ribbon.Image = UIImage.FromBundle("Assets/Buttons/bookmark_solid.png"); ribbon.Frame = new RectangleF(pageView.Frame.Right - 35, pageView.Frame.Y, 25, 33.78f); this.AddSubview(ribbon); } // Do not add text if this is annotation thumbnail if (!String.IsNullOrEmpty(text)) { // titleLabel UILabel titleLabel = new UILabel(); titleLabel.Frame = new RectangleF(0, pageView.Frame.Bottom + 4, this.Frame.Width, 42); titleLabel.Font = UIFont.SystemFontOfSize(16f); titleLabel.BackgroundColor = UIColor.Clear; titleLabel.TextColor = eBriefingAppearance.DarkGrayColor; titleLabel.Lines = 2; titleLabel.LineBreakMode = UILineBreakMode.TailTruncation; titleLabel.TextAlignment = UITextAlignment.Center; titleLabel.Text = text; this.AddSubview(titleLabel); } } } } } }); }; worker.RunWorkerAsync(); } catch (Exception ex) { Logger.WriteLineDebugging("ThumbnailView - GenerateThumbnail: {0}", ex.ToString()); } }
async private void GenerateThumbnail(String bookID, String pageID, String text, bool bookmark = false) { try { // spinner RTSpinKitView spinner = eBriefingAppearance.GenerateBounceSpinner(); spinner.Center = this.Center; this.AddSubview(spinner); // Generate pdf thumbnail Page page = await eBriefingService.Run(() => BooksOnDeviceAccessor.GetPage(bookID, pageID)); Annotation annotation = null; if (String.IsNullOrEmpty(text)) { annotation = await eBriefingService.Run(() => BooksOnDeviceAccessor.GetAnnotation(bookID, pageID)); } spinner.StopAnimating(); if (page != null) { String localPath = DownloadedFilesCache.BuildCachedFilePath(page.URL); if (!String.IsNullOrEmpty(localPath)) { CGPDFDocument pdfDoc = CGPDFDocument.FromFile(localPath); if (pdfDoc != null) { CGPDFPage pdfPage = pdfDoc.GetPage(1); if (pdfPage != null) { UIImage pdfImg = ImageHelper.PDF2Image(pdfPage, this.Frame.Width, UIScreen.MainScreen.Scale); // pageView UIImageView pageView = new UIImageView(); pageView.Frame = new CGRect(0, (this.Frame.Height / 2) - (pdfImg.Size.Height / 2), pdfImg.Size.Width, pdfImg.Size.Height); // If this is annotation thumbnail, draw annotation overlay on top of pdf if (annotation != null) { Dictionary <String, PSPDFInkAnnotation> dictionary = AnnotationsDataAccessor.GenerateAnnDictionary((nuint)page.PageNumber - 1, annotation); if (dictionary != null) { foreach (KeyValuePair <String, PSPDFInkAnnotation> item in dictionary) { // Create full size annotation UIImage annImg = ImageHelper.DrawPSPDFAnnotation(item.Key, item.Value); if (annImg != null) { // Scale down the annotation image annImg = annImg.Scale(new CGSize(pdfImg.Size.Width, pdfImg.Size.Height)); // Overlap pdfImg and annImg pdfImg = ImageHelper.Overlap(pdfImg, annImg, CGPoint.Empty, CGPoint.Empty, UIScreen.MainScreen.Scale); } } } } pageView.Image = pdfImg; this.AddSubview(pageView); if (pdfImg.Size.Height < this.Frame.Height) { this.BackgroundColor = UIColor.Clear; } // THIS IS REQUIRED TO SKIP iCLOUD BACKUP SkipBackup2iCloud.SetAttribute(localPath); // Add ribbon if this is bookmark thumbnail if (bookmark) { UIImageView ribbon = new UIImageView(); ribbon.Image = UIImage.FromBundle("Assets/Buttons/bookmark_solid.png"); ribbon.Frame = new CGRect(pageView.Frame.Right - 35, pageView.Frame.Y, 25, 33.78f); this.AddSubview(ribbon); } // Do not add text if this is annotation thumbnail if (!String.IsNullOrEmpty(text)) { // titleLabel UILabel titleLabel = eBriefingAppearance.GenerateLabel(16); titleLabel.Frame = new CGRect(0, pageView.Frame.Bottom + 4, this.Frame.Width, 42); titleLabel.Lines = 2; titleLabel.LineBreakMode = UILineBreakMode.TailTruncation; titleLabel.TextAlignment = UITextAlignment.Center; titleLabel.Text = text; this.AddSubview(titleLabel); } } } } } } catch (Exception ex) { Logger.WriteLineDebugging("ThumbnailView - GenerateThumbnail: {0}", ex.ToString()); } }