Convert word to pdf

Source: Internet
Author: User
Tags acrobat distiller

The project needs to convert word to pdf online. Many Methods on the Internet do not support IIS or just WINFORM. You have sorted out the following available methods.

The main com components to be called include Acrobat Distiller [produced by the PDF Company] And WORD. These two components require the installation of Adobe Acrobat Professional 7.0 and later on the machine, because the software is a paid software, it needs to be cracked. The word component requires the installation of microsoft office word 2003 software on the machine.
During deployment to IIS, because the IIS NetWork Service [For IIS6.0] user does not have the permission to call the local COM component, when the code is in IIS, the conversion fails. It took some time, the reason is that the COM component is an administrator user and IIS is a network Service. I want to solve the problem by changing the permissions of the COM component, but it has changed a lot from one error to another. In addition, you cannot grant permissions to components that have not been registered.
The solution is in the web. use identity simulation in config, in <system. in the web> section, add <identity impersonate = "true" userName = "your userName" password = "password"/>, so that IIS has the permission to call the com component.
The above is only a theoretical solution. In actual testing, there are still various problems, such as the closure of word conversion and the deletion of intermediate documents. The implementation method is as follows:
1: In the web. use identity simulation in config, in <system. add <identity impersonate = "true" userName = "your userName" password = "password"/> to the web> section.
2: install Adobe Acrobat Professional 7.0 and its cracking (the uncracked version can only be used for 30 days ).
3. Install a virtual printer: Go to the control panel of WINDOWS, enter the printer, click the "printer and fax" icon, and click "Next" in the installation dialog box ", pay attention to removing the automatic detection and installation of plug-and-play printers. When selecting a printer appears, select "Generic" in the "manufacturer" column. In the "Printer" column, select "MS Publisher Color Printer" and click "Next" until the installation is complete. The following code does not provide the current Printer, but uses the default "MS Publisher Color Printer ".
4: you must add the reference Interop. nvidistxlib. dll and Microsoft. Office. Interop. Word. dll, which can be added from the COM component. Since the two components have been extracted, copy them directly to the BIN directory.

Main Implementation Code:

Using oWord = Microsoft. Office. Interop. Word;
Using System. Diagnostics;

Private void wordConvert (string wordname, string wordPath, string pdfPath)
{
OWord. _ Document m_Document = null;
OWord. _ Application m_wordApplication = null;
Object oMissing = Type. Missing;
OWord. ApplicationClass word = new Microsoft. Office. Interop. Word. ApplicationClass ();
Object obj = System. Reflection. BindingFlags. InvokeMethod;
Type wordType = word. GetType ();
OWord. Documents docs = word. Documents;
Type docsType = docs. GetType ();
Object objDocName = wordPath;
OWord. document doc = (oWord. document) docsType. invokeMember ("Open", System. reflection. bindingFlags. invokeMethod, null, docs, new Object [] {objDocName, true, true });
// Print the output to the specified file
Type docType = doc. GetType ();
Object printFileName = @ "c: \ test. ps ";
DocType. InvokeMember ("PrintOut", System. Reflection. BindingFlags. InvokeMethod, null, doc, new object [] {false, false, oWord. WdPrintOutRange. wdPrintAllDocument, printFileName });
Object savechanges = Microsoft. Office. Interop. Word. WdSaveOptions. wdSaveChanges;
Object saveasPath = Server. MapPath ("../") + "docsave \ 000 temp" + wordname + ". doc ";
// Must be saved!
Doc. saveAs (ref saveasPath, ref oMissing, ref oMissing, ref oMissing, ref oMissing );
// Must be disabled
Doc. Close (ref savechanges, ref oMissing, ref oMissing );
WordType. InvokeMember ("Quit", System. Reflection. BindingFlags. InvokeMethod, null, word, null );
// Delete Save As File
Try
{
System. IO. File. Delete (Server. MapPath ("../") + "docsave \ 000 temp" + wordname + ". doc ");
}
Catch
{
}
String o1 = "c :\\ test. ps"; // synchronize with the generated PS File
String o2 = pdfPath;
String o3 = "";
// Reference the object that converts PS to PDF
Try
{
Required distxlib. Required distillerclass pdf = new required distxlib. Required distillerclass ();
Type encoding Type = pdf. GetType ();
Primitive type. InvokeMember ("FileToPDF", System. Reflection. BindingFlags. InvokeMethod, null, pdf, new object [] {o1, o2, o3 });
Pdf = null;
System. IO. File. Delete (Server. MapPath ("../") + "PDF \" + wordname + ". log"); // clear the conversion log File
}
Catch {throw new Exception ("An error occurred while converting PS to PDF! ");}
// The dist.exe process is stopped to prevent errors that occur when the specified deployment is used for multiple times.
Foreach (Process proc in System. Diagnostics. Process. GetProcesses ())
{
Int begpos;
Int endpos;

String sProcName = proc. ToString ();
Begpos = sProcName. IndexOf ("(") + 1;
Endpos = sProcName. IndexOf (")");

SProcName = sProcName. Substring (begpos, endpos-begpos );

If (sProcName. ToLower (). CompareTo ("includist") = 0)
{
Try
{
Proc. Kill ();
}
Catch {}
Break;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.