how to edit or update pdf file using c#

 how to edit or update pdf file using c#


first we need to install plugin of nuget
"Spire.Pdf"




1).Create Model Classes

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EncriptionDecription.Models
{
    public class PDFModel
    {
        public HttpPostedFileBase ImageFile { set; get; }
        public int CompanyID { set; get; }
    }
    public class Company{
        public int ID { set; get; }
        public string Name { set; get; }
        public string Url { set; get; }
        public string Logo { set; get; }
    }
}




2).Create Controller 

using EncriptionDecription.Models;
using Spire.Pdf;
using Spire.Pdf.General.Find;
using Spire.Pdf.Graphics;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Web.Mvc;

namespace EncriptionDecription.Controllers
{
    public class PDFController : Controller
    {
        public List<Company> Companies = new List<Company>();
        public  PDFController()
        {
            Companies.Add(new Company
            {
                ID=1,Name="Tritex",Url="Tritexsolutions.com",Logo="LogoTritex"
            });
            Companies.Add(new Company
            {
                ID = 2,
                Name = "Nextgen",
                Url = "Nextgen.com",
                Logo = "LogoNextgen"
            });
        }

        public ActionResult Index()
        {
            ViewBag.myfile = "";
            TempData["Companies"] = this.Companies;
            return View();
        }

        [HttpPost]
        public ActionResult Index(PDFModel pdfmodel)
        {
            TempData["Companies"] = this.Companies;
            string rootpath= @"C:\Users\rituraj.vaishanav\source\repos\EncriptionDecription\EncriptionDecription\Files\";
            Spire.Pdf.PdfDocument pdfDoc = new Spire.Pdf.PdfDocument();
            pdfDoc.LoadFromFile(rootpath + pdfmodel.ImageFile.FileName);
            var pages = pdfDoc.Pages;
            foreach (PdfPageBase page in pages)
            {
                PdfTextFindCollection CompanyLogo = page.FindText("<CompanyLogo></CompanyLogo>", TextFindParameter.IgnoreCase);
                PdfTextFindCollection CompanyName = page.FindText("<CompanyName></CompanyName>", TextFindParameter.IgnoreCase);
                PdfTextFindCollection pdfsharptext = page.FindText("Evaluation Warning : The document was created with Spire.PDF for .NET.", TextFindParameter.IgnoreCase);
                var company = this.Companies.Find(x => x.ID == pdfmodel.CompanyID);
                String logoText = company.Logo;
                String nameText = company.Url;
                //Creates a brush
                PdfBrush brush = new PdfSolidBrush(Color.DarkBlue);
                //Defines a font
                PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Regular));
                RectangleF rec;
                foreach (PdfTextFind find in CompanyLogo.Finds)
                {
                    rec = find.Bounds;
                    page.Canvas.DrawRectangle(PdfBrushes.White, rec);
                    page.Canvas.DrawString(logoText, font, brush, rec);
                }
                foreach (PdfTextFind find in CompanyName.Finds)
                {
                    rec = find.Bounds;
                    page.Canvas.DrawRectangle(PdfBrushes.White, rec);
                    page.Canvas.DrawString(nameText, font, brush, rec);
                }

                foreach (PdfTextFind find in pdfsharptext.Finds)
                {
                    rec = find.Bounds;
                    page.Canvas.DrawRectangle(PdfBrushes.White, rec);
                    page.Canvas.DrawString("", font, brush, rec);
                }
                //
            }
            var filename = rootpath + "new" + Guid.NewGuid() + ".pdf";
            pdfDoc.SaveToFile(filename);
            ViewBag.myfile = filename;
            return View();
        }
}




3).Create HTML

@using EncriptionDecription.Models;
@{
    ViewBag.Title = "Index";
}

<div class="row">
    <div class="col-md-8 col-md-offset-2">
        @using (Html.BeginForm("Index", "PDF", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            <div class="row">
                <div class="col-md-6">
                    <input type="file" name="ImageFile" required />
                </div>
                <div class="col-md-6">
                    <select name="CompanyID">
                        @foreach (var company in (List<Company>)TempData["Companies"])
                        {
                        <option value="@company.ID">@company.Name</option>
                        }
                    </select>
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
            </div>
            if (ViewBag.myfile != "")
            {
                <a href="@ViewBag.myfile">download file</a>;
            }
        }

    </div>
</div>









No comments:

Post a comment

Thank You For Your Great Contribution

Featured Post

how to find duplicate item from array using multiple object keys in angular or javascript

 how to find duplicate item from array using multiple object keys There is a way to find based of multiple key find duplicate records in ang...

Popular Posts