How to Convert Word Document to PDF using C#
Document wordDocument = null;
string Sourcepath = ConfigurationSettings.AppSettings["SourcePath"];
string DestinationPath = ConfigurationSettings.AppSettings["DestinationPath"];
DirectoryInfo dirInfo = new DirectoryInfo(Sourcepath);
FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");
foreach (FileInfo wordFile in wordFiles)
{
object paramSourceDocPath = (Object)wordFile.FullName;
//(Object)wordFile.Name
string paramExportFilePath = paramSourceDocPath.ToString().Replace(".doc", ".pdf");
// string paramExportFilePath = DestinationPath + wordFile.Name.ToString().Replace(".doc", ".xps");
if (!File.Exists(paramExportFilePath.ToString()))
{
Application wordApplication = new Application();
object paramMissing = Type.Missing;
wordApplication.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;
WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;
//WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatXPS;
bool paramOpenAfterExport = false;
WdExportOptimizeFor paramExportOptimizeFor =
WdExportOptimizeFor.wdExportOptimizeForPrint;
WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
int paramStartPage = 0;
int paramEndPage = 0;
WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
bool paramIncludeDocProps = true;
bool paramKeepIRM = true;
WdExportCreateBookmarks paramCreateBookmarks =
WdExportCreateBookmarks.wdExportCreateWordBookmarks;
bool paramDocStructureTags = true;
bool paramBitmapMissingFonts = true;
bool paramUseISO19005_1 = false;
try
{
// Open the source document.
wordDocument = wordApplication.Documents.Open(
ref paramSourceDocPath, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing);
// Toc Update if needed
if (wordDocument.TablesOfContents.Count > 0)
{
for (int i = 2; i <= wordDocument.TablesOfContents.Count; i++)
{
try
{
wordDocument.TablesOfContents[i].Update();
}
catch (Exception e)
{
}
}
}
// Toc Reading from Document
string[] paragraphs = null;
if (wordDocument.TablesOfContents.Count > 0)
{
for (int i = 1; i <= wordDocument.TablesOfContents.Count; i++)
{
Microsoft.Office.Interop.Word.Range oRangeTOC = (Microsoft.Office.Interop.Word.Range)wordDocument.TablesOfContents[i].Range;
string strDelimiters = "\r";
string strTOC = oRangeTOC.Text ;
strTOC = strTOC.TrimEnd(strDelimiters.ToCharArray());
paragraphs = strTOC.Split(strDelimiters.ToCharArray());
for (int j = 0; j < paragraphs.Length; j++)
{
string str = paragraphs[j].Substring(0, paragraphs[j].IndexOf("\t"));
}
}
int num = wordDocument.ComputeStatistics(WdStatistic.wdStatisticPages, ref paramMissing);
}
// Export it in the specified format.
if (wordDocument != null)
wordDocument.ExportAsFixedFormat(paramExportFilePath,
paramExportFormat, paramOpenAfterExport,
paramExportOptimizeFor, paramExportRange, paramStartPage,
paramEndPage, paramExportItem, paramIncludeDocProps,
paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
paramBitmapMissingFonts, paramUseISO19005_1,
ref paramMissing);
}
catch (Exception ex)
{
// Respond to the error
}
finally
{
// Close and release the Document object.
if (wordDocument != null)
{
wordDocument.Close(ref paramMissing, ref paramMissing,
ref paramMissing);
wordDocument = null;
}
// Quit Word and release the ApplicationClass object.
if (wordApplication != null)
{
wordApplication.Quit(ref paramMissing, ref paramMissing,
ref paramMissing);
wordApplication = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
string Sourcepath = ConfigurationSettings.AppSettings["SourcePath"];
string DestinationPath = ConfigurationSettings.AppSettings["DestinationPath"];
DirectoryInfo dirInfo = new DirectoryInfo(Sourcepath);
FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");
foreach (FileInfo wordFile in wordFiles)
{
object paramSourceDocPath = (Object)wordFile.FullName;
//(Object)wordFile.Name
string paramExportFilePath = paramSourceDocPath.ToString().Replace(".doc", ".pdf");
// string paramExportFilePath = DestinationPath + wordFile.Name.ToString().Replace(".doc", ".xps");
if (!File.Exists(paramExportFilePath.ToString()))
{
Application wordApplication = new Application();
object paramMissing = Type.Missing;
wordApplication.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;
WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;
//WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatXPS;
bool paramOpenAfterExport = false;
WdExportOptimizeFor paramExportOptimizeFor =
WdExportOptimizeFor.wdExportOptimizeForPrint;
WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
int paramStartPage = 0;
int paramEndPage = 0;
WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
bool paramIncludeDocProps = true;
bool paramKeepIRM = true;
WdExportCreateBookmarks paramCreateBookmarks =
WdExportCreateBookmarks.wdExportCreateWordBookmarks;
bool paramDocStructureTags = true;
bool paramBitmapMissingFonts = true;
bool paramUseISO19005_1 = false;
try
{
// Open the source document.
wordDocument = wordApplication.Documents.Open(
ref paramSourceDocPath, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing);
// Toc Update if needed
if (wordDocument.TablesOfContents.Count > 0)
{
for (int i = 2; i <= wordDocument.TablesOfContents.Count; i++)
{
try
{
wordDocument.TablesOfContents[i].Update();
}
catch (Exception e)
{
}
}
}
// Toc Reading from Document
string[] paragraphs = null;
if (wordDocument.TablesOfContents.Count > 0)
{
for (int i = 1; i <= wordDocument.TablesOfContents.Count; i++)
{
Microsoft.Office.Interop.Word.Range oRangeTOC = (Microsoft.Office.Interop.Word.Range)wordDocument.TablesOfContents[i].Range;
string strDelimiters = "\r";
string strTOC = oRangeTOC.Text ;
strTOC = strTOC.TrimEnd(strDelimiters.ToCharArray());
paragraphs = strTOC.Split(strDelimiters.ToCharArray());
for (int j = 0; j < paragraphs.Length; j++)
{
string str = paragraphs[j].Substring(0, paragraphs[j].IndexOf("\t"));
}
}
int num = wordDocument.ComputeStatistics(WdStatistic.wdStatisticPages, ref paramMissing);
}
// Export it in the specified format.
if (wordDocument != null)
wordDocument.ExportAsFixedFormat(paramExportFilePath,
paramExportFormat, paramOpenAfterExport,
paramExportOptimizeFor, paramExportRange, paramStartPage,
paramEndPage, paramExportItem, paramIncludeDocProps,
paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
paramBitmapMissingFonts, paramUseISO19005_1,
ref paramMissing);
}
catch (Exception ex)
{
// Respond to the error
}
finally
{
// Close and release the Document object.
if (wordDocument != null)
{
wordDocument.Close(ref paramMissing, ref paramMissing,
ref paramMissing);
wordDocument = null;
}
// Quit Word and release the ApplicationClass object.
if (wordApplication != null)
{
wordApplication.Quit(ref paramMissing, ref paramMissing,
ref paramMissing);
wordApplication = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
Hi,
ReplyDeletecan u pls let me know How to read the content of the TOC.
i want to read the content paragraph which is pointed from a TOC
Hi Samuel,
Deletecheck whether it will be usefull for you or not.
How to Read TOC Headings from word document using C#
which i have posted now.
Hi Sreenivasa,
ReplyDeleteThanks for the prompt response, I am actually looking for the Content which is pointed/Linked by the TOC..
in TOC when we do "Ctrl+Click" on any item, it takes to the appropriate content.. i want to read that content and save that content seperately. It is like GOTO particular content. but how to do that in C#? Thanks in advance
Hi I got one solution, for reading the content.
ReplyDeletehttp://social.msdn.microsoft.com/Forums/en-SG/worddev/thread/61dc6ebd-d9b6-472e-9762-5923f0e10510
You can convert word file to pdf and vice versa by using Asopse.Words for .NET. Try it.
ReplyDeletehttp://www.aspose.com/.net/word-component.aspx