コード例 #1
0
ファイル: WordUtils.cs プロジェクト: waodng/VSIX
 /// <summary>
 /// Inserts a watermark into a document.
 /// </summary>
 /// <param name="doc">The input document.</param>
 /// <param name="watermarkText">Text of the watermark.</param>
 public static void InsertWatermarkText(Aspose.Words.Document doc, string watermarkText)
 {
     // Create a watermark shape. This will be a WordArt shape.
     // You are free to try other shape types as watermarks.
     Aspose.Words.Drawing.Shape watermark = new Aspose.Words.Drawing.Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);
     // Set up the text of the watermark.
     watermark.TextPath.Text       = watermarkText;
     watermark.TextPath.FontFamily = "Arial";
     watermark.Width  = 500;
     watermark.Height = 100;
     // Text will be directed from the bottom-left to the top-right corner.
     watermark.Rotation = -40;
     // Remove the following two lines if you need a solid black text.
     watermark.Fill.Color  = System.Drawing.Color.Gray; // Try LightGray to get more Word-style watermark
     watermark.StrokeColor = System.Drawing.Color.Gray; // Try LightGray to get more Word-style watermark
     // Place the watermark in the page center.
     watermark.RelativeHorizontalPosition = Aspose.Words.Drawing.RelativeHorizontalPosition.Page;
     watermark.RelativeVerticalPosition   = Aspose.Words.Drawing.RelativeVerticalPosition.Page;
     watermark.WrapType            = Aspose.Words.Drawing.WrapType.None;
     watermark.VerticalAlignment   = Aspose.Words.Drawing.VerticalAlignment.Center;
     watermark.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;
     // Create a new paragraph and append the watermark to this paragraph.
     Aspose.Words.Paragraph watermarkPara = new Aspose.Words.Paragraph(doc);
     watermarkPara.AppendChild(watermark);
     // Insert the watermark into all headers of each document section.
     foreach (Aspose.Words.Section sect in doc.Sections)
     {
         // There could be up to three different headers in each section, since we want
         // the watermark to appear on all pages, insert into all headers.
         InsertWatermarkIntoHeader(watermarkPara, sect, Aspose.Words.HeaderFooterType.HeaderPrimary);
         InsertWatermarkIntoHeader(watermarkPara, sect, Aspose.Words.HeaderFooterType.HeaderFirst);
         InsertWatermarkIntoHeader(watermarkPara, sect, Aspose.Words.HeaderFooterType.HeaderEven);
     }
 }
コード例 #2
0
        /// <summary>
        /// word书签模版 导出 word 数据  返回文件地址
        /// </summary>
        /// <typeparam name="T">数据源类型</typeparam>
        /// <param name="Source">数据源</param>
        /// <param name="TempletePath">模版路径(相对)</param>
        /// <param name="dateTimeFormatter">时间格式 选填</param>
        /// <returns></returns>
        public static MemoryStream GetWordTempByMergeField <T>(this T Source, string TempletePath, string dateTimeFormatter = "yyyy-MM-dd") where T : class, new()
        {
            TempletePath = System.AppDomain.CurrentDomain.BaseDirectory + TempletePath;
            Document        doc    = new Document(TempletePath);
            DocumentBuilder buider = new DocumentBuilder(doc);
            var             ps     = Source.GetType().GetProperties();

            foreach (var p in ps)
            {
                string markName = p.Name;
                var    typeName = p.PropertyType.GenericTypeArguments.Length > 0 ? p.PropertyType.GenericTypeArguments[0].FullName : p.PropertyType.ToString();
                if (!buider.MoveToMergeField(markName))
                {
                    continue;
                }
                string val = string.Empty;
                switch (typeName)
                {
                case "System.DateTime":
                    var o = p.GetValue(Source);
                    if (o != null)
                    {
                        DateTime dt;
                        bool     b = DateTime.TryParse(o.ToString(), out dt);
                        if (b && dt > DateTime.MinValue)
                        {
                            val = dt.ToString(dateTimeFormatter);
                        }
                        buider.Write(string.Format("{0}", val));
                    }
                    break;

                case "System.Drawing.Image":
                    var img = p.GetValue(Source) as System.Drawing.Image;
                    if (img != null)
                    {
                        Aspose.Words.Drawing.Shape shape = buider.InsertImage(img);
                        //if (img.Height <= shape.Height)
                        //{
                        //    shape.Height = img.Height;
                        //}
                        if (img.Height > 300)    //xu_lz 20160908
                        {
                            shape.Height = 300;
                        }
                        else
                        {
                            shape.Height = img.Height;
                        }
                        if (img.Width > 400)
                        {
                            shape.Width = 400;
                        }
                        else
                        {
                            shape.Width = img.Width;
                        }
                    }
                    break;

                default:
                    val = string.Format("{0}", p.GetValue(Source));
                    buider.Write(string.Format("{0}", val));
                    break;
                }
            }
            string       fileLastName = TempletePath.Split('.')[TempletePath.Split('.').Length - 1];
            MemoryStream ms           = new MemoryStream();

            switch (fileLastName.ToLower().Trim())
            {
            case "doc":
                doc.Save(ms, SaveFormat.Doc);
                break;

            case "docx":
                doc.Save(ms, SaveFormat.Docx);
                break;

            default:
                doc.Save(ms, SaveFormat.Doc);
                break;
            }
            ms.Position = 0;
            return(ms);
        }
コード例 #3
0
 /// <summary>
 /// Convert Excel TextBox to Word TextBox
 /// </summary>
 /// <param name="excelTextBox">Excel TextBox</param>
 /// <param name="doc">Parent document</param>
 /// <returns>Word Shape</returns>
 private Aspose.Words.Drawing.Shape ConvertTextBoxToShape(Aspose.Cells.Drawing.TextBox excelTextBox, DocumentBase doc)
 {
     //Create a new TextBox
     Aspose.Words.Drawing.Shape wordsShape = new Aspose.Words.Drawing.Shape(doc, Aspose.Words.Drawing.ShapeType.TextBox);
     //Import TextBox properties inhereted from Shape
     ImportShapeProperties(wordsShape, (Shape)excelTextBox);
     //Import TextBox properties
     wordsShape.TextBox.LayoutFlow = ConvertDrawingTextOrientationType(excelTextBox.TextOrientationType);
     //Import text
     Run run = new Run(doc);
     if (!string.IsNullOrEmpty(excelTextBox.Text))
         run.Text = excelTextBox.Text;
     else
         run.Text = string.Empty;
     //Import text formating
     ImportFont(run.Font, excelTextBox.Font);
     //Create paragraph
     Paragraph paragraph = new Paragraph(doc);
     //Import horizontal alignment
     paragraph.ParagraphFormat.Alignment = ConvertHorizontalAlignment(excelTextBox.TextHorizontalAlignment);
     //Insert text into the paragraph
     paragraph.AppendChild(run);
     //insert Pragraph into textbox
     wordsShape.AppendChild(paragraph);
     return wordsShape;
 }
コード例 #4
0
        /// <summary>
        /// Convert Excel Shape to Word Shape
        /// </summary>
        /// <param name="excelShape">Excel Shape</param>
        /// <param name="doc">Parent document</param>
        /// <returns>Word Shape</returns>
        private Aspose.Words.Drawing.Shape ConvertShapeToShape(Aspose.Cells.Drawing.Shape excelShape, DocumentBase doc)
        {
            //Create words Shape
            Aspose.Words.Drawing.Shape wordsShape = new Aspose.Words.Drawing.Shape(doc, ConvertDrawingShapetype(excelShape.MsoDrawingType));
            //Import properties
            ImportShapeProperties(wordsShape, excelShape);

            wordsShape.Stroked = true;
            wordsShape.Filled = true;

            return wordsShape;
        }
コード例 #5
0
 /// <summary>
 /// Convert Excel Picture to Word Shape
 /// </summary>
 /// <param name="excelPicture">Excel Picture</param>
 /// <param name="doc">Parent document</param>
 /// <returns>Word Shape</returns>
 private Aspose.Words.Drawing.Shape ConvertPictureToShape(Aspose.Cells.Drawing.Picture excelPicture, DocumentBase doc)
 {
     //Create new Shape
     Aspose.Words.Drawing.Shape wordsShape = new Aspose.Words.Drawing.Shape(doc, Aspose.Words.Drawing.ShapeType.Image);
     //Set image
     wordsShape.ImageData.ImageBytes = excelPicture.Data;
     //Import Picture properties inhereted from Shape
     ImportShapeProperties(wordsShape, (Aspose.Cells.Drawing.Shape)excelPicture);
     return wordsShape;
 }
コード例 #6
0
 /// <summary>
 /// Convert Excel Chart to Word Shape
 /// </summary>
 /// <param name="excelChart">Excel Chart</param>
 /// <param name="doc">Parent document</param>
 /// <returns>Word Shape</returns>
 private Aspose.Words.Drawing.Shape ConvertCartToShape(Aspose.Cells.Charts.Chart excelChart, DocumentBase doc)
 {
     //Create a new Shape
     Aspose.Words.Drawing.Shape wordsShape = new Aspose.Words.Drawing.Shape(doc, Aspose.Words.Drawing.ShapeType.Image);
     //Convert Chart to Bitmap. Now only supports to convert 2D chart to image. If the chart is 3D chart,return null.
     Bitmap chartPicture = excelChart.ToImage();
     if (chartPicture != null)
     {
         wordsShape.ImageData.SetImage(chartPicture);
         //Import Chart properties inhereted from Shape
         ImportShapeProperties(wordsShape, (Shape)excelChart.ChartObject);
         return wordsShape;
     }
     else
     {
         return null;
     }
 }