Skip to content

Commit

Permalink
Convert diagram to PPTX sample added
Browse files Browse the repository at this point in the history
  • Loading branch information
shyamg1991 committed Jul 24, 2023
1 parent 2a7358c commit 2422c6a
Show file tree
Hide file tree
Showing 145 changed files with 50,156 additions and 0 deletions.
Binary file not shown.

Large diffs are not rendered by default.

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using WebApplication1.Models;
using Syncfusion.EJ2.Diagrams;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Syncfusion.Presentation;
using System.IO;
using System.Globalization;

namespace WebApplication1.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;

public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}

public IActionResult Index()
{
ViewBag.nodes = OrganizationalDetails.GetData();
ViewBag.getConnectorDefaults = "ConnectorDefaults";
ViewBag.getNodeDefaults = "nodeDefaults";
ViewBag.GetLayoutInfo = "getLayoutInfo";
return View();

}

[HttpPost]
public ActionResult save([FromBody] Dictionary<string, object> NCProperty)
{
//Create an instance for PowerPoint presentation
IPresentation pptxDoc = Presentation.Create();
//Add slide to the presentation
ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank);
//Resize a slide size to diagram canvas size.
slide.SlideSize.Width = 1000;
slide.SlideSize.Height = 1000;

//parse a node collection
List<DiagramNode> Nodes = JsonConvert.DeserializeObject<List<DiagramNode>>(NCProperty["nodeProperty"].ToString());
foreach (var node in Nodes)
{
//Adds a new shape to the slide.
IShape shape = slide.Shapes.AddShape(AutoShapeType.Rectangle, node.OffsetX, node.OffsetY, node.Width, node.Height);
//Adds a shape id and its level.
shape.Description = node.Id + "&" + node.AddInfo.ToString();

// Apply the color for shape
string hexColorCode = GetShapeColorBasedOnLevel(node.AddInfo.ToString());
shape.Fill.FillType = FillType.Solid;
shape.Fill.SolidFill.Color = ColorObject.FromArgb(int.Parse(hexColorCode.Substring(0, 2), NumberStyles.AllowHexSpecifier),
int.Parse(hexColorCode.Substring(2, 2), NumberStyles.AllowHexSpecifier),
int.Parse(hexColorCode.Substring(4, 2), NumberStyles.AllowHexSpecifier));

// Disable the line format of shape.
shape.LineFormat.Fill.FillType = FillType.None;

// Apply VerticalAlignment
shape.TextBody.VerticalAlignment = VerticalAlignmentType.Middle;

foreach (var annotation in node.Annotations)
{
//get the node label
string content = annotation.Content;
//Adds a paragraph to the shape.
IParagraph paragraph = shape.TextBody.AddParagraph();
// Apply HorizontalAlignment
paragraph.HorizontalAlignment = HorizontalAlignmentType.Center;
//Adds a text to the paragraph.
ITextPart textPart = paragraph.AddTextPart(content);
// Apply text properties.
textPart.Font.Color = ColorObject.White;
textPart.Font.FontSize = 10;
}
}
//To get connectors collection
List<DiagramConnector> Connectors = JsonConvert.DeserializeObject<List<DiagramConnector>>(NCProperty["connectorProperty"].ToString());
//Iterate an connectors
foreach (var connector in Connectors)
{
IShape sourceShape = GetShapeWithID(connector.SourceID, slide.Shapes);
IShape targetShape = GetShapeWithID(connector.TargetID, slide.Shapes);
if (sourceShape != null && targetShape != null)
{
int targetShapeConnectionPortIndex = GetConnectionPortIndexBasedOnLevel(targetShape.Description.Split('&')[1]);
//Add elbow connector on the slide and connect the end points of connector with specified port positions 0 and 4 of the source and target shapes
IConnector connectorShape = slide.Shapes.AddConnector(ConnectorType.Elbow, sourceShape, 2, targetShape, targetShapeConnectionPortIndex);
//Adds a connector id.
connectorShape.Description = connector.Id;
//Apply connector line color.
connectorShape.LineFormat.Fill.FillType = FillType.Solid;
connectorShape.LineFormat.Fill.SolidFill.Color = ColorObject.Black;
}
}

//Save the PowerPoint Presentation
pptxDoc.Save("Diagram.pptx");
//Closes the Presentation
pptxDoc.Close();

return null;
}

/// <summary>
/// Gets a shape object with corresponding shape ID.
/// </summary>
/// <param name="id">Represents the id of shape.</param>
/// <param name="shapes">Represents a shapes collection.</param>
/// <returns>Returns shape object with corresponding shape ID.</returns>
private IShape GetShapeWithID(string id, IShapes shapes)
{
foreach (IShape shape in shapes)
{
if (shape.Description.Split('&')[0] == id)
return shape;
}
return null;
}
/// <summary>
/// Gets a connection port index of target shape based on its level.
/// </summary>
/// <param name="level">Represents a level of shape.</param>
/// <returns>Returns a connection port index of target shape based on its level.</returns>
private int GetConnectionPortIndexBasedOnLevel(string level)
{
switch (level)
{
case "level2":
case "level4":
return 0;

case "level3":
return 3;

case "level5":
default:
return 1;
}
}
/// <summary>
/// Gets a shape color based on its level.
/// </summary>
/// <param name="level">Represent a level of shape.</param>
/// <returns>Returns a shape color based on its level.</returns>
private string GetShapeColorBasedOnLevel(string level)
{
switch (level)
{
case "level1":
case "level2":
case "level3":
return "71AF17";

case "level4":
return "1859B7";

case "level5":
default:
return "2E95D8";
}
}
}
public class OrganizationalDetails
{
public string Id { get; set; }
public string Role { get; set; }
public string Color { get; set; }
public string Manager { get; set; }
public string ChartType { get; set; }
public string Level { get; set; }

public OrganizationalDetails(string id, string role, string color, string manager, string chartType, string level)
{
this.Id = id;
this.Role = role;
this.Color = color;
this.Manager = manager;
this.ChartType = chartType;
this.Level = level;
}

public static List<OrganizationalDetails> GetData()
{
List<OrganizationalDetails> organizationaldetails = new List<OrganizationalDetails>();
organizationaldetails.Add(new OrganizationalDetails("parent", "Board", "#71AF17", "", "", "level1"));
organizationaldetails.Add(new OrganizationalDetails("1", "General Manager", "#71AF17", "parent", "right", "level2"));
organizationaldetails.Add(new OrganizationalDetails("11", "Assistant General Manager", "#71AF17", "1", "", "level3"));
organizationaldetails.Add(new OrganizationalDetails("2", "Human Resource Manager", "#1859B7", "1", "right", "level4"));
organizationaldetails.Add(new OrganizationalDetails("3", "Trainers", "#2E95D8", "2", "", "level5"));
organizationaldetails.Add(new OrganizationalDetails("4", "Recruiting Team", "#2E95D8", "2", "", "level5"));
organizationaldetails.Add(new OrganizationalDetails("6", "Design Manager", "#1859B7", "1", "right", "level4"));
organizationaldetails.Add(new OrganizationalDetails("7", "Design Supervisor", "#2E95D8", "6", "", "level5"));
organizationaldetails.Add(new OrganizationalDetails("8", "Development Supervisor", "#2E95D8", "6", "", "level5"));
organizationaldetails.Add(new OrganizationalDetails("9", "Drafting Supervisor", "#2E95D8", "6", "", "level5"));
organizationaldetails.Add(new OrganizationalDetails("10", "Operations Manager", "#1859B7", "1", "right", "level4"));
organizationaldetails.Add(new OrganizationalDetails("11", "Statistics Department", "#2E95D8", "10", "", "level5"));

return organizationaldetails;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.EJ2.AspNet.Core" Version="22.1.39" />
<PackageReference Include="Syncfusion.Presentation.Net.Core" Version="22.1.39" />
</ItemGroup>



</Project>
25 changes: 25 additions & 0 deletions ConvertDiagramToPPTX/ConvertDiagramToPPTX/ConvertDiagramToPPTX.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.33214.272
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConvertDiagramToPPTX", "ConvertDiagramToPPTX.csproj", "{1D4D3B73-3A4E-41CB-8B32-0305E52EDB72}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1D4D3B73-3A4E-41CB-8B32-0305E52EDB72}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1D4D3B73-3A4E-41CB-8B32-0305E52EDB72}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1D4D3B73-3A4E-41CB-8B32-0305E52EDB72}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1D4D3B73-3A4E-41CB-8B32-0305E52EDB72}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {58094B53-AC94-4BD0-B2CF-6E58327B35D6}
EndGlobalSection
EndGlobal
Binary file not shown.
11 changes: 11 additions & 0 deletions ConvertDiagramToPPTX/ConvertDiagramToPPTX/Models/ErrorViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace WebApplication1.Models
{
public class ErrorViewModel
{
public string RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
}
}
26 changes: 26 additions & 0 deletions ConvertDiagramToPPTX/ConvertDiagramToPPTX/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace WebApplication1
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:57814",
"sslPort": 44362
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"WebApplication1": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
57 changes: 57 additions & 0 deletions ConvertDiagramToPPTX/ConvertDiagramToPPTX/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace WebApplication1
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
Loading

0 comments on commit 2422c6a

Please sign in to comment.