Share via


read and edit pdf using c#

Question

Wednesday, April 8, 2009 3:07 PM

Hi All

I want to edit the pdf content using c# code.

If any one have concept or code please help me.

Thanks

All replies (8)

Wednesday, April 8, 2009 3:38 PM ✅Answered

 Check this out

http://sourceforge.net/projects/itextsharp/


Wednesday, April 8, 2009 10:42 PM ✅Answered

Refer to this

http://csharpdotnetfreak.blogspot.com/2008/12/export-gridview-to-pdf-using-itextsharp.html

One more example here

        form1.Controls.Clear();
        form1.Controls.Add(grdv1);
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        grdv1.RenderControl(htw);
        string html = "<html><body>" + sw.ToString() + "</body></html>";
        Response.Clear();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=Export.pdf");
        Document document = new Document(PageSize.A4, 80, 50, 30, 65);
        PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);
        document.AddAuthor("Hua Min");
        document.AddSubject("Export To pdf");
        document.Open();
        string tempFile = Path.GetTempFileName();
        using (StreamWriter tempwriter = new StreamWriter(tempFile, false))
        {
            tempwriter.Write(html);
        }
        HtmlParser.Parse(document, tempFile);
        document.Close();
        writer.Close();
        File.Delete(tempFile);
        writer = null;
        document = null;
        Response.End();


Friday, April 17, 2009 3:09 AM

Hi,

This is very useful when we want to create new pdf files.

But if I already have template pdf file and want to change only some specific text (#changeword# in below example).
Here is an example to make it more clear.

Sample pdf file.

======PDF Starts=====
This is a static text. Which can not be changed.
Only the next word "#changeword#" can be changed. What ever you provide from ASP.NET form will be replaced. 
======PDF Ends=====

Can I achive this functionality using "iTextSharp" ? If yes, How ? If not, can you please suggest the alternate way.

 

Thanks in advance,
Tapan Chapadia

 

 

 


Friday, April 17, 2009 4:00 AM

Refer to this

http://www.eggheadcafe.com/community/aspnet/17/10079110/problem-with-itextsharp-o.aspx


Friday, April 17, 2009 5:47 AM

Refer to this

http://www.eggheadcafe.com/community/aspnet/17/10079110/problem-with-itextsharp-o.aspx

Hi,

Thanks for the replay. But it is regarding reading the pdf. How do I modify any existing pdf at specific location.
As an example you can consider a pdf invitation card in which i just want to replace "To #Name#"

Thanks,
Tapan Chapadia

 

 


Friday, April 17, 2009 6:06 AM

You can see within this part (that is from that link above), it's replacing "\r" with "", it's similar as what you want

PdfReader reader = new PdfReader(inFileName);
outFile = new StreamWriter(outFileName, false, System.Text.Encoding.UTF8);
for (int page = 1; page <= reader.NumberOfPages; page++)
{
String Text = "";
Text = ExtractTextFromPDFBytes(reader.GetPageContent(page)) + " ";
**Text = Text.Replace("\r", "");
**String[] split = Text.Split('\n');
for (int iCount = 0; iCount < split.Length; iCount++)
{
arrCheck.Add(split.GetValue(iCount).ToString().Trim());
outFile.Write(split.GetValue(iCount).ToString().Trim());
outFile.WriteLine();
}
}
Response.Write("Success");
return true;


Friday, April 17, 2009 6:06 AM

You can see within this part (that is from that link above), it's replacing "\r" with "", it's similar as what you want

PdfReader reader = new PdfReader(inFileName);
outFile = new StreamWriter(outFileName, false, System.Text.Encoding.UTF8);
for (int page = 1; page <= reader.NumberOfPages; page++)
{
String Text = "";
Text = ExtractTextFromPDFBytes(reader.GetPageContent(page)) + " ";
**Text = Text.Replace("\r", "");
**String[] split = Text.Split('\n');
for (int iCount = 0; iCount < split.Length; iCount++)
{
arrCheck.Add(split.GetValue(iCount).ToString().Trim());
outFile.Write(split.GetValue(iCount).ToString().Trim());
outFile.WriteLine();
}
}
Response.Write("Success");
return true;


Friday, April 17, 2009 6:07 AM

You can see within this part (that is from that link above), it's replacing "\r" with "", it's similar as what you want

PdfReader reader = new PdfReader(inFileName);
outFile = new StreamWriter(outFileName, false, System.Text.Encoding.UTF8);
for (int page = 1; page <= reader.NumberOfPages; page++)
{
String Text = "";
Text = ExtractTextFromPDFBytes(reader.GetPageContent(page)) + " ";
**Text = Text.Replace("\r", "");
**String[] split = Text.Split('\n');
for (int iCount = 0; iCount < split.Length; iCount++)
{
arrCheck.Add(split.GetValue(iCount).ToString().Trim());
outFile.Write(split.GetValue(iCount).ToString().Trim());
outFile.WriteLine();
}
}
Response.Write("Success");
return true;