-
Notifications
You must be signed in to change notification settings - Fork 730
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: UWP and Android PdfDocument Sample and Runtime test
- Loading branch information
1 parent
319ea60
commit a1e526d
Showing
15 changed files
with
475 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/SamplesApp/UITests.Shared/Windows_Data/Pdf/PdfDocumentRenderTest.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<UserControl | ||
x:Class="UITests.Shared.Windows_Data_Pdf.PdfDocumentRenderTest" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:toolkit="using:Uno.UI.Toolkit" | ||
mc:Ignorable="d" | ||
d:DesignHeight="300" | ||
d:DesignWidth="400"> | ||
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> | ||
<Grid.RowDefinitions> | ||
<RowDefinition Height="Auto"/> | ||
<RowDefinition Height="Auto"/> | ||
<RowDefinition/> | ||
</Grid.RowDefinitions> | ||
<StackPanel Orientation="Horizontal" | ||
Margin="0,10,0,0"> | ||
<Button x:Name="LoadButton" Content="Load document" Click="LoadDocument" Margin="5,0"/> | ||
<PasswordBox x:Name="PasswordBox" Width="200" PlaceholderText="Optional password"/> | ||
</StackPanel> | ||
<StackPanel x:Name="RenderingPanel" Visibility="Collapsed" | ||
Grid.Row="1"> | ||
<StackPanel Orientation="Horizontal" Margin="0,10,0,0"> | ||
<TextBlock Name="ViewPageLabel" VerticalAlignment="center">View page</TextBlock> | ||
<!-- | ||
Always give a TextBox a name that's accessible to a screen reader. In this case, | ||
reference the labeling TextBlock to have the accessible name set on the TextBox. | ||
--> | ||
<TextBox x:Name="PageNumberBox" InputScope="Number" Width="30" Text="1" TextAlignment="Right" Margin="5,0,5,0" | ||
AutomationProperties.LabeledBy="{Binding ElementName=ViewPageLabel}"/> | ||
<TextBlock VerticalAlignment="Center">of <Run x:Name="PageCountText"/>.</TextBlock> | ||
</StackPanel> | ||
<StackPanel Orientation="Horizontal" Margin="0,10,0,0"> | ||
<!-- | ||
Always give a ComboBox a name that's accessible to a screen reader. Given that there's no labeling | ||
TextBlock to reference, explicitly set the accessible name on the ComboBox. A shipping app would | ||
localize this name. | ||
--> | ||
<ComboBox x:Name="Options" AutomationProperties.Name="View page size" SelectedIndex="0"> | ||
<ComboBoxItem>Arrange to Width</ComboBoxItem> | ||
<ComboBoxItem>Actual size</ComboBoxItem> | ||
<ComboBoxItem>Half size on beige background</ComboBoxItem> | ||
<ComboBoxItem>Crop to center of page</ComboBoxItem> | ||
</ComboBox> | ||
<Button Click="ViewPage" Content="View" Margin="10,0,0,0"/> | ||
</StackPanel> | ||
</StackPanel> | ||
<ScrollViewer Margin="12,20,12,12" | ||
HorizontalScrollBarVisibility="Auto" | ||
Grid.Row="2" | ||
x:Name="viewer"> | ||
<!-- | ||
Always give an Image a name that's accessible to screen reader, (unless the Image is purely decorative.) | ||
A shipping app would localize this name. | ||
--> | ||
<Border Background="White" | ||
toolkit:UIElementExtensions.Elevation="2" | ||
HorizontalAlignment="Center" | ||
VerticalAlignment="Top" | ||
Margin="10" > | ||
<Image x:Name="Output" | ||
AutomationProperties.Name="PDF document content" | ||
Stretch="None" | ||
/> | ||
</Border> | ||
</ScrollViewer> | ||
<SymbolIcon Symbol="Permissions" | ||
Margin="12" | ||
HorizontalAlignment="Right" | ||
VerticalAlignment="Top" | ||
Width="25" | ||
Visibility="Collapsed" | ||
Grid.Row="2" | ||
x:Name="Protected"/> | ||
<ProgressRing x:Name="ProgressControl" | ||
Grid.Row="2" | ||
Height="50" | ||
Width="50" | ||
IsActive="True" | ||
Visibility="Collapsed" | ||
HorizontalAlignment="Center" | ||
VerticalAlignment="Center" | ||
/> | ||
</Grid> | ||
</UserControl> |
130 changes: 130 additions & 0 deletions
130
src/SamplesApp/UITests.Shared/Windows_Data/Pdf/PdfDocumentRenderTest.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
using System; | ||
using Windows.Foundation; | ||
using Windows.UI.Xaml; | ||
using Windows.UI.Xaml.Controls; | ||
using Uno.UI.Samples.Controls; | ||
using Windows.Data.Pdf; | ||
using Windows.Storage.Pickers; | ||
using Windows.Storage.Streams; | ||
using Windows.Storage; | ||
using Windows.UI.Xaml.Media.Imaging; | ||
|
||
namespace UITests.Shared.Windows_Data_Pdf | ||
{ | ||
[SampleControlInfo("Windows.Data.Pdf", "PdfDocument", description: "Demonstrates use of Windows.Data.Pdf.PdfDocument", ignoreInSnapshotTests: true)] | ||
public sealed partial class PdfDocumentRenderTest : UserControl | ||
{ | ||
const int WrongPassword = unchecked((int)0x8007052b); // HRESULT_FROM_WIN32(ERROR_WRONG_PASSWORD) | ||
const int GenericFail = unchecked((int)0x80004005); // E_FAIL | ||
private PdfDocument pdfDocument; | ||
|
||
public PdfDocumentRenderTest() | ||
{ | ||
this.InitializeComponent(); | ||
} | ||
|
||
private async void LoadDocument(object sender, RoutedEventArgs args) | ||
{ | ||
LoadButton.IsEnabled = false; | ||
|
||
pdfDocument = null; | ||
Output.Source = null; | ||
PageNumberBox.Text = "1"; | ||
RenderingPanel.Visibility = Visibility.Collapsed; | ||
|
||
var picker = new FileOpenPicker(); | ||
picker.FileTypeFilter.Add(".pdf"); | ||
StorageFile file = await picker.PickSingleFileAsync(); | ||
if (file != null) | ||
{ | ||
ProgressControl.Visibility = Visibility.Visible; | ||
try | ||
{ | ||
var stream = await file.OpenAsync(FileAccessMode.Read); | ||
pdfDocument = await PdfDocument.LoadFromStreamAsync(stream, PasswordBox.Password); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Windows.UI.Popups.MessageDialog dialog = ex.HResult switch | ||
{ | ||
WrongPassword => new("Document is password-protected and password is incorrect.", "Error"), | ||
GenericFail => new("Document is not a valid PDF.", "Error"), | ||
_ => new(ex.Message, "Error") | ||
}; | ||
await dialog.ShowAsync(); | ||
} | ||
|
||
if (pdfDocument != null) | ||
{ | ||
RenderingPanel.Visibility = Visibility.Visible; | ||
if (pdfDocument.IsPasswordProtected) | ||
{ | ||
Protected.Visibility = Visibility.Visible; | ||
} | ||
else | ||
{ | ||
Protected.Visibility = Visibility.Collapsed; | ||
} | ||
PageCountText.Text = pdfDocument.PageCount.ToString(); | ||
} | ||
ProgressControl.Visibility = Visibility.Collapsed; | ||
} | ||
LoadButton.IsEnabled = true; | ||
} | ||
|
||
private async void ViewPage(object sender, RoutedEventArgs args) | ||
{ | ||
uint pageNumber; | ||
if (!uint.TryParse(PageNumberBox.Text, out pageNumber) || (pageNumber < 1) || (pageNumber > pdfDocument.PageCount)) | ||
{ | ||
Windows.UI.Popups.MessageDialog dialog = new("Invalid page number.", "Error"); | ||
await dialog.ShowAsync(); | ||
return; | ||
} | ||
|
||
Output.Source = null; | ||
ProgressControl.Visibility = Visibility.Visible; | ||
|
||
// Convert from 1-based page number to 0-based page index. | ||
uint pageIndex = pageNumber - 1; | ||
|
||
using (PdfPage page = pdfDocument.GetPage(pageIndex)) | ||
{ | ||
var stream = new InMemoryRandomAccessStream(); | ||
switch (Options.SelectedIndex) | ||
{ | ||
// Arrange size to viewport with | ||
case 0: | ||
await page.RenderToStreamAsync(stream, new() { DestinationWidth = (uint)(viewer.ViewportWidth - 20) }); | ||
break; | ||
// View actual size. | ||
case 1: | ||
await page.RenderToStreamAsync(stream); | ||
break; | ||
|
||
// View half size on beige background. | ||
case 2: | ||
var options1 = new PdfPageRenderOptions(); | ||
options1.BackgroundColor = Windows.UI.Colors.Beige; | ||
options1.DestinationHeight = (uint)(page.Size.Height / 2); | ||
options1.DestinationWidth = (uint)(page.Size.Width / 2); | ||
await page.RenderToStreamAsync(stream, options1); | ||
break; | ||
|
||
// Crop to center. | ||
case 3: | ||
var options2 = new PdfPageRenderOptions(); | ||
var rect = page.Dimensions.TrimBox; | ||
options2.SourceRect = new Rect(rect.X + rect.Width / 4, rect.Y + rect.Height / 4, rect.Width / 2, rect.Height / 2); | ||
await page.RenderToStreamAsync(stream, options2); | ||
break; | ||
} | ||
BitmapImage src = new BitmapImage(); | ||
await src.SetSourceAsync(stream); | ||
Output.Source = src; | ||
} | ||
ProgressControl.Visibility = Visibility.Collapsed; | ||
} | ||
} | ||
} | ||
|
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.