Recently, a project needs to convert files (such as Word, Excel, txt, and image) to PDF files. After searching the documents, there are many implementation methods, such as calling a virtual printer for conversion, use third-party components (such as itextsharp and sharppdf. Now both of them are used. When HTML is converted to a PDF file, I cut the HTML into an image and convert it to a PDF file. If you have good suggestions, you can discuss it together.
What are the best implementation methods for converting PDF files to excel files?
First paste some source code,: http://download.csdn.net/source/2646952
C # code
/// <Summary>
/// Convert EXCEL to PDF
/// </Summary>
/// <Param name = "file_inputname"> </param>
Public void printsheettopdf (string fileinputname)
{
Try
{
Getprintername (ref printername, ref obj_printer_settings, ref otype );
Printdocument prtdoc = new printdocument ();
Currentprintername = prtdoc. printersettings. printername; // get the default printer name
// Change the default printer
Setdefaprinprinter (printername. Trim ());
// Notify the change
Sendpolicymessage (hwnd_broadcast, wm_wininichange, 0, "Windows ");
If (fileinputname = "")
{
Return;
}
Filepath = filesavepath + "//" + getfilename (fileinputname) + ". pdf ";
Otype. invokemember ("init", system. reflection. bindingflags. invokemethod, null, obj_printer_settings, null );
Otype. invokemember ("setvalue", system. reflection. bindingflags. invokemethod, null, obj_printer_settings, new object [] {"output", filepath });
Otype. invokemember ("setvalue", system. reflection. bindingflags. invokemethod, null, obj_printer_settings, new object [] {"showsettings", "never "});
Otype. invokemember ("setvalue", system. reflection. bindingflags. invokemethod, null, obj_printer_settings, new object [] {"showpdf", "no "});
Otype. invokemember ("setvalue", system. reflection. bindingflags. invokemethod, null, obj_printer_settings, new object [] {"showprogress", "no "});
Otype. invokemember ("setvalue", system. reflection. bindingflags. invokemethod, null, obj_printer_settings, new object [] {"showprogressfinished", "no "});
Otype. invokemember ("setvalue", system. reflection. bindingflags. invokemethod, null, obj_printer_settings, new object [] {"suppresserrors", "yes "});
Otype. invokemember ("setvalue", system. reflection. bindingflags. invokemethod, null, obj_printer_settings, new object [] {"confirmoverwrite", "no "});
Otype. invokemember ("writesettings", system. reflection. bindingflags. invokemethod, null, obj_printer_settings, new object [] {true });
If (file. exists (filepath) = true)
{
File. Delete (filepath );
}
Object objmissing = system. type. missing;
Object objvalue = true;
Microsoft. Office. InterOP. Excel. Application objexcel = new Microsoft. Office. InterOP. Excel. Application ();
Microsoft. office. interOP. excel. workbook objworkbook = objexcel. workbooks. open (filesavepath + "//" + fileinputname, objmissing, objvalue, objmissing, objmissing, missing, fail, objmissing, fail, objmissing );
Objexcel. Visible = false;
For (INT I = 1; I <objworkbook. worksheets. Count; I ++)
{
_ Worksheet active_workbook = (_ worksheet) objexcel. worksheets. get_item (I );
Active_workbook.pagesetup.zoom = false;
Active_workbook.pagesetup.fittopageswide = 1;
Active_workbook.pagesetup.fittopagestall = 10;
}
Objexcel. activeworkbook. printout (objmissing, objmissing );
For (INT f_intstep = 1; f_intstep <20000; f_intstep ++)
{
System. Threading. thread. Sleep (100 );
If (file. exists (filepath) = true)
{
Break;
}
}
Objworkbook. Close (false, fileinputname, false );
Objexcel. Quit ();
Objworkbook = NULL;
Objexcel = NULL;
Int K = 0;
Intptr T = new intptr (objexcel. hwnd );
Getwindowthreadprocessid (T, out k );
System. Diagnostics. PROCESS p = system. Diagnostics. process. getprocpolicyid (k );
P. Kill ();
Obj_printer_settings = NULL;
// Change the default printer
Setdefaprinprinter (currentprintername. Trim ());
// Notify the change
Sendpolicymessage (hwnd_broadcast, wm_wininichange, 0, "Windows ");
}
Catch (exception E)
{
Obj_printer_settings = NULL;
// Throw E;
}
}