-3

I want to create or find the app which reads PDF file on Android with the preview option. So it puts the thumbnails of all pages on 1.

How can I do this on Xamarin with C#?

  • Here is a similar discussion for you.(https://stackoverflow.com/questions/41176549/open-pdf-file-in-xamarin-forms/41180688) – Junior Jiang Feb 07 '19 at 06:38

1 Answers1

2

Please follow these steps.

Step 1: You have to add the provider in AndroidManifest.xml under application tag

<application>
    <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="your package name.provider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
              android:name="android.support.FILE_PROVIDER_PATHS"
              android:resource="@xml/file_paths" />
          </provider>
     </application>

Step 2: Open file in PDF viewer from Assets folder

  private void OpenPdfInExternalApplication(string filename)
    {
        AssetManager assetManager = Activity.Assets;
        string fileName1 = filename + ".pdf";
        var file = new File(Activity.FilesDir, fileName1);

        Uri uri = null;
        Intent intent = new Intent(Intent.ActionView);
        try
        {
            // Get the input stream
            Stream input = assetManager.Open(fileName1);
            Stream output;
            if (Build.VERSION.SdkInt >= BuildVersionCodes.N)
              {
                output = Activity.OpenFileOutput(file.Name, FileCreationMode.Private);
                uri = FileProvider.GetUriForFile(Activity, Activity.PackageName + ".provider", file);
                 intent.SetDataAndType(uri, "application/pdf");
              }
              else
              {
                   output = Activity.OpenFileOutput(file.Name, FileCreationMode.WorldReadable);
                  intent.SetDataAndType(Uri.Parse("file://" + Activity.FilesDir + "/" + fileName),
                 "application/pdf");
              }
            input.CopyTo(output);
            intent.AddFlags(ActivityFlags.GrantPrefixUriPermission);
            intent.AddFlags(ActivityFlags.GrantReadUriPermission);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }

        try
        {
            StartActivity(intent);
        }
        catch (ActivityNotFoundException ex)
        {
            Toast.MakeText(Activity, ex.Message, ToastLength.Long).Show();
        }
    }
ramya br
  • 225
  • 2
  • 17
  • Thanks a lot! That what I want it's to do a cycle through all pages and get thumbnails for them and make a new PDF. How this will be? Maybe there are ready apps for this? – user2874493 Feb 09 '19 at 14:49
  • I think something like this: page=PDF.getPage; pic[1]=makeThumbnail(page); createPDF(arrayPages); – user2874493 Feb 09 '19 at 14:51