linux环境实现预览word,⽂档在线预览的实现
最近在研究企业⽂档管理,这个是基本上所有企业都需要的软件,当然也是有很多种解决⽅案。对于企业⽂档来说,最基本的需求就是独⽴存储,共享。这种需求只需要建⽴⼀个Windows共享⽂件夹或者架⼀个Samba服务器即可实现,⽆法做复杂的权限管理,统计等。另⼀种⽅案就是架⼀个Web应⽤,⽐如SharePoint,就可以实现。
既然是WEB应⽤,进⼀步的需求是能够在线查看⽂档,根据⽤户需求可能不允许下载,不允许打印⽂档。这⼀点微软的⾼级解决⽅案是使⽤RMS,能够设置每个⽤户的打开权限,是否打印等,要求必须是域内,⽽且只管理Office⽂件的权限,对txt,pdf就没办法了。另外⼀个解决⽅案是在线⽂档预览,⽤户在⽹页中查看⽂档内容,⽤户⽆需拿到原始⽂档,如果有权限的话,可以允许⽤户下载⽂档。这就就是百度⽂库,⾖丁之类的⽹站的功能。下⾯来说说怎么实现。
1.⽂档统⼀转换为pdf
这⾥的⽂档我们要看是什么格式,不同的格式有不同的转换⽅法。
1.1 Office⽂档转换pdf
对于Office⽂档(Word,Excel,PowerPoint),那么可以调⽤Office提供的COM接⼝,把⽂档另存为PDF。
这个要求服务器上必须安装Office,同时要注意权限,不然很容易导致在本地调试时可以转换为PDF,但是⼀旦部署到服务器上去就不⾏。另外还需要注意的是,如果Office转换pdf时发⽣异常,可能导致Office的进程驻留在服务器,不断驻留Office进程会导致服务器资源耗尽。
这是Office⽂档转换为pdf的代码:
//将word⽂档转换成PDF格式public static bool ConvertWord2Pdf(string sourcePath, string targetPath)
{
bool result;
Word.WdExportFormat exportFormat= Word.WdExportFormat.wdExportFormatPDF;
object paramMissing = Type.Missing;
Word.Application wordApplication = new Word.Application();
Word.Document wordDocument = null;
try
{
object paramSourceDocPath = sourcePath;
string paramExportFilePath = targetPath;
Word.WdExportFormat paramExportFormat = exportFormat;
Word.WdExportOptimizeFor paramExportOptimizeFor =
Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
int paramStartPage = 0;
int paramEndPage = 0;
Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
Word.WdExportCreateBookmarks paramCreateBookmarks =
Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
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);
if (wordDocument != null)
wordDocument.ExportAsFixedFormat(paramExportFilePath, paramExportFormat, false,
paramExportOptimizeFor, paramExportRange, paramStartPage, paramEndPage, paramExportItem, true,
true, paramCreateBookmarks, true,
true, false,
ref paramMissing);
result = true;
}
finally
{
if (wordDocument != null)
{
wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing); wordDocument = null;
}
if (wordApplication != null)
{
wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing); wordApplication = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
/
/将excel⽂档转换成PDF格式public static bool ConvertExcel2Pdf(string sourcePath, string targetPath)
{
bool result;
object missing = Type.Missing;
Excel.XlFixedFormatType targetType= Excel.XlFixedFormatType.xlTypePDF;
Excel.Application application = null;
Excel.Workbook workBook = null;
try
{
application = new Excel.Application();
object target = targetPath;
workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing
result = true;
}
catch
{
result = false;
}
finally
{
if (workBook != null)
{
workBook.Close(true, missing, missing);
workBook = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
//将ppt⽂档转换成PDF格式public static bool ConvertPowerPoint2Pdf(string sourcePath, string targetPath)
{
bool result;
flash swf播放器PowerPoint.PpSaveAsFileType targetFileType= PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
PowerPoint.Application application = null;
PowerPoint.Presentation persentation = null;
try
{
application = new PowerPoint.Application();
persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFals
persentation.SaveAs(targetPath, targetFileType, MsoTriState.msoTrue);
result = true;
}
catch
{
result = false;
}
finally
{
if (persentation != null)
{
persentation.Close();
persentation = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
1.2 纯⽂本转换pdf
如果是⽂本需要转换为PDF,我们可以使⽤iTextSharp这个组件,对于纯⽂本,注意的是源⽂件中没有设置字体之类的,需要在转换成PDF时指定字体,否则对于中⽂可能由于没有设置字体⽽转换不出来。
//将Txt转换为PDF public static bool ConvertText2Pdf(string sourcePath, string targetPath)
{
var text = FileHelper.ReadTextFile(sourcePath);
Document document = new Document(PageSize.A4);
try
{
//step 2:创建⼀个writer⽤于监听Document以及通过PDF-stream指向⼀个⽂件
PdfWriter.GetInstance(document, new FileStream(targetPath, FileMode.Create));
//step 3: 打开document document.Open();
var f = GetFont();
//step 4: 添加⼀段话到document中 document.Add(new Paragraph(text, f));
}
catch (Exception ex)
{
return false;
}
finally
{
if (document.IsOpen())
/
/step 5: 关闭document document.Close();
}
return true;
}
private static Font GetFont()
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论