|
| 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 | +} |
0 commit comments