Wednesday, December 26, 2007

Creating MS-Word document from C#

In my workplace, I am developing procurement application. In this application, one of the requirement is to auto generate the tender form in MS-Word format for the selected vendors in the application. Vendor details and related informations are taken from the backend.

To auto generate the document, I did the following.
  1. Get the MS-Word template file (filename.dot), in which the format the client wants, from the client
  2. From the developer side, I create the bookmarks in which they want to auto populate data.
  3. In the project workspace I added the reference of Word COMponent. ( I have used MS Office 2007 Component)
  4. Use the following code to generate the doc file.

    // Include the following namespaces
    using Microsoft.Office.Core;
    using Microsoft.Office.Interop.Word;

    ....
    ....
    ....

    Microsoft.Office.Interop.Word.

    ApplicationClass wordApp = new Microsoft.Office.Interop.Word. ApplicationClass();

    string fileDesPath = @"C:\temp\output.doc";    // Output file
    object oTemplate = @"C:\temp\template.dot";   // Template file

    object fileDes = fileDesPath;
    object missing = System.Reflection. Missing.Value;
    object Visible=true;

    Microsoft.Office.Interop.Word.

    Document adoc = wordApp.Documents.Add(ref oTemplate, ref missing, ref missing, ref missing);

    object oBookMark = "name"; // In template we have to find the bookmark "name" to replace the real data
    adoc.Bookmarks.get_Item(
    ref oBookMark).Range.Text = "Prakash";

    object filename = fileDes;

    adoc.SaveAs(

    ref filename, ref missing, ref missing, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref missing);

    wordApp.Visible =

    true;
    doc.Close(ref nullobj, ref nullobj, ref nullobj);

 

Monday, December 24, 2007

iTextSharp

Today, I got the work to create the PDF document from ASP.net application. Using google I have searched any components which are available for free download. At that time, I got this component, iTextSharp, from Souceforge.net. Initially it is developed in Java with the name of iText.

We can get the latest version of this component at http://sourceforge.net/projects/itextsharp/. In this link they provide the example code also. This component is very useful and easy to use for ASP.net developers.

Sample code

// step 1: creation of a document-object
Document document = new Document();
try
{
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.GetInstance(document, new FileStream(@"C:\temp\Chap0101.pdf" , FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we Add a paragraph to the document
document.Add(new Paragraph( "Hello World"));
}
catch (DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch ( IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}

// step 5: we close the document
document.Close();

Some Article Links

iTextSharp(iText#) example in asp.net - http://blog.rubypdf.com/2006/10/25/itextsharpitext-example-in-aspnet/
How to generate Arabic PDF files - http://www.developerfusion.co.uk/forums/p/40095/146877/

Friday, December 21, 2007

DLL Registration failure in VC++

Filter or DLL or .ocx registration Error :
-------------------------------------------


Eventhough the DLL compiles and links well  while registering the filter or DLL or.ocx using regsvr32 utility, I got this Error.

---------------------------
RegSvr32
---------------------------
.\Debug\HTTPWriterSinkFilterd.ax was loaded, but the DllRegisterServer entry point was not found.

.\Debug\HTTPWriterSinkFilterd.ax does not appear to be a .DLL or .OCX file.
---------------------------
OK  
---------------------------

Solution Steps :
---------------

 
 1.I Opened that DLL file in "Depends viewer" available in a visual studio tools
 2. no function is exported from the DLL or filter file.
 3.For Exporting the Functions, I used the .def file (module definition file).
 4. Next I have opened the DLL,ax files in "Depends viewer" that are successfully registered 
 There the Dependency viewer displays the exported functions.
 5. So the problem is in Exporting the functions
 6. Next I opened the Project->Settings-> Linker->input ...

 and I checked anywhere my .def file is specified there is nothing like that.

 So I added the the following options

  Project -> Settings-> Linker -> Input -> /DEF:My.def


Now I compiled the application and register it with regsvr32 utitily.
Now the registeration is done successfully .

if I opened that registered DLL in "Depends viewer", It displays the exported functions.

 

PostScript :
------------ 

 In Some DLLs,even though they created the application as DLL,
without including this  "/DEF:my.def", that dll also registers well.