Update 20250222
This commit is contained in:
89
OfficeToPDFConverter/Class1.cs
Normal file
89
OfficeToPDFConverter/Class1.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.InteropServices.ComTypes;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Office.Interop.Word;
|
||||
using Microsoft.Office.Interop.Excel;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace OfficeToPDFConverter
|
||||
{
|
||||
public class Converter
|
||||
{
|
||||
public string Convert_Word(string B64Word,string path)
|
||||
{
|
||||
Microsoft.Office.Interop.Word.Application w = new Microsoft.Office.Interop.Word.Application();
|
||||
|
||||
try
|
||||
{
|
||||
Helper.clsFileHelper_Converter fh = new Helper.clsFileHelper_Converter();
|
||||
string outputfile = "";
|
||||
string inputfile = "";
|
||||
string tmpfilename = DateTime.Now.ToString("yyyyMMddhhmmss");
|
||||
outputfile = tmpfilename + ".pdf";
|
||||
inputfile = tmpfilename + ".tmp";
|
||||
inputfile = path + @"\" + inputfile;
|
||||
outputfile = path + @"\" + outputfile;
|
||||
|
||||
fh.SaveBase64ToFile(B64Word, path);
|
||||
w.Documents.Open(FileName: inputfile.ToString(), ReadOnly: true);
|
||||
object oMissing = System.Reflection.Missing.Value;
|
||||
w.ActiveDocument.ExportAsFixedFormat(outputfile.ToString(), Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF, false, Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint, Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument, 0, 0, Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentContent, true, true, Microsoft.Office.Interop.Word.WdExportCreateBookmarks.wdExportCreateNoBookmarks, true, false, false);
|
||||
w.ActiveDocument.Saved = true;
|
||||
w.ActiveDocument.Close(SaveChanges: false);
|
||||
|
||||
w.Quit(false);
|
||||
w = null;
|
||||
string b64 = fh.Base64FromFile(outputfile);
|
||||
System.IO.File.Delete(outputfile);
|
||||
return b64;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return "";
|
||||
}
|
||||
finally
|
||||
{
|
||||
try { w.Quit(false); } catch { }
|
||||
w = null;
|
||||
}
|
||||
|
||||
}
|
||||
public string Convert_Excel(string B64Word, string path)
|
||||
{
|
||||
Microsoft.Office.Interop.Excel.Application x = new Microsoft.Office.Interop.Excel.Application();
|
||||
try
|
||||
{
|
||||
Helper.clsFileHelper_Converter fh = new Helper.clsFileHelper_Converter();
|
||||
string outputfile = "";
|
||||
string inputfile = "";
|
||||
string tmpfilename = DateTime.Now.ToString("yyyyMMddhhmmss");
|
||||
outputfile = tmpfilename + ".pdf";
|
||||
inputfile = tmpfilename + ".tmp";
|
||||
inputfile = path + @"\" + inputfile;
|
||||
outputfile = path + @"\" + outputfile;
|
||||
|
||||
fh.SaveBase64ToFile(B64Word, path);
|
||||
x.Workbooks.Open(inputfile);
|
||||
x.ActiveWorkbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, outputfile.ToString(), Microsoft.Office.Interop.Excel.XlFixedFormatQuality.xlQualityStandard);
|
||||
x.ActiveWorkbook.Close(SaveChanges: false);
|
||||
x.Quit();
|
||||
x = null;
|
||||
string b64 = fh.Base64FromFile(outputfile);
|
||||
System.IO.File.Delete(outputfile);
|
||||
return b64;
|
||||
}
|
||||
catch { return ""; }
|
||||
finally { try { x.Quit(); } catch { } x = null; }
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user