| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- using System.Web;
- using AutoMapper;
- using BlackRock.Reporting.API.Core;
- using BlackRock.Reporting.API.Core.Models;
- using Microsoft.AspNetCore.Mvc;
- using PuppeteerSharp;
- using iTextSharp.text;
- using iTextSharp.text.pdf;
-
- namespace BlackRock.Reporting.API.Controllers
- {
- [Route("api/[controller]")]
- public class PDFGeneratorController : Controller
- {
- private readonly IHostEnvironment host;
- private readonly IGenerator generator;
- private readonly IMapper mapper;
-
- public PDFGeneratorController(IHostEnvironment host, IGenerator generator, IMapper mapper)
- {
- this.mapper = mapper;
- this.generator = generator;
- this.host = host;
- }
-
- [HttpGet("{url}")]
- public async Task<IActionResult> Get([FromQuery] OptionsForPdf pdfOptions, string url = "http://localhost:3000/#/dashboard")
- {
- if (string.IsNullOrEmpty(url))
- return BadRequest();
-
- var result = HttpUtility.UrlDecode(url);
- // Temporary name of file which will be downloaded
- var fileName = Guid.NewGuid().ToString() + ".pdf";
- // Path to wwwroot folder combined with name of pdf file
- string path = Path.Combine(host.ContentRootPath, $"wwwroot/pdfs/{fileName}");
-
- var options = mapper.Map<OptionsForPdf, PdfOptions>(pdfOptions);
-
- await generator.Generate(result, path, options);
-
- // Document document = new Document(PageSize.A4, 36, 36, 36 + 100, 36); // note height should be set here
- // MyEvent e = new MyEvent(host);
- // PdfWriter pw = PdfWriter.GetInstance(document, new FileStream(Path.Combine(host.ContentRootPath, "logtest.pdf"), FileMode.Create));
- // pw.PageEvent = e;
- // document.Open();
-
- // for (int i = 0; i < 100; i++)
- // {
- // document.Add(new Phrase("TESTING\n"));
- // }
-
- // document.Close();
-
- ModifyPDF(path);
- FileStream stream = new FileStream(path, FileMode.Open);
- return File(stream, "application/pdf", fileName);
- }
- [HttpGet("isolate/{url}")]
- public async Task<IActionResult> GetIsolated([FromQuery] OptionsForPdf pdfOptions, string url = "http://localhost:3000/#/dashboard")
- {
- if (string.IsNullOrEmpty(url))
- return BadRequest();
-
- var result = HttpUtility.UrlDecode(url);
- // Temporary name of file which will be downloaded
- var fileName = Guid.NewGuid().ToString() + ".pdf";
- // Path to wwwroot folder combined with name of pdf file
- string path = Path.Combine(host.ContentRootPath, $"wwwroot/pdfs/{fileName}");
-
- var options = mapper.Map<OptionsForPdf, PdfOptions>(pdfOptions);
-
- await generator.Isolate(result, path, options);
- FileStream stream = new FileStream(path, FileMode.Open);
- return File(stream, "application/pdf", fileName);
- }
-
- private void Nesto()
- {
- //string oldFile = Path.Combine(host.ContentRootPath, "0bd7e2f4-b01e-4c17-8f1e-99562d2bab4a.pdf");
- string oldFile = Path.Combine(host.ContentRootPath, "oldFile.pdf");
- string newFile = Path.Combine(host.ContentRootPath, "newFilxex.pdf");
-
- // open the reader
- PdfReader reader = new PdfReader(oldFile);
- Rectangle size = reader.GetPageSizeWithRotation(1);
- Document document = new Document(size);
-
- AcroFields form = reader.AcroFields;
- try
- {
- for (int pagew = 1; pagew <= reader.NumberOfPages; pagew++)
- {
-
- }
- }
- catch { }
-
- // open the writer
- FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
- PdfWriter writer = PdfWriter.GetInstance(document, fs);
- document.Open();
-
- // the pdf content
- PdfContentByte cb = writer.DirectContent;
-
- // select the font properties
- BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
- cb.SetColorFill(BaseColor.DARK_GRAY);
- cb.SetFontAndSize(bf, 8);
-
- // write the text in the pdf content
- cb.BeginText();
- string text = "Some random blablablabla...";
- // put the alignment and coordinates here
- cb.ShowTextAligned(1, text, 520, 640, 0);
- cb.EndText();
- cb.BeginText();
- text = "Other random blabla...";
- // put the alignment and coordinates here
- cb.ShowTextAligned(2, text, 100, 200, 0);
- cb.EndText();
-
- // create the new page and add it to the pdf
- PdfImportedPage page = writer.GetImportedPage(reader, 1);
- var pages = reader.GetPageN(1);
- var content = writer.DirectContent;
- var template = content.CreateTemplate(100, 100);
- float width = 100, height = 100;
- // PdfDictionary
- // pages.CreateTemplate(writer,100,100);
- // cb.AddTemplate(page, 0, 0);
- // cb.AddTemplate()
- // Document document = new Document(PageSize.A4, 36, 36, 36 + 100, 36); // note height should be set here
- MyEvent e = new MyEvent(host);
- //PdfWriter pw = PdfWriter.GetInstance(document, new FileStream(Path.Combine(host.ContentRootPath, "logtest.pdf"), FileMode.Create));
- writer.PageEvent = e;
- document.Open();
-
- // for (int i = 0; i < 100; i++)
- // {
- // document.Add(new Phrase("TESTING\n"));
- // }
-
- //document.Close();
- // close the streams and voilá the file should be changed :)
- document.Close();
- fs.Close();
- writer.Close();
- reader.Close();
- }
-
- private void ModifyPDF(string path)
- {
- var filePath = path;
- byte[] bytes = System.IO.File.ReadAllBytes(filePath);
- var pdfReader = new PdfReader(bytes);
- using (Stream output = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
- {
- using (PdfStamper pdfStamper = new PdfStamper(pdfReader, output))
- {
- for (int pageIndex = 1; pageIndex <= pdfReader.NumberOfPages; pageIndex++)
- {
- pdfStamper.FormFlattening = false;
- Rectangle pageRectangle = pdfReader.GetPageSizeWithRotation(pageIndex);
- PdfContentByte pdfData = pdfStamper.GetOverContent(pageIndex);
- pdfData.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 10);
- PdfGState graphicsState = new PdfGState
- {
- FillOpacity = 0.3F
- };
- pdfData.SetGState(graphicsState);
- Image image = Image.GetInstance(Path.Combine(host.ContentRootPath, "logo.png"));
- image.SetAbsolutePosition(pageRectangle.Width/2-image.Width/2,pageRectangle.Height-image.Height-5);
- pdfData.AddImage(image);
- // pdfData.BeginText();
-
-
- // // select the font properties
- // BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
- // pdfData.SetColorFill(BaseColor.BLACK);
- // pdfData.SetFontAndSize(bf, 8);
-
- // write the text in the pdf content
- // pdfData.BeginText();
- // string text = "Faisal Pathan";
- // // put the alignment and coordinates here
- // pdfData.ShowTextAligned(1, text, 70, 775, 0);
- // pdfData.EndText();
-
- // pdfData.BeginText();
- // string text1 = "faisalmpathan@gmail.com";
- // pdfData.ShowTextAligned(1, text1, 550, 775, 0);
- // pdfData.ShowTextAligned(0,"logo",pageRectangle.Width/2,pageRectangle.Height-15,0);
- // pdfData.EndText();
-
-
- }
- }
- output.Close();
- output.Dispose();
- }
- }
- }
- public partial class MyEvent : PdfPageEventHelper
- {
- public MyEvent(IHostEnvironment host)
- {
- this.host = host;
- }
- iTextSharp.text.Image image1;
- PdfPTable header = new PdfPTable(3);
- private readonly IHostEnvironment host;
-
- public override void OnOpenDocument(PdfWriter writer, Document document)
- {
- image1 = Image.GetInstance(Path.Combine(host.ContentRootPath, "logo.png"));
-
- image1.SetAbsolutePosition(20, (document.PageSize.Height - 120));
-
- //header.WriteSelectedRows(0, -1, document.Left, document.Top, writer.DirectContent);
-
- }
-
- public override void OnEndPage(PdfWriter writer, Document document)
- {
-
-
- PdfPTable tbHeader = new PdfPTable(1);
-
- tbHeader.AddCell(image1);
- //tbHeader.TotalWidth = image1.Width;
- // tbHeader.WriteSelectedRows(0, -1, 10, document.PageSize.Height - 15, writer.DirectContent);
- tbHeader.WriteSelectedRows(0, -1, document.PageSize.Width / 2 - image1.Width / 2, document.PageSize.Height - 15, writer.DirectContent);
- }
- }
- }
|