emma oj

emma oj

  • 1.5k
  • 195
  • 23.6k

How to get pdf copy of a page in c#

Jun 29 2021 3:08 PM


using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Text;
using System.IO;

using System.Collections.Generic;
using iTextSharp.text;
using iTextSharp.text.pdf;


public partial class IssuePage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
           
    }

  
    protected void Button1_Click(object sender, EventArgs e)
    {

       
        for (int i = 1; i < 3; i++)
       {
    Document document = new Document(PageSize.A4, 10f, 10f, 30f, 0f);
    document.Open();
    PdfPTable table = new PdfPTable(2);
    String pathin = @"D:\Employee.pdf";
    String pathout = @"D:\Employee" + i + ".pdf";
    PdfReader reader = new PdfReader(pathin);
    PdfStamper stamper = new PdfStamper(reader, new FileStream(pathout, FileMode.Create));
    string text = "<p>C<sub>32</sub>H<sub>16</sub>CuN<sub>8</sub></p>";
    StringReader stringReader = new StringReader(text);
    ColumnText ct = new ColumnText(stamper.GetOverContent(1));
    TextReader tr = stringReader;
   // List<IElement> htmlarraylist = HTMLWorker.ParseToList(tr, null).Cast<IElement>().ToList();
    System.Collections.Generics.List<IElement> htmlarraylist = HTMLWorker.ParseToList(tr, null).Cast<IElement>().ToList();
   
    for (int k = 0; k < htmlarraylist.Count; k++)
    {
        ct.AddElement(htmlarraylist[k]);
    }
    Rectangle rect = new Rectangle(46, 190, 530, 36);
    ct.SetSimpleColumn(36, 36, PageSize.A4.Width - 36, PageSize.A4.Height - 175);
    ct.Go();
    stamper.Close();
    document.Add(table);
    document.Close();
            }
       }
    }

 

 

But i keep gettinf this error

as ahown in the snapshot below.

i.is.cc/1pF4R9rK.png

How do i fix this error

 


Answers (3)