3
Answers

How to get pdf copy of a page in c#

emma oj

emma oj

3y
670
1


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)
3
Satya Karki

Satya Karki

9 56.7k 3.6m 3y
You can use jspdf. You can format the printing area into html or cshtml and then easily generate pdf with jspdf.
 
Check this sample:
https://codepen.io/REMCOOLE/pen/xxOGoKo
 
For asp.net mvc
https://debugonweb.com/2018/10/jspdf/ 
Accepted
2
Leon D

Leon D

491 2.7k 387 3y

Hi,

 

You can probably try Spire.PDF, it supports lots of PDF manipulations. 

 

https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/PDF-Conversion/Convert-HTML-String-to-PDF-in-C.html

1
Ben Thompson

Ben Thompson

NA 251 28 3y
You can also use the LEADTOOLS SDK to extract individual pages from a source document into multiple ones:
String pathin = @"D:\Employee.pdf";
PDFFile file = new PDFFile(pathin);
  1. String pathin = @"D:\Employee.pdf";  
  2. PDFFile file = new PDFFile(pathin);  
  3.   
  4. for (int i = 1; i < 4; i++)  
  5. {  
  6.    String pathout = @"D:\Employee" + i + ".pdf";  
  7.    file.ExtractPages(i, i, pathout);  
  8. }  
 
There are other ways the LEADTOOLS SDK can be used to split PDF documents. We have a tutorial available showing other methods:
https://www.leadtools.com/help/sdk/v21/tutorials/dotnet-console-split-a-pdf-file-into-multiple-files.html
You can download the free evaluation of the toolkit from the official site.
https://www.leadtools.com/downloads/