我現在有一個使用Microsoft將docx(以字節[]格式)轉換爲pdf格式(以字節格式)的函數.Office.Interop.Word使用openXML或類似的格式將docx []格式轉換爲pdf格式[pdf格式]
它的效果很好。除了它不能在線工作,因爲它需要將WinOffice安裝在服務器上,我無法做任何事情。
所以我需要去別的東西,我正在考慮openXML(除非你知道更好的方法)。
但是我到底會如何解決這個問題? 我只是想把這個docx文件,轉換並以pdf格式返回它[]格式。
我在以前的Microsoft.Office代碼看起來像這樣
public static byte[] ConvertDocx2PDF(byte[] DocxFile, string FileName)
{
try
{
string path = Path.Combine(HttpRuntime.AppDomainAppPath, "MailFiles/DOCX2PDF");
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
Guid id = Guid.NewGuid();
FileName = id.ToString() + FileName;
path += "/" + FileName;
if (File.Exists(path))
File.Delete(path);
File.WriteAllBytes(path, DocxFile);
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
object oMissing = System.Reflection.Missing.Value;
word.Visible = false;
word.ScreenUpdating = false;
// Cast as Object for word Open method
Object filename = (Object)path;
// Use the dummy value as a placeholder for optional arguments
Microsoft.Office.Interop.Word.Document doc = word.Documents.Open(ref filename, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
doc.Activate();
object outputFileName = (object)path.ToLower().Replace(".docx", ".pdf");
object fileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
if (File.Exists(outputFileName.ToString()))
File.Delete(outputFileName.ToString());
// Save document into PDF Format
doc.SaveAs(ref outputFileName,
ref fileFormat, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
((Microsoft.Office.Interop.Word._Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
doc = null;
((Microsoft.Office.Interop.Word._Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
word = null;
try
{
File.Delete(path);
}
catch { }
return File.ReadAllBytes(path.ToLower().Replace(".docx", ".pdf"));
}
catch (Exception e)
{
}
byte[] erroByte = new byte[0];
return erroByte;
}
至於說。它工作的很好,但不能在我的服務器上工作。
任何想法如何在openXML或任何其他?
謝謝您的時間
http://stackoverflow.com/a/607679/56778 –