HTML转WORDWORD转PDF.docx

上传人:b****1 文档编号:523878 上传时间:2023-04-29 格式:DOCX 页数:15 大小:18.44KB
下载 相关 举报
HTML转WORDWORD转PDF.docx_第1页
第1页 / 共15页
HTML转WORDWORD转PDF.docx_第2页
第2页 / 共15页
HTML转WORDWORD转PDF.docx_第3页
第3页 / 共15页
HTML转WORDWORD转PDF.docx_第4页
第4页 / 共15页
HTML转WORDWORD转PDF.docx_第5页
第5页 / 共15页
HTML转WORDWORD转PDF.docx_第6页
第6页 / 共15页
HTML转WORDWORD转PDF.docx_第7页
第7页 / 共15页
HTML转WORDWORD转PDF.docx_第8页
第8页 / 共15页
HTML转WORDWORD转PDF.docx_第9页
第9页 / 共15页
HTML转WORDWORD转PDF.docx_第10页
第10页 / 共15页
HTML转WORDWORD转PDF.docx_第11页
第11页 / 共15页
HTML转WORDWORD转PDF.docx_第12页
第12页 / 共15页
HTML转WORDWORD转PDF.docx_第13页
第13页 / 共15页
HTML转WORDWORD转PDF.docx_第14页
第14页 / 共15页
HTML转WORDWORD转PDF.docx_第15页
第15页 / 共15页
亲,该文档总共15页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

HTML转WORDWORD转PDF.docx

《HTML转WORDWORD转PDF.docx》由会员分享,可在线阅读,更多相关《HTML转WORDWORD转PDF.docx(15页珍藏版)》请在冰点文库上搜索。

HTML转WORDWORD转PDF.docx

HTML转WORDWORD转PDF

/archive/2013/03/07/2948114.html

/archive/2010/11/10/1874187.html

HTML转WORDWORD转PDF---实践

文章:

/archive/2010/11/10/1874187.html是我摘自网络的,并未作验证。

因为有读者参考此文章在开发中遇到了一些问题,所以我建了一个WinForm应用程序,验证了下,希望能帮到人。

我的开发环境:

windowsXPSP3+VS2010(C#)+Office2010

新建一个WinForm应用程序,工程名:

WindowsApplication2,界面如下:

接着就是为按钮添加事件和代码(略)

说明:

1. 需要先安装AcrobatDistillr软件,具体参考:

/item/bb2826714a9f8d42ef1e53a1

2. 添加引用:

word和Acrobat5Distillr,同时设置这两个引用的属性中“嵌入互操作类型”为False

整个工程下载地址:

/joeblackzqq/5117901

Daima

从网上找的代码,先收藏下。

功能:

实现HTML转WORD,WORD转PDF

usingSystem;

using;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Text;

using;

usingWord=;

usingoWord=;

usingSystem.Reflection;

usingSystem.Configuration;

usingSystem.Web;

using;

using;

using;

using;

using;

using;

using;

namespaceWindowsApplication2

{

    publicpartialclassForm1:

Form

    {

        publicForm1()

        {

            InitializeComponent();

        }

        privatevoidbutton1_Click(objectsender,EventArgse)

        {

            objectoMissing=;

            objectoEndOfDoc="\\endofdoc";/*\endofdocisapredefinedbookmark*/

            //StartWordandcreateanewdocument.

            Word._ApplicationoWord;

            Word._DocumentoDoc;

            oWord=newWord.Application();

            oWord.Visible=true;

            oDoc=oMissing,refoMissing,

                refoMissing,refoMissing);

            //Insertaparagraphatthebeginningofthedocument.

            Word.ParagraphoPara1;

            oPara1=oMissing);

            ="Heading1";

            =1;

            =24;   //24ptspacingafterparagraph.

            ;

            //Insertaparagraphattheendofthedocument.

            Word.ParagraphoPara2;

            objectoRng=oEndOfDoc).Range;

            oPara2=oRng);

            ="Heading2";

            =6;

            ;

            //Insertanotherparagraph.

            Word.ParagraphoPara3;

            oRng=oEndOfDoc).Range;

            oPara3=oRng);

            ="Thisisasentenceofnormaltext.Nowhereisatable:

";

            =0;

            =24;

            ;

            //Inserta3x5table,fillitwithdata,andmakethefirstrow

            //boldanditalic.

            Word.TableoTable;

            Word.RangewrdRng=oEndOfDoc).Range;

            oTable=,3,5,refoMissing,refoMissing);

            =6;

            intr,c;

            stringstrText;

            for(r=1;r<=3;r++)

                for(c=1;c<=5;c++)

                {

                    strText="r"+r+"c"+c;

                    oTable.Cell(r,c).Range.Text=strText;

                }

            oTable.Rows[1].=1;

            oTable.Rows[1].=1;

            //Addsometextafterthetable.

            Word.ParagraphoPara4;

            oRng=oEndOfDoc).Range;

            oPara4=oRng);

            ;

            ="Andhere'sanothertable:

";

            =24;

            ;

            //Inserta5x2table,fillitwithdata,andchangethecolumnwidths.

            wrdRng=oEndOfDoc).Range;

            oTable=,5,2,refoMissing,refoMissing);

            =6;

            for(r=1;r<=5;r++)

                for(c=1;c<=2;c++)

                {

                    strText="r"+r+"c"+c;

                    oTable.Cell(r,c).Range.Text=strText;

                }

            oTable.Columns[1].Width=oWord.InchesToPoints

(2);//Changewidthofcolumns1&2

            oTable.Columns[2].Width=oWord.InchesToPoints(3);

            //Keepinsertingtext.Whenyougetto7inchesfromtopofthe

            //document,insertahardpagebreak.

            objectoPos;

            doubledPos=oWord.InchesToPoints(7);

            oEndOfDoc).Range.InsertParagraphAfter();

            do

            {

                wrdRng=oEndOfDoc).Range;

                =6;

                wrdRng.InsertAfter("Alineoftext");

                wrdRng.InsertParagraphAfter();

                oPos=wrdRng.get_Information

                               (;

            }

            while(dPos>=Convert.ToDouble(oPos));

            objectoCollapseEnd=;

            objectoPageBreak=;

            wrdRng.Collapse(refoCollapseEnd);

            wrdRng.InsertBreak(refoPageBreak);

            wrdRng.Collapse(refoCollapseEnd);

            wrdRng.InsertAfter("We'renowonpage2.Here'smychart:

");

            wrdRng.InsertParagraphAfter();

            //Insertachart.

            Word.InlineShapeoShape;

            objectoClassType="MSGraph.Chart.8";

            wrdRng=oEndOfDoc).Range;

            oShape=oClassType,refoMissing,

                refoMissing,refoMissing,refoMissing,

                refoMissing,refoMissing,refoMissing);

            //DemonstrateuseoflateboundoChartandoChartAppobjectsto

            //manipulatethechartobjectwithMSGraph.

            objectoChart;

            objectoChartApp;

            oChart=;

            oChartApp=oChart.GetType().InvokeMember("Application",

                BindingFlags.GetProperty,null,oChart,null);

            //ChangethecharttypetoLine.

            object[]Parameters=newObject[1];

            Parameters[0]=4;//xlLine=4

            oChart.GetType().InvokeMember("ChartType",BindingFlags.SetProperty,

                null,oChart,Parameters);

            //UpdatethechartimageandquitMSGraph.

            oChartApp.GetType().InvokeMember("Update",

                BindingFlags.InvokeMethod,null,oChartApp,null);

            oChartApp.GetType().InvokeMember("Quit",

                BindingFlags.InvokeMethod,null,oChartApp,null);

            //...Ifdesired,youcanproceedfromhereusingtheMicrosoftGraph

            //ObjectmodelontheoChartandoChartAppobjectstomakeadditional

            //changestothechart.

            //Setthewidthofthechart.

            oShape.Width=oWord.InchesToPoints(6.25f);

            oShape.Height=oWord.InchesToPoints(3.57f);

            //Addtextafterthechart.

            wrdRng=oEndOfDoc).Range;

            wrdRng.InsertParagraphAfter();

            wrdRng.InsertAfter("THEEND.");

            //Closethisform.

            this.Close();

        }

        privatevoidbutton2_Click(objectsender,EventArgse)

        {

            strings="";

            if(openFileDialog1.ShowDialog()==DialogResult.OK)

            {

                s=openFileDialog1.FileName;

            }

            else

            {

                return;

            }

            //在此处放置用户代码以初始化页面

            Word.ApplicationClassword=newWord.ApplicationClass();

            TypewordType=word.GetType();

            Word.Documentsdocs=word.Documents;

            //打开文件

            TypedocsType=docs.GetType();

            objectfileName=s;

            Word.Documentdoc=(Word.Document)docsType.InvokeMember("Open",

            ,null,docs,newObject[]{fileName,false,false});

            //转换格式,另存为

            TypedocType=doc.GetType();

            objectsaveFileName="d:

\\Reports\\aaa.doc";

            //下面是MicrosoftWord9ObjectLibrary的写法,如果是10,可能写成:

            /*

            docType.InvokeMember("SaveAs",,

             null,doc,newobject[]{saveFileName,;

            */

            ///其它格式:

            ///wdFormatHTML

            ///wdFormatDocument

            ///wdFormatDOSText

            ///wdFormatDOSTextLineBreaks

            ///wdFormatEncodedText

            ///wdFormatRTF

            ///wdFormatTemplate

            ///wdFormatText

            ///wdFormatTextLineBreaks

            ///wdFormatUnicodeText

            docType.InvokeMember("SaveAs",,

             null,doc,newobject[]{saveFileName,});

            //退出Word

            wordType.InvokeMember("Quit",,

             null,word,null);

        }

        privatevoidWordConvert(strings)

        {

            oWord.ApplicationClassword=new;

            TypewordType=word.GetType();

            //打开WORD文档

            /*对应脚本中的

             varword=newActiveXObject("Word.Application");

             vardoc   =;

            */

            oWord.Documentsdocs=word.Documents;

            TypedocsType=docs.GetType();

            objectobjDocName=s;

            oWord.Documentdoc=(oWord.Document)docsType.InvokeMember("Open",,null,docs,newObject[]{objDocName,true,true});

            //打印输出到指定文件

            //你可以使用doc.PrintOut();方法,次方法调用中的参数设置较繁琐,建议使用Type.InvokeMember来调用时可以不用将PrintOut的参数设置全,只设置4个主要参数

            TypedocType=doc.GetType();

            objectprintFileName=@"c:

\aaa.ps";

            docType.InvokeMember("PrintOut",,null,doc,newobject[]{false,false,,printFileName});

            //newobject[]{false,false,,printFileName}

            //对应脚本中的word.PrintOut(false,false,0,psfile);的参数

            //退出WORD

            //对应脚本中的word.Quit();

            wordType.InvokeMember("Quit",,null,word,null);

            objecto1="c:

\\aaa.ps";

            objecto2="c:

\\aaa.pdf";

            objecto3="";

            //引用将PS转换成PDF的对象

            //trycatch之间对应的是脚本中的PDF.FileToPDF(psfile,pdffile,"");    //你可以使用pdfConvert.FileToPDF("c:

\\test.ps","c:

\\test.pdf","");这样的转换方法,本人只是为了保持与WORD相同的调用方式

            try

            {

                ACRODISTXLib.PdfDistillerClasspdf=newACRODISTXLib.PdfDistillerClass();

                TypepdfType=pdf.GetType();

                pdfType.InvokeMember("FileToPDF",,null,pdf,newobject[]{o1,o2,o3});

                pdf=null;

            }

            catch{}//读者自己补写错误处理

            //为防止本方法调用多次时发

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2