Visit Map

Sunday, July 17, 2005

Creating word to PDF

Hi,

I had opportunity to convert the word document to PDF. The word document is passed to a procedure. The procedure parseS the word document to create a PDF. The code is given below
I have used ITextDotnet which is converted from Java to J#.
You can download the ITEXTDOTNET code from Sourceforge.net
http://sourceforge.net/projects/itextdotnet/

using System;
using Word;
//use the ITextDotnet
using ds=com.lowagie.text;
//include the pdf libary
using com.lowagie.text.pdf;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
namespace WindowsApplication3
{
///
/// Summary description for WordDoc.
///

public class WordDoc
{
public WordDoc()
{
//
// TODO: Add constructor logic here
//
}
public void ProccessWordDoc(Word.Document doc)
{


// step 1: creation of a document-object
com.lowagie.text.Document document = new ds.Document(ds.PageSize.A5, 36, 72, 108, 180);

// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file

PdfWriter.getInstance(document, new FileStream(doc.Name.Replace(".doc",".pdf"), FileMode.Create));

// step 3: we open the document
document.open();

// step 4: we add a paragraph to the document


//com.lowagie.text.Image img=new com.lowagie.text.Image()

foreach(Word.Paragraph para in doc.Paragraphs)
{
com.lowagie.text.Paragraph paragraph = new ds.Paragraph();
switch(para.Alignment)
{
case WdParagraphAlignment.wdAlignParagraphCenter:
paragraph.setAlignment(ds.ElementConst.ALIGN_CENTER);
break;
case WdParagraphAlignment.wdAlignParagraphJustify:
paragraph.setAlignment(ds.ElementConst.ALIGN_JUSTIFIED);
break;
case WdParagraphAlignment.wdAlignParagraphLeft:
paragraph.setAlignment(ds.ElementConst.ALIGN_LEFT);
break;
case WdParagraphAlignment.wdAlignParagraphRight:
paragraph.setAlignment(ds.ElementConst.ALIGN_RIGHT);
break;
}
paragraph.add(para.Range.Text);

document.add(paragraph);

}


// step 5: we close the document
document.close();
}
}
}
Hope you like my Idea.
This is a basic prelimary version.

3 comments:

Sudhakar said...

cool...I wrote a similar app long time back using Reporter.NET here

http://www.codeproject.com/dotnet/pdfconverternet.asp

:-)

Sudhakar

Raja Sekhar Amirapu said...

Hi Sudhakar,
Thanks for your comments
I am improving it and i will publish a fullfleged article soon

patrick said...

Hi! Cool website template.... web application