//把Word文档转换为Pdf文档 #region private static void WordToPdf(string WordfileName, string PdfFileName) { //Object missing = System.Reflection.Missing.Value; //object bSave = false; //WPS.Application app = new WPS.Application(); //app.Visible = false; //WPS.Document doc = app.Documents.Open(WordfileName, false, true, false, "", "", false, "", "", 0, 0, false, false, 0, false); //doc.ExportPdf(PdfFileName, "", ""); //app.Quit(ref bSave, ref missing, ref missing); //System.Runtime.InteropServices.Marshal.ReleaseComObject(app); //app = null; Object missing = System.Reflection.Missing.Value; object bSave = false; Word.Application app = new Word.Application();//应用对象 //WPS.Application app = new WPS.Application(); app.Visible = false; Word.Document doc = app.Documents.Open(WordfileName, false, true, false, "", "", false, "", "", 0, 0, false, false, 0, false); doc.ExportAsFixedFormat(PdfFileName, Word.WdExportFormat.wdExportFormatPDF); app.Quit(ref bSave, ref missing, ref missing); System.Runtime.InteropServices.Marshal.ReleaseComObject(app); app = null; }
/// <summary> /// WPS组件 /// </summary> /// <param name="sourcePath"></param> /// <param name="targetPath"></param> public void WPSWordToPdf(string sourcePath, string targetPath) { app = new Word.ApplicationClass(); Word.Document doc = app.Documents.Open(sourcePath, Visible: false); doc.ExportAsFixedFormat(targetPath, Word.WdExportFormat.wdExportFormatPDF); doc.Close(); app.Quit(); }
public static void Merge(string[] filesToMerge, string outputFilename, bool insertPageBreaks) { Word._Application wordApplication = new Word.Application(); object pageBreak = Word.WdBreakType.wdSectionBreakContinuous; object outputFile = outputFilename; object fileName = @"S:\ODS\Insight 2 Oncology\Quality Division Project\Visual Review Scores\Scoring Template\MergeTemplate.docx"; object end = Word.WdCollapseDirection.wdCollapseEnd; try { wordApplication.Visible = false; Word.Document wordDocument = wordApplication.Documents.Add(ref fileName); Word.Selection selection = wordApplication.Selection; foreach (string file in filesToMerge) { if (file.Contains("Scores.docx")) { selection.PageSetup.PaperSize = Word.WdPaperSize.wdPaper11x17; selection.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape; selection.InsertFile(file); selection.Collapse(ref end); } if (!file.Contains("Scores.docx") && insertPageBreaks) { selection.InsertFile(file); selection.InsertBreak(pageBreak); } } wordApplication.Selection.Document.Content.Select(); wordDocument.PageSetup.TopMargin = (float)50; wordDocument.PageSetup.RightMargin = (float)50; wordDocument.PageSetup.LeftMargin = (float)50; wordDocument.PageSetup.BottomMargin = (float)50; selection.ParagraphFormat.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceSingle; selection.ParagraphFormat.SpaceAfter = 0.0F; selection.ParagraphFormat.SpaceBefore = 0.0F; wordDocument.SaveAs(outputFile + ".docx"); wordDocument.Close(); wordDocument = wordApplication.Documents.Open(outputFile + ".docx"); wordDocument.ExportAsFixedFormat(outputFile + ".pdf", Word.WdExportFormat.wdExportFormatPDF); wordDocument = null; } catch (Exception ex) { throw ex; } finally { wordApplication.Quit(); } }
private Boolean word_exec(OfficeDomain office) { Boolean result = false; if (Array.IndexOf(word, office.prefix) > -1) { log("word开始工作..."); Word.Document doc = null; try { //打开,参数含义:路径、格式不兼容不警告、只读 doc = wordApp.Documents.Open(office.path, false, true); //转换 doc.ExportAsFixedFormat(office.pdfFileName, Word.WdExportFormat.wdExportFormatPDF); result = true; log("word工作完毕,准备关闭文件.."); } catch (Exception e) { error("word工作异常:{0}", e.Message); } finally { if (doc != null) { try { doc.Close(); log("word文件成功关闭.."); } catch (Exception e) { error("word文件关闭时发生错误{1},重启{0}.exe", word_name, e.Message); word_start(); } } } } return(result); }