Przeglądaj źródła

Add button for generating pdf in previewer.

Bebo-Maker 3 lat temu
rodzic
commit
9afe4ec104

+ 43 - 0
QuestPDF.Previewer/PreviewerUtils.cs

@@ -0,0 +1,43 @@
+using System.Diagnostics;
+using Avalonia.Controls;
+using QuestPDF.Fluent;
+using QuestPDF.Infrastructure;
+
+namespace QuestPDF.Previewer
+{
+    internal static class PreviewerUtils
+    {
+        public static async Task<bool> SavePdfWithDialog(IDocument? document, Window dialogOwner)
+        {
+            if (document == null)
+                return false;
+            
+            var dialog = new SaveFileDialog()
+            {
+                DefaultExtension = ".pdf",
+                InitialFileName = document.GetMetadata().Title ?? "Document",
+                Filters = new List<FileDialogFilter>()
+                {
+                    new FileDialogFilter()
+                    {
+                        Extensions = new List<string>() { "pdf" },
+                    }
+                }
+            };
+
+            var filePath = await dialog.ShowAsync(dialogOwner);
+            if (string.IsNullOrWhiteSpace(filePath))
+                return false;
+
+            var dirPath = Path.GetDirectoryName(filePath);
+
+            if (!Directory.Exists(dirPath))
+                return false;
+
+            //TODO Catch layout exceptions.
+            document.GeneratePdf(filePath);
+            Process.Start("explorer.exe", filePath);
+            return true;
+        }
+    }
+}

+ 15 - 3
QuestPDF.Previewer/PreviewerWindow.axaml

@@ -23,8 +23,20 @@
 			</ExperimentalAcrylicBorder.Material>
 		</ExperimentalAcrylicBorder>
 
-		<ScrollViewer Margin="0,40,0,0">
-			<previewer:PreviewerControl x:Name="PreviewerSurface" Margin="25" />
-		</ScrollViewer>
+		<Grid Margin="0,40,0,0">
+			<Grid.RowDefinitions>
+				<RowDefinition Height="40"/>
+				<RowDefinition Height="*"/>
+			</Grid.RowDefinitions>
+
+			<DockPanel LastChildFill="False" VerticalAlignment="Center" Margin="20,0" Width="{Binding #PreviewerSurface.Width, Mode=OneWay}">
+				<TextBlock VerticalAlignment="Center" Text="QuestPDF Document Preview" DockPanel.Dock="Left" FontWeight="SemiBold" />
+				<Button x:Name="GeneratePdf" Content="Generate PDF" DockPanel.Dock="Right" />
+			</DockPanel>
+			
+			<ScrollViewer Grid.Row="1" VerticalAlignment="Top">
+				<previewer:PreviewerControl x:Name="PreviewerSurface" Margin="25" />
+			</ScrollViewer>
+		</Grid>
 	</Panel>
 </FluentWindow>

+ 7 - 2
QuestPDF.Previewer/PreviewerWindow.axaml.cs

@@ -1,6 +1,8 @@
-using Avalonia;
+using System.Diagnostics;
+using Avalonia;
 using Avalonia.Controls;
 using Avalonia.Markup.Xaml;
+using QuestPDF.Fluent;
 using QuestPDF.Infrastructure;
 
 namespace QuestPDF.Previewer
@@ -24,6 +26,9 @@ namespace QuestPDF.Previewer
 
             _previewHost = this.FindControl<PreviewerControl>("PreviewerSurface");
 
+            this.FindControl<Button>("GeneratePdf")
+                .Click += (_, __) => _ = PreviewerUtils.SavePdfWithDialog(Document, this);
+
             DocumentProperty.Changed.Subscribe(v => _previewHost.Document = v.NewValue.Value);
             HotReloadManager.Register(InvalidatePreview);
         }
@@ -33,7 +38,7 @@ namespace QuestPDF.Previewer
             AvaloniaXamlLoader.Load(this);
         }
 
-        public void InvalidatePreview()
+        private void InvalidatePreview()
         {
             _previewHost.InvalidateDocument();
         }