|
| 1 | +using Microsoft.AspNetCore.Mvc; |
| 2 | +using QRCode.Models; |
| 3 | +using Syncfusion.Drawing; |
| 4 | +using Syncfusion.Pdf; |
| 5 | +using Syncfusion.Pdf.Barcode; |
| 6 | +using Syncfusion.Pdf.Graphics; |
| 7 | +using Syncfusion.Pdf.Parsing; |
| 8 | +using System.Diagnostics; |
| 9 | + |
| 10 | +namespace QRCode.Controllers |
| 11 | +{ |
| 12 | + public class HomeController : Controller |
| 13 | + { |
| 14 | + private readonly ILogger<HomeController> _logger; |
| 15 | + |
| 16 | + public HomeController(ILogger<HomeController> logger) |
| 17 | + { |
| 18 | + _logger = logger; |
| 19 | + } |
| 20 | + |
| 21 | + public IActionResult AddQRToPDF() |
| 22 | + { |
| 23 | + //Load the existing PDF file |
| 24 | + PdfLoadedDocument loadedDocument = new PdfLoadedDocument("data/input.pdf"); |
| 25 | + |
| 26 | + //Create a QR code |
| 27 | + PdfQRBarcode qrBarcode = new PdfQRBarcode(); |
| 28 | + |
| 29 | + qrBarcode.Text = "support@adventure-works.com"; |
| 30 | + |
| 31 | + qrBarcode.XDimension = 2.5f; |
| 32 | + |
| 33 | + //Get the first page of the PDF document |
| 34 | + PdfLoadedPage? loadedPage = loadedDocument.Pages[0] as PdfLoadedPage; |
| 35 | + |
| 36 | + //Draw the QR code on that page |
| 37 | + qrBarcode.Draw(loadedPage!, new PointF(50, 710)); |
| 38 | + |
| 39 | + // Return the PDF as a downloadable file |
| 40 | + return ExportPDF(loadedDocument, "qrcode.pdf"); |
| 41 | + } |
| 42 | + |
| 43 | + public IActionResult CustomizeQR() |
| 44 | + { |
| 45 | + //Load the existing PDF file |
| 46 | + PdfLoadedDocument loadedDocument = new PdfLoadedDocument("data/input.pdf"); |
| 47 | + |
| 48 | + //Create a QR code |
| 49 | + PdfQRBarcode qrBarcode = new PdfQRBarcode(); |
| 50 | + |
| 51 | + // Set QR code properties |
| 52 | + qrBarcode.Text = "https://www.syncfusion.com/document-sdk"; |
| 53 | + |
| 54 | + //Set the QR size |
| 55 | + qrBarcode.Size = new SizeF(75, 75); |
| 56 | + |
| 57 | + qrBarcode.XDimension = 5; |
| 58 | + |
| 59 | + // Error correction configuration |
| 60 | + qrBarcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High; |
| 61 | + |
| 62 | + // Encoding mode optimization |
| 63 | + qrBarcode.InputMode = InputMode.BinaryMode; |
| 64 | + |
| 65 | + // Version control (1-40 or Auto) |
| 66 | + qrBarcode.Version = QRCodeVersion.Version10; |
| 67 | + |
| 68 | + // Foreground color for QR pattern |
| 69 | + qrBarcode.ForeColor = Color.White; |
| 70 | + |
| 71 | + // Background color |
| 72 | + qrBarcode.BackColor = new PdfColor(46, 197, 190); |
| 73 | + |
| 74 | + //Set quiet zone (margin) around the QR code |
| 75 | + qrBarcode.QuietZone = new PdfBarcodeQuietZones() { All = 5 }; |
| 76 | + |
| 77 | + //Get the first page of the PDF document |
| 78 | + PdfLoadedPage? loadedPage = loadedDocument.Pages[0] as PdfLoadedPage; |
| 79 | + |
| 80 | + //Draw the QR code on that page |
| 81 | + qrBarcode.Draw(loadedPage!, new PointF(50, 710)); |
| 82 | + |
| 83 | + // Return the PDF as a downloadable file |
| 84 | + return ExportPDF(loadedDocument, "qrcode-customization.pdf"); |
| 85 | + } |
| 86 | + |
| 87 | + public IActionResult QRWithLogo() |
| 88 | + { |
| 89 | + //Load the existing PDF file |
| 90 | + PdfLoadedDocument loadedDocument = new PdfLoadedDocument("data/input.pdf"); |
| 91 | + |
| 92 | + //Create a QR code |
| 93 | + PdfQRBarcode qrBarcode = new PdfQRBarcode(); |
| 94 | + |
| 95 | + // Set QR code properties |
| 96 | + qrBarcode.Text = "https://www.syncfusion.com/document-sdk"; |
| 97 | + |
| 98 | + //Set the QR size |
| 99 | + qrBarcode.Size = new SizeF(75, 75); |
| 100 | + |
| 101 | + qrBarcode.XDimension = 5; |
| 102 | + |
| 103 | + // Error correction configuration |
| 104 | + qrBarcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High; |
| 105 | + |
| 106 | + // Encoding mode optimization |
| 107 | + qrBarcode.InputMode = InputMode.BinaryMode; |
| 108 | + |
| 109 | + // Version control (1-40 or Auto) |
| 110 | + qrBarcode.Version = QRCodeVersion.Version10; |
| 111 | + |
| 112 | + // Foreground color for QR pattern |
| 113 | + qrBarcode.ForeColor = Color.White; |
| 114 | + |
| 115 | + // Background color |
| 116 | + qrBarcode.BackColor = new PdfColor(46, 197, 190); |
| 117 | + |
| 118 | + //Set quiet zone (margin) around the QR code |
| 119 | + qrBarcode.QuietZone = new PdfBarcodeQuietZones() { All = 5 }; |
| 120 | + |
| 121 | + //Get the first page of the PDF document |
| 122 | + PdfLoadedPage? loadedPage = loadedDocument.Pages[0] as PdfLoadedPage; |
| 123 | + |
| 124 | + //Create logo for QR code |
| 125 | + QRCodeLogo logo = new QRCodeLogo(PdfBitmap.FromStream(new FileStream("data\\logo.png", FileMode.Open, FileAccess.Read))); |
| 126 | + |
| 127 | + //Set logo in qrcode |
| 128 | + qrBarcode.Logo = logo; |
| 129 | + |
| 130 | + //Draw the QR code on that page |
| 131 | + qrBarcode.Draw(loadedPage!, new PointF(50, 710)); |
| 132 | + |
| 133 | + // Return the PDF as a downloadable file |
| 134 | + return ExportPDF(loadedDocument, "qrcode-with-logo.pdf"); |
| 135 | + } |
| 136 | + |
| 137 | + public IActionResult QRToHeader() |
| 138 | + { |
| 139 | + PdfDocument document = new PdfDocument(); |
| 140 | + |
| 141 | + //Add page to the document |
| 142 | + PdfPage page = document.Pages.Add(); |
| 143 | + |
| 144 | + //Get the page size |
| 145 | + SizeF pageSize = page.GetClientSize(); |
| 146 | + |
| 147 | + //Create the header template and add to the document |
| 148 | + document.Template.Top = CreateHeaderTemplate(new SizeF(pageSize.Width, 100)); |
| 149 | + |
| 150 | + //Read the text from the text file |
| 151 | + string text = System.IO.File.ReadAllText("data\\input.txt"); |
| 152 | + |
| 153 | + PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12); |
| 154 | + |
| 155 | + PdfTextElement element = new PdfTextElement(text, font); |
| 156 | + |
| 157 | + element.Draw(page, new PointF(0, 0)); |
| 158 | + |
| 159 | + // Create a memory stream to hold the output PDF |
| 160 | + MemoryStream outputStream = new MemoryStream(); |
| 161 | + |
| 162 | + // Save the document to the memory stream |
| 163 | + document.Save(outputStream); |
| 164 | + |
| 165 | + // Reset the stream position to the beginning |
| 166 | + outputStream.Position = 0; |
| 167 | + |
| 168 | + // Close the document and release resources |
| 169 | + document.Close(true); |
| 170 | + |
| 171 | + // Return the PDF as a downloadable file |
| 172 | + return File(outputStream, "application/pdf", "qrcode-in-header.pdf"); |
| 173 | + } |
| 174 | + |
| 175 | + //Create header template and return |
| 176 | + PdfPageTemplateElement CreateHeaderTemplate(SizeF headerSize) |
| 177 | + { |
| 178 | + //Create PdfPageTemplateElement |
| 179 | + PdfPageTemplateElement header = new PdfPageTemplateElement(new RectangleF(0, 0, headerSize.Width, headerSize.Height)); |
| 180 | + |
| 181 | + string headerText = "PDF Succinctly"; |
| 182 | + |
| 183 | + PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16, PdfFontStyle.Regular); |
| 184 | + |
| 185 | + //Measure the text size |
| 186 | + SizeF textSize = font.MeasureString(headerText); |
| 187 | + |
| 188 | + //Draw the text with center alignment |
| 189 | + header.Graphics.DrawString("PDF Succinctly", font, PdfBrushes.Black, new PointF(0, (headerSize.Height - font.Height) / 2)); |
| 190 | + |
| 191 | + //Create a QR code and draw |
| 192 | + PdfQRBarcode qrBarcode = new PdfQRBarcode() |
| 193 | + { |
| 194 | + Text = "https://www.syncfusion.com/succinctly-free-ebooks/pdf", |
| 195 | + Size = new SizeF(75, 75), |
| 196 | + }; |
| 197 | + |
| 198 | + //Draw the QR code on the header |
| 199 | + qrBarcode.Draw(header.Graphics, new PointF(headerSize.Width - (qrBarcode.Size.Width + 40), (headerSize.Height - qrBarcode.Size.Height) / 2)); |
| 200 | + |
| 201 | + //Draw line to separate header |
| 202 | + header.Graphics.DrawLine(new PdfPen(PdfBrushes.LightGray, 0.5f), new PointF(0, headerSize.Height - 5), new PointF(headerSize.Width, headerSize.Height - 5)); |
| 203 | + |
| 204 | + return header; |
| 205 | + } |
| 206 | + |
| 207 | + |
| 208 | + // Helper method to save the modified PDF and return it as a file result |
| 209 | + private FileStreamResult ExportPDF(PdfLoadedDocument document, string fileName) |
| 210 | + { |
| 211 | + // Create a memory stream to hold the output PDF |
| 212 | + MemoryStream outputStream = new MemoryStream(); |
| 213 | + |
| 214 | + // Save the document to the memory stream |
| 215 | + document.Save(outputStream); |
| 216 | + |
| 217 | + // Reset the stream position to the beginning |
| 218 | + outputStream.Position = 0; |
| 219 | + |
| 220 | + // Close the document and release resources |
| 221 | + document.Close(true); |
| 222 | + |
| 223 | + // Return the PDF as a downloadable file |
| 224 | + return File(outputStream, "application/pdf", fileName); |
| 225 | + } |
| 226 | + |
| 227 | + public IActionResult Index() |
| 228 | + { |
| 229 | + return View(); |
| 230 | + } |
| 231 | + |
| 232 | + public IActionResult Privacy() |
| 233 | + { |
| 234 | + return View(); |
| 235 | + } |
| 236 | + |
| 237 | + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] |
| 238 | + public IActionResult Error() |
| 239 | + { |
| 240 | + return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); |
| 241 | + } |
| 242 | + } |
| 243 | +} |
0 commit comments