1- using CreatePdfDemoSample . Services ;
1+ using CreateMAUIPDFSample . Services ;
22using Syncfusion . Drawing ;
33using Syncfusion . Pdf ;
44using Syncfusion . Pdf . Graphics ;
88using PointF = Syncfusion . Drawing . PointF ;
99using SizeF = Syncfusion . Drawing . SizeF ;
1010
11-
12- namespace CreatePdfDemoSample
11+ namespace CreateMAUIPDFSample
1312{
1413 public partial class MainPage : ContentPage
1514 {
@@ -27,14 +26,11 @@ public void createPdf_Click(object sender, EventArgs e)
2726 PdfPage page = document . Pages . Add ( ) ;
2827 //Create PDF graphics for the page.
2928 PdfGraphics graphics = page . Graphics ;
30-
3129 RectangleF TotalPriceCellBounds = RectangleF . Empty ;
3230 RectangleF QuantityCellBounds = RectangleF . Empty ;
33-
3431 //Get the page width and height
3532 float pageWidth = page . GetClientSize ( ) . Width ;
3633 float pageHeight = page . GetClientSize ( ) . Height ;
37-
3834 //Set the header height
3935 float headerHeight = 90 ;
4036 //Create brush with light blue color.
@@ -45,45 +41,34 @@ public void createPdf_Click(object sender, EventArgs e)
4541 PdfBrush darkBlueBrush = new PdfSolidBrush ( darkBlue ) ;
4642 //Create brush with white color.
4743 PdfBrush whiteBrush = new PdfSolidBrush ( Color . FromArgb ( 255 , 255 , 255 , 255 ) ) ;
48-
4944 //Get the font file stream from assembly.
5045 Assembly assembly = typeof ( MainPage ) . GetTypeInfo ( ) . Assembly ;
51- string basePath = "CreatePdfDemoSample .Resources.Pdf." ;
46+ string basePath = "CreateMAUIPDFSample .Resources.Pdf." ;
5247 Stream fontStream = assembly . GetManifestResourceStream ( basePath + "arial.ttf" ) ;
53-
5448 //Create PdfTrueTypeFont from stream with different size.
5549 PdfTrueTypeFont headerFont = new ( fontStream , 30 , PdfFontStyle . Regular ) ;
5650 PdfTrueTypeFont arialRegularFont = new ( fontStream , 18 , PdfFontStyle . Regular ) ;
5751 PdfTrueTypeFont arialBoldFont = new ( fontStream , 9 , PdfFontStyle . Bold ) ;
58-
5952 //Create string format.
6053 PdfStringFormat format = new ( ) ;
6154 format . Alignment = PdfTextAlignment . Center ;
6255 format . LineAlignment = PdfVerticalAlignment . Middle ;
63-
6456 float y = 0 ;
6557 float x = 0 ;
66-
6758 //Set the margins of address.
6859 float margin = 30 ;
69-
7060 //Set the line space.
7161 float lineSpace = 10 ;
72-
7362 //Create border pen and draw the border to PDF page.
7463 PdfColor borderColor = Color . FromArgb ( 255 , 142 , 170 , 219 ) ;
7564 PdfPen borderPen = new ( borderColor , 1f ) ;
7665 graphics . DrawRectangle ( borderPen , new RectangleF ( 0 , 0 , pageWidth , pageHeight ) ) ;
77-
7866 //Create a new PdfGrid
7967 PdfGrid grid = new ( ) ;
80-
8168 //Add five columns to the grid.
8269 grid . Columns . Add ( 5 ) ;
83-
8470 //Create the header row of the grid.
8571 PdfGridRow [ ] headerRow = grid . Headers . Add ( 1 ) ;
86-
8772 //Set style to the header row and set value to the header cells.
8873 headerRow [ 0 ] . Style . BackgroundBrush = new PdfSolidBrush ( new PdfColor ( 68 , 114 , 196 ) ) ;
8974 headerRow [ 0 ] . Style . TextBrush = PdfBrushes . White ;
@@ -93,15 +78,13 @@ public void createPdf_Click(object sender, EventArgs e)
9378 headerRow [ 0 ] . Cells [ 2 ] . Value = "Price ($)" ;
9479 headerRow [ 0 ] . Cells [ 3 ] . Value = "Quantity" ;
9580 headerRow [ 0 ] . Cells [ 4 ] . Value = "Total ($)" ;
96-
9781 //Add products to the grid table.
9882 AddProducts ( "CA-1098" , "AWC Logo Cap" , 8.99 , 2 , 17.98 , grid ) ;
9983 AddProducts ( "LJ-0192" , "Long-Sleeve Logo Jersey,M" , 49.99 , 3 , 149.97 , grid ) ;
10084 AddProducts ( "So-B909-M" , "Mountain Bike Socks,M" , 9.50 , 2 , 19 , grid ) ;
10185 AddProducts ( "LJ-0192" , "Long-Sleeve Logo Jersey,M" , 49.99 , 4 , 199.96 , grid ) ;
10286 AddProducts ( "FK-5136" , "ML Fork" , 175.49 , 6 , 1052.94 , grid ) ;
10387 AddProducts ( "HL-U509" , "Sports-100 Helmet,Black" , 34.99 , 1 , 34.99 , grid ) ;
104-
10588 #region Header
10689
10790 //Fill the header with light blue brush
@@ -137,7 +120,6 @@ public void createPdf_Click(object sender, EventArgs e)
137120 y += arialRegularFont . Height + lineSpace ;
138121 //Draw text to PDF page with provided font and location.
139122 graphics . DrawString ( "Date: " + DateTime . Now . ToString ( "dddd dd, MMMM yyyy" ) , arialRegularFont , PdfBrushes . Black , new PointF ( x , y ) ) ;
140-
141123 y = headerHeight + margin ;
142124 x = margin ;
143125 //Draw text to PDF page with provided font and location.
@@ -150,7 +132,6 @@ public void createPdf_Click(object sender, EventArgs e)
150132 graphics . DrawString ( "9920 BridgePointe Parkway," , arialRegularFont , PdfBrushes . Black , new PointF ( x , y ) ) ;
151133 y += arialRegularFont . Height + lineSpace ;
152134 graphics . DrawString ( "9365550136" , arialRegularFont , PdfBrushes . Black , new PointF ( x , y ) ) ;
153-
154135 #region Grid
155136 //Set width to grid columns.
156137 grid . Columns [ 0 ] . Width = 110 ;
@@ -227,10 +208,8 @@ public void createPdf_Click(object sender, EventArgs e)
227208 borderPen . DashStyle = PdfDashStyle . Custom ;
228209 borderPen . DashPattern = new float [ ] { 3 , 3 } ;
229210 graphics . DrawLine ( borderPen , new PointF ( 0 , pageHeight - 100 ) , new PointF ( pageWidth , pageHeight - 100 ) ) ;
230-
231211 //Get the image file stream from assembly.
232212 Stream imageStream = assembly . GetManifestResourceStream ( basePath + "AdventureWork.png" ) ;
233-
234213 //Create PDF bitmap image from stream.
235214 PdfBitmap bitmap = new ( imageStream ) ;
236215 //Draw the image to PDF page.
@@ -241,26 +220,16 @@ public void createPdf_Click(object sender, EventArgs e)
241220 size = arialRegularFont . MeasureString ( "800 Interchange Blvd." ) ;
242221 x = pageWidth - size . Width - margin ;
243222 graphics . DrawString ( "800 Interchange Blvd." , arialRegularFont , PdfBrushes . Black , new PointF ( x , y ) ) ;
244-
245223 //Calculate the text position and draw the text to PDF page.
246224 y += arialRegularFont . Height + lineSpace ;
247225 size = arialRegularFont . MeasureString ( "Suite 2501, Austin, TX 78721" ) ;
248226 x = pageWidth - size . Width - margin ;
249227 graphics . DrawString ( "Suite 2501, Austin, TX 78721" , arialRegularFont , PdfBrushes . Black , new PointF ( x , y ) ) ;
250-
251228 //Calculate the text position and draw the text to PDF page.
252229 y += arialRegularFont . Height + lineSpace ;
253230 size = arialRegularFont . MeasureString ( "Any Questions? support@adventure-works.com" ) ;
254231 x = pageWidth - size . Width - margin ;
255232 graphics . DrawString ( "Any Questions? support@adventure-works.com" , arialRegularFont , PdfBrushes . Black , new PointF ( x , y ) ) ;
256-
257-
258- //Add a new blank PDF page.
259- PdfPage page1 = document . Pages . Add ( ) ;
260-
261- //Draw the image to the PDF page.
262- page1 . Graphics . DrawImage ( bitmap , 0 , 0 , 200 , 200 ) ;
263-
264233 using MemoryStream ms = new ( ) ;
265234 //Save the PDF document to MemoryStream.
266235 document . Save ( ms ) ;
@@ -270,7 +239,6 @@ public void createPdf_Click(object sender, EventArgs e)
270239 //Saves the memory stream as file using the SaveService instance.
271240 SaveService save = new SaveService ( ) ;
272241 save . SaveAndView ( "output.pdf" , "application/pdf" , ms ) ;
273-
274242 void Grid_BeginCellLayout ( object sender , PdfGridBeginCellLayoutEventArgs args )
275243 {
276244 PdfGrid grid = sender as PdfGrid ;
@@ -286,8 +254,6 @@ void Grid_BeginCellLayout(object sender, PdfGridBeginCellLayoutEventArgs args)
286254 }
287255 }
288256 }
289-
290-
291257 //Create and row for the grid.
292258 void AddProducts ( string productId , string productName , double price , int quantity , double total , PdfGrid grid )
293259 {
@@ -312,9 +278,6 @@ private float GetTotalAmount(PdfGrid grid)
312278 Total += result ;
313279 }
314280 return Total ;
315-
316281 }
317-
318282 }
319-
320283}
0 commit comments