diff --git a/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document.sln b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document.sln new file mode 100644 index 000000000..7801dfddc --- /dev/null +++ b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36518.9 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Create-Word-Document", "Create-Word-Document\Create-Word-Document.csproj", "{CE151BB6-BA83-4EDB-A1D4-5C6CE40B73D5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {CE151BB6-BA83-4EDB-A1D4-5C6CE40B73D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CE151BB6-BA83-4EDB-A1D4-5C6CE40B73D5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CE151BB6-BA83-4EDB-A1D4-5C6CE40B73D5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CE151BB6-BA83-4EDB-A1D4-5C6CE40B73D5}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {5267B523-467B-45B6-8F87-A14336A32BBE} + EndGlobalSection +EndGlobal diff --git a/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/.gitignore b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/.gitignore new file mode 100644 index 000000000..ff5b00c50 --- /dev/null +++ b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/.gitignore @@ -0,0 +1,264 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# Azure Functions localsettings file +local.settings.json + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +project.fragment.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +#*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc \ No newline at end of file diff --git a/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Create-Word-Document.csproj b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Create-Word-Document.csproj new file mode 100644 index 000000000..09bbd41fd --- /dev/null +++ b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Create-Word-Document.csproj @@ -0,0 +1,42 @@ + + + + net8.0 + v4 + Exe + enable + enable + Create_Word_Document + + + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + + + + + + + + + + + + + Always + + + + diff --git a/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Data/AdventureCycle.jpg b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Data/AdventureCycle.jpg new file mode 100644 index 000000000..86f87c5cb Binary files /dev/null and b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Data/AdventureCycle.jpg differ diff --git a/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Data/Mountain-200.jpg b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Data/Mountain-200.jpg new file mode 100644 index 000000000..21ee3adf8 Binary files /dev/null and b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Data/Mountain-200.jpg differ diff --git a/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Data/Mountain-300.jpg b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Data/Mountain-300.jpg new file mode 100644 index 000000000..867b882db Binary files /dev/null and b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Data/Mountain-300.jpg differ diff --git a/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Data/Road-550-W.jpg b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Data/Road-550-W.jpg new file mode 100644 index 000000000..291232706 Binary files /dev/null and b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Data/Road-550-W.jpg differ diff --git a/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Fonts/Times New Roman.ttf b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Fonts/Times New Roman.ttf new file mode 100644 index 000000000..51261a005 Binary files /dev/null and b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Fonts/Times New Roman.ttf differ diff --git a/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Fonts/calibri-regular.ttf b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Fonts/calibri-regular.ttf new file mode 100644 index 000000000..8b4e81831 Binary files /dev/null and b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Fonts/calibri-regular.ttf differ diff --git a/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Function1.cs b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Function1.cs new file mode 100644 index 000000000..842c2ea85 --- /dev/null +++ b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Function1.cs @@ -0,0 +1,262 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Azure.Functions.Worker; +using Microsoft.Extensions.Logging; +using Syncfusion.DocIO; +using Syncfusion.DocIO.DLS; +using System.Reflection; + +namespace Create_Word_Document; + +public class Function1 +{ + private readonly ILogger _logger; + + public Function1(ILogger logger) + { + _logger = logger; + } + + [Function("CreateWordDocument")] + public async Task Run([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req) + { + try + { + // Creating a new document. + WordDocument document = new WordDocument(); + //Adding a new section to the document. + WSection section = document.AddSection() as WSection; + //Set Margin of the section + section.PageSetup.Margins.All = 72; + //Set page size of the section + section.PageSetup.PageSize = new Syncfusion.Drawing.SizeF(612, 792); + + //Create Paragraph styles + WParagraphStyle style = document.AddParagraphStyle("Normal") as WParagraphStyle; + style.CharacterFormat.FontName = "Calibri"; + style.CharacterFormat.FontSize = 11f; + style.ParagraphFormat.BeforeSpacing = 0; + style.ParagraphFormat.AfterSpacing = 8; + style.ParagraphFormat.LineSpacing = 13.8f; + + style = document.AddParagraphStyle("Heading 1") as WParagraphStyle; + style.ApplyBaseStyle("Normal"); + style.CharacterFormat.FontName = "Calibri Light"; + style.CharacterFormat.FontSize = 16f; + style.CharacterFormat.TextColor = Syncfusion.Drawing.Color.FromArgb(46, 116, 181); + style.ParagraphFormat.BeforeSpacing = 12; + style.ParagraphFormat.AfterSpacing = 0; + style.ParagraphFormat.Keep = true; + style.ParagraphFormat.KeepFollow = true; + style.ParagraphFormat.OutlineLevel = OutlineLevel.Level1; + + IWParagraph paragraph = section.HeadersFooters.Header.AddParagraph(); + // Gets the image stream. + var assembly = Assembly.GetExecutingAssembly(); + var stream = assembly.GetManifestResourceStream("Create_Word_Document.Data.AdventureCycle.jpg"); + IWPicture picture = paragraph.AppendPicture(stream); + picture.TextWrappingStyle = TextWrappingStyle.InFrontOfText; + picture.VerticalOrigin = VerticalOrigin.Margin; + picture.VerticalPosition = -45; + picture.HorizontalOrigin = HorizontalOrigin.Column; + picture.HorizontalPosition = 263.5f; + picture.WidthScale = 20; + picture.HeightScale = 15; + + paragraph.ApplyStyle("Normal"); + paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left; + WTextRange textRange = paragraph.AppendText("Adventure Works Cycles") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Calibri"; + textRange.CharacterFormat.TextColor = Syncfusion.Drawing.Color.Red; + + //Appends paragraph. + paragraph = section.AddParagraph(); + paragraph.ApplyStyle("Heading 1"); + paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center; + textRange = paragraph.AppendText("Adventure Works Cycles") as WTextRange; + textRange.CharacterFormat.FontSize = 18f; + textRange.CharacterFormat.FontName = "Calibri"; + + //Appends paragraph. + paragraph = section.AddParagraph(); + paragraph.ParagraphFormat.FirstLineIndent = 36; + paragraph.BreakCharacterFormat.FontSize = 12f; + textRange = paragraph.AppendText("Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European and Asian commercial markets. While its base operation is in Bothell, Washington with 290 employees, several regional sales teams are located throughout their market base.") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + + //Appends paragraph. + paragraph = section.AddParagraph(); + paragraph.ParagraphFormat.FirstLineIndent = 36; + paragraph.BreakCharacterFormat.FontSize = 12f; + textRange = paragraph.AppendText("In 2000, AdventureWorks Cycles bought a small manufacturing plant, Importadores Neptuno, located in Mexico. Importadores Neptuno manufactures several critical subcomponents for the AdventureWorks Cycles product line. These subcomponents are shipped to the Bothell location for final product assembly. In 2001, Importadores Neptuno, became the sole manufacturer and distributor of the touring bicycle product group.") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + + paragraph = section.AddParagraph(); + paragraph.ApplyStyle("Heading 1"); + paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left; + textRange = paragraph.AppendText("Product Overview") as WTextRange; + textRange.CharacterFormat.FontSize = 16f; + textRange.CharacterFormat.FontName = "Calibri"; + + //Appends table. + IWTable table = section.AddTable(); + table.ResetCells(3, 2); + table.TableFormat.Borders.BorderType = BorderStyle.None; + table.TableFormat.IsAutoResized = true; + //Appends paragraph. + paragraph = table[0, 0].AddParagraph(); + paragraph.ParagraphFormat.AfterSpacing = 0; + paragraph.BreakCharacterFormat.FontSize = 12f; + //Appends picture to the paragraph. + var assembly1 = Assembly.GetExecutingAssembly(); + using (var stream1 = assembly1.GetManifestResourceStream("Create_Word_Document.Data.Mountain-200.jpg")) + picture = paragraph.AppendPicture(stream1); + picture.TextWrappingStyle = TextWrappingStyle.TopAndBottom; + picture.VerticalOrigin = VerticalOrigin.Paragraph; + picture.VerticalPosition = 4.5f; + picture.HorizontalOrigin = HorizontalOrigin.Column; + picture.HorizontalPosition = -2.15f; + picture.WidthScale = 79; + picture.HeightScale = 79; + + //Appends paragraph. + paragraph = table[0, 1].AddParagraph(); + paragraph.ApplyStyle("Heading 1"); + paragraph.ParagraphFormat.AfterSpacing = 0; + paragraph.ParagraphFormat.LineSpacing = 12f; + paragraph.AppendText("Mountain-200"); + //Appends paragraph. + paragraph = table[0, 1].AddParagraph(); + paragraph.ParagraphFormat.AfterSpacing = 0; + paragraph.ParagraphFormat.LineSpacing = 12f; + paragraph.BreakCharacterFormat.FontSize = 12f; + paragraph.BreakCharacterFormat.FontName = "Times New Roman"; + textRange = paragraph.AppendText("Product No: BK-M68B-38\r") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Times New Roman"; + textRange = paragraph.AppendText("Size: 38\r") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Times New Roman"; + textRange = paragraph.AppendText("Weight: 25\r") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Times New Roman"; + textRange = paragraph.AppendText("Price: $2,294.99\r") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Times New Roman"; + //Appends paragraph. + paragraph = table[0, 1].AddParagraph(); + paragraph.ParagraphFormat.AfterSpacing = 0; + paragraph.ParagraphFormat.LineSpacing = 12f; + paragraph.BreakCharacterFormat.FontSize = 12f; + + //Appends paragraph. + paragraph = table[1, 0].AddParagraph(); + paragraph.ApplyStyle("Heading 1"); + paragraph.ParagraphFormat.AfterSpacing = 0; + paragraph.ParagraphFormat.LineSpacing = 12f; + paragraph.AppendText("Mountain-300 "); + //Appends paragraph. + paragraph = table[1, 0].AddParagraph(); + paragraph.ParagraphFormat.AfterSpacing = 0; + paragraph.ParagraphFormat.LineSpacing = 12f; + paragraph.BreakCharacterFormat.FontSize = 12f; + paragraph.BreakCharacterFormat.FontName = "Times New Roman"; + textRange = paragraph.AppendText("Product No: BK-M47B-38\r") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Times New Roman"; + textRange = paragraph.AppendText("Size: 35\r") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Times New Roman"; + textRange = paragraph.AppendText("Weight: 22\r") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Times New Roman"; + textRange = paragraph.AppendText("Price: $1,079.99\r") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Times New Roman"; + //Appends paragraph. + paragraph = table[1, 0].AddParagraph(); + paragraph.ParagraphFormat.AfterSpacing = 0; + paragraph.ParagraphFormat.LineSpacing = 12f; + paragraph.BreakCharacterFormat.FontSize = 12f; + + //Appends paragraph. + paragraph = table[1, 1].AddParagraph(); + paragraph.ApplyStyle("Heading 1"); + paragraph.ParagraphFormat.LineSpacing = 12f; + //Appends picture to the paragraph. + var assembly2 = Assembly.GetExecutingAssembly(); + using (var stream2 = assembly2.GetManifestResourceStream("Create_Word_Document.Data.Mountain-300.jpg")) + picture = paragraph.AppendPicture(stream2); + picture.TextWrappingStyle = TextWrappingStyle.TopAndBottom; + picture.VerticalOrigin = VerticalOrigin.Paragraph; + picture.VerticalPosition = 8.2f; + picture.HorizontalOrigin = HorizontalOrigin.Column; + picture.HorizontalPosition = -14.95f; + picture.WidthScale = 75; + picture.HeightScale = 75; + + //Appends paragraph. + paragraph = table[2, 0].AddParagraph(); + paragraph.ApplyStyle("Heading 1"); + paragraph.ParagraphFormat.LineSpacing = 12f; + //Appends picture to the paragraph. + var assembly3 = Assembly.GetExecutingAssembly(); + using (var stream3 = assembly3.GetManifestResourceStream("Create_Word_Document.Data.Road-550-W.jpg")) + picture = paragraph.AppendPicture(stream3); + picture.TextWrappingStyle = TextWrappingStyle.TopAndBottom; + picture.VerticalOrigin = VerticalOrigin.Paragraph; + picture.VerticalPosition = 3.75f; + picture.HorizontalOrigin = HorizontalOrigin.Column; + picture.HorizontalPosition = -5f; + picture.WidthScale = 92; + picture.HeightScale = 92; + + //Appends paragraph. + paragraph = table[2, 1].AddParagraph(); + paragraph.ApplyStyle("Heading 1"); + paragraph.ParagraphFormat.AfterSpacing = 0; + paragraph.ParagraphFormat.LineSpacing = 12f; + paragraph.AppendText("Road-150 "); + //Appends paragraph. + paragraph = table[2, 1].AddParagraph(); + paragraph.ParagraphFormat.AfterSpacing = 0; + paragraph.ParagraphFormat.LineSpacing = 12f; + paragraph.BreakCharacterFormat.FontSize = 12f; + paragraph.BreakCharacterFormat.FontName = "Times New Roman"; + textRange = paragraph.AppendText("Product No: BK-R93R-44\r") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Times New Roman"; + textRange = paragraph.AppendText("Size: 44\r") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Times New Roman"; + textRange = paragraph.AppendText("Weight: 14\r") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Times New Roman"; + textRange = paragraph.AppendText("Price: $3,578.27\r") as WTextRange; + textRange.CharacterFormat.FontSize = 12f; + textRange.CharacterFormat.FontName = "Times New Roman"; + //Appends paragraph. + section.AddParagraph(); + + MemoryStream memoryStream = new MemoryStream(); + //Saves the Word document file. + document.Save(memoryStream, FormatType.Docx); + memoryStream.Position = 0; + var bytes = memoryStream.ToArray(); + return new FileContentResult(bytes, "application/vnd.openxmlformats-officedocument.wordprocessingml.document") + { + FileDownloadName = "document.docx" + }; + } + catch (Exception ex) + { + // Log the error with details for troubleshooting + _logger.LogError(ex, "Error converting Word document to Image."); + // Prepare error message including exception details + var msg = $"Exception: {ex.Message}\n\n{ex}"; + // Return a 500 Internal Server Error response with the message + return new ContentResult { StatusCode = 500, Content = msg, ContentType = "text/plain; charset=utf-8" }; + } + } +} \ No newline at end of file diff --git a/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Program.cs b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Program.cs new file mode 100644 index 000000000..704475c64 --- /dev/null +++ b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Program.cs @@ -0,0 +1,14 @@ +using Microsoft.Azure.Functions.Worker; +using Microsoft.Azure.Functions.Worker.Builder; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +var builder = FunctionsApplication.CreateBuilder(args); + +builder.ConfigureFunctionsWebApplication(); + +builder.Services + .AddApplicationInsightsTelemetryWorkerService() + .ConfigureFunctionsApplicationInsights(); + +builder.Build().Run(); diff --git a/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Properties/launchSettings.json b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Properties/launchSettings.json new file mode 100644 index 000000000..da03237cd --- /dev/null +++ b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Properties/launchSettings.json @@ -0,0 +1,9 @@ +{ + "profiles": { + "Create_Word_Document": { + "commandName": "Project", + "commandLineArgs": "--port 7150", + "launchBrowser": false + } + } +} \ No newline at end of file diff --git a/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Properties/serviceDependencies.json b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Properties/serviceDependencies.json new file mode 100644 index 000000000..df4dcc9d8 --- /dev/null +++ b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Properties/serviceDependencies.json @@ -0,0 +1,11 @@ +{ + "dependencies": { + "appInsights1": { + "type": "appInsights" + }, + "storage1": { + "type": "storage", + "connectionId": "AzureWebJobsStorage" + } + } +} \ No newline at end of file diff --git a/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Properties/serviceDependencies.local.json b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Properties/serviceDependencies.local.json new file mode 100644 index 000000000..b804a2893 --- /dev/null +++ b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/Properties/serviceDependencies.local.json @@ -0,0 +1,11 @@ +{ + "dependencies": { + "appInsights1": { + "type": "appInsights.sdk" + }, + "storage1": { + "type": "storage.emulator", + "connectionId": "AzureWebJobsStorage" + } + } +} \ No newline at end of file diff --git a/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/host.json b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/host.json new file mode 100644 index 000000000..ee5cf5f83 --- /dev/null +++ b/Getting-Started/Azure/Azure_Functions/Azure_Functions_Flex_Consumption/Create-Word-Document/host.json @@ -0,0 +1,12 @@ +{ + "version": "2.0", + "logging": { + "applicationInsights": { + "samplingSettings": { + "isEnabled": true, + "excludedTypes": "Request" + }, + "enableLiveMetricsFilters": true + } + } +} \ No newline at end of file diff --git a/Getting-Started/Azure/Azure_Functions/Console_App_Flex_Consumption/Create-Word-Document.sln b/Getting-Started/Azure/Azure_Functions/Console_App_Flex_Consumption/Create-Word-Document.sln new file mode 100644 index 000000000..ed11d45df --- /dev/null +++ b/Getting-Started/Azure/Azure_Functions/Console_App_Flex_Consumption/Create-Word-Document.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36518.9 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Create-Word-Document", "Create-Word-Document\Create-Word-Document.csproj", "{3FE039D4-92C4-45EA-893B-3739A8ED8EA0}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3FE039D4-92C4-45EA-893B-3739A8ED8EA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3FE039D4-92C4-45EA-893B-3739A8ED8EA0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3FE039D4-92C4-45EA-893B-3739A8ED8EA0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3FE039D4-92C4-45EA-893B-3739A8ED8EA0}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {E557FE5F-0DD4-461E-98B3-59A290B7F822} + EndGlobalSection +EndGlobal diff --git a/Getting-Started/Azure/Azure_Functions/Console_App_Flex_Consumption/Create-Word-Document/Create-Word-Document.csproj b/Getting-Started/Azure/Azure_Functions/Console_App_Flex_Consumption/Create-Word-Document/Create-Word-Document.csproj new file mode 100644 index 000000000..008202f7d --- /dev/null +++ b/Getting-Started/Azure/Azure_Functions/Console_App_Flex_Consumption/Create-Word-Document/Create-Word-Document.csproj @@ -0,0 +1,17 @@ + + + + Exe + net8.0 + Create_Word_Document + enable + enable + + + + + Always + + + + diff --git a/Getting-Started/Azure/Azure_Functions/Console_App_Flex_Consumption/Create-Word-Document/Output/.gitkeep b/Getting-Started/Azure/Azure_Functions/Console_App_Flex_Consumption/Create-Word-Document/Output/.gitkeep new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/Getting-Started/Azure/Azure_Functions/Console_App_Flex_Consumption/Create-Word-Document/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Getting-Started/Azure/Azure_Functions/Console_App_Flex_Consumption/Create-Word-Document/Output/Output.docx b/Getting-Started/Azure/Azure_Functions/Console_App_Flex_Consumption/Create-Word-Document/Output/Output.docx new file mode 100644 index 000000000..8624c3e4a Binary files /dev/null and b/Getting-Started/Azure/Azure_Functions/Console_App_Flex_Consumption/Create-Word-Document/Output/Output.docx differ diff --git a/Getting-Started/Azure/Azure_Functions/Console_App_Flex_Consumption/Create-Word-Document/Program.cs b/Getting-Started/Azure/Azure_Functions/Console_App_Flex_Consumption/Create-Word-Document/Program.cs new file mode 100644 index 000000000..04d009c9d --- /dev/null +++ b/Getting-Started/Azure/Azure_Functions/Console_App_Flex_Consumption/Create-Word-Document/Program.cs @@ -0,0 +1,40 @@ +using System; +using System.IO; +using System.Net.Http; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + try + { + Console.Write("Please enter your Azure Function URL: "); + string url = Console.ReadLine(); + if (string.IsNullOrWhiteSpace(url)) return; + // Create a new HttpClient instance for sending HTTP requests + using var http = new HttpClient(); + using var content = new StringContent(string.Empty); + using var res = await http.PostAsync(url, content); + // Read the response body as a byte array + var resBytes = await res.Content.ReadAsByteArrayAsync(); + // Extract the media type from the response headers + string mediaType = res.Content.Headers.ContentType?.MediaType ?? string.Empty; + // Decide the output file path the response is an docx or txt + string outputPath = mediaType.Contains("word", StringComparison.OrdinalIgnoreCase) + || mediaType.Contains("officedocument", StringComparison.OrdinalIgnoreCase) + || mediaType.Equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document", StringComparison.OrdinalIgnoreCase) + ? Path.GetFullPath("../../../Output/Output.docx") + : Path.GetFullPath("../../../Output/function-error.txt"); + // Write the response bytes to the output file + await File.WriteAllBytesAsync(outputPath, resBytes); + Console.WriteLine($"Saved: {outputPath}"); + } + catch (Exception ex) + { + throw; + } + + + } +} \ No newline at end of file diff --git a/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document.sln b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document.sln new file mode 100644 index 000000000..c7ca8c62d --- /dev/null +++ b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36518.9 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Open-and-Save-Word-Document", "Open-and-Save-Word-Document\Open-and-Save-Word-Document.csproj", "{D44F928E-428B-479F-9815-C9FE04AC1D0E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D44F928E-428B-479F-9815-C9FE04AC1D0E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D44F928E-428B-479F-9815-C9FE04AC1D0E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D44F928E-428B-479F-9815-C9FE04AC1D0E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D44F928E-428B-479F-9815-C9FE04AC1D0E}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {15D5F4CC-4C85-4DB1-A610-533A6BA31CE0} + EndGlobalSection +EndGlobal diff --git a/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/.gitignore b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/.gitignore new file mode 100644 index 000000000..ff5b00c50 --- /dev/null +++ b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/.gitignore @@ -0,0 +1,264 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# Azure Functions localsettings file +local.settings.json + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +project.fragment.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +#*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc \ No newline at end of file diff --git a/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/Fonts/Times New Roman.ttf b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/Fonts/Times New Roman.ttf new file mode 100644 index 000000000..51261a005 Binary files /dev/null and b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/Fonts/Times New Roman.ttf differ diff --git a/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/Fonts/calibri-regular.ttf b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/Fonts/calibri-regular.ttf new file mode 100644 index 000000000..8b4e81831 Binary files /dev/null and b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/Fonts/calibri-regular.ttf differ diff --git a/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/Function1.cs b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/Function1.cs new file mode 100644 index 000000000..393e9fcc8 --- /dev/null +++ b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/Function1.cs @@ -0,0 +1,63 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Azure.Functions.Worker; +using Microsoft.Extensions.Logging; +using Syncfusion.DocIO; +using Syncfusion.DocIO.DLS; + +namespace Open_and_Save_Word_Document; + +public class Function1 +{ + private readonly ILogger _logger; + + public Function1(ILogger logger) + { + _logger = logger; + } + + [Function("OpenAndSaveDocument")] + public async Task Run([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req) + { + try + { + // Create a memory stream to hold the incoming request body (Word document bytes) + await using MemoryStream inputStream = new MemoryStream(); + // Copy the request body into the memory stream + await req.Body.CopyToAsync(inputStream); + // Check if the stream is empty (no file content received) + if (inputStream.Length == 0) + return new BadRequestObjectResult("No file content received in request body."); + // Reset stream position to the beginning for reading + inputStream.Position = 0; + // Load the Word document from the stream (auto-detects format type) + using WordDocument document = new WordDocument(inputStream, Syncfusion.DocIO.FormatType.Automatic); + //Access the section in a Word document. + IWSection section = document.Sections[0]; + //Add a new paragraph to the section. + IWParagraph paragraph = section.AddParagraph(); + paragraph.ParagraphFormat.FirstLineIndent = 36; + paragraph.BreakCharacterFormat.FontSize = 12f; + IWTextRange text = paragraph.AppendText("In 2000, Adventure Works Cycles bought a small manufacturing plant, Importadores Neptuno, located in Mexico. Importadores Neptuno manufactures several critical subcomponents for the Adventure Works Cycles product line. These subcomponents are shipped to the Bothell location for final product assembly. In 2001, Importadores Neptuno, became the sole manufacturer and distributor of the touring bicycle product group."); + text.CharacterFormat.FontSize = 12f; + MemoryStream memoryStream = new MemoryStream(); + //Saves the Word document file. + document.Save(memoryStream, FormatType.Docx); + memoryStream.Position = 0; + var bytes = memoryStream.ToArray(); + return new FileContentResult(bytes, "application/vnd.openxmlformats-officedocument.wordprocessingml.document") + { + FileDownloadName = "document.docx" + }; + } + catch (Exception ex) + { + // Log the error with details for troubleshooting + _logger.LogError(ex, "Error Open and Save document."); + // Prepare error message including exception details + var msg = $"Exception: {ex.Message}\n\n{ex}"; + // Return a 500 Internal Server Error response with the message + return new ContentResult { StatusCode = 500, Content = msg, ContentType = "text/plain; charset=utf-8" }; + } + } +} \ No newline at end of file diff --git a/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/Open-and-Save-Word-Document.csproj b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/Open-and-Save-Word-Document.csproj new file mode 100644 index 000000000..38b698e51 --- /dev/null +++ b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/Open-and-Save-Word-Document.csproj @@ -0,0 +1,28 @@ + + + + net8.0 + v4 + Exe + enable + enable + Open_and_Save_Word_Document + + + + + + + + + + + + + + + Always + + + + diff --git a/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/Program.cs b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/Program.cs new file mode 100644 index 000000000..704475c64 --- /dev/null +++ b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/Program.cs @@ -0,0 +1,14 @@ +using Microsoft.Azure.Functions.Worker; +using Microsoft.Azure.Functions.Worker.Builder; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +var builder = FunctionsApplication.CreateBuilder(args); + +builder.ConfigureFunctionsWebApplication(); + +builder.Services + .AddApplicationInsightsTelemetryWorkerService() + .ConfigureFunctionsApplicationInsights(); + +builder.Build().Run(); diff --git a/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/Properties/launchSettings.json b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/Properties/launchSettings.json new file mode 100644 index 000000000..6d7b4653d --- /dev/null +++ b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/Properties/launchSettings.json @@ -0,0 +1,9 @@ +{ + "profiles": { + "Open_and_Save_Word_Document": { + "commandName": "Project", + "commandLineArgs": "--port 7245", + "launchBrowser": false + } + } +} \ No newline at end of file diff --git a/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/Properties/serviceDependencies.json b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/Properties/serviceDependencies.json new file mode 100644 index 000000000..df4dcc9d8 --- /dev/null +++ b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/Properties/serviceDependencies.json @@ -0,0 +1,11 @@ +{ + "dependencies": { + "appInsights1": { + "type": "appInsights" + }, + "storage1": { + "type": "storage", + "connectionId": "AzureWebJobsStorage" + } + } +} \ No newline at end of file diff --git a/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/Properties/serviceDependencies.local.json b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/Properties/serviceDependencies.local.json new file mode 100644 index 000000000..b804a2893 --- /dev/null +++ b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/Properties/serviceDependencies.local.json @@ -0,0 +1,11 @@ +{ + "dependencies": { + "appInsights1": { + "type": "appInsights.sdk" + }, + "storage1": { + "type": "storage.emulator", + "connectionId": "AzureWebJobsStorage" + } + } +} \ No newline at end of file diff --git a/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/host.json b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/host.json new file mode 100644 index 000000000..ee5cf5f83 --- /dev/null +++ b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Open-and-Save-Word-Document/host.json @@ -0,0 +1,12 @@ +{ + "version": "2.0", + "logging": { + "applicationInsights": { + "samplingSettings": { + "isEnabled": true, + "excludedTypes": "Request" + }, + "enableLiveMetricsFilters": true + } + } +} \ No newline at end of file diff --git a/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Console_App_Flex_Consumption/Open-and-Save-Word-Document.sln b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Console_App_Flex_Consumption/Open-and-Save-Word-Document.sln new file mode 100644 index 000000000..9418fdcd1 --- /dev/null +++ b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Console_App_Flex_Consumption/Open-and-Save-Word-Document.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36518.9 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Open-and-Save-Word-Document", "Open-and-Save-Word-Document\Open-and-Save-Word-Document.csproj", "{DAF312BC-78C5-45D7-8ADE-DD1B6E4625EB}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {DAF312BC-78C5-45D7-8ADE-DD1B6E4625EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DAF312BC-78C5-45D7-8ADE-DD1B6E4625EB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DAF312BC-78C5-45D7-8ADE-DD1B6E4625EB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DAF312BC-78C5-45D7-8ADE-DD1B6E4625EB}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {45682F7A-71E6-4883-B958-1E2CAACC455A} + EndGlobalSection +EndGlobal diff --git a/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Console_App_Flex_Consumption/Open-and-Save-Word-Document/Data/Input.docx b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Console_App_Flex_Consumption/Open-and-Save-Word-Document/Data/Input.docx new file mode 100644 index 000000000..efb84ef4a Binary files /dev/null and b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Console_App_Flex_Consumption/Open-and-Save-Word-Document/Data/Input.docx differ diff --git a/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Console_App_Flex_Consumption/Open-and-Save-Word-Document/Open-and-Save-Word-Document.csproj b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Console_App_Flex_Consumption/Open-and-Save-Word-Document/Open-and-Save-Word-Document.csproj new file mode 100644 index 000000000..432c9c9b7 --- /dev/null +++ b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Console_App_Flex_Consumption/Open-and-Save-Word-Document/Open-and-Save-Word-Document.csproj @@ -0,0 +1,20 @@ + + + + Exe + net8.0 + Open_and_Save_Word_Document + enable + enable + + + + + Always + + + Always + + + + diff --git a/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Console_App_Flex_Consumption/Open-and-Save-Word-Document/Output/.gitkeep b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Console_App_Flex_Consumption/Open-and-Save-Word-Document/Output/.gitkeep new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Console_App_Flex_Consumption/Open-and-Save-Word-Document/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Console_App_Flex_Consumption/Open-and-Save-Word-Document/Output/Output.docx b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Console_App_Flex_Consumption/Open-and-Save-Word-Document/Output/Output.docx new file mode 100644 index 000000000..14cf3a72e Binary files /dev/null and b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Console_App_Flex_Consumption/Open-and-Save-Word-Document/Output/Output.docx differ diff --git a/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Console_App_Flex_Consumption/Open-and-Save-Word-Document/Program.cs b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Console_App_Flex_Consumption/Open-and-Save-Word-Document/Program.cs new file mode 100644 index 000000000..d28d3712a --- /dev/null +++ b/Read-and-Save-document/Open-and-save-Word-document/Azure/Azure_Functions/Console_App_Flex_Consumption/Open-and-Save-Word-Document/Program.cs @@ -0,0 +1,47 @@ +using System; +using System.IO; +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + try + { + Console.Write("Please enter your Azure Functions URL : "); + string url = Console.ReadLine(); + if (string.IsNullOrEmpty(url)) return; + // Create a new HttpClient instance for sending HTTP requests + using var http = new HttpClient(); + // Read all bytes from the input Word document + byte[] bytes = await File.ReadAllBytesAsync(@"Data/Input.docx"); + // Wrap the file bytes into a ByteArrayContent object for HTTP transmission + using var content = new ByteArrayContent(bytes); + // Set the content type header to indicate binary data + content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream"); + // Send a POST request to the provided Azure Functions URL with the file content + using var res = await http.PostAsync(url, content); + // Read the response body as a byte array + var resBytes = await res.Content.ReadAsByteArrayAsync(); + // Extract the media type from the response headers + string mediaType = res.Content.Headers.ContentType?.MediaType ?? string.Empty; + // Decide the output file path the response is an image or txt + string outputPath = mediaType.Contains("word", StringComparison.OrdinalIgnoreCase) + || mediaType.Contains("officedocument", StringComparison.OrdinalIgnoreCase) + || mediaType.Equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document", StringComparison.OrdinalIgnoreCase) + ? Path.GetFullPath(@"../../../Output/Output.docx") + : Path.GetFullPath(@"../../../function-error.txt"); + // Write the response bytes to the output file + await File.WriteAllBytesAsync(outputPath, resBytes); + Console.WriteLine($"Saved: {outputPath}"); + } + catch (Exception ex) + { + throw; + } + + + } +} \ No newline at end of file diff --git a/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image.sln b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image.sln new file mode 100644 index 000000000..1c3560f07 --- /dev/null +++ b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36518.9 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Convert-Word-document-to-Image", "Convert-Word-document-to-Image\Convert-Word-document-to-Image.csproj", "{27E1A66A-E7A9-40B7-B492-E83A47434125}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {27E1A66A-E7A9-40B7-B492-E83A47434125}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {27E1A66A-E7A9-40B7-B492-E83A47434125}.Debug|Any CPU.Build.0 = Debug|Any CPU + {27E1A66A-E7A9-40B7-B492-E83A47434125}.Release|Any CPU.ActiveCfg = Release|Any CPU + {27E1A66A-E7A9-40B7-B492-E83A47434125}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {3EDDD737-74D3-4676-8F55-EFED88F635C7} + EndGlobalSection +EndGlobal diff --git a/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/.gitignore b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/.gitignore new file mode 100644 index 000000000..ff5b00c50 --- /dev/null +++ b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/.gitignore @@ -0,0 +1,264 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# Azure Functions localsettings file +local.settings.json + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +project.fragment.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +#*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc \ No newline at end of file diff --git a/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Convert-Word-document-to-Image.csproj b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Convert-Word-document-to-Image.csproj new file mode 100644 index 000000000..ba72b2e22 --- /dev/null +++ b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Convert-Word-document-to-Image.csproj @@ -0,0 +1,29 @@ + + + + net8.0 + v4 + Exe + enable + enable + Convert_Word_document_to_Image + + + + + + + + + + + + + + + + Always + + + + diff --git a/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Fonts/Times New Roman.ttf b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Fonts/Times New Roman.ttf new file mode 100644 index 000000000..51261a005 Binary files /dev/null and b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Fonts/Times New Roman.ttf differ diff --git a/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Fonts/calibri-regular.ttf b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Fonts/calibri-regular.ttf new file mode 100644 index 000000000..8b4e81831 Binary files /dev/null and b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Fonts/calibri-regular.ttf differ diff --git a/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Function1.cs b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Function1.cs new file mode 100644 index 000000000..61cb7df76 --- /dev/null +++ b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Function1.cs @@ -0,0 +1,90 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Azure.Functions.Worker; +using Microsoft.Extensions.Logging; +using Syncfusion.DocIO; +using Syncfusion.DocIO.DLS; +using Syncfusion.DocIORenderer; + +namespace Convert_Word_document_to_Image; + +public class Function1 +{ + private readonly ILogger _logger; + + public Function1(ILogger logger) + { + _logger = logger; + } + + [Function("ConvertWordDocumenttoImage")] + public async Task Run([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req) + { + try + { + // Create a memory stream to hold the incoming request body (Word document bytes) + await using MemoryStream inputStream = new MemoryStream(); + // Copy the request body into the memory stream + await req.Body.CopyToAsync(inputStream); + // Check if the stream is empty (no file content received) + if (inputStream.Length == 0) + return new BadRequestObjectResult("No file content received in request body."); + // Reset stream position to the beginning for reading + inputStream.Position = 0; + // Load the Word document from the stream (auto-detects format type) + using WordDocument document = new WordDocument(inputStream, Syncfusion.DocIO.FormatType.Automatic); + // Attach font substitution handler to manage missing fonts + document.FontSettings.SubstituteFont += FontSettings_SubstituteFont; + // Initialize the DocIORenderer to perform image conversion. + DocIORenderer render = new DocIORenderer(); + // Convert Word document to image as stream. + Stream imageStream = document.RenderAsImages(0, ExportImageFormat.Png); + // Reset the stream position. + imageStream.Position = 0; + // Create a memory stream to hold the Image output + await using MemoryStream outputStream = new MemoryStream(); + // Copy the contents of the image stream to the memory stream. + await imageStream.CopyToAsync(outputStream); + // Convert the Image stream to a byte array + var imageBytes = outputStream.ToArray(); + //Reset the stream position. + imageStream.Position = 0; + // Reset stream position to the beginning for reading + outputStream.Position = 0; + // Create a file result to return the PNG as a downloadable file + return new FileContentResult(imageBytes, "image/png") + { + FileDownloadName = "document-1.png" + }; + } + catch (Exception ex) + { + // Log the error with details for troubleshooting + _logger.LogError(ex, "Error converting Word document to Image."); + // Prepare error message including exception details + var msg = $"Exception: {ex.Message}\n\n{ex}"; + // Return a 500 Internal Server Error response with the message + return new ContentResult { StatusCode = 500, Content = msg, ContentType = "text/plain; charset=utf-8" }; + } + } + /// + /// Event handler for font substitution during Image conversion + /// + /// + /// + private void FontSettings_SubstituteFont(object sender, SubstituteFontEventArgs args) + { + // Define the path to the Fonts folder in the application base directory + string fontsFolder = Path.Combine(AppContext.BaseDirectory, "Fonts"); + // If the original font is Calibri, substitute with calibri-regular.ttf + if (args.OriginalFontName == "Calibri") + { + args.AlternateFontStream = File.OpenRead(Path.Combine(fontsFolder, "calibri-regular.ttf")); + } + // Otherwise, substitute with Times New Roman + else + { + args.AlternateFontStream = File.OpenRead(Path.Combine(fontsFolder, "Times New Roman.ttf")); + } + } +} \ No newline at end of file diff --git a/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Program.cs b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Program.cs new file mode 100644 index 000000000..704475c64 --- /dev/null +++ b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Program.cs @@ -0,0 +1,14 @@ +using Microsoft.Azure.Functions.Worker; +using Microsoft.Azure.Functions.Worker.Builder; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +var builder = FunctionsApplication.CreateBuilder(args); + +builder.ConfigureFunctionsWebApplication(); + +builder.Services + .AddApplicationInsightsTelemetryWorkerService() + .ConfigureFunctionsApplicationInsights(); + +builder.Build().Run(); diff --git a/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Properties/PublishProfiles/app214398 - Zip Deploy.pubxml b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Properties/PublishProfiles/app214398 - Zip Deploy.pubxml new file mode 100644 index 000000000..5f0a614b4 --- /dev/null +++ b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Properties/PublishProfiles/app214398 - Zip Deploy.pubxml @@ -0,0 +1,17 @@ + + + + + ZipDeploy + AzureWebSite + Release + Any CPU + https://app214398-h8ekaggncdhfctfd.centralindia-01.azurewebsites.net + false + /subscriptions/77f174f1-17bb-44ad-a52f-01c4c3ad6be3/resourceGroups/Ticket-214398/providers/Microsoft.Web/sites/app214398 + true + linux-x64 + https://app214398-h8ekaggncdhfctfd.scm.centralindia-01.azurewebsites.net/ + true + + \ No newline at end of file diff --git a/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Properties/launchSettings.json b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Properties/launchSettings.json new file mode 100644 index 000000000..12c0126d9 --- /dev/null +++ b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Properties/launchSettings.json @@ -0,0 +1,9 @@ +{ + "profiles": { + "Convert_Word_document_to_Image": { + "commandName": "Project", + "commandLineArgs": "--port 7041", + "launchBrowser": false + } + } +} \ No newline at end of file diff --git a/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Properties/serviceDependencies.json b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Properties/serviceDependencies.json new file mode 100644 index 000000000..df4dcc9d8 --- /dev/null +++ b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Properties/serviceDependencies.json @@ -0,0 +1,11 @@ +{ + "dependencies": { + "appInsights1": { + "type": "appInsights" + }, + "storage1": { + "type": "storage", + "connectionId": "AzureWebJobsStorage" + } + } +} \ No newline at end of file diff --git a/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Properties/serviceDependencies.local.json b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Properties/serviceDependencies.local.json new file mode 100644 index 000000000..b804a2893 --- /dev/null +++ b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/Properties/serviceDependencies.local.json @@ -0,0 +1,11 @@ +{ + "dependencies": { + "appInsights1": { + "type": "appInsights.sdk" + }, + "storage1": { + "type": "storage.emulator", + "connectionId": "AzureWebJobsStorage" + } + } +} \ No newline at end of file diff --git a/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/host.json b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/host.json new file mode 100644 index 000000000..ee5cf5f83 --- /dev/null +++ b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-Image/host.json @@ -0,0 +1,12 @@ +{ + "version": "2.0", + "logging": { + "applicationInsights": { + "samplingSettings": { + "isEnabled": true, + "excludedTypes": "Request" + }, + "enableLiveMetricsFilters": true + } + } +} \ No newline at end of file diff --git a/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Console_App_Flex_Consumption/Convert-Word-to-Image.sln b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Console_App_Flex_Consumption/Convert-Word-to-Image.sln new file mode 100644 index 000000000..16af38782 --- /dev/null +++ b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Console_App_Flex_Consumption/Convert-Word-to-Image.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36518.9 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Convert-Word-to-Image", "Convert-Word-to-Image\Convert-Word-to-Image.csproj", "{7779F22A-E69B-413A-AD62-0A6AE2D310C4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7779F22A-E69B-413A-AD62-0A6AE2D310C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7779F22A-E69B-413A-AD62-0A6AE2D310C4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7779F22A-E69B-413A-AD62-0A6AE2D310C4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7779F22A-E69B-413A-AD62-0A6AE2D310C4}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {98F70608-92CD-45F6-A833-574B61BB8246} + EndGlobalSection +EndGlobal diff --git a/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Console_App_Flex_Consumption/Convert-Word-to-Image/Convert-Word-to-Image.csproj b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Console_App_Flex_Consumption/Convert-Word-to-Image/Convert-Word-to-Image.csproj new file mode 100644 index 000000000..499fe7c25 --- /dev/null +++ b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Console_App_Flex_Consumption/Convert-Word-to-Image/Convert-Word-to-Image.csproj @@ -0,0 +1,17 @@ + + + + Exe + net8.0 + Convert_Word_to_Image + enable + enable + + + + + Always + + + + diff --git a/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Console_App_Flex_Consumption/Convert-Word-to-Image/Data/Input.docx b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Console_App_Flex_Consumption/Convert-Word-to-Image/Data/Input.docx new file mode 100644 index 000000000..d5a2b51ef Binary files /dev/null and b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Console_App_Flex_Consumption/Convert-Word-to-Image/Data/Input.docx differ diff --git a/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Console_App_Flex_Consumption/Convert-Word-to-Image/Output/.gitkeep b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Console_App_Flex_Consumption/Convert-Word-to-Image/Output/.gitkeep new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Console_App_Flex_Consumption/Convert-Word-to-Image/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Console_App_Flex_Consumption/Convert-Word-to-Image/Output/image-1.png b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Console_App_Flex_Consumption/Convert-Word-to-Image/Output/image-1.png new file mode 100644 index 000000000..53f62743d Binary files /dev/null and b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Console_App_Flex_Consumption/Convert-Word-to-Image/Output/image-1.png differ diff --git a/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Console_App_Flex_Consumption/Convert-Word-to-Image/Program.cs b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Console_App_Flex_Consumption/Convert-Word-to-Image/Program.cs new file mode 100644 index 000000000..2a4359cba --- /dev/null +++ b/Word-to-Image-conversion/Convert-Word-to-image/Azure/Azure_Functions/Console_App_Flex_Consumption/Convert-Word-to-Image/Program.cs @@ -0,0 +1,45 @@ +using System; +using System.IO; +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + try + { + Console.Write("Please enter your Azure Functions URL : "); + string url = Console.ReadLine(); + if (string.IsNullOrEmpty(url)) return; + // Create a new HttpClient instance for sending HTTP requests + using var http = new HttpClient(); + // Read all bytes from the input Word document + byte[] bytes = await File.ReadAllBytesAsync(@"Data/Input.docx"); + // Wrap the file bytes into a ByteArrayContent object for HTTP transmission + using var content = new ByteArrayContent(bytes); + // Set the content type header to indicate binary data + content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream"); + // Send a POST request to the provided Azure Functions URL with the file content + using var res = await http.PostAsync(url, content); + // Read the response body as a byte array + var resBytes = await res.Content.ReadAsByteArrayAsync(); + // Extract the media type from the response headers (e.g., "image/png") + string mediaType = res.Content.Headers.ContentType?.MediaType ?? string.Empty; + // Decide the output file path the response is an image or txt + string outputPath = mediaType.Contains("image", StringComparison.OrdinalIgnoreCase) + ? Path.GetFullPath(@"../../../Output/image-1.png") + : Path.GetFullPath(@"../../../function-error.txt"); + // Write the response bytes to the output file + await File.WriteAllBytesAsync(outputPath, resBytes); + Console.WriteLine($"Saved: {outputPath}"); + } + catch (Exception ex) + { + throw; + } + + + } +} \ No newline at end of file diff --git a/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF.sln b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF.sln new file mode 100644 index 000000000..43fe027fa --- /dev/null +++ b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36518.9 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Convert-Word-document-to-PDF", "Convert-Word-document-to-PDF\Convert-Word-document-to-PDF.csproj", "{7F5EA5A5-BC0C-423E-8DFD-638FF6D1CB8F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7F5EA5A5-BC0C-423E-8DFD-638FF6D1CB8F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7F5EA5A5-BC0C-423E-8DFD-638FF6D1CB8F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7F5EA5A5-BC0C-423E-8DFD-638FF6D1CB8F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7F5EA5A5-BC0C-423E-8DFD-638FF6D1CB8F}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {D23FC586-A5A3-4DED-84C7-C2FF9B2D87E6} + EndGlobalSection +EndGlobal diff --git a/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/.gitignore b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/.gitignore new file mode 100644 index 000000000..ff5b00c50 --- /dev/null +++ b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/.gitignore @@ -0,0 +1,264 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# Azure Functions localsettings file +local.settings.json + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +project.fragment.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +#*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc \ No newline at end of file diff --git a/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/Convert-Word-document-to-PDF.csproj b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/Convert-Word-document-to-PDF.csproj new file mode 100644 index 000000000..41e14a046 --- /dev/null +++ b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/Convert-Word-document-to-PDF.csproj @@ -0,0 +1,29 @@ + + + + net8.0 + v4 + Exe + enable + enable + Convert_Word_document_to_PDF + + + + + + + + + + + + + + + + Always + + + + diff --git a/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/Fonts/Times New Roman.ttf b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/Fonts/Times New Roman.ttf new file mode 100644 index 000000000..51261a005 Binary files /dev/null and b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/Fonts/Times New Roman.ttf differ diff --git a/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/Fonts/calibri-regular.ttf b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/Fonts/calibri-regular.ttf new file mode 100644 index 000000000..8b4e81831 Binary files /dev/null and b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/Fonts/calibri-regular.ttf differ diff --git a/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/Function1.cs b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/Function1.cs new file mode 100644 index 000000000..4e59ae385 --- /dev/null +++ b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/Function1.cs @@ -0,0 +1,92 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Azure.Functions.Worker; +using Microsoft.Extensions.Logging; +using Syncfusion.DocIO; +using Syncfusion.DocIO.DLS; +using Syncfusion.DocIORenderer; +using Syncfusion.Pdf; + +namespace Convert_Word_document_to_PDF; + +public class Function1 +{ + private readonly ILogger _logger; + + public Function1(ILogger logger) + { + _logger = logger; + } + + [Function("ConvertWordtoPDF")] + public async Task Run([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req) + { + try + { + // Create a memory stream to hold the incoming request body (Word document bytes) + await using MemoryStream inputStream = new MemoryStream(); + // Copy the request body into the memory stream + await req.Body.CopyToAsync(inputStream); + // Check if the stream is empty (no file content received) + if (inputStream.Length == 0) + return new BadRequestObjectResult("No file content received in request body."); + // Reset stream position to the beginning for reading + inputStream.Position = 0; + // Load the Word document from the stream (auto-detects format type) + using WordDocument document = new WordDocument(inputStream, FormatType.Automatic); + // Attach font substitution handler to manage missing fonts + document.FontSettings.SubstituteFont += FontSettings_SubstituteFont; + // Initialize DocIORenderer to convert Word document to PDF + using DocIORenderer renderer = new DocIORenderer(); + // Convert the Word document to a PDF document + using PdfDocument pdfDoc = renderer.ConvertToPDF(document); + // Create a memory stream to hold the PDF output + await using MemoryStream outputStream = new MemoryStream(); + // Save the PDF into the output stream + pdfDoc.Save(outputStream); + // Close the PDF document and release resources + pdfDoc.Close(true); + // Reset stream position to the beginning for reading + outputStream.Position = 0; + // Convert the PDF stream to a byte array + var pdfBytes = outputStream.ToArray(); + + // Create a file result to return the PDF as a downloadable file + var fileResult = new FileContentResult(pdfBytes, "application/pdf") + { + FileDownloadName = "converted.pdf" + }; + // Return the PDF file result to the client + return fileResult; + } + catch (Exception ex) + { + // Log the error with details for troubleshooting + _logger.LogError(ex, "Error converting DOCX to PDF."); + // Prepare error message including exception details + var msg = $"Exception: {ex.Message}\n\n{ex}"; + // Return a 500 Internal Server Error response with the message + return new ContentResult { StatusCode = 500, Content = msg, ContentType = "text/plain; charset=utf-8" }; + } + } + /// + /// Event handler for font substitution during PDF conversion + /// + /// + /// + private void FontSettings_SubstituteFont(object sender, SubstituteFontEventArgs args) + { + // Define the path to the Fonts folder in the application base directory + string fontsFolder = Path.Combine(AppContext.BaseDirectory, "Fonts"); + // If the original font is Calibri, substitute with calibri-regular.ttf + if (args.OriginalFontName == "Calibri") + { + args.AlternateFontStream = File.OpenRead(Path.Combine(fontsFolder, "calibri-regular.ttf")); + } + // Otherwise, substitute with Times New Roman + else + { + args.AlternateFontStream = File.OpenRead(Path.Combine(fontsFolder, "Times New Roman.ttf")); + } + } +} \ No newline at end of file diff --git a/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/Program.cs b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/Program.cs new file mode 100644 index 000000000..704475c64 --- /dev/null +++ b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/Program.cs @@ -0,0 +1,14 @@ +using Microsoft.Azure.Functions.Worker; +using Microsoft.Azure.Functions.Worker.Builder; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +var builder = FunctionsApplication.CreateBuilder(args); + +builder.ConfigureFunctionsWebApplication(); + +builder.Services + .AddApplicationInsightsTelemetryWorkerService() + .ConfigureFunctionsApplicationInsights(); + +builder.Build().Run(); diff --git a/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/Properties/launchSettings.json b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/Properties/launchSettings.json new file mode 100644 index 000000000..c71392df0 --- /dev/null +++ b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/Properties/launchSettings.json @@ -0,0 +1,9 @@ +{ + "profiles": { + "Convert_Word_document_to_PDF": { + "commandName": "Project", + "commandLineArgs": "--port 7094", + "launchBrowser": false + } + } +} \ No newline at end of file diff --git a/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/Properties/serviceDependencies.json b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/Properties/serviceDependencies.json new file mode 100644 index 000000000..4dd197a45 --- /dev/null +++ b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/Properties/serviceDependencies.json @@ -0,0 +1,12 @@ +{ + "dependencies": { + "appInsights1": { + "type": "appInsights", + "connectionId": "APPLICATIONINSIGHTS_CONNECTION_STRING" + }, + "storage1": { + "type": "storage", + "connectionId": "AzureWebJobsStorage" + } + } +} \ No newline at end of file diff --git a/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/Properties/serviceDependencies.local.json b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/Properties/serviceDependencies.local.json new file mode 100644 index 000000000..b804a2893 --- /dev/null +++ b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/Properties/serviceDependencies.local.json @@ -0,0 +1,11 @@ +{ + "dependencies": { + "appInsights1": { + "type": "appInsights.sdk" + }, + "storage1": { + "type": "storage.emulator", + "connectionId": "AzureWebJobsStorage" + } + } +} \ No newline at end of file diff --git a/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/host.json b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/host.json new file mode 100644 index 000000000..ee5cf5f83 --- /dev/null +++ b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Azure_Function_Flex_Consumption/Convert-Word-document-to-PDF/host.json @@ -0,0 +1,12 @@ +{ + "version": "2.0", + "logging": { + "applicationInsights": { + "samplingSettings": { + "isEnabled": true, + "excludedTypes": "Request" + }, + "enableLiveMetricsFilters": true + } + } +} \ No newline at end of file diff --git a/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Console_App_Flex_Consumption/WordToPDF.sln b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Console_App_Flex_Consumption/WordToPDF.sln new file mode 100644 index 000000000..d64a64d60 --- /dev/null +++ b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Console_App_Flex_Consumption/WordToPDF.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36518.9 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WordToPDF", "WordToPDF\WordToPDF.csproj", "{1DD39977-80B8-494E-8155-2A98D29713DB}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {1DD39977-80B8-494E-8155-2A98D29713DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1DD39977-80B8-494E-8155-2A98D29713DB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1DD39977-80B8-494E-8155-2A98D29713DB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1DD39977-80B8-494E-8155-2A98D29713DB}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {63C4142A-8C4D-4042-AE41-71AAEB1069CA} + EndGlobalSection +EndGlobal diff --git a/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Console_App_Flex_Consumption/WordToPDF/Data/Input.docx b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Console_App_Flex_Consumption/WordToPDF/Data/Input.docx new file mode 100644 index 000000000..d5a2b51ef Binary files /dev/null and b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Console_App_Flex_Consumption/WordToPDF/Data/Input.docx differ diff --git a/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Console_App_Flex_Consumption/WordToPDF/Output/.gitkeep b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Console_App_Flex_Consumption/WordToPDF/Output/.gitkeep new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Console_App_Flex_Consumption/WordToPDF/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Console_App_Flex_Consumption/WordToPDF/Output/Output.pdf b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Console_App_Flex_Consumption/WordToPDF/Output/Output.pdf new file mode 100644 index 000000000..f0295eb2f Binary files /dev/null and b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Console_App_Flex_Consumption/WordToPDF/Output/Output.pdf differ diff --git a/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Console_App_Flex_Consumption/WordToPDF/Program.cs b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Console_App_Flex_Consumption/WordToPDF/Program.cs new file mode 100644 index 000000000..4148f01c5 --- /dev/null +++ b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Console_App_Flex_Consumption/WordToPDF/Program.cs @@ -0,0 +1,36 @@ +using System; +using System.IO; +using System.Net.Http; +using System.Threading.Tasks; + +class Program +{ + static async Task Main() + { + Console.Write("Please enter your Azure Functions URL : "); + // Read the URL entered by the user and trim whitespace + string url = Console.ReadLine()?.Trim(); + // If no URL was entered, exit the program + if (string.IsNullOrEmpty(url)) return; + // Create a new HttpClient instance for sending requests + using var http = new HttpClient(); + // Read all bytes from the input Word document file + var bytes = await File.ReadAllBytesAsync(@"Data/Input.docx"); + // Create HTTP content from the document bytes + using var content = new ByteArrayContent(bytes); + // Set the content type header to application/octet-stream (binary data) + content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream"); + // Send a POST request to the Azure Function with the document content + using var res = await http.PostAsync(url, content); + // Read the response content as a byte array + var resBytes = await res.Content.ReadAsByteArrayAsync(); + // Get the media type (e.g., application/pdf or text/plain) from the response headers + string mediaType = res.Content.Headers.ContentType?.MediaType ?? string.Empty; + string outFile = mediaType.Contains("pdf", StringComparison.OrdinalIgnoreCase) + ? Path.GetFullPath(@"../../../Output/Output.pdf") + : Path.GetFullPath(@"../../../Output/function-error.txt"); + // Write the response bytes to the chosen output file + await File.WriteAllBytesAsync(outFile, resBytes); + Console.WriteLine($"Saved: {outFile} "); + } +} \ No newline at end of file diff --git a/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Console_App_Flex_Consumption/WordToPDF/WordToPDF.csproj b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Console_App_Flex_Consumption/WordToPDF/WordToPDF.csproj new file mode 100644 index 000000000..bcf2c883b --- /dev/null +++ b/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/Azure/Azure_Functions/Console_App_Flex_Consumption/WordToPDF/WordToPDF.csproj @@ -0,0 +1,16 @@ + + + + Exe + net8.0 + enable + enable + + + + + Always + + + +