Vediamo più in dettaglio cosa bisogna fare!
Per la conversione di formato è necessario utilizzare le librerie
Microsoft.Office.Interop.Word
messe a disposizione da Office. Utilizzando queste è sufficiente caricare il file excel a livello applicativo e salvarlo con l'altro formato (save as).using Word = Microsoft.Office.Interop.Word;
....
public static string ConvertWordFileToPDF(String filename, String filePath)
{
String newPdfFilename = null;
using (new SwitchCulture())
{
Word.ApplicationClass wordApplication= null;
Word.Document wordDocument= null;
object missing = Type.Missing;
Word.WdExportFormat paramExportFormat = Word.WdExportFormat.wdExportFormatPDF;
Word.WdExportOptimizeFor paramExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
Word.WdExportCreateBookmarks paramCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
bool paramOpenAfterExport = false;
int paramStartPage = 0;
int paramEndPage = 0;
bool paramIncludeDocProps = true;
bool paramKeepIRM = true;
bool paramDocStructureTags = true;
bool paramBitmapMissingFonts = true;
bool paramUseISO19005_1 = false;
try
{
newPdfFilename = filename + ".pdf";
HttpServerUtility Server = HttpContext.Current.Server;
string excelFileNamePath = Server.MapPath(filePath + "/" + filename);
string pdfFileNamePath = Server.MapPath(filePath + "/" + newPdfFilename);
wordApplication = new Word.ApplicationClass();
// Open the source document.
wordDocument = wordApplication.Documents.Open(ref wordFileNamePath,
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);
// Save it in the target format.
if (wordDocument!= null)
wordDocument.ExportAsFixedFormat(pdfFileNamePath,
paramExportFormat, paramOpenAfterExport,
paramExportOptimizeFor, paramExportRange, paramStartPage,
paramEndPage, paramExportItem, paramIncludeDocProps,
paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
paramBitmapMissingFonts, paramUseISO19005_1,
ref missing);
}
catch (Exception ex)
{
LogFactory.Logger.WriteLog(ex);
}
finally
{
// Close the document object.
if (wordDocument!= null)
{
wordDocument.Close(ref missing, ref missing, ref missing);
wordDocument= null;
}
// Quit Word and release the ApplicationClass object.
if (wordApplication!= null)
{
wordApplication.Quit();
wordApplication= null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
return newPdfFilename;
}
Per quanto riguarda la classe
SwitchCulture
vale lo stesso discorso fatto per Excel. UPDATE 14.05.2010
Vi segnalo alcuni riferimenti per approfondire l'argomento:
Saving Word 2007 Documents to PDF and XPS Formats
Nessun commento:
Posta un commento