UIViewController currentController = UIApplication.SharedApplication.KeyWindow.RootViewController;
while (currentController.PresentedViewController != null)
currentController = currentController.PresentedViewController;
UIView currentView = currentController.View;
QLPreviewController preview = new QLPreviewController();
QLPreviewItem item = new QLPreviewItemBundle(fileData.FileName, fileData.FilePath);
preview.DataSource = new PreviewControllerDS(item);
currentController.PresentViewController(preview, true, null);
Asked
Active
Viewed 793 times
-1

Vimal Patel
- 2,785
- 2
- 24
- 38

Abhishek Singh
- 31
- 7
1 Answers
0
To begin, create a class that inherits from QLPreviewControllerDataSource
.
public class QuickLookSource : QLPreviewControllerDataSource
{
List documents;
public QuickLookSource(List docs)
{
documents = docs;
}
public override nint PreviewItemCount(QLPreviewController controller)
{
return documents.Count;
}
public override IQLPreviewItem GetPreviewItem(QLPreviewController controller, nint index)
{
return new PreviewItem(documents[(int)index]);
}
}
public class PreviewItem : QLPreviewItem
{
NSUrl fileUrl;
public override NSUrl ItemUrl => fileUrl;
public override string ItemTitle => fileUrl.LastPathComponent;
public PreviewItem(string url)
{
fileUrl = NSUrl.FromFilename(url);
}
}
Then to present a preview, create a new QLPreviewController
and set DataSource
to you data source class. Since the controller can preview multiple files, make sure to set CurrentPreviewItemIndex
before navigating to the controller:
var previewController = new QLPreviewController();
previewController.DataSource = new QuickLookSource(source.Documents);
previewController.CurrentPreviewItemIndex = indexPath.Row;
NavigationController.PushViewController(previewController, true);
// You can present modally instead
// PresentViewController(previewController, true, null);

Junior Jiang
- 12,430
- 1
- 10
- 30
-
I have created this class which is inheriting "QLPreviewControllerDataSource". And the preview functionality is working perfectly fine. Now i want to edit that previewed pdf and add annotations, for that we need to create a new class which inherits "QLPreviewControllerDelegate", after that i am stuck now, what next to do i order to annote the pdf. – Abhishek Singh Feb 24 '21 at 04:50
-
@AbhishekSingh Sorry, I think `QLPreviewController` can not edit pdf file and add annotaions. – Junior Jiang Feb 24 '21 at 06:04
-
During WWDC 2019, the framework got a nice upgrade with the introduction of editing mode. QLPreviewItemEditingMode is the new addition to the framework that allows us to annotate images and PDFs and trim or convert videos easily. For Reference: https://medium.com/better-programming/edit-pdfs-images-and-videos-using-quick-look-in-ios-13-ad2131080587 – Abhishek Singh Feb 24 '21 at 06:11
-
@AbhishekSingh Oh, that's cool! I will check that. – Junior Jiang Feb 24 '21 at 06:37
-
If you could help me implementing this for Xamarin IOS, it will be a great help. – Abhishek Singh Feb 24 '21 at 07:13
-
@AbhishekSingh From shared link, do you set `preview.SetEditing(true, true);`? It should make the `previewController` be edited. – Junior Jiang Feb 24 '21 at 07:43
-
Yes i have added preview.SetEditing(true, true) before PresentViewController(previewController, true, null); Then I have created an EditingDocuments Class which inherits QLPreviewControllerDelegate. This class overrides below 3 methods:- 1.GetEditingMode 2. DidSaveEditedCopy 3. DidUpdateContents Now how can i use this class for editing pdf now? – Abhishek Singh Feb 24 '21 at 08:55
-
@AbhishekSingh Okey, could you share a sample project link here? I will have a try in local site to find the solution. – Junior Jiang Feb 24 '21 at 09:16
-
Yeah its done, I am able to edit the pdf's now. – Abhishek Singh Feb 24 '21 at 09:32
-
@AbhishekSingh Okey, you could update your solution in answer. Then it will be helpful for others who have the same problem. – Junior Jiang Feb 25 '21 at 03:24
-
Yes sure, i will. :) – Abhishek Singh Mar 03 '21 at 12:46