Open PDF file with page number/index in UWP apps - windows

Is there any option to Open a PDF file that is available in local state folder (inside app installation directory) with the page number. There is one method (launchFileAsync) to open such files but I don't find any option to pass page number/index to open specific page.
Thank you! Any help is appreciated.

UWP provides the PdfDocument class for parsing PDF files, and this class provides the GetPage method for obtaining the object of the corresponding page (PdfPage) through the index.
This is simple code:
PdfDocument pdfDocument;
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("xxx.pdf");
pdfDocument = await PdfDocument.LoadFromFileAsync(file);
using (var firstPage = pdfDocument.GetPage(0))
{
var stream = new InMemoryRandomAccessStream();
await firstPage.RenderToStreamAsync(stream);
// do something...
}
Here is the complete code example:
PDF document sample

Related

.svg Image in MAUI Blazor

i am trying to set an image in to a razor page in MAUI Blazor.
In MAUI (only), there was the aproach, that you have a .svg image in the folder Resources/Images. MAUI then converts the .svg image in a .png image which you can use in the XAML file. like so:
Now i have the same picture in a MAUI Blazor app and i hoped that i can put my picture in the same way expect that i have to use the HTML style like so:
<img src="one_list2.png">
But this doesn't work at all. I tryed with or witout path, path with slashes, backslashes etc. nothing works.
Trying to put a .png image into the wwwroot folder works. But this isn't the goal. I found it very nice to put a svg image which is then converted into a png depending of its size. This way all pictures would be converted exactly in the perfect size you will need lossless.
Thanks
First, Add the image to Resources\Raw and set it to MauiAsset compilation type
Second, Check the project file to avoid setting the image in the other folder.
In razor component HTML:
<img src="#imageSource">
In the code part:
private string? imageSource;
protected override async Task OnInitializedAsync()
{
try
{
using var stream =
await FileSystem.OpenAppPackageFileAsync("testimage.png");
using var reader = new StreamReader(stream);
byte[] result;
using (var streamReader = new MemoryStream())
{
stream.CopyTo(streamReader);
result = streamReader.ToArray();
}
imageSource = Convert.ToBase64String(result);
imageSource = string.Format("data:image/png;base64,{0}", imageSource);
}
catch (Exception ex)
{
//error
}
}
More information you can refer to ASP.NET Core Blazor Hybrid static files.

How to use Share in Xamarin.iOS

I am following the documentation here:
https://learn.microsoft.com/en-us/xamarin/essentials/share?tabs=android
To implement the share functionality in my Xamarin.iOS app. My code:
byte[] pdf = await DownloadPdfFile();
var fn = "myfile.pdf";
var file = Path.Combine(FileSystem.CacheDirectory, fn);
await File.WriteAllBytesAsync(file, pdf);
await Share.RequestAsync(new ShareFileRequest
{
Title = ViewModel.Parameter.Title,
File = new ShareFile(file)
});
Nothing happens when I run this code.
My guess is you need to add some permissions in your info.plist
<key>NSPhotoLibraryAddUsageDescription</key>
<string>This app needs access to the photo gallery for saving photos and videos.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>This app needs access to photos gallery for saving photos and videos.</string>
Let me know in case you have queries
I solved it. There is a bug when running on iPad as mentioned here and you can fix this by specifying the PresentationSourceBounds:
await Share.RequestAsync(new ShareFileRequest
{
Title = "My title",
File = new ShareFile(file),
PresentationSourceBounds = new System.Drawing.Rectangle((int)View.Frame.Width, 0, (int)(View.Frame.Width), (int)(View.Frame.Height))
});

How can I auto save a captured image in a UWP app?

I have this code:
CameraCaptureUI captureUI = new CameraCaptureUI();
captureUI.PhotoSettings.AllowCropping = false;
StorageFile photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
How can I implement to auto save image option when click on the capture button?
How can I implement to auto save image option when click on the capture button?
The StorageFile containing the captured photo is given a dynamically generated name and saved in our app's local folder if we do not cancel the capture, so if we click on the capture button without clicking the confirm button, the photo will be saved automatically in our app's TempState folder.
For more info, refer Capture photos and video with Windows built-in camera UI.
To better organize your captured photos, you may want to move the file to a different folder. Please refer to the following sample which shows how to copy the latest capture photo from the TempState folder to the LocalFolder.
For example:
CameraCaptureUI captureUI = new CameraCaptureUI();
captureUI.PhotoSettings.AllowCropping = false;
StorageFile photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.TemporaryFolder;
var allFiles =await localFolder.GetFilesAsync();
foreach (StorageFile item in allFiles.OrderByDescending(a => a.DateCreated))
{
StorageFolder destinationFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("ProfilePhotoFolder", CreationCollisionOption.OpenIfExists);
await item.CopyAsync(destinationFolder, DateTimeOffset.Now.ToString("yyyyMMddHHmmssfff") + ".jpg", NameCollisionOption.ReplaceExisting);
await item.DeleteAsync();
return;
}

Save Image from My Pictures Folder to Local App Folder in Windows 8 Metro App

I'm using a button to select an image from the Pictures folder, what I want is to save that selected image to the app's Local Storage in order to be able to show that image using binding in a GridView. Is this posible? How could I do it? Thanks
You would do it something like this:
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".png");
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
await file.CopyAsync(Windows.Storage.ApplicationData.Current.LocalFolder);
}

webBrowser in windows forms: how to load html page from resources?

I have program which has webBrowser component. I need that this component would navigate to page which is in Resources ("1.htm"). Is there anyway to do it? My general wish is that after debuging I would have only one .exe file of program, and all htm pages would be build in it (like pictures and icons), or isnt that posible?
You can use DocumentText property for that
webBrowser1.DocumentText = WindowsFormsApplication1.Properties.Resources.1htm;
or
using (Stream stream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream("1.htm"))
{
using (StreamReader reader = new StreamReader(stream))
{
webBrowser1.DocumentText = reader.ReadToEnd();
}
}

Resources