I am writing a project on WPF (C #), and the question arose: how to display the pdf page of the document in the window, while there should not be any buttons for editing, printing, just reading the page itself. in Russian). For Windows 7.
Closed due to the fact that the question is too common for participants AK ♦ , freim , LFC , overthesanity , aleksandr barakin 10 Apr at 16:21 .
Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .
- The question is too general and very similar to "do everything for me." - AK ♦
- Describes a specific problem to which I want to find a solution. What's wrong? - Rishat Khusainov
|
1 answer
Windows 10
You can use the UWP API ( Windows.Data.Pdf ). To use it, you need to add links to:
- UWP metadata library, the path to which is
C:\Program Files (x86)\Windows Kits\10\UnionMetadata\(версия)\Windows.winmd(to add a link to it in the library selection dialog box, change the filter to "All files "); - the System.Runtime.WindowsRuntime assembly (
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll).
Then the code to display the page of the PDF document in WPF will look like this:
XAML
<Window 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" mc:Ignorable="d" x:Class="WpfApp1.MainWindow" Title="MainWindow" Height="400" Width="600" > <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="grid" > <Button Content="Button" HorizontalAlignment="Left" Height="50" Margin="20,20,0,0" VerticalAlignment="Top" Width="217" Click="Button_Click"/> <Image HorizontalAlignment="Stretch" Margin="40,100,40,40" VerticalAlignment="Stretch" x:Name="img"/> </Grid> </Window> C #
using System; using System.IO; using System.Collections.Generic; using System.Text; using System.Windows; using System.Windows.Media.Imaging; using System.Windows.Controls; using Windows.Data.Pdf; using Windows.Storage; using Windows.Storage.Streams; namespace WpfApp1 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private async void Button_Click(object sender, RoutedEventArgs e) { StorageFile file = await StorageFile.GetFileFromPathAsync(@"C:\files\document.pdf"); PdfDocument pdf = await PdfDocument.LoadFromFileAsync(file); PdfPage page = pdf.GetPage(0); BitmapImage image = new BitmapImage(); using (var stream = new InMemoryRandomAccessStream()) { await page.RenderToStreamAsync(stream); image.BeginInit(); image.CacheOption = BitmapCacheOption.OnLoad; image.StreamSource = stream.AsStream(); image.EndInit(); } img.Source = image; } } } Source: Lander Verhack. Creating a PDF Viewer in WPF using Windows 10 APIs
Previous versions of Windows
Use any third-party library, for example:
CefSharp - analog WebBrowser, which also supports PDF display
- Thank you, but I need it for Windows 7. I agree, I didn’t specify my jamb, I apologize) - Rishat Khusainov
- @RishatHusainov Use third-party components, such as CefSharp ( cefsharp.imtqy.com ) or Adobe Reader ActiveX ( codeproject.com/Articles/380019/Using-Adobe-Reader-in-a-WPF-app ) - MSDN.WhiteKnight
- Could you tell me, when creating a User Control (Adobe Reader ActiveX), how to call the toolbox. The article says that you need to press Ctrl + W, X, but this combination does not work - Rishat Khusainov
- @ Rishat Khusainov "View - Panel of Elements" in the studio menu - MSDN.WhiteKnight
- Thank you very much, you are the only one who helped the novice, and not just noted the question as a common one) - Rishat Khusainov
|