Skip to content

Commit d4c1d29

Browse files
committed
Added Video for Layer
1 parent 21f3bdc commit d4c1d29

File tree

78 files changed

+74957
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+74957
-0
lines changed

Videos/Layer/Layer.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36310.24 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Layer", "Layer\Layer.csproj", "{C465AA8A-0C10-4098-BB7D-81BD08D68042}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{C465AA8A-0C10-4098-BB7D-81BD08D68042}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{C465AA8A-0C10-4098-BB7D-81BD08D68042}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{C465AA8A-0C10-4098-BB7D-81BD08D68042}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{C465AA8A-0C10-4098-BB7D-81BD08D68042}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {050DD7C2-C963-46F7-9A70-4CD761EE46A0}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
using System.Diagnostics;
2+
using Layer.Models;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Syncfusion.Pdf;
5+
using Syncfusion.Drawing;
6+
using Syncfusion.Pdf.Graphics;
7+
using Syncfusion.Pdf.Parsing;
8+
9+
namespace Layer.Controllers
10+
{
11+
public class HomeController : Controller
12+
{
13+
private readonly ILogger<HomeController> _logger;
14+
15+
public HomeController(ILogger<HomeController> logger)
16+
{
17+
_logger = logger;
18+
}
19+
20+
public IActionResult CreateLayer()
21+
{
22+
PdfDocument document = new PdfDocument();
23+
24+
document.PageSettings.Margins.All = 0;
25+
26+
PdfPage page = document.Pages.Add();
27+
28+
PdfPageLayer backgroundLayer = page.Layers.Add("Background");
29+
30+
PdfBrush backgroundBrush = new PdfSolidBrush(Color.LightBlue);
31+
backgroundLayer.Graphics.DrawRectangle(backgroundBrush, new RectangleF(0, 0, page.Size.Width, page.Size.Height));
32+
33+
PdfPageLayer mainContentLayer = page.Layers.Add("Main Content");
34+
35+
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16);
36+
PdfBrush textBrush = new PdfSolidBrush(Color.Black);
37+
mainContentLayer.Graphics.DrawString("Welcome to PDF Layers!", font, textBrush, new PointF(50, 50));
38+
39+
MemoryStream outputStream = new MemoryStream();
40+
41+
document.Save(outputStream);
42+
43+
outputStream.Position = 0;
44+
45+
document.Close(true);
46+
47+
return File(outputStream, "application/pdf", "Layer.pdf");
48+
}
49+
50+
public IActionResult AddLayerToDocument()
51+
{
52+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Data/input.pdf");
53+
54+
loadedDocument.FindText("PDF Succinctly", 0, out var matchRect);
55+
56+
PdfLayer? parentLayer = null;
57+
58+
if (matchRect != null && matchRect.Count > 0)
59+
{
60+
parentLayer = AddLayer(loadedDocument, "PDF Succinctly", matchRect[0], PdfBrushes.Red, null);
61+
62+
var childTexts = new[]
63+
{
64+
new { Text = "Introduction", Brush = PdfBrushes.Green },
65+
new { Text = "The PDF Standard", Brush = PdfBrushes.Blue },
66+
};
67+
68+
foreach (var item in childTexts)
69+
{
70+
loadedDocument.FindText(item.Text, 0, out var childRect);
71+
if (childRect != null && childRect.Count > 0)
72+
{
73+
AddLayer(loadedDocument, item.Text, childRect[0], item.Brush, parentLayer);
74+
}
75+
}
76+
}
77+
78+
return ExportPDFFile(loadedDocument, "Output.pdf");
79+
}
80+
81+
public IActionResult SetLayerVisibility()
82+
{
83+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Data/layer.pdf");
84+
85+
PdfLayer layer = loadedDocument.Layers[0];
86+
87+
layer.Visible = false;
88+
89+
return ExportPDFFile(loadedDocument, "LayerVisibility.pdf");
90+
}
91+
92+
public IActionResult RemoveLayer()
93+
{
94+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Data/layer.pdf");
95+
96+
PdfLoadedPage? loadedPage = loadedDocument.Pages[0] as PdfLoadedPage;
97+
98+
PdfPageLayerCollection layers = loadedPage!.Layers;
99+
100+
layers.RemoveAt(0);
101+
102+
return ExportPDFFile(loadedDocument, "RemoveLayer.pdf");
103+
}
104+
105+
106+
private FileStreamResult ExportPDFFile(PdfLoadedDocument document, string fileName)
107+
{
108+
// Save the given document into a memory stream
109+
MemoryStream outputStream = new MemoryStream();
110+
document.Save(outputStream);
111+
112+
// Reset stream to the beginning for correct file delivery
113+
outputStream.Position = 0;
114+
115+
// Close the document and release resources
116+
document.Close(true);
117+
118+
// Return the memory stream as a downloadable PDF file
119+
return File(outputStream, "application/pdf", fileName);
120+
}
121+
122+
123+
/// <summary>
124+
/// Adds a layer to the PDF document at the specified location.
125+
/// </summary>
126+
/// <param name="loadedDocument">The loaded PDF document.</param>
127+
/// <param name="layerName">Name of the layer.</param>
128+
/// <param name="rect">Rectangle area to draw.</param>
129+
/// <param name="brush">Brush color for drawing.</param>
130+
/// <param name="parentLayer">Optional parent layer for nesting.</param>
131+
/// <returns>The created PdfLayer.</returns>
132+
PdfLayer AddLayer(PdfLoadedDocument loadedDocument, string layerName, RectangleF rect, PdfBrush brush, PdfLayer? parentLayer)
133+
{
134+
// Create a new layer, nested if parentLayer is provided
135+
PdfLayer layer = parentLayer == null
136+
? loadedDocument.Layers.Add(layerName)
137+
: parentLayer.Layers.Add(layerName);
138+
139+
// Create graphics for the layer on the first page
140+
PdfGraphics graphics = layer.CreateGraphics(loadedDocument.Pages[0]!);
141+
142+
// Apply transparency and draw the rectangle
143+
graphics.Save();
144+
graphics.SetTransparency(0.5f);
145+
graphics.DrawRectangle(brush, rect);
146+
graphics.Restore();
147+
148+
return layer;
149+
}
150+
151+
public IActionResult Index()
152+
{
153+
return View();
154+
}
155+
156+
public IActionResult Privacy()
157+
{
158+
return View();
159+
}
160+
161+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
162+
public IActionResult Error()
163+
{
164+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
165+
}
166+
}
167+
}

Videos/Layer/Layer/Data/input.pdf

117 KB
Binary file not shown.

Videos/Layer/Layer/Data/layer.pdf

120 KB
Binary file not shown.

Videos/Layer/Layer/Layer.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Layer.Models
2+
{
3+
public class ErrorViewModel
4+
{
5+
public string? RequestId { get; set; }
6+
7+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
8+
}
9+
}

Videos/Layer/Layer/Program.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace Layer
2+
{
3+
public class Program
4+
{
5+
public static void Main(string[] args)
6+
{
7+
var builder = WebApplication.CreateBuilder(args);
8+
9+
// Add services to the container.
10+
builder.Services.AddControllersWithViews();
11+
12+
var app = builder.Build();
13+
14+
// Configure the HTTP request pipeline.
15+
if (!app.Environment.IsDevelopment())
16+
{
17+
app.UseExceptionHandler("/Home/Error");
18+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
19+
app.UseHsts();
20+
}
21+
22+
app.UseHttpsRedirection();
23+
app.UseStaticFiles();
24+
25+
app.UseRouting();
26+
27+
app.UseAuthorization();
28+
29+
app.MapControllerRoute(
30+
name: "default",
31+
pattern: "{controller=Home}/{action=Index}/{id?}");
32+
33+
app.Run();
34+
}
35+
}
36+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:14808",
8+
"sslPort": 44387
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"applicationUrl": "http://localhost:5007",
17+
"environmentVariables": {
18+
"ASPNETCORE_ENVIRONMENT": "Development"
19+
}
20+
},
21+
"https": {
22+
"commandName": "Project",
23+
"dotnetRunMessages": true,
24+
"launchBrowser": true,
25+
"applicationUrl": "https://localhost:7069;http://localhost:5007",
26+
"environmentVariables": {
27+
"ASPNETCORE_ENVIRONMENT": "Development"
28+
}
29+
},
30+
"IIS Express": {
31+
"commandName": "IISExpress",
32+
"launchBrowser": true,
33+
"environmentVariables": {
34+
"ASPNETCORE_ENVIRONMENT": "Development"
35+
}
36+
}
37+
}
38+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@{
2+
ViewData["Title"] = "Home Page";
3+
}
4+
5+
<div>
6+
<h2 style="margin-bottom: 20px">Working with Layers in PDF</h2>
7+
<div>
8+
<button style="width: 250px; margin-bottom: 20px; height: 40px;display:block;font-size:18px;"
9+
onclick="location.href='@Url.Action("CreateLayer", "Home")'">
10+
Create Layer
11+
</button>
12+
<button style="width: 250px; margin-bottom: 20px; height: 40px;display:block;font-size:18px;"
13+
onclick="location.href='@Url.Action("AddLayerToDocument", "Home")'">
14+
Add Layer
15+
</button>
16+
<button style="width: 250px; margin-bottom: 20px; height: 40px;display:block;font-size:18px;"
17+
onclick="location.href='@Url.Action("SetLayerVisibility", "Home")'">
18+
Set Layer Visibility
19+
</button>
20+
<button style="width: 250px; margin-bottom: 20px; height: 40px;display:block;font-size:18px;"
21+
onclick="location.href='@Url.Action("RemoveLayer", "Home")'">
22+
Remove Layer
23+
</button>
24+
</div>
25+
</div>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@{
2+
ViewData["Title"] = "Privacy Policy";
3+
}
4+
<h1>@ViewData["Title"]</h1>
5+
6+
<p>Use this page to detail your site's privacy policy.</p>

0 commit comments

Comments
 (0)