Click here to Skip to main content
15,917,617 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

I have developed an application in .net 2.0 framework (VS2005) where i need to convert the word document to pdf automatically. Could you please help me with converting word to secured pdf document from c# web application....please find the code below which i am using for converting to pdf

C#
public void GeneratePDF()
{
    Microsoft.Office.Interop.Word.Application wordApp = null;
    
    Microsoft.Office.Interop.Word.Document doc = null;

    object visible = true;
  //  object readOnly = true;
    object sourceFilename = this._sourceDocFileNameFullPath;
    string targetFilename = this._destPDFFilePath;
    object saveChanges = false;

    try
    {
        wordApp = new Application();
        
        doc = wordApp.Documents.Open(ref sourceFilename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                                              ref missing, ref missing, ref missing, ref  missing, ref  visible, ref missing, ref missing,
                                              ref missing, ref missing);

        doc.ExportAsFixedFormat(targetFilename, WdExportFormat.wdExportFormatPDF, false, WdExportOptimizeFor.wdExportOptimizeForOnScreen,
                                WdExportRange.wdExportAllDocument, 0, 0, WdExportItem.wdExportDocumentContent, false, false,
                                WdExportCreateBookmarks.wdExportCreateNoBookmarks, false, false, false, ref missing);

        saveChanges = false;
    }
    catch (Exception ex)
    {
        //This exception is logged in DB and handled in caller method
        throw ex;
    }
    finally
    {
        if (doc != null)
        {
            doc.Close(ref saveChanges, ref missing, ref missing);
        }
        if (wordApp != null)
        {
            wordApp.Quit(ref saveChanges, ref missing, ref missing);
        }
    }
}


Please let me know if there are any other options for generating secured pdf from c# .net code.

Thanks,
Prashant
Posted
Updated 12-Jun-13 18:00pm
v2
Comments
Ahmed Bensaid 13-Jun-13 5:46am    
What's the problem with your code ? Do you have an error ?
Prashant Bangaluru 21-Jun-13 5:53am    
Hi Ahmed,
Thanks for the reply but the real problem is while saving word document to a PDF it should be saved as secured pdf, means it should say as secured in the title bar of the document.

You either need to use the free libraries to build the PDf file if you wish to create it programatically or you can use third party tools like cute pdf to generate the pdf file from word after it is created
 
Share this answer
 
You may try with this:
C#
using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

...

// Create a new Microsoft Word application object
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();

// C# doesn't have optional arguments so we'll need a dummy value
object oMissing = System.Reflection.Missing.Value;

// Get list of Word files in specified directory
DirectoryInfo dirInfo = new DirectoryInfo(@"\\server\folder");
FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");

word.Visible = false;
word.ScreenUpdating = false;

foreach (FileInfo wordFile in wordFiles)
{
    // Cast as Object for word Open method
    Object filename = (Object)wordFile.FullName;

    // Use the dummy value as a placeholder for optional arguments
    Document doc = word.Documents.Open(ref filename, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    doc.Activate();

    object outputFileName = wordFile.FullName.Replace(".doc", ".pdf");
    object fileFormat = WdSaveFormat.wdFormatPDF;

    // Save document into PDF Format
    doc.SaveAs(ref outputFileName,
        ref fileFormat, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing);

    // Close the Word document, but leave the Word application open.
    // doc has to be cast to type _Document so that it will find the
    // correct Close method.                
    object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
    ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
    doc = null;
}

// word has to be cast to type _Application so that it will find
// the correct Quit method.
((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
word = null;
 
Share this answer
 
v2

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