1+ @page " /Export"
2+
3+ @using Syncfusion .PdfExport ;
4+ @using Syncfusion .Blazor .Diagram
5+ @using Syncfusion .Blazor .Buttons
6+ @inject IJSRuntime JS ;
7+
8+ <SfButton Content =" ExportPDF" OnClick =" @ExportPDFAsync" />
9+ <SfButton Content =" ExportPng" OnClick =" @ExportPngAsync" />
10+ <SfDiagramComponent Height =" 600px" @ref =" @_diagram" />
11+
12+ @code
13+ {
14+ // Reference the diagram
15+ SfDiagramComponent _diagram ;
16+
17+ private async void ExportPDFAsync ()
18+ {
19+ DiagramExportSettings _print = new DiagramExportSettings ();
20+ _print .Region = DiagramPrintExportRegion .PageSettings ;
21+ _print .PageWidth = 500 ;
22+ _print .PageHeight = 800 ;
23+ _print .Orientation = PageOrientation .Portrait ;
24+ _print .FitToPage = true ;
25+ _print .Margin = new DiagramThickness () { Left = 30 , Top = 20 , Right = 10 , Bottom = 10 };
26+ _print .ClipBounds = new DiagramRect () { X = 200 , Y = 200 , Width = 200 , Height = 200 };
27+ // To export the diagram into base64
28+ var _images = await _diagram .ExportAsync (DiagramExportFormat .PNG , _print );
29+ var _pdforientation = PageOrientation .Portrait == PageOrientation .Landscape ? PdfPageOrientation .Landscape : PdfPageOrientation .Portrait ;
30+ await ExportToPdfAsync (" diagram" , _pdforientation , true , _images );
31+ }
32+ //
33+ private async Task <string > ExportToPdfAsync (string fileName , PdfPageOrientation orientation , bool allowDownload , string [] images )
34+ {
35+ PdfDocument _document = new PdfDocument ();
36+ _document .PageSettings .Orientation = orientation ;
37+ _document .PageSettings .Margins = new PdfMargins () { Left = 0 , Right = 0 , Top = 0 , Bottom = 0 };
38+ string _base64String ;
39+ var _dict = images ;
40+ for (int i = 0 ; i < _dict .Count (); i ++ )
41+ {
42+ _base64String = _dict [i ];
43+ using (MemoryStream _initialStream = new MemoryStream (Convert .FromBase64String (_base64String .Split (" base64," )[1 ])))
44+ {
45+ Stream _stream = _initialStream as Stream ;
46+ PdfPage _page = _document .Pages .Add ();
47+ PdfGraphics _graphics = _page .Graphics ;
48+ #pragma warning disable CA2000
49+ PdfBitmap _image = new PdfBitmap (_stream );
50+ #pragma warning restore CA2000
51+ _graphics .DrawImage (_image , 0 , 0 );
52+ }
53+ }
54+ using (MemoryStream _memoryStream = new MemoryStream ())
55+ {
56+ _document .Save (_memoryStream );
57+ _memoryStream .Position = 0 ;
58+ _base64String = Convert .ToBase64String (_memoryStream .ToArray ());
59+ if (allowDownload )
60+ {
61+ await JSRuntimeExtensions .InvokeAsync <string >(JS , " downloadPdf" , new object [] { _base64String , fileName });
62+ _base64String = string .Empty ;
63+ }
64+ else
65+ {
66+ _base64String = " data:application/pdf;base64," + _base64String ;
67+ }
68+ _document .Dispose ();
69+ }
70+ return _base64String ;
71+ }
72+ }
0 commit comments