生成文档的试验代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Aspose.Words;
using Aspose.Words.Properties;
using log4net;
using System.IO;
using System.Diagnostics;
using Aspose.Words.Tables;
using Aspose.Words.Drawing;
using Aspose.Words.Lists;
using Aspose.Words.Fields;

namespace Test
{
    public partial class Form1 : Form
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(Form1).Name);

        private String tbmessage = "";

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Aspose.Words.License license = new Aspose.Words.License();
            license.SetLicense("License.lic");


        }

        /**
         * 这是生成文档的试验代码
         * @parameter sender
         * @parameter e
         */
        private void button1_Click(object sender, EventArgs e)
        {
            tbStatus.Text = "正在创建。。。。。。";
            Document doc = new Document("c://4add.doc");
            DocumentBuilder builder = new DocumentBuilder(doc);
            try
            {
                //写入字符串
                builder.Writeln("Hello World");
                builder.Writeln("{FOR REPLACE}");

                //Form Field
                string[] items = { "One", "Two", "Three" };

                builder.InsertComboBox("DropDown", items, 0);

                //在插入文本前定制文件属性.

                Aspose.Words.Font font = builder.Font;

                font.Size = 24;

                font.Bold = true;

                font.Color = Color.Blue;

                font.Name = "Arial";

                font.Underline = Underline.Dash;

                builder.Write("Sample text.");


                /**
                 * Set paragraph formatting properties
                 * */
                ParagraphFormat paragraphFormat = builder.ParagraphFormat;
                //paragraphFormat.Alignment = ParagraphAlignment.Center;
                //paragraphFormat.LeftIndent = 0;
                //paragraphFormat.RightIndent = 50;
                //paragraphFormat.SpaceAfter = 25;

                // Output text
                builder.Writeln("I'm a very nice formatted paragraph. I'm intended to demonstrate how the left and right indents affect word wrapping.");
                builder.Writeln("I'm another nice formatted paragraph. I'm intended to demonstrate how the space after paragraph looks like.");


               
                //读出属性值
                Console.WriteLine("1. Document name: {0}", "output.doc");

                Console.WriteLine("2. Built-in Properties");
                foreach (DocumentProperty prop in doc.BuiltInDocumentProperties)

                    Console.WriteLine("{0} : {1}", prop.Name, prop.Value);

                Console.WriteLine("3. Custom Properties");

                foreach (DocumentProperty prop in doc.CustomDocumentProperties)

                  Console.WriteLine("{0} : {1}", prop.Name, prop.Value);

                //输出Doc的Style
                StyleCollection styles = doc.Styles;
                foreach (Style style in styles)
                   Console.WriteLine("style name:"+style.Name);

                //增加一个Section
                Section sectionToAdd = new Section(doc);
                doc.Sections.Add(sectionToAdd);

                SectionCollection sections = doc.Sections;
                Console.WriteLine("doc.sections.length=" + sections.Count);

               
                /**
                 * Moving Cursor
                 **/
                //移动到文件头和文件尾
                builder.MoveToDocumentEnd();
                builder.Writeln("This is the end of the document.");

                builder.MoveToDocumentStart();
                builder.Writeln("This is the beginning of the document.");

                //移动到文件的任意节点
                builder.MoveTo(doc.FirstSection.Body.LastParagraph);
                builder.MoveToDocumentEnd();

 

                //移动到Section
                builder.MoveToSection(1);
                builder.Writeln("This is the 3rd section.");

                //移动到Section中的Paragraph
                // Parameters are 0-index. Moves to third paragraph.
                builder.MoveToParagraph(1, 0);
                builder.Writeln("This is the 2rd paragraph.");

 

 

                //移动到页眉和页脚
                // Specify that we want headers and footers different for first, even and odd pages.
                builder.PageSetup.DifferentFirstPageHeaderFooter = true;
                builder.PageSetup.OddAndEvenPagesHeaderFooter = true;

                // Create three pages in the document.
                builder.MoveToSection(0);
                builder.Writeln("Page1");
                builder.InsertBreak(BreakType.PageBreak);
                builder.Writeln("Page2");
                builder.InsertBreak(BreakType.PageBreak);
                builder.Writeln("Page3");

                // Create the headers.
                builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
                builder.Write("Header First");
                builder.MoveToHeaderFooter(HeaderFooterType.HeaderEven);
                builder.Write("Header Even");
                builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
                builder.Write("Header Odd");

                //Move to a table cell.
                // All parameters are 0-index. Moves to the 2nd table, 3rd row, 5th cell.
                //builder.MoveToCell(1, 2, 4, 0);
                //builder.Writeln("Hello World!");

 

                //得到当前的Node和Paragraph
                Node curNode = builder.CurrentNode;
                Paragraph curParagraph = builder.CurrentParagraph;

 

                /**
                 * 替换**/

                doc.Range.Replace("{FOR REPLACE}", "IS_PLACED", false, false);

                /** Range**/
                string text = doc.Range.Text;
                string text2 = doc.Sections[0].Range.Text;

                /**
                 * 插入一个文档
                 * */
                //先退到文档开头,插入模板
                builder.MoveToSection(0);
                builder.MoveToDocumentStart();
                //curNode = builder.CurrentNode;
                //curParagraph = builder.CurrentParagraph;
                //插入的一种方式
                Document doctemp = new Document("c://testtemplate.doc");
                //InsertDocument(curParagraph, doctemp);

                //插入的第二种方式
                doc.AppendDocument(doctemp,ImportFormatMode.KeepSourceFormatting);


                //插入一个PAGE 域
                builder.Writeln("Page Number: ");
               //FieldStart pageField = builder.InsertField("PAGE","");
                builder.InsertField("PAGE", "");

            
                doc.UpdatePageLayout();
                doc.UpdateFields();

                //保存成PDF
                //保存成Doc
                builder.Document.Save("c://" + "output.doc");
                tbStatus.Text = "创建成功";


                //计算页码
                builder.MoveToDocumentEnd();
                int secCount = doc.Sections.Count;
                builder.MoveToSection(secCount - 1);


                int pages = doc.PageCount;
                int curPage = doc.BuiltInDocumentProperties.Pages;

                tbMessage.Text = "文件的当前页码数是:" + curPage+"/r/n";
                tbMessage.Text += "文件所有页码数(含封面和目录页)是:"+pages +"/r/n";


              

            }
            catch (Exception ex) {
                tbMessage.Text = "创建失败:"

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值