Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I Need to convert doc/docx to pdf with iTextSharp.dll.

I have converted different extensions to pdf format.

C#
string[] fileType = hdnPath.Value.Split('.');
       StreamReader str = new StreamReader(hdnPath.Value);
       Document doc = new Document();
     //  Element ea = new Element();
       if (fileType[fileType.Length - 1] == "txt")
       {
           PdfWriter.GetInstance(doc, new FileStream(hdnPath.Value.Replace(".txt", ".pdf"), FileMode.Create));
       }
       else if (fileType[fileType.Length - 1] == "docx")
       {
           PdfWriter.GetInstance(doc, new FileStream(hdnPath.Value.Replace(".docx", ".pdf"), FileMode.Create));
       }
       else if (fileType[fileType.Length - 1] == "aspx")
       {
           PdfWriter.GetInstance(doc, new FileStream(hdnPath.Value.Replace(".aspx", ".pdf"), FileMode.Create));
       }
       else if (fileType[fileType.Length - 1] == "cs")
       {
           PdfWriter.GetInstance(doc, new FileStream(hdnPath.Value.Replace(".cs", ".pdf"), FileMode.Create));
       }
       else if (fileType[fileType.Length - 1] == "xlsx")
       {
           PdfWriter.GetInstance(doc, new FileStream(hdnPath.Value.Replace(".xlsx", ".pdf"), FileMode.Create));
       }
       else if (fileType[fileType.Length - 1] == "xlsx")
       {
           lblErrorMessage.Text = "Not required to convert pdf to pdf format";
       }
       else
       {
           lblErrorMessage.Text = "We can't convert this file";
       }
       doc.Open();
       doc.Add(new Paragraph(str.ReadToEnd()));
       doc.Close();



In the above code .docx and xlsx format to pdf are not working
Posted
Updated 2-Feb-13 20:36pm
v2
Comments
ZurdoDev 1-Feb-13 16:34pm    
What's not working?

You may go another path: convert by use of COM Interop for Office[^].
Cheers
Andi
 
Share this answer
 
Refer this Article[^]
 
Share this answer
 
Comments
Abhinav S 12-Sep-13 14:12pm    
5.
http://sourceforge.net/projects/itextsharp/[^] is possibly the most popular third party solution available to convert files from one format to another. You may want to look at it when trying to convert docx to pdf.
 
Share this answer
 
Comments
kranthi_013 12-Sep-13 10:46am    
how can i convert .docx and .xlsx to pdf using itextsharp in c# ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900