public bool ConvertToPdf(String docFolder, String docFile, String pdfFolder, String pdfFile) { WdExportFormat exportFormat = WdExportFormat.wdExportFormatPDF; bool result; object paramMissing = Type.Missing; try { object paramSourceDocPath = docFolder + "/" + docFile; string paramExportFilePath = pdfFolder + "/" + pdfFile; WdExportFormat paramExportFormat = exportFormat; 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; _wordDoc = _wordApp.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); if (_wordDoc != null) _wordDoc.ExportAsFixedFormat(paramExportFilePath, paramExportFormat, paramOpenAfterExport, paramExportOptimizeFor, paramExportRange, paramStartPage, paramEndPage, paramExportItem, paramIncludeDocProps, paramKeepIRM, paramCreateBookmarks, paramDocStructureTags, paramBitmapMissingFonts, paramUseISO19005_1, ref paramMissing); result = true; } catch (Exception) { return false; } finally { _wordDoc.Close(ref paramMissing, ref paramMissing, ref paramMissing); _wordApp.Quit(ref paramMissing, ref paramMissing, ref paramMissing); GC.Collect(); GC.WaitForPendingFinalizers(); } return result; }
/// <summary> /// Save a Microsoft Word Document as PDF /// </summary> /// <param name="doc"></param> /// <param name="outputFileName"></param> public void SaveWordDocumentAsPdf(_Document doc, String outputFileName) { doc.ExportAsFixedFormat(outputFileName, PdfExportParams.ExportFormat, PdfExportParams.OpenAfterExport, PdfExportParams.OptimizeFor, PdfExportParams.ExportRange, PdfExportParams.StartPage, PdfExportParams.EndPage, PdfExportParams.ExportItem, PdfExportParams.IncludeDocProps, PdfExportParams.KeepIRM, PdfExportParams.CreateBookmarks, PdfExportParams.DocStructureTags, PdfExportParams.BitmapMissingFonts, PdfExportParams.UseISO19005_1, PdfExportParams.FixedFormatExtClassPtr ); }
public void ChangeToPDF(object WordPath, string PDFPath) { Console.WriteLine("开始将Word转成PDF"); _Document doc = wordApp.Documents.Open(ref WordPath, ref Nothing); doc.ExportAsFixedFormat(PDFPath, WdExportFormat.wdExportFormatPDF, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, 1, 1, WdExportItem.wdExportDocumentContent, true, true, WdExportCreateBookmarks.wdExportCreateNoBookmarks, true, true, false); object NotSave = WdSaveOptions.wdDoNotSaveChanges; doc.Close(ref NotSave, ref Nothing, ref Nothing); }
protected override bool doConvertToPdf(string saveAsFile) { WdExportFormat wdExportFormat = WdExportFormat.wdExportFormatPDF; _Document arg_20_0 = this.WordDoc; WdExportFormat arg_20_2 = wdExportFormat; bool arg_20_3 = false; WdExportOptimizeFor arg_20_4 = WdExportOptimizeFor.wdExportOptimizeForPrint; WdExportRange arg_20_5 = WdExportRange.wdExportAllDocument; int arg_20_6 = 1; int arg_20_7 = 1; WdExportItem arg_20_8 = WdExportItem.wdExportDocumentContent; bool arg_20_9 = false; bool arg_20_10 = true; WdExportCreateBookmarks arg_20_11 = WdExportCreateBookmarks.wdExportCreateNoBookmarks; bool arg_20_12 = true; bool arg_20_13 = true; bool arg_20_14 = false; object value = System.Reflection.Missing.Value; arg_20_0.ExportAsFixedFormat(saveAsFile, arg_20_2, arg_20_3, arg_20_4, arg_20_5, arg_20_6, arg_20_7, arg_20_8, arg_20_9, arg_20_10, arg_20_11, arg_20_12, arg_20_13, arg_20_14, ref value); return(true); }
/// <summary> /// 转为PDF /// </summary> /// <param name="dstDir"></param> /// <param name="docFileName"></param> /// <returns></returns> public Boolean ConvertToPDF(String dstDir, String docFileName) { string wjm = Path.GetFileNameWithoutExtension(docFileName); string path = FormatPath(wjm); //该目录存在 dstDir += path; string pdfFile = dstDir + wjm + ".pdf"; string docFile = dstDir + docFileName; WdExportFormat exportFormat = WdExportFormat.wdExportFormatPDF; bool result; object paramMissing = Type.Missing; //ApplicationClass wordApplication = new ApplicationClass(); this._wordApp = new ApplicationClass(); try { object paramSourceDocPath = docFile; string paramExportFilePath = pdfFile; WdExportFormat paramExportFormat = exportFormat; 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; _wordDoc = _wordApp.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); if (_wordDoc != null) _wordDoc.ExportAsFixedFormat(paramExportFilePath, paramExportFormat, paramOpenAfterExport, paramExportOptimizeFor, paramExportRange, paramStartPage, paramEndPage, paramExportItem, paramIncludeDocProps, paramKeepIRM, paramCreateBookmarks, paramDocStructureTags, paramBitmapMissingFonts, paramUseISO19005_1, ref paramMissing); result = true; } catch (Exception) { return false; } finally { _wordDoc.Close(ref paramMissing, ref paramMissing, ref paramMissing); _wordApp.Quit(ref paramMissing, ref paramMissing, ref paramMissing); GC.Collect(); GC.WaitForPendingFinalizers(); } return result; }
private void button1_Click(object sender, EventArgs e) { if (cmbLettori.SelectedIndex != -1) { string dati = creaStringa(); clsLettore l = new clsLettore("Biblioteca.mdf"); l.codice = Convert.ToInt32(cmbLettori.SelectedValue); l.getDati(); Percorso = System.Windows.Forms.Application.StartupPath; //MessageBox.Show(Percorso); FileDOC = Percorso + "/" + "PDF\\" + lblTitolo.Text + "_" + l.cognome + l.nome + ".docx"; FilePDF = Percorso + "/" + "PDF\\" + lblTitolo.Text + "_" + l.cognome + l.nome + ".pdf"; l.dispose(); // Creo una NUOVA istanza di WORD Application myWord = new Microsoft.Office.Interop.Word.Application(); // Rendo visibile la nuova Word Application //myWord.Visible = false; myWord.Visible = false; //Aggiungo un NUOVO Documento myDoc = new Document(); myDoc = myWord.Documents.Add(); start = myDoc.Sentences[myDoc.Sentences.Count].End - 1; end = myDoc.Sentences[myDoc.Sentences.Count].End; myRange = myDoc.Range(ref start, ref end); // Aggiungo il Testo myRange.Text = dati + "\n"; myDoc.SaveAs2(FileDOC); // Chiudo il Documento myDoc.Close(); // Chiudo il Word Application myWord.Quit(); this.Cursor = Cursors.WaitCursor; myWord = new Microsoft.Office.Interop.Word.Application(); myWord.Visible = false; myDoc = new Document(); myDoc = myWord.Documents.Open(@FileDOC); myDoc.ExportAsFixedFormat(@FilePDF, WdExportFormat.wdExportFormatPDF); myDoc.Close(); myWord.Quit(); this.Cursor = Cursors.Default; MessageBox.Show("PDF creato con successo"); File.Delete(@FileDOC); cmbLettori.SelectedIndex = -1; cmbLettori.SelectedIndex = -1; } else { MessageBox.Show("Seleziona il lettore"); } }