コード例 #1
0
ファイル: WordUtils.cs プロジェクト: waodng/VSIX
 /// <summary>
 /// Inserts a watermark into a document header.
 /// </summary>
 /// <param name="watermarkPara"></param>
 /// <param name="sect"></param>
 /// <param name="headerType"></param>
 public static void InsertWatermarkIntoHeader(Aspose.Words.Paragraph watermarkPara, Aspose.Words.Section sect, Aspose.Words.HeaderFooterType headerType)
 {
     Aspose.Words.HeaderFooter header = sect.HeadersFooters[headerType];
     if (null == header)
     {
         // There is no header of the specified type in the current section, create it.
         header = new Aspose.Words.HeaderFooter(sect.Document, headerType);
         sect.HeadersFooters.Add(header);
     }
     // Insert a clone of the watermark into the header.
     header.AppendChild(watermarkPara.Clone(true));
 }
コード例 #2
0
 /// <summary>
 /// 生成页码
 /// </summary>
 /// <param name="builder"></param>
 public static void AutoGenPageNum(Aspose.Words.Document doc, Aspose.Words.DocumentBuilder builder)
 {
     Aspose.Words.HeaderFooter footer = new Aspose.Words.HeaderFooter(doc, Aspose.Words.HeaderFooterType.FooterPrimary);
     doc.FirstSection.HeadersFooters.Add(footer);
     // Add a paragraph with text to the footer.
     footer.AppendParagraph("").ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Center;
     // We want to insert a field like this: {PAGE} / {NUMPAGES}
     // TODO Go to the primary footer
     builder.MoveToHeaderFooter(Aspose.Words.HeaderFooterType.FooterPrimary);
     // TODO Add fields for current page number
     builder.InsertField("PAGE");
     // TODO Add any custom text formatter
     builder.Write(" / ");
     // TODO Add field for total page numbers in document
     builder.InsertField("NUMPAGES");
     // Finally update the outer field to recalcaluate the final value.
     // Doing this will automatically update the inner fields at the same time.
     // field.Update();
 }
コード例 #3
0
ファイル: WordUtils.cs プロジェクト: waodng/VSIX
        /// <summary>
        /// 引用Aspose.Words.dll导出word数据库字典文档
        /// 注意:不依赖微软office办公软件
        /// </summary>
        /// <param name="databaseName"></param>
        /// <param name="tables"></param>
        public static void ExportWordByAsposeWords(string fileName, string databaseName, List <TableDto> tables)
        {
            Aspose.Words.Document doc = new Aspose.Words.Document();

            // TODO document properties
            doc.BuiltInDocumentProperties.Subject           = "设计文档";
            doc.BuiltInDocumentProperties.ContentType       = "数据库字典";
            doc.BuiltInDocumentProperties.Title             = "数据库字典文档";
            doc.BuiltInDocumentProperties.Author            = doc.BuiltInDocumentProperties.LastSavedBy = doc.BuiltInDocumentProperties.Manager = "trycache";
            doc.BuiltInDocumentProperties.Company           = "waodng";
            doc.BuiltInDocumentProperties.Version           = doc.BuiltInDocumentProperties.RevisionNumber = 1;
            doc.BuiltInDocumentProperties.ContentStatus     = "初稿";
            doc.BuiltInDocumentProperties.NameOfApplication = "DBCHM";
            doc.BuiltInDocumentProperties.LastSavedTime     = doc.BuiltInDocumentProperties.CreatedTime = System.DateTime.Now;

            // TODO header and footer setting
            Aspose.Words.HeaderFooter header = new Aspose.Words.HeaderFooter(doc, Aspose.Words.HeaderFooterType.HeaderPrimary);
            doc.FirstSection.HeadersFooters.Add(header);
            // Add a paragraph with text to the header.
            header.AppendParagraph("数据库字典文档").ParagraphFormat.Alignment =
                Aspose.Words.ParagraphAlignment.Right;
            Aspose.Words.HeaderFooter footer = new Aspose.Words.HeaderFooter(doc, Aspose.Words.HeaderFooterType.FooterPrimary);
            doc.FirstSection.HeadersFooters.Add(footer);
            // Add a paragraph with text to the footer.
            footer.AppendParagraph(" 版权所有 @ waodng  ").ParagraphFormat.Alignment =
                Aspose.Words.ParagraphAlignment.Center;

            Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);

            // TODO 创建文档标题书签
            CreateBookmark(builder, Aspose.Words.ParagraphAlignment.Center, Aspose.Words.OutlineLevel.Level1, 25,
                           asposeBookmark_prefix + "0", "数据库字典文档");
            builder.ParagraphFormat.OutlineLevel = Aspose.Words.OutlineLevel.BodyText;
            builder.Writeln("—— " + databaseName);

            // TODO 换行
            builder.InsertBreak(Aspose.Words.BreakType.ParagraphBreak);
            builder.InsertBreak(Aspose.Words.BreakType.ParagraphBreak);
            builder.InsertBreak(Aspose.Words.BreakType.ParagraphBreak);

            // TODO 数据库字典文档修订日志表
            CreateBookmark(builder, Aspose.Words.ParagraphAlignment.Center, Aspose.Words.OutlineLevel.Level2, 16,
                           asposeBookmarkLog, AppConst.LOG_CHAPTER_NAME);
            CreateLogTable(builder);
            builder.InsertBreak(Aspose.Words.BreakType.PageBreak);

            // TODO 创建数据库字典文档数据库概况一览表
            CreateBookmark(builder, Aspose.Words.ParagraphAlignment.Center, Aspose.Words.OutlineLevel.Level2, 16,
                           asposeBookmarkOverview, AppConst.TABLE_CHAPTER_NAME);
            CreateOverviewTable(builder, tables);
            builder.InsertBreak(Aspose.Words.BreakType.PageBreak);

            // TODO 创建书签
            CreateBookmark(builder, Aspose.Words.ParagraphAlignment.Left, Aspose.Words.OutlineLevel.Level2, 16,
                           asposeBookmark_prefix + 0, AppConst.TABLE_STRUCTURE_CHAPTER_NAME);

            int i = 0; // 计数器

            // TODO 遍历数据库表集合
            foreach (var table in tables)
            {
                string bookmarkName = table.TableName + " " + (!string.IsNullOrWhiteSpace(table.Comment) ? table.Comment : "");

                // TODO 创建书签
                CreateBookmark(builder, Aspose.Words.ParagraphAlignment.Left, Aspose.Words.OutlineLevel.Level3, 16,
                               asposeBookmark_prefix + i, table.TableOrder + "、" + bookmarkName);

                // TODO 遍历数据库表字段集合
                // TODO 创建表格
                Aspose.Words.Tables.Table asposeTable = builder.StartTable();

                // 清除段落样式
                builder.ParagraphFormat.ClearFormatting();

                #region 表格列设置,列标题,列宽,字体等
                // Make the header row.
                builder.InsertCell();
                // Set the left indent for the table. Table wide formatting must be applied after
                // at least one row is present in the table.
                asposeTable.Alignment      = Aspose.Words.Tables.TableAlignment.Center;
                asposeTable.PreferredWidth = PreferredWidth.FromPercent(120);
                asposeTable.AllowAutoFit   = false;
                // Set height and define the height rule for the header row.
                builder.RowFormat.Height     = 40.0;
                builder.RowFormat.HeightRule = Aspose.Words.HeightRule.AtLeast;
                // Some special features for the header row.
                builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.FromArgb(198, 217, 241);
                builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Center;
                builder.Font.Size = 14;
                builder.Font.Name = "Arial";
                builder.Font.Bold = true;
                builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(8);
                builder.Write("序号");

                // We don't need to specify the width of this cell because it's inherited from the previous cell.
                builder.InsertCell();
                builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(20);
                builder.Write("列名");

                builder.InsertCell();
                builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(12);
                builder.Write("数据类型");

                builder.InsertCell();
                builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(8);
                builder.Write("长度");

                builder.InsertCell();
                builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(8);
                builder.Write("小数位");

                builder.InsertCell();
                builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(8);
                builder.Write("主键");

                builder.InsertCell();
                builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(8);
                builder.Write("自增");

                builder.InsertCell();
                builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(8);
                builder.Write("允许空");

                builder.InsertCell();
                builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(10);
                builder.Write("默认值");

                builder.InsertCell();
                builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(30);
                builder.Write("列说明");
                builder.EndRow();
                #endregion

                foreach (var column in table.Columns)
                {
                    #region 遍历表格数据行写入
                    // Set features for the other rows and cells.
                    builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.White;
                    builder.CellFormat.Width             = 100.0;
                    builder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;
                    //builder.CellFormat.FitText = true;
                    // Reset height and define a different height rule for table body
                    builder.RowFormat.Height     = 60.0;
                    builder.RowFormat.HeightRule = Aspose.Words.HeightRule.AtLeast;
                    builder.InsertCell();
                    // Reset font formatting.
                    builder.Font.Size = 12;
                    builder.Font.Bold = false;
                    builder.Write(column.ColumnOrder); // 序号

                    builder.InsertCell();
                    builder.Write(column.ColumnName); // 列名

                    builder.InsertCell();
                    builder.Write(column.ColumnTypeName); // 数据类型

                    builder.InsertCell();
                    builder.Write(column.Length); // 长度

                    builder.InsertCell();
                    builder.Write(column.Scale); // 小数位

                    builder.InsertCell();
                    builder.Write(column.IsPK); // 主键

                    builder.InsertCell();
                    builder.Write(column.IsIdentity); // 自增

                    builder.InsertCell();
                    builder.Write(column.CanNull); // 是否为空

                    builder.InsertCell();
                    builder.Font.Size = 10;
                    builder.Write(column.DefaultVal); // 默认值

                    builder.InsertCell();
                    builder.Font.Size = 10;
                    builder.Write(column.Comment); // 列说明

                    builder.EndRow();
                    #endregion
                }

                // TODO 表格创建完成,结束
                //asposeTable.PreferredWidth = Aspose.Words.Tables.PreferredWidth.Auto;
                //asposeTable.AutoFit(Aspose.Words.Tables.AutoFitBehavior.AutoFitToContents);
                builder.EndTable();

                i++;

                // TODO page breaks
                if (i < tables.Count)
                {
                    builder.InsertBreak(Aspose.Words.BreakType.PageBreak);
                }
            }

            // TODO 添加水印
            //InsertWatermarkText(doc, "DBCHM-51Try.Top");

            doc.Save(fileName);
        }