我有這個問題的服務器解決方案,所以我轉換的Word文檔爲PDF編程。我現在需要在瀏覽器中調用同名的PDF文件。 (Doc1.docx到Doc1.pdf)您可以使用變量來調用文檔。這是C#後端代碼,很好地完成了這個技巧。還請記住添加對Microsoft Word 12.0對象庫的引用。調用此方法或使其成爲一個類。然後再調用文檔。
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Microsoft.Office.Interop.Word;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ApplicationClass wordApplication = new ApplicationClass();
Document wordDocument = null;
object paramSourceDocPath = @"D:\Websites\Docs\Doc1.docx";
object paramMissing = Type.Missing;
string paramExportFilePath = @"D:\Websites\Docs\Doc1.pdf";
WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;
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;
try
{
// Open the source document.
wordDocument = wordApplication.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);
// Export it in the specified format.
if (wordDocument != null)
wordDocument.ExportAsFixedFormat(
paramExportFilePath,
paramExportFormat,
paramOpenAfterExport,
paramExportOptimizeFor,
paramExportRange,
paramStartPage,
paramEndPage,
paramExportItem,
paramIncludeDocProps,
paramKeepIRM,
paramCreateBookmarks,
paramDocStructureTags,
paramBitmapMissingFonts,
paramUseISO19005_1,
ref paramMissing);
}
catch (Exception ex)
{
// Respond to the error
}
finally
{
// Close and release the Document object.
if (wordDocument != null)
{
wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
wordDocument = null;
}
// Quit Word and release the ApplicationClass object.
if (wordApplication != null)
{
wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
wordApplication = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
爲什麼不只是轉換這些文檔圖像。做這樣的服務器端不應該太難。 – Denis
靜態圖像可能已經足夠 - 但是我知道的唯一選項與轉換爲XPS的選項基本相同,即放入Office自動化或使用Aspose。我想知道是否有其他選擇。 –
我相信有更多的提供商存在可以將Office文檔轉換爲圖像(而不是XPS)的方式。彩虹PDF例如服務器解決方案爲$ 2000 http://rainbowpdf.com/server-based-solutions/ – Denis