├── .github
└── workflows
│ └── publish_to_nuget.org.yml
├── .gitignore
├── AvaloniaUI.PrintToPDF.nuspec
├── AvaloniaUI.PrintToPDF.sln
├── Directory.Build.props
├── LICENSE
├── README.md
├── build.sh
├── demo
├── App.xaml
├── App.xaml.cs
├── Assets
│ └── Fonts
│ │ ├── SourceSansPro-Bold.ttf
│ │ ├── SourceSansPro-BoldItalic.ttf
│ │ ├── SourceSansPro-Italic.ttf
│ │ └── SourceSansPro-Regular.ttf
├── AvaloniaUI.PrintToPDF.Demo.csproj
├── Dialog.cs
├── MainWindow.xaml
├── MainWindow.xaml.cs
├── Model.cs
├── Program.cs
├── nuget.config
└── pages
│ ├── CalendarPage.xaml
│ ├── CalendarPage.xaml.cs
│ ├── CanvasPage.xaml
│ ├── CanvasPage.xaml.cs
│ ├── TextBoxPage.xaml
│ └── TextBoxPage.xaml.cs
├── doc
└── sample.pdf
└── src
├── AvaloniaUI.PrintToPDF.csproj
├── Print.cs
└── VisualExtensions.cs
/.github/workflows/publish_to_nuget.org.yml:
--------------------------------------------------------------------------------
1 | name: build-nuget-package
2 |
3 | on:
4 | workflow_dispatch:
5 |
6 | jobs:
7 |
8 | build:
9 | runs-on: ubuntu-latest
10 | steps:
11 | - uses: actions/checkout@v2
12 | with:
13 | submodules: recursive
14 | - name: Setup .NET Core
15 | uses: actions/setup-dotnet@v3
16 | with:
17 | dotnet-version: 8.0.x
18 | - name: Build control
19 | run: |
20 | ./build.sh
21 | - name: Publish to nuget.org
22 | uses: brandedoutcast/publish-nuget@v2.5.2
23 | with:
24 | PROJECT_FILE_PATH: src/AvaloniaUI.PrintToPDF.csproj
25 | NUGET_KEY: ${{secrets.NUGET_API_KEY}}
26 | NUGET_SOURCE: https://api.nuget.org
27 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | ## Visual Studio Code specific files and folder
7 | .vscode/*
8 | !.vscode/settings.json
9 | !.vscode/tasks.json
10 | !.vscode/launch.json
11 | !.vscode/extensions.jsons
12 |
13 | # User-specific files
14 | *.suo
15 | *.user
16 | *.userosscache
17 | *.sln.docstates
18 |
19 | # User-specific files (MonoDevelop/Xamarin Studio)
20 | *.userprefs
21 |
22 | # Build results
23 | [Dd]ebug/
24 | [Dd]ebugPublic/
25 | [Rr]elease/
26 | [Rr]eleases/
27 | x64/
28 | x86/
29 | bld/
30 | [Bb]in/
31 | [Oo]bj/
32 | [Ll]og/
33 |
34 | # Visual Studio 2015/2017 cache/options directory
35 | .vs/
36 | # Uncomment if you have tasks that create the project's static files in wwwroot
37 | #wwwroot/
38 |
39 | # Visual Studio 2017 auto generated files
40 | Generated\ Files/
41 |
42 | # MSTest test Results
43 | [Tt]est[Rr]esult*/
44 | [Bb]uild[Ll]og.*
45 |
46 | # NUNIT
47 | *.VisualState.xml
48 | TestResult.xml
49 |
50 | # Build Results of an ATL Project
51 | [Dd]ebugPS/
52 | [Rr]eleasePS/
53 | dlldata.c
54 |
55 | # Benchmark Results
56 | BenchmarkDotNet.Artifacts/
57 |
58 | # .NET Core
59 | project.lock.json
60 | project.fragment.lock.json
61 | artifacts/
62 | **/Properties/launchSettings.json
63 |
64 | # StyleCop
65 | StyleCopReport.xml
66 |
67 | # Files built by Visual Studio
68 | *_i.c
69 | *_p.c
70 | *_i.h
71 | *.ilk
72 | *.meta
73 | *.obj
74 | *.iobj
75 | *.pch
76 | *.pdb
77 | *.ipdb
78 | *.pgc
79 | *.pgd
80 | *.rsp
81 | *.sbr
82 | *.tlb
83 | *.tli
84 | *.tlh
85 | *.tmp
86 | *.tmp_proj
87 | *.log
88 | *.vspscc
89 | *.vssscc
90 | .builds
91 | *.pidb
92 | *.svclog
93 | *.scc
94 |
95 | # Chutzpah Test files
96 | _Chutzpah*
97 |
98 | # Visual C++ cache files
99 | ipch/
100 | *.aps
101 | *.ncb
102 | *.opendb
103 | *.opensdf
104 | *.sdf
105 | *.cachefile
106 | *.VC.db
107 | *.VC.VC.opendb
108 |
109 | # Visual Studio profiler
110 | *.psess
111 | *.vsp
112 | *.vspx
113 | *.sap
114 |
115 | # Visual Studio Trace Files
116 | *.e2e
117 |
118 | # TFS 2012 Local Workspace
119 | $tf/
120 |
121 | # Guidance Automation Toolkit
122 | *.gpState
123 |
124 | # ReSharper is a .NET coding add-in
125 | _ReSharper*/
126 | *.[Rr]e[Ss]harper
127 | *.DotSettings.user
128 |
129 | # JustCode is a .NET coding add-in
130 | .JustCode
131 |
132 | # TeamCity is a build add-in
133 | _TeamCity*
134 |
135 | # DotCover is a Code Coverage Tool
136 | *.dotCover
137 |
138 | # AxoCover is a Code Coverage Tool
139 | .axoCover/*
140 | !.axoCover/settings.json
141 |
142 | # Visual Studio code coverage results
143 | *.coverage
144 | *.coveragexml
145 |
146 | # NCrunch
147 | _NCrunch_*
148 | .*crunch*.local.xml
149 | nCrunchTemp_*
150 |
151 | # MightyMoose
152 | *.mm.*
153 | AutoTest.Net/
154 |
155 | # Web workbench (sass)
156 | .sass-cache/
157 |
158 | # Installshield output folder
159 | [Ee]xpress/
160 |
161 | # DocProject is a documentation generator add-in
162 | DocProject/buildhelp/
163 | DocProject/Help/*.HxT
164 | DocProject/Help/*.HxC
165 | DocProject/Help/*.hhc
166 | DocProject/Help/*.hhk
167 | DocProject/Help/*.hhp
168 | DocProject/Help/Html2
169 | DocProject/Help/html
170 |
171 | # Click-Once directory
172 | publish/
173 |
174 | # Publish Web Output
175 | *.[Pp]ublish.xml
176 | *.azurePubxml
177 | # Note: Comment the next line if you want to checkin your web deploy settings,
178 | # but database connection strings (with potential passwords) will be unencrypted
179 | *.pubxml
180 | *.publishproj
181 |
182 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
183 | # checkin your Azure Web App publish settings, but sensitive information contained
184 | # in these scripts will be unencrypted
185 | PublishScripts/
186 |
187 | # NuGet Packages
188 | *.nupkg
189 | # The packages folder can be ignored because of Package Restore
190 | **/[Pp]ackages/*
191 | # except build/, which is used as an MSBuild target.
192 | !**/[Pp]ackages/build/
193 | # Uncomment if necessary however generally it will be regenerated when needed
194 | #!**/[Pp]ackages/repositories.config
195 | # NuGet v3's project.json files produces more ignorable files
196 | *.nuget.props
197 | *.nuget.targets
198 |
199 | # Microsoft Azure Build Output
200 | csx/
201 | *.build.csdef
202 |
203 | # Microsoft Azure Emulator
204 | ecf/
205 | rcf/
206 |
207 | # Windows Store app package directories and files
208 | AppPackages/
209 | BundleArtifacts/
210 | Package.StoreAssociation.xml
211 | _pkginfo.txt
212 | *.appx
213 |
214 | # Visual Studio cache files
215 | # files ending in .cache can be ignored
216 | *.[Cc]ache
217 | # but keep track of directories ending in .cache
218 | !*.[Cc]ache/
219 |
220 | # Others
221 | ClientBin/
222 | ~$*
223 | *~
224 | *.dbmdl
225 | *.dbproj.schemaview
226 | *.jfm
227 | *.pfx
228 | *.publishsettings
229 | orleans.codegen.cs
230 |
231 | # Including strong name files can present a security risk
232 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
233 | #*.snk
234 |
235 | # Since there are multiple workflows, uncomment next line to ignore bower_components
236 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
237 | #bower_components/
238 |
239 | # RIA/Silverlight projects
240 | Generated_Code/
241 |
242 | # Backup & report files from converting an old project file
243 | # to a newer Visual Studio version. Backup files are not needed,
244 | # because we have git ;-)
245 | _UpgradeReport_Files/
246 | Backup*/
247 | UpgradeLog*.XML
248 | UpgradeLog*.htm
249 | ServiceFabricBackup/
250 | *.rptproj.bak
251 |
252 | # SQL Server files
253 | *.mdf
254 | *.ldf
255 | *.ndf
256 |
257 | # Business Intelligence projects
258 | *.rdl.data
259 | *.bim.layout
260 | *.bim_*.settings
261 | *.rptproj.rsuser
262 |
263 | # Microsoft Fakes
264 | FakesAssemblies/
265 |
266 | # GhostDoc plugin setting file
267 | *.GhostDoc.xml
268 |
269 | # Node.js Tools for Visual Studio
270 | .ntvs_analysis.dat
271 | node_modules/
272 |
273 | # Visual Studio 6 build log
274 | *.plg
275 |
276 | # Visual Studio 6 workspace options file
277 | *.opt
278 |
279 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
280 | *.vbw
281 |
282 | # Visual Studio LightSwitch build output
283 | **/*.HTMLClient/GeneratedArtifacts
284 | **/*.DesktopClient/GeneratedArtifacts
285 | **/*.DesktopClient/ModelManifest.xml
286 | **/*.Server/GeneratedArtifacts
287 | **/*.Server/ModelManifest.xml
288 | _Pvt_Extensions
289 |
290 | # Paket dependency manager
291 | .paket/paket.exe
292 | paket-files/
293 |
294 | # FAKE - F# Make
295 | .fake/
296 |
297 | # JetBrains Rider
298 | .idea/
299 | *.sln.iml
300 |
301 | # CodeRush
302 | .cr/
303 |
304 | # Python Tools for Visual Studio (PTVS)
305 | __pycache__/
306 | *.pyc
307 |
308 | # Cake - Uncomment if you are using it
309 | # tools/**
310 | # !tools/packages.config
311 |
312 | # Tabs Studio
313 | *.tss
314 |
315 | # Telerik's JustMock configuration file
316 | *.jmconfig
317 |
318 | # BizTalk build output
319 | *.btp.cs
320 | *.btm.cs
321 | *.odx.cs
322 | *.xsd.cs
323 |
324 | # OpenCover UI analysis results
325 | OpenCover/
326 |
327 | # Azure Stream Analytics local run output
328 | ASALocalRun/
329 |
330 | # MSBuild Binary and Structured Log
331 | *.binlog
332 |
333 | # NVidia Nsight GPU debugger configuration file
334 | *.nvuser
335 |
336 | # MFractors (Xamarin productivity tool) working folder
337 | .mfractor/
338 |
339 | .vscode
340 | demo/*.pdf
341 |
--------------------------------------------------------------------------------
/AvaloniaUI.PrintToPDF.nuspec:
--------------------------------------------------------------------------------
1 |
2 |
3 | AvaloniaUI.PrintToPDF
4 | 0.2.0
5 | oaz
6 | MIT
7 | https://github.com/Oaz/AvaloniaUI.PrintToPDF
8 | Create PDF documents from Avalonia UI visuals
9 |
10 |
11 | Copyright © Olivier Azeau
12 | en-US
13 | Avalonia Skia PDF
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/AvaloniaUI.PrintToPDF.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.26124.0
5 | MinimumVisualStudioVersion = 15.0.26124.0
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AvaloniaUI.PrintToPDF", "src\AvaloniaUI.PrintToPDF.csproj", "{D31D6849-B2BE-4577-A870-93A74498CA59}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AvaloniaUI.PrintToPDF.Demo", "demo\AvaloniaUI.PrintToPDF.Demo.csproj", "{BB7FADBD-EB1E-4706-A8FE-41FCAF4C56B6}"
9 | EndProject
10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{230A970C-168A-418D-B17E-A0F8555A4AE4}"
11 | ProjectSection(SolutionItems) = preProject
12 | build.sh = build.sh
13 | README.md = README.md
14 | Directory.Build.props = Directory.Build.props
15 | EndProjectSection
16 | EndProject
17 | Global
18 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
19 | Debug|Any CPU = Debug|Any CPU
20 | Debug|x64 = Debug|x64
21 | Debug|x86 = Debug|x86
22 | Release|Any CPU = Release|Any CPU
23 | Release|x64 = Release|x64
24 | Release|x86 = Release|x86
25 | EndGlobalSection
26 | GlobalSection(SolutionProperties) = preSolution
27 | HideSolutionNode = FALSE
28 | EndGlobalSection
29 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
30 | {D31D6849-B2BE-4577-A870-93A74498CA59}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31 | {D31D6849-B2BE-4577-A870-93A74498CA59}.Debug|Any CPU.Build.0 = Debug|Any CPU
32 | {D31D6849-B2BE-4577-A870-93A74498CA59}.Debug|x64.ActiveCfg = Debug|Any CPU
33 | {D31D6849-B2BE-4577-A870-93A74498CA59}.Debug|x64.Build.0 = Debug|Any CPU
34 | {D31D6849-B2BE-4577-A870-93A74498CA59}.Debug|x86.ActiveCfg = Debug|Any CPU
35 | {D31D6849-B2BE-4577-A870-93A74498CA59}.Debug|x86.Build.0 = Debug|Any CPU
36 | {D31D6849-B2BE-4577-A870-93A74498CA59}.Release|Any CPU.ActiveCfg = Release|Any CPU
37 | {D31D6849-B2BE-4577-A870-93A74498CA59}.Release|Any CPU.Build.0 = Release|Any CPU
38 | {D31D6849-B2BE-4577-A870-93A74498CA59}.Release|x64.ActiveCfg = Release|Any CPU
39 | {D31D6849-B2BE-4577-A870-93A74498CA59}.Release|x64.Build.0 = Release|Any CPU
40 | {D31D6849-B2BE-4577-A870-93A74498CA59}.Release|x86.ActiveCfg = Release|Any CPU
41 | {D31D6849-B2BE-4577-A870-93A74498CA59}.Release|x86.Build.0 = Release|Any CPU
42 | {BB7FADBD-EB1E-4706-A8FE-41FCAF4C56B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
43 | {BB7FADBD-EB1E-4706-A8FE-41FCAF4C56B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
44 | {BB7FADBD-EB1E-4706-A8FE-41FCAF4C56B6}.Debug|x64.ActiveCfg = Debug|Any CPU
45 | {BB7FADBD-EB1E-4706-A8FE-41FCAF4C56B6}.Debug|x64.Build.0 = Debug|Any CPU
46 | {BB7FADBD-EB1E-4706-A8FE-41FCAF4C56B6}.Debug|x86.ActiveCfg = Debug|Any CPU
47 | {BB7FADBD-EB1E-4706-A8FE-41FCAF4C56B6}.Debug|x86.Build.0 = Debug|Any CPU
48 | {BB7FADBD-EB1E-4706-A8FE-41FCAF4C56B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
49 | {BB7FADBD-EB1E-4706-A8FE-41FCAF4C56B6}.Release|Any CPU.Build.0 = Release|Any CPU
50 | {BB7FADBD-EB1E-4706-A8FE-41FCAF4C56B6}.Release|x64.ActiveCfg = Release|Any CPU
51 | {BB7FADBD-EB1E-4706-A8FE-41FCAF4C56B6}.Release|x64.Build.0 = Release|Any CPU
52 | {BB7FADBD-EB1E-4706-A8FE-41FCAF4C56B6}.Release|x86.ActiveCfg = Release|Any CPU
53 | {BB7FADBD-EB1E-4706-A8FE-41FCAF4C56B6}.Release|x86.Build.0 = Release|Any CPU
54 | EndGlobalSection
55 | EndGlobal
56 |
--------------------------------------------------------------------------------
/Directory.Build.props:
--------------------------------------------------------------------------------
1 |
2 |
3 | 11.1.3
4 | 2.88.8
5 |
6 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 Olivier Azeau
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # AvaloniaUI.PrintToPDF
2 |
3 | This is an experimental lib to create PDF documents from [Avalonia UI](https://github.com/AvaloniaUI/Avalonia) visuals.
4 |
5 | The PDF files are generated by the [Skia](https://github.com/mono/SkiaSharp) PDF backend.
6 |
7 | ## Demo
8 |
9 | The demo app generates PDF files using some pages from the Avalonia Control Catalog.
10 |
11 | Here is [a sample PDF file](https://github.com/Oaz/AvaloniaUI.PrintToPDF/raw/master/doc/sample.pdf).
12 |
13 | ## Usage
14 |
15 | One of the simplest code is a full screenshot (in vector graphics) of a desktop app main window:
16 |
17 | ```C#
18 | AvaloniaUI.PrintToPDF.Print.ToFile(
19 | "app_screenshot.pdf",
20 | (Application.Current.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime).MainWindow
21 | );
22 | ```
23 |
--------------------------------------------------------------------------------
/build.sh:
--------------------------------------------------------------------------------
1 | dotnet pack src/AvaloniaUI.PrintToPDF.csproj -c Release
2 |
--------------------------------------------------------------------------------
/demo/App.xaml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/demo/App.xaml.cs:
--------------------------------------------------------------------------------
1 | using Avalonia;
2 | using Avalonia.Controls.ApplicationLifetimes;
3 | using Avalonia.Markup.Xaml;
4 |
5 | namespace AvaloniaUI.PrintToPDF.Demo
6 | {
7 | public class App : Application
8 | {
9 | public override void Initialize()
10 | {
11 | AvaloniaXamlLoader.Load(this);
12 | }
13 |
14 | public override void OnFrameworkInitializationCompleted()
15 | {
16 | if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
17 | {
18 | desktop.MainWindow = new MainWindow();
19 | }
20 |
21 | base.OnFrameworkInitializationCompleted();
22 | }
23 | }
24 | }
--------------------------------------------------------------------------------
/demo/Assets/Fonts/SourceSansPro-Bold.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Oaz/AvaloniaUI.PrintToPDF/810dd086e72f4d2c46c00b8e8aa2cf7b0a0b64fd/demo/Assets/Fonts/SourceSansPro-Bold.ttf
--------------------------------------------------------------------------------
/demo/Assets/Fonts/SourceSansPro-BoldItalic.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Oaz/AvaloniaUI.PrintToPDF/810dd086e72f4d2c46c00b8e8aa2cf7b0a0b64fd/demo/Assets/Fonts/SourceSansPro-BoldItalic.ttf
--------------------------------------------------------------------------------
/demo/Assets/Fonts/SourceSansPro-Italic.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Oaz/AvaloniaUI.PrintToPDF/810dd086e72f4d2c46c00b8e8aa2cf7b0a0b64fd/demo/Assets/Fonts/SourceSansPro-Italic.ttf
--------------------------------------------------------------------------------
/demo/Assets/Fonts/SourceSansPro-Regular.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Oaz/AvaloniaUI.PrintToPDF/810dd086e72f4d2c46c00b8e8aa2cf7b0a0b64fd/demo/Assets/Fonts/SourceSansPro-Regular.ttf
--------------------------------------------------------------------------------
/demo/AvaloniaUI.PrintToPDF.Demo.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | False
4 | WinExe
5 | net7.0;net8.0
6 |
7 |
8 |
9 | %(Filename)
10 |
11 |
12 | Designer
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/demo/Dialog.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 | using System.Threading.Tasks;
4 | using Avalonia;
5 | using Avalonia.Controls.ApplicationLifetimes;
6 | using Avalonia.Platform.Storage;
7 |
8 | namespace AvaloniaUI.PrintToPDF.Demo
9 | {
10 | static class Dialog
11 | {
12 | public static async void Save(string title, string defaultFilename, Func saveAction)
13 | {
14 | var file = await Save(title, defaultFilename);
15 | if (file == null)
16 | return;
17 | await using var stream = await file.OpenWriteAsync();
18 | await saveAction(stream);
19 | }
20 |
21 | public static Task Save(string title, string defaultFilename)
22 | {
23 | var saveDialog = new FilePickerSaveOptions()
24 | {
25 | Title = title,
26 | FileTypeChoices = PdfFilters,
27 | SuggestedFileName = defaultFilename
28 | };
29 | var mainWindow = (Application.Current!.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow;
30 | return mainWindow!.StorageProvider.SaveFilePickerAsync(saveDialog);
31 | }
32 |
33 | private static FilePickerFileType[] PdfFilters => new FilePickerFileType[]
34 | {
35 | new ("PDF files (.pdf)") { Patterns = new [] {"*.pdf"} },
36 | new ("All files") {Patterns = new [] {"*"} }
37 | };
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/demo/MainWindow.xaml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/demo/MainWindow.xaml.cs:
--------------------------------------------------------------------------------
1 | using Avalonia.Controls;
2 | using Avalonia.Markup.Xaml;
3 |
4 | namespace AvaloniaUI.PrintToPDF.Demo
5 | {
6 | public class MainWindow : Window
7 | {
8 | public MainWindow()
9 | {
10 | InitializeComponent();
11 | DataContext = new Model(this);
12 | }
13 |
14 | private void InitializeComponent()
15 | {
16 | AvaloniaXamlLoader.Load(this);
17 | }
18 | }
19 | }
--------------------------------------------------------------------------------
/demo/Model.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using Avalonia;
5 | using Avalonia.Controls;
6 | using ReactiveUI;
7 |
8 | namespace AvaloniaUI.PrintToPDF.Demo
9 | {
10 | class Model : ReactiveObject
11 | {
12 | public Model(Window window)
13 | {
14 | _window = window;
15 | if(Environment.GetCommandLineArgs().Contains("--pdf"))
16 | {
17 | var outputFilename = "batchAllPages.pdf";
18 | Console.WriteLine($"Printing to {outputFilename}");
19 | Print.ToFileAsync(outputFilename, AllPages.Layout(_maximumPageSize)).GetAwaiter().GetResult();
20 | Environment.Exit(0);
21 | }
22 | }
23 |
24 | private readonly Window _window;
25 | private readonly Size _maximumPageSize = new Size(2000, 2000);
26 |
27 | private IEnumerable AllPages =>
28 | from tabItem in _window.FindAllVisuals()
29 | select tabItem.Content as Control;
30 |
31 |
32 | public void SaveCurrentPageAsPdf() =>
33 | Dialog.Save("Save current page as PDF", "currentPage.pdf", async stream =>
34 | {
35 | var output = from tabItem in _window.FindAllVisuals()
36 | where tabItem.IsSelected
37 | select tabItem.Content as Control;
38 | await Print.ToStreamAsync(stream, output);
39 | });
40 |
41 | public void SaveAllPagesAsPdf() => Dialog.Save("Save all pages as PDF", "allPages.pdf", async stream =>
42 | {
43 | await Print.ToStreamAsync(stream, AllPages.Layout(_maximumPageSize));
44 | });
45 |
46 | public void SaveFullWindowAsPdf() =>
47 | Dialog.Save("Save full window as PDF", "fullWindow.pdf", async stream =>
48 | {
49 | await Print.ToStreamAsync(stream, _window);
50 | });
51 |
52 | public void ExitApp()
53 | {
54 | _window.Close();
55 | }
56 | }
57 | }
--------------------------------------------------------------------------------
/demo/Program.cs:
--------------------------------------------------------------------------------
1 | using Avalonia;
2 |
3 | namespace AvaloniaUI.PrintToPDF.Demo
4 | {
5 | static class Program
6 | {
7 | // Initialization code. Don't use any Avalonia, third-party APIs or any
8 | // SynchronizationContext-reliant code before AppMain is called: things aren't initialized
9 | // yet and stuff might break.
10 | public static void Main(string[] args) => BuildAvaloniaApp()
11 | .StartWithClassicDesktopLifetime(args);
12 |
13 | // Avalonia configuration, don't remove; also used by visual designer.
14 | private static AppBuilder BuildAvaloniaApp()
15 | => AppBuilder.Configure()
16 | .UsePlatformDetect()
17 | .LogToTrace();
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/demo/nuget.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/demo/pages/CalendarPage.xaml:
--------------------------------------------------------------------------------
1 |
4 |
5 | Calendar
6 | A calendar control for selecting dates
7 |
8 |
12 |
13 |
14 |
16 |
17 |
18 |
20 |
21 |
22 |
24 |
25 |
26 |
27 |
28 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
40 |
41 |
42 |
44 |
45 |
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/demo/pages/CalendarPage.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using Avalonia.Controls;
3 | using Avalonia.Markup.Xaml;
4 |
5 | namespace AvaloniaUI.PrintToPDF.Demo.Pages
6 | {
7 | public class CalendarPage : UserControl
8 | {
9 | public CalendarPage()
10 | {
11 | this.InitializeComponent();
12 |
13 | var today = DateTime.Today;
14 | var cal1 = this.FindControl("DisplayDatesCalendar");
15 | cal1.DisplayDateStart = today.AddDays(-25);
16 | cal1.DisplayDateEnd = today.AddDays(25);
17 |
18 | var cal2 = this.FindControl("BlackoutDatesCalendar");
19 | cal2.BlackoutDates.AddDatesInPast();
20 | cal2.BlackoutDates.Add(new CalendarDateRange(today.AddDays(6)));
21 | }
22 |
23 | private void InitializeComponent()
24 | {
25 | AvaloniaXamlLoader.Load(this);
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/demo/pages/CanvasPage.xaml:
--------------------------------------------------------------------------------
1 |
4 |
5 | Canvas
6 | A panel which lays out its children by explicit coordinates
7 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/demo/pages/CanvasPage.xaml.cs:
--------------------------------------------------------------------------------
1 | using Avalonia.Controls;
2 | using Avalonia.Markup.Xaml;
3 |
4 | namespace AvaloniaUI.PrintToPDF.Demo.Pages
5 | {
6 | public class CanvasPage : UserControl
7 | {
8 | public CanvasPage()
9 | {
10 | this.InitializeComponent();
11 | }
12 |
13 | private void InitializeComponent()
14 | {
15 | AvaloniaXamlLoader.Load(this);
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/demo/pages/TextBoxPage.xaml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 |
8 |
9 |
13 |
14 |
15 |
16 |
17 |
21 |
22 |
28 |
29 |
30 |
31 |
34 |
35 |
36 |
37 |
38 |
40 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
--------------------------------------------------------------------------------
/demo/pages/TextBoxPage.xaml.cs:
--------------------------------------------------------------------------------
1 | using Avalonia.Controls;
2 | using Avalonia.Markup.Xaml;
3 |
4 | namespace AvaloniaUI.PrintToPDF.Demo.Pages
5 | {
6 | public class TextBoxPage : UserControl
7 | {
8 | public TextBoxPage()
9 | {
10 | this.InitializeComponent();
11 | }
12 |
13 | private void InitializeComponent()
14 | {
15 | AvaloniaXamlLoader.Load(this);
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/doc/sample.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Oaz/AvaloniaUI.PrintToPDF/810dd086e72f4d2c46c00b8e8aa2cf7b0a0b64fd/doc/sample.pdf
--------------------------------------------------------------------------------
/src/AvaloniaUI.PrintToPDF.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.1
5 | 0.6.0
6 | Olivier Azeau
7 | AvaloniaUI.PrintToPDF
8 | $(Product)
9 | $(Version)
10 | $(Version)
11 | en
12 | Create PDF documents from Avalonia UI visuals
13 | Copyright © Olivier Azeau
14 | https://github.com/Oaz/$(Product)
15 | Avalonia PDF
16 | Release
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/src/Print.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Linq;
5 | using System.Threading.Tasks;
6 | using Avalonia;
7 | using Avalonia.Skia.Helpers;
8 | using SkiaSharp;
9 |
10 | namespace AvaloniaUI.PrintToPDF
11 | {
12 | public static class Print
13 | {
14 | [Obsolete("Use async alternative")]
15 | public static void ToFile(string fileName, params Visual[] visuals) => ToFileAsync(fileName, visuals).GetAwaiter().GetResult();
16 | [Obsolete("Use async alternative")]
17 | public static void ToFile(string fileName, IEnumerable visuals) => ToFileAsync(fileName, visuals).GetAwaiter().GetResult();
18 |
19 | public static async Task ToFileAsync(string fileName, params Visual[] visuals) => await ToFileAsync(fileName, visuals.AsEnumerable());
20 | public static async Task ToFileAsync(string fileName, IEnumerable visuals)
21 | {
22 | using var doc = SKDocument.CreatePdf(fileName);
23 | await ToDocument(doc, visuals);
24 | doc.Close();
25 | }
26 |
27 | [Obsolete("Use async alternative")]
28 | public static void ToStream(Stream stream, params Visual[] visuals) => ToStreamAsync(stream, visuals).GetAwaiter().GetResult();
29 | [Obsolete("Use async alternative")]
30 | public static void ToStream(Stream stream, IEnumerable visuals) => ToStreamAsync(stream, visuals).GetAwaiter().GetResult();
31 |
32 | public static async Task ToStreamAsync(Stream stream, params Visual[] visuals) => await ToStreamAsync(stream, visuals.AsEnumerable());
33 | public static async Task ToStreamAsync(Stream stream, IEnumerable visuals)
34 | {
35 | using var doc = SKDocument.CreatePdf(stream);
36 | await ToDocument(doc, visuals);
37 | doc.Close();
38 | }
39 |
40 | private static async Task ToDocument(SKDocument doc, IEnumerable visuals)
41 | {
42 | foreach (var visual in visuals)
43 | {
44 | var bounds = visual.Bounds;
45 | var page = doc.BeginPage((float)bounds.Width, (float)bounds.Height);
46 | await DrawingContextHelper.RenderAsync(page, visual);
47 | doc.EndPage();
48 | }
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/VisualExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using Avalonia;
5 | using Avalonia.Layout;
6 | using Avalonia.VisualTree;
7 |
8 | namespace AvaloniaUI.PrintToPDF
9 | {
10 | public static class VisualExtensions
11 | {
12 | public static IEnumerable Layout(this IEnumerable layoutables, Size size)
13 | {
14 | var enumerable = layoutables as Layoutable[] ?? layoutables.ToArray();
15 | foreach (var layoutable in enumerable)
16 | {
17 | layoutable.Measure(size);
18 | layoutable.Arrange(new Rect(layoutable.DesiredSize));
19 | }
20 | return enumerable;
21 | }
22 |
23 | public static IEnumerable FindAllVisuals(this Visual root)
24 | {
25 | var result = new List();
26 | FindAllVisuals(root, result);
27 | return result;
28 | }
29 | private static void FindAllVisuals(Visual root, IList result)
30 | {
31 | if (root is Layoutable l)
32 | l.ApplyTemplate();
33 | if (root is T t)
34 | result.Add(t);
35 | foreach (var child in root.GetVisualChildren())
36 | FindAllVisuals(child, result);
37 | }
38 | }
39 | }
--------------------------------------------------------------------------------