C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » ASP.NET »

Problem with converting .doc file to .pdf file and also filling with details +iTextSharp


Posted Date: 16 Dec 2008      Posted By: Sidewinder2      Member Level: Gold     Points: 1   Responses: 2



Hello Mate,

I am having a problem in converting a .doc file in to .pdf file using iTextSharp.dll .And also i would like to wirte some details in to the form. the problem is the conversion of .doc to .pdf file is not working and also filling in to the converted .pdf file is also not working.Could you please tell me a solution?



This is my code

in this below method i am passing the full path of an existing .doc file

public void Convert_And_Fill(string filepath)

{

StreamReader sr;

sr = File.OpenText(filepath);

String strpdf = sr.ReadToEnd();

sr.Close();

FileInfo f1 = new FileInfo(filepath);

string delim = ".";

char[] delimiter11 = delim.ToCharArray();

string[] docfilename = Convert.ToString(f1.Name).Split(delimiter11);



Document document = new Document();


string newpdffile = Server.MapPath("~/Admin/FormUpload/" + docfilename[0] + ".pdf");

PdfWriter.GetInstance(document, new FileStream(newpdffile, FileMode.Create));

document.Open();

document.Add(new Paragraph(strpdf));

document.Close();


FillForm(filename, newpdffile);

FileInfo file = new FileInfo(strCompleteFilePath);

//Checking if file exists

if (file.Exists)

{

DateTime start_time = DateTime.Now;

//Clear the content of the response

Response.ClearContent();

//LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the header

Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);

//Add the file size into the response header

Response.AddHeader("Content-Length", file.Length.ToString());

//Set the ContentType

//Response.ContentType = ReturnExtension(file.Extension.ToLower());

//Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)

Response.TransmitFile(file.FullName);

Response.End();



}

}



And the Method used to fill in the pdf document is



private void FillForm(string filename, string filepath)

{

try

{

string pdfTemplate = filepath;

string newFile = Server.MapPath("~/Admin/FormUpload/completed_" + filename);

strCompleteFilePath = newFile;

PdfReader pdfReader = new PdfReader(pdfTemplate);

PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(

newFile, FileMode.Create));

AcroFields pdfFormFields = pdfStamper.AcroFields;

object[] Parameters = new object[1];

Parameters[0] = Session["UserName"].ToString();

DataSet dsattorney = new DataSet();

dsattorney = aobj.GetLawyer_Details(Parameters);//here i am getting the values from the database to fill in to the pdf document.

if (dsattorney.Tables.Count != 0 && dsattorney.Tables[0].Rows.Count != 0)

{

string fname = dsattorney.Tables[0].Rows[0]["NAME"].ToString();

string mname = dsattorney.Tables[0].Rows[0]["MIDDLE_INITIAL"].ToString();

string lname = dsattorney.Tables[0].Rows[0]["LAST_NAME"].ToString();

string name = fname + " " + mname + " " + lname;

string add1 = dsattorney.Tables[0].Rows[0]["STREET_ADD"].ToString();

string add2 = dsattorney.Tables[0].Rows[0]["CITY"].ToString();

string address = add1 + " " + add2;

string add3 = dsattorney.Tables[0].Rows[0]["STATE"].ToString();

string mobile = dsattorney.Tables[0].Rows[0]["MOBILE"].ToString();

string fax = dsattorney.Tables[0].Rows[0]["FAX"].ToString();

string email = dsattorney.Tables[0].Rows[0]["EMAIL"].ToString();



pdfFormFields.SetField("FillText87", name);

pdfFormFields.SetField("FillText86", address);

pdfFormFields.SetField("FillText84", add3);

pdfFormFields.SetField("FillText82", mobile);

pdfFormFields.SetField("FillText80", email);

pdfFormFields.SetField("FillText81", fax);

}

// report by reading values from completed PDF

string sTmp = "W-4 Completed for " + pdfFormFields.GetField("f1_09(0)") + " " +

pdfFormFields.GetField("f1_10(0)");

// flatten the form to remove editting options, set it to false

// to leave the form open to subsequent manual edits

pdfStamper.FormFlattening = false;

// close the pdf

pdfStamper.Close();

}

catch (Exception ex)

{

throw ex;

}

}





Responses

Author: Amit Shah    16 Dec 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 6

iTextsharp provides the sample SDK for all his functionality so go accordingly
let me know if you have problem in that, send me attachment of ur forms so i can solve it and send you back!!!!!



--------------
Hello Mate,

I am having a problem in converting a .doc file in to .pdf file using iTextSharp.dll .And also i would like to wirte some details in to the form. the problem is the conversion of .doc to .pdf file is not working and also filling in to the converted .pdf file is also not working.Could you please tell me a solution?



This is my code

in this below method i am passing the full path of an existing .doc file

public void Convert_And_Fill(string filepath)

{

StreamReader sr;

sr = File.OpenText(filepath);

String strpdf = sr.ReadToEnd();

sr.Close();

FileInfo f1 = new FileInfo(filepath);

string delim = ".";

char[] delimiter11 = delim.ToCharArray();

string[] docfilename = Convert.ToString(f1.Name).Split(delimiter11);



Document document = new Document();


string newpdffile = Server.MapPath("~/Admin/FormUpload/" + docfilename[0] + ".pdf");

PdfWriter.GetInstance(document, new FileStream(newpdffile, FileMode.Create));

document.Open();

document.Add(new Paragraph(strpdf));

document.Close();


FillForm(filename, newpdffile);

FileInfo file = new FileInfo(strCompleteFilePath);

//Checking if file exists

if (file.Exists)

{

DateTime start_time = DateTime.Now;

//Clear the content of the response

Response.ClearContent();

//LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the header

Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);

//Add the file size into the response header

Response.AddHeader("Content-Length", file.Length.ToString());

//Set the ContentType

//Response.ContentType = ReturnExtension(file.Extension.ToLower());

//Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)

Response.TransmitFile(file.FullName);

Response.End();



}

}



And the Method used to fill in the pdf document is



private void FillForm(string filename, string filepath)

{

try

{

string pdfTemplate = filepath;

string newFile = Server.MapPath("~/Admin/FormUpload/completed_" + filename);

strCompleteFilePath = newFile;

PdfReader pdfReader = new PdfReader(pdfTemplate);

PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(

newFile, FileMode.Create));

AcroFields pdfFormFields = pdfStamper.AcroFields;

object[] Parameters = new object[1];

Parameters[0] = Session["UserName"].ToString();

DataSet dsattorney = new DataSet();

dsattorney = aobj.GetLawyer_Details(Parameters);//here i am getting the values from the database to fill in to the pdf document.

if (dsattorney.Tables.Count != 0 && dsattorney.Tables[0].Rows.Count != 0)

{

string fname = dsattorney.Tables[0].Rows[0]["NAME"].ToString();

string mname = dsattorney.Tables[0].Rows[0]["MIDDLE_INITIAL"].ToString();

string lname = dsattorney.Tables[0].Rows[0]["LAST_NAME"].ToString();

string name = fname + " " + mname + " " + lname;

string add1 = dsattorney.Tables[0].Rows[0]["STREET_ADD"].ToString();

string add2 = dsattorney.Tables[0].Rows[0]["CITY"].ToString();

string address = add1 + " " + add2;

string add3 = dsattorney.Tables[0].Rows[0]["STATE"].ToString();

string mobile = dsattorney.Tables[0].Rows[0]["MOBILE"].ToString();

string fax = dsattorney.Tables[0].Rows[0]["FAX"].ToString();

string email = dsattorney.Tables[0].Rows[0]["EMAIL"].ToString();



pdfFormFields.SetField("FillText87", name);

pdfFormFields.SetField("FillText86", address);

pdfFormFields.SetField("FillText84", add3);

pdfFormFields.SetField("FillText82", mobile);

pdfFormFields.SetField("FillText80", email);

pdfFormFields.SetField("FillText81", fax);

}

// report by reading values from completed PDF

string sTmp = "W-4 Completed for " + pdfFormFields.GetField("f1_09(0)") + " " +

pdfFormFields.GetField("f1_10(0)");

// flatten the form to remove editting options, set it to false

// to leave the form open to subsequent manual edits

pdfStamper.FormFlattening = false;

// close the pdf

pdfStamper.Close();

}

catch (Exception ex)

{

throw ex;

}

}



Author: Sidewinder2    16 Dec 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 1

Could you please tell me some more in detail?



Thanks For Ur Reply,
Myself



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : Login Control Authentication
Previous : Extract metadata/content from pdf & doc
Return to Discussion Forum
Post New Message
Category: ASP.NET

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use