Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Solution>
<Project Path="CreateMAUIPDFSample/CreateMAUIPDFSample.csproj" />
</Solution>
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:CreatePdfDemoSample"
x:Class="CreatePdfDemoSample.App">
xmlns:local="clr-namespace:CreateMAUIPDFSample"
x:Class="CreateMAUIPDFSample.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.Extensions.DependencyInjection;

namespace CreateMAUIPDFSample
{
public partial class App : Application
{
public App()
{
InitializeComponent();
}

protected override Window CreateWindow(IActivationState? activationState)
{
return new Window(new AppShell());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Shell
x:Class="CreatePdfDemoSample.AppShell"
x:Class="CreateMAUIPDFSample.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:CreatePdfDemoSample"
Shell.FlyoutBehavior="Disabled"
Title="CreatePdfDemoSample">
xmlns:local="clr-namespace:CreateMAUIPDFSample"
Title="CreateMAUIPDFSample">

<ShellContent
Title="Home"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace CreatePdfDemoSample
namespace CreateMAUIPDFSample
{
public partial class AppShell : Shell
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
<!-- <TargetFrameworks>$(TargetFrameworks);net8.0-tizen</TargetFrameworks> -->
<TargetFrameworks>net10.0-android;net10.0-ios;net10.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net10.0-windows10.0.19041.0</TargetFrameworks>

<!-- Note for MacCatalyst:
The default runtime is maccatalyst-x64, except in Release config, in which case the default is maccatalyst-x64;maccatalyst-arm64.
Expand All @@ -14,28 +12,30 @@
<!-- For example: <RuntimeIdentifiers>maccatalyst-x64;maccatalyst-arm64</RuntimeIdentifiers> -->

<OutputType>Exe</OutputType>
<RootNamespace>CreatePdfDemoSample</RootNamespace>
<RootNamespace>CreateMAUIPDFSample</RootNamespace>
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<!-- Display name -->
<ApplicationTitle>CreatePdfDemoSample</ApplicationTitle>
<ApplicationTitle>CreateMAUIPDFSample</ApplicationTitle>

<!-- App Identifier -->
<ApplicationId>com.companyname.createpdfdemosample</ApplicationId>
<ApplicationId>com.companyname.CreateMAUIPDFSample</ApplicationId>

<!-- Versions -->
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
<ApplicationVersion>1</ApplicationVersion>

<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">11.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">13.1</SupportedOSPlatformVersion>
<!-- To develop, package, and publish an app to the Microsoft Store, see: https://aka.ms/MauiTemplateUnpackaged -->
<WindowsPackageType>None</WindowsPackageType>

<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">15.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">15.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
</PropertyGroup>

<ItemGroup>
Expand All @@ -48,7 +48,7 @@
<!-- Images -->
<MauiImage Include="Resources\Images\*" />
<MauiImage Update="Resources\Images\dotnet_bot.png" Resize="True" BaseSize="300,185" />

<!-- Custom Fonts -->
<MauiFont Include="Resources\Fonts\*" />

Expand All @@ -63,16 +63,33 @@
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Resources\Pdf\AdventureCycle.txt" />
<EmbeddedResource Include="Resources\Pdf\AdventureWork.png" />
<EmbeddedResource Include="Resources\Pdf\arial.ttf" />
<EmbeddedResource Include="Resources\Pdf\AdventureCycle.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Pdf\AdventureWork.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="Resources\Pdf\arial.ttf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageReference Include="Syncfusion.Pdf.NET" Version="*" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="10.0.0" />
<PackageReference Include="Syncfusion.Pdf.NET" Version="33.1.44" />
</ItemGroup>

<ItemGroup>
<Compile Update="Platforms\Android\SaveAndroid.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
<Compile Update="Platforms\iOS\SaveIOS.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
<Compile Update="Services\SaveService.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<IsFirstTimeProjectOpen>False</IsFirstTimeProjectOpen>
<ActiveDebugFramework>net8.0-android</ActiveDebugFramework>
<ActiveDebugProfile>Pixel 5 - API 33 (Android 13.0 - API 33)</ActiveDebugProfile>
<SelectedPlatformGroup>Emulator</SelectedPlatformGroup>
<DefaultDevice>pixel_5_-_api_33</DefaultDevice>
<ActiveDebugFramework>net10.0-windows10.0.19041.0</ActiveDebugFramework>
<ActiveDebugProfile>Windows Machine</ActiveDebugProfile>
<SelectedPlatformGroup>RemoteDevice</SelectedPlatformGroup>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetPlatformIdentifier)'=='iOS'">
<RuntimeIdentifier>ios-arm64</RuntimeIdentifier>
<PlatformTarget>arm64</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-android|AnyCPU'">
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net10.0-android|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
</PropertyGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CreateMAUIPDFSample.MainPage">

<ScrollView>
<Button
x:Name="Btn"
Text="Create PDF"
FontAttributes="Bold"
Grid.Row="3"
Clicked="createPdf_Click"
HorizontalOptions="Center"
VerticalOptions="Center"/>
</ScrollView>

</ContentPage>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using CreatePdfDemoSample.Services;
using CreateMAUIPDFSample.Services;
using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
Expand All @@ -8,8 +8,7 @@
using PointF = Syncfusion.Drawing.PointF;
using SizeF = Syncfusion.Drawing.SizeF;


namespace CreatePdfDemoSample
namespace CreateMAUIPDFSample
{
public partial class MainPage : ContentPage
{
Expand All @@ -27,14 +26,11 @@ public void createPdf_Click(object sender, EventArgs e)
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;

RectangleF TotalPriceCellBounds = RectangleF.Empty;
RectangleF QuantityCellBounds = RectangleF.Empty;

//Get the page width and height
float pageWidth = page.GetClientSize().Width;
float pageHeight = page.GetClientSize().Height;

//Set the header height
float headerHeight = 90;
//Create brush with light blue color.
Expand All @@ -45,45 +41,34 @@ public void createPdf_Click(object sender, EventArgs e)
PdfBrush darkBlueBrush = new PdfSolidBrush(darkBlue);
//Create brush with white color.
PdfBrush whiteBrush = new PdfSolidBrush(Color.FromArgb(255, 255, 255, 255));

//Get the font file stream from assembly.
Assembly assembly = typeof(MainPage).GetTypeInfo().Assembly;
string basePath = "CreatePdfDemoSample.Resources.Pdf.";
string basePath = "CreateMAUIPDFSample.Resources.Pdf.";
Stream fontStream = assembly.GetManifestResourceStream(basePath + "arial.ttf");

//Create PdfTrueTypeFont from stream with different size.
PdfTrueTypeFont headerFont = new(fontStream, 30, PdfFontStyle.Regular);
PdfTrueTypeFont arialRegularFont = new(fontStream, 18, PdfFontStyle.Regular);
PdfTrueTypeFont arialBoldFont = new(fontStream, 9, PdfFontStyle.Bold);

//Create string format.
PdfStringFormat format = new();
format.Alignment = PdfTextAlignment.Center;
format.LineAlignment = PdfVerticalAlignment.Middle;

float y = 0;
float x = 0;

//Set the margins of address.
float margin = 30;

//Set the line space.
float lineSpace = 10;

//Create border pen and draw the border to PDF page.
PdfColor borderColor = Color.FromArgb(255, 142, 170, 219);
PdfPen borderPen = new(borderColor, 1f);
graphics.DrawRectangle(borderPen, new RectangleF(0, 0, pageWidth, pageHeight));

//Create a new PdfGrid
PdfGrid grid = new();

//Add five columns to the grid.
grid.Columns.Add(5);

//Create the header row of the grid.
PdfGridRow[] headerRow = grid.Headers.Add(1);

//Set style to the header row and set value to the header cells.
headerRow[0].Style.BackgroundBrush = new PdfSolidBrush(new PdfColor(68, 114, 196));
headerRow[0].Style.TextBrush = PdfBrushes.White;
Expand All @@ -93,15 +78,13 @@ public void createPdf_Click(object sender, EventArgs e)
headerRow[0].Cells[2].Value = "Price ($)";
headerRow[0].Cells[3].Value = "Quantity";
headerRow[0].Cells[4].Value = "Total ($)";

//Add products to the grid table.
AddProducts("CA-1098", "AWC Logo Cap", 8.99, 2, 17.98, grid);
AddProducts("LJ-0192", "Long-Sleeve Logo Jersey,M", 49.99, 3, 149.97, grid);
AddProducts("So-B909-M", "Mountain Bike Socks,M", 9.50, 2, 19, grid);
AddProducts("LJ-0192", "Long-Sleeve Logo Jersey,M", 49.99, 4, 199.96, grid);
AddProducts("FK-5136", "ML Fork", 175.49, 6, 1052.94, grid);
AddProducts("HL-U509", "Sports-100 Helmet,Black", 34.99, 1, 34.99, grid);

#region Header

//Fill the header with light blue brush
Expand Down Expand Up @@ -137,7 +120,6 @@ public void createPdf_Click(object sender, EventArgs e)
y += arialRegularFont.Height + lineSpace;
//Draw text to PDF page with provided font and location.
graphics.DrawString("Date: " + DateTime.Now.ToString("dddd dd, MMMM yyyy"), arialRegularFont, PdfBrushes.Black, new PointF(x, y));

y = headerHeight + margin;
x = margin;
//Draw text to PDF page with provided font and location.
Expand All @@ -150,7 +132,6 @@ public void createPdf_Click(object sender, EventArgs e)
graphics.DrawString("9920 BridgePointe Parkway,", arialRegularFont, PdfBrushes.Black, new PointF(x, y));
y += arialRegularFont.Height + lineSpace;
graphics.DrawString("9365550136", arialRegularFont, PdfBrushes.Black, new PointF(x, y));

#region Grid
//Set width to grid columns.
grid.Columns[0].Width = 110;
Expand Down Expand Up @@ -227,10 +208,8 @@ public void createPdf_Click(object sender, EventArgs e)
borderPen.DashStyle = PdfDashStyle.Custom;
borderPen.DashPattern = new float[] { 3, 3 };
graphics.DrawLine(borderPen, new PointF(0, pageHeight - 100), new PointF(pageWidth, pageHeight - 100));

//Get the image file stream from assembly.
Stream imageStream = assembly.GetManifestResourceStream(basePath + "AdventureWork.png");

//Create PDF bitmap image from stream.
PdfBitmap bitmap = new(imageStream);
//Draw the image to PDF page.
Expand All @@ -241,26 +220,16 @@ public void createPdf_Click(object sender, EventArgs e)
size = arialRegularFont.MeasureString("800 Interchange Blvd.");
x = pageWidth - size.Width - margin;
graphics.DrawString("800 Interchange Blvd.", arialRegularFont, PdfBrushes.Black, new PointF(x, y));

//Calculate the text position and draw the text to PDF page.
y += arialRegularFont.Height + lineSpace;
size = arialRegularFont.MeasureString("Suite 2501, Austin, TX 78721");
x = pageWidth - size.Width - margin;
graphics.DrawString("Suite 2501, Austin, TX 78721", arialRegularFont, PdfBrushes.Black, new PointF(x, y));

//Calculate the text position and draw the text to PDF page.
y += arialRegularFont.Height + lineSpace;
size = arialRegularFont.MeasureString("Any Questions? support@adventure-works.com");
x = pageWidth - size.Width - margin;
graphics.DrawString("Any Questions? support@adventure-works.com", arialRegularFont, PdfBrushes.Black, new PointF(x, y));


//Add a new blank PDF page.
PdfPage page1 = document.Pages.Add();

//Draw the image to the PDF page.
page1.Graphics.DrawImage(bitmap, 0, 0, 200, 200);

using MemoryStream ms = new();
//Save the PDF document to MemoryStream.
document.Save(ms);
Expand All @@ -270,7 +239,6 @@ public void createPdf_Click(object sender, EventArgs e)
//Saves the memory stream as file using the SaveService instance.
SaveService save = new SaveService();
save.SaveAndView("output.pdf", "application/pdf", ms);

void Grid_BeginCellLayout(object sender, PdfGridBeginCellLayoutEventArgs args)
{
PdfGrid grid = sender as PdfGrid;
Expand All @@ -286,8 +254,6 @@ void Grid_BeginCellLayout(object sender, PdfGridBeginCellLayoutEventArgs args)
}
}
}


//Create and row for the grid.
void AddProducts(string productId, string productName, double price, int quantity, double total, PdfGrid grid)
{
Expand All @@ -312,9 +278,6 @@ private float GetTotalAmount(PdfGrid grid)
Total += result;
}
return Total;

}

}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.Extensions.Logging;

namespace CreatePdfDemoSample
namespace CreateMAUIPDFSample
{
public static class MauiProgram
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Android.Content.PM;
using Android.OS;

namespace CreatePdfDemoSample
namespace CreateMAUIPDFSample
{
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Android.App;
using Android.Runtime;

namespace CreatePdfDemoSample
namespace CreateMAUIPDFSample
{
[Application]
public class MainApplication : MauiApplication
Expand Down
Loading
Loading