将单词转换为安全pdf [英] Converting word to secured pdf

查看:81
本文介绍了将单词转换为安全pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在.net 2.0框架(VS2005)中开发了一个应用程序,我需要自动将word文档转换为pdf。能否帮助我将文字从c#web应用程序转换为安全的pdf文档....请在下面找到我用于转换为pdf的代码



< pre lang =c#> public void GeneratePDF()
{
Microsoft .Office.Interop.Word.Application wordApp = null ;

Microsoft.Office.Interop.Word.Document doc = null ;

对象 visible = true ;
// object readOnly = true;
object sourceFilename = this ._ sourceDocFileNameFullPath;
string targetFilename = this ._ destPDFFilePath;
object saveChanges = false ;

尝试
{
wordApp = new 应用程序();

doc = wordApp.Documents.Open( ref sourceFilename, ref 缺失, 参考缺失,参考缺失,参考缺失, ref 缺失, ref 缺失,
ref 缺失,参考缺失,参考缺失, ref 缺失, ref 可见, ref 缺失, ref 缺失,
ref 缺失, ref 缺失);

doc.ExportAsFixedFormat(targetFilename,WdExportFormat.wdExportFormatPDF, false ,WdExportOptimizeFor.wdExportOptimizeForOnScreen,
WdExportRange.wdExportAllDocument, 0 0 ,WdExportItem.wdExportDocumentContent, false false
WdExportCreateBookmarks.wdExportCreateNoBookmarks, false false false ref 缺失);

saveChanges = false ;
}
catch (例外情况)
{
// 此异常记录在数据库中并以调用方法处理
throw ex;
}
最后
{
如果(doc != null
{
doc.Close( ref saveChanges, ref 缺失, ref 缺失);
}
if (wordApp!= null
{
wordApp.Quit( ref saveChanges, ref 缺失,参考缺失);
}
}
}





如果有任何其他选项可以让我知道从c#.net代码获得pdf格式。



谢谢,

Prashant

解决方案

< blockquote>你需要使用免费库来构建PDf文件,如果你想以编程方式创建它,或者你可以使用第三方工具如cute pdf在创建后从word生成pdf文件


你可以试试这个:

 使用 Microsoft.Office.Interop.Word; 
使用系统;
使用 System.Collections.Generic;
使用 System.IO;
使用 System.Linq;
使用 System.Text;

...

// 创建一个新的Microsoft Word应用程序对象
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();

// C#没有可选参数,所以我们需要一个虚拟值
object oMissing = System.Reflection.Missing.Value;

// 获取指定目录中的Word文件列表
DirectoryInfo dirInfo = new DirectoryInfo( @ \\ server\folder);
FileInfo [] wordFiles = dirInfo.GetFiles( * .doc);

word.Visible = false ;
word.ScreenUpdating = false ;

foreach (FileInfo wordFile in wordFiles)
{
// 为单词Open方法转换为对象
Object filename =( Object )wordFile.FullName;

// 使用虚拟值作为可选参数的占位符
Document doc = word.Documents.Open( ref filename, ref oMissing,
< span class =code-keyword> 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 = wordFile.FullName.Replace( 。doc 。pdf);
object fileFormat = WdSaveFormat.wdFormatPDF;

// 将文档保存为PDF格式
doc.SaveAs ( ref outputFileName,
ref fileFormat, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
< span class =code-keyword> ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);

// 关闭Word文档,但保持Word应用程序处于打开状态。
// doc必须强制转换为_Document类型,以便找到
// 正确关闭方法。
object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
((_ Document)doc).Close( ref saveChanges, ref oMissing, ref oMissing);
doc = null ;
}

// 必须强制转换为_Application类型以便它将找到
// 正确的退出方法。
( (_Application)word).Quit( ref oMissing, ref oMissing,参考 oMissing);
word = null ;


Hi Everyone,

I have developed an application in .net 2.0 framework (VS2005) where i need to convert the word document to pdf automatically. Could you please help me with converting word to secured pdf document from c# web application....please find the code below which i am using for converting to pdf

public void GeneratePDF()
{
    Microsoft.Office.Interop.Word.Application wordApp = null;
    
    Microsoft.Office.Interop.Word.Document doc = null;

    object visible = true;
  //  object readOnly = true;
    object sourceFilename = this._sourceDocFileNameFullPath;
    string targetFilename = this._destPDFFilePath;
    object saveChanges = false;

    try
    {
        wordApp = new Application();
        
        doc = wordApp.Documents.Open(ref sourceFilename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                                              ref missing, ref missing, ref missing, ref  missing, ref  visible, ref missing, ref missing,
                                              ref missing, ref missing);

        doc.ExportAsFixedFormat(targetFilename, WdExportFormat.wdExportFormatPDF, false, WdExportOptimizeFor.wdExportOptimizeForOnScreen,
                                WdExportRange.wdExportAllDocument, 0, 0, WdExportItem.wdExportDocumentContent, false, false,
                                WdExportCreateBookmarks.wdExportCreateNoBookmarks, false, false, false, ref missing);

        saveChanges = false;
    }
    catch (Exception ex)
    {
        //This exception is logged in DB and handled in caller method
        throw ex;
    }
    finally
    {
        if (doc != null)
        {
            doc.Close(ref saveChanges, ref missing, ref missing);
        }
        if (wordApp != null)
        {
            wordApp.Quit(ref saveChanges, ref missing, ref missing);
        }
    }
}



Please let me know if there are any other options for generating secured pdf from c# .net code.

Thanks,
Prashant

解决方案

You either need to use the free libraries to build the PDf file if you wish to create it programatically or you can use third party tools like cute pdf to generate the pdf file from word after it is created


You may try with this:

using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

...

// Create a new Microsoft Word application object
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();

// C# doesn't have optional arguments so we'll need a dummy value
object oMissing = System.Reflection.Missing.Value;

// Get list of Word files in specified directory
DirectoryInfo dirInfo = new DirectoryInfo(@"\\server\folder");
FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");

word.Visible = false;
word.ScreenUpdating = false;

foreach (FileInfo wordFile in wordFiles)
{
    // Cast as Object for word Open method
    Object filename = (Object)wordFile.FullName;

    // Use the dummy value as a placeholder for optional arguments
    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 = wordFile.FullName.Replace(".doc", ".pdf");
    object fileFormat = WdSaveFormat.wdFormatPDF;

    // 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);

    // Close the Word document, but leave the Word application open.
    // doc has to be cast to type _Document so that it will find the
    // correct Close method.                
    object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
    ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
    doc = null;
}

// word has to be cast to type _Application so that it will find
// the correct Quit method.
((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
word = null;


这篇关于将单词转换为安全pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆