我准备在通过Word提供的API实现印章功能,在Word文档中插入印章,印章显示为一张透明背景的图片,但是可以通过点击右键查看印章信息,如盖章时间等,也就是每个印章包含不同的信息。请问各位大虾可以通过什么来实现呢?通过C++、VB写ActiveX控件,还是说VSTO可以直接实现,我学的C#,可以通过C#实现这个对象吗?在线跪求解答解决方案。 

解决方案 »

  1.   

    http://blog.csdn.net/johnsuna/article/details/7915588 可以参考一下这个。另外,以下这些代码或许有用:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing.Text;namespace 电子印章
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private Font myFont;
            private int roll;
            private int textWide;
            private int sealSize;
            private int textSpace;
            private int pictureSize;
            bool isPictureCheck;
            Brush myBrush;
            Color myColor;
            string text1;
            string text2;
            string text3;
            int textHigh;
            int textSize1;
            int textSize2;
            int textHigh1;
            int textHigh2;
            int textWidth1;
            int textWidth2;
            int lineWidth;
            Pen myPen;        void DrawSealText(Graphics g , Brush myBrush)
            {  
                int nChinese = this.textBox1.Text.Length;
                String myText = this.textBox1.Text;
                String[] chinese = new string[50];
                int i;
        //将单个字保存在数组中
            for(i = 0; i < nChinese; i ++)
            {
                    chinese[i] = myText.Substring(i , 1);
            }
                //定义字体大小
                SizeF sf = g.MeasureString(chinese[0], myFont);
                RectangleF rectF = new RectangleF(0, 0, sf.Width, sf.Height);
                //定义字中心到圆心的半径
                float r = this.panel1.Width  * this.sealSize / 200 - this.roll; 
                //定义字的位置
                float xTemp,yTemp;
                //定义字旋转角度
                float angle;
                
                //圆心
                Point cPoint = new Point();
            cPoint.X = cPoint.Y = this.panel1.Width/2;
            
            //显示签章单位
                //字体居中
                // Set format of string.
                StringFormat drawFormat = new StringFormat();
                //drawFormat.Trimming = StringTrimming.EllipsisCharacter;
                drawFormat.Alignment = StringAlignment.Center;
                
                //g.TranslateTransform(point.X, point.Y);//设置新的坐标原点             
                //g.RotateTransform(-50);//旋转270度 
                //g.DrawString(myText, myFont, myBrush, rectF, drawFormat);//以新的原点画文本 
                //g.ResetTransform();//还原坐点原点 
                this.myFont = new Font(this.fontComboBox.Text, this.textWide);
                //字数为偶数时
                if (nChinese % 2 == 0)
                {
                    //显示左半边字
                    for (i = 0; i < nChinese / 2; i++)
                    {
                        angle = (float)((nChinese / 2 - i - 1.0 / 2) * this.textSpace);
                        sf = g.MeasureString(chinese[i], myFont);
                        rectF = new RectangleF(0, 0, sf.Width, sf.Height);
                        xTemp = (float)(cPoint.X - r * Math.Sin(angle * Math.PI / 180) - sf.Width / 2 * Math.Cos(angle * Math.PI / 180));
                        yTemp = (float)(cPoint.Y - r * Math.Cos(angle * Math.PI / 180) + sf.Height / 2 * Math.Sin(angle * Math.PI / 180));
                        g.TranslateTransform(xTemp, yTemp);//设置新的坐标原点             
                        g.RotateTransform(-angle);//旋转 
                        g.DrawString(chinese[i], myFont, myBrush, rectF, drawFormat);//以新的原点画文本 
                        g.ResetTransform();//还原坐点原点 
                    }                //显示右半边字
                    for (; i < nChinese; i++)
                    {
                        angle = (float)((i - nChinese / 2 + 1.0 / 2) * this.textSpace);
                        xTemp = (float)(cPoint.X + r * Math.Sin(angle * Math.PI / 180) - sf.Width / 2 * Math.Cos(angle * Math.PI / 180));
                        yTemp = (float)(cPoint.Y - r * Math.Cos(angle * Math.PI / 180) - sf.Height / 2 * Math.Sin(angle * Math.PI / 180));
                        sf = g.MeasureString(chinese[i], myFont);
                        rectF = new RectangleF(0, 0, sf.Width, sf.Height);
                        g.TranslateTransform(xTemp, yTemp);//设置新的坐标原点             
                        g.RotateTransform(angle);//旋转 
                        g.DrawString(chinese[i], myFont, myBrush, rectF, drawFormat);//以新的原点画文本 
                        g.ResetTransform();//还原坐点原点 
                    }
                }
                //字数为奇数时
                else
                {
                    //显示左半边字和中间字
                    for (i = 0; i < (nChinese + 1) / 2; i++)
                    {
                        angle = (float)(((nChinese - 1) / 2 - i) * this.textSpace);
                        sf = g.MeasureString(chinese[i], myFont);
                        rectF = new RectangleF(0, 0, sf.Width, sf.Height);
                        xTemp = (float)(cPoint.X - r * Math.Sin(angle * Math.PI / 180) - sf.Width / 2 * Math.Cos(angle * Math.PI / 180));
                        yTemp = (float)(cPoint.Y - r * Math.Cos(angle * Math.PI / 180) + sf.Height / 2 * Math.Sin(angle * Math.PI / 180));
                        g.TranslateTransform(xTemp, yTemp);//设置新的坐标原点             
                        g.RotateTransform(-angle);//旋转 
                        g.DrawString(chinese[i], myFont, myBrush, rectF, drawFormat);//以新的原点画文本 
                        g.ResetTransform();//还原坐点原点 
                    }
                    //显示右半边字
                    for (; i < nChinese; i++)
                    {
                        angle = (float)((i - (nChinese - 1) / 2) * this.textSpace);
                        xTemp = (float)(cPoint.X + r * Math.Sin(angle * Math.PI / 180) - sf.Width / 2 * Math.Cos(angle * Math.PI / 180));
                        yTemp = (float)(cPoint.Y - r * Math.Cos(angle * Math.PI / 180) - sf.Height / 2 * Math.Sin(angle * Math.PI / 180));
                        sf = g.MeasureString(chinese[i], myFont);
                        rectF = new RectangleF(0, 0, sf.Width, sf.Height);
                        g.TranslateTransform(xTemp, yTemp);//设置新的坐标原点             
                        g.RotateTransform(angle);//旋转 
                        g.DrawString(chinese[i], myFont, myBrush, rectF, drawFormat);//以新的原点画文本 
                        g.ResetTransform();//还原坐点原点 
                    }
                }            //定义字体大小            if (!(text2 == null || text2.Length == 0))
                {
                    Font font1 = new Font(this.fontComboBox.Text,this.textSize1);
                    SizeF sf1 = g.MeasureString(text2, font1);
                    g.DrawString(text2, font1, myBrush, cPoint.X - sf1.Width / 2, cPoint.Y + this.textHigh1);
                }
                if (!(text3 == null || text3.Length == 0))
                {
                    Font font2 = new Font(this.fontComboBox.Text, this.textSize2);
                    SizeF sf2 = g.MeasureString(text3, font2);
                    g.DrawString(text3, font2, myBrush, cPoint.X - sf2.Width / 2, cPoint.Y + this.textHigh2);
                }
            }        Color GetMyColor()
            {
                if (this.red.Checked)
                    return Color.Red;
                else if (this.blue.Checked)
                    return Color.Blue;
                else
                    return Color.Purple;
            }        Brush GetMyBrush()
            {
                if (this.red.Checked)
                    return Brushes.Red;
                else if (this.blue.Checked)
                    return Brushes.Blue;
                else
                    return Brushes.Purple;
            }        private void DrawCircle(Graphics g)
            {
                this.myPen = new Pen(myColor, this.lineWidth); //创建一个自定义画笔对象
                int centre_X = this.panel1.Width / 2;
                int centre_Y = this.panel1.Height / 2;            //给印章定位
                int myRect_X = centre_X - this.panel1.Width * this.sealSize / 200;
                int myRect_Y = centre_Y - this.panel1.Height * this.sealSize / 200;
                int myRect_Width;
                int myRect_Height;
                if (this.panel1.Height < this.panel1.Width)
                {
                    myRect_Width = this.panel1.Height * this.sealSize / 100;
                    myRect_Height = this.panel1.Height * this.sealSize / 100;
                }
                else
                {
                    myRect_Width = this.panel1.Width * this.sealSize / 100;
                    myRect_Height = this.panel1.Width * this.sealSize / 100;
                }            Rectangle rect = new Rectangle(myRect_X, myRect_Y, myRect_Width, myRect_Height);
                //            Rectangle rect = new Rectangle(4,4,150,150);
                g.DrawEllipse(myPen, rect); //画出一个内切于矩形rect的圆
            }
            private void DrawPicture(Graphics g)
            {
                Point[] pPoints = new Point[10];
                //圆心
                Point cPoint = new Point();
                cPoint.X = cPoint.Y = this.panel1.Width / 2;
                int i;
                for (i = 0; i < 5; i++)
                {
                    pPoints[i * 2].X = (int)(cPoint.X + this.pictureSize * Math.Sin(72.0 * i * Math.PI / 180));
                    pPoints[i * 2].Y = (int)(cPoint.Y - this.pictureSize * Math.Cos(72.0 * i * Math.PI / 180));
                    pPoints[i * 2 + 1].X = (int)(cPoint.X + this.pictureSize * 0.4 * Math.Sin((36 + 72.0 * i) * Math.PI / 180));
                    pPoints[i * 2 + 1].Y = (int)(cPoint.Y - this.pictureSize * 0.4 * Math.Cos((36 + 72.0 * i) * Math.PI / 180));
                }
                //pPoints[10].X = pPoints[0].X;
                //pPoints[10].Y = pPoints[0].Y;
                g.FillPolygon(myBrush, pPoints);
            }(未完,后续)
      

  2.   

    (续前)
            private void Drawing()
            {
                Graphics g = this.panel1.CreateGraphics(); //创建一个Graphics对象            Color myColor = GetMyColor();
                this.roll = Convert.ToInt16(this.rollDomainUpDown.Text);
                this.textWide = Convert.ToInt16(this.textWideDomainUpDown.SelectedItem.ToString());
                this.sealSize = Convert.ToInt16(this.sizeDomainUpDown.Text);
                this.textSpace = Convert.ToInt16(this.spaceDomainUpDown.Text);
                this.pictureSize = Convert.ToInt16(this.pictureSizeDomainUpDown.Text);
                this.isPictureCheck = this.pictureCheckBox.Checked;
                this.myBrush = GetMyBrush();
                this.myColor = GetMyColor();
                this.text1 = this.textBox1.Text;
                this.text2 = this.textBox2.Text;
                this.text3 = this.textBox3.Text;
                this.textSize1 = Convert.ToInt16(this.textSizeDomainUpDown1.Text);
                this.textSize2 = Convert.ToInt16(this.textSizeDomainUpDown2.Text);
                this.textHigh1 = Convert.ToInt16(this.textHighDomainUpDown1.Text);
                this.textHigh2 = Convert.ToInt16(this.textHighDomainUpDown2.Text);
                this.lineWidth = Convert.ToInt16(this.lineWidthDomainUpDown.Text);
                this.myFont = new Font(this.fontComboBox.Text, this.textWide);            this.DrawCircle(g);
                if (this.isPictureCheck)
                {
                    this.DrawPicture(g);
                }
                DrawSealText(g, GetMyBrush());
            }        private void Form1_Load(object sender, EventArgs e)
            {
                //增加系统字体
                InstalledFontCollection Font = new InstalledFontCollection();     // 建立InstalledFontCollection类型把系统字体载入到FONT中
                FontFamily[] fontfamily = Font.Families;  //得到FontFamily的对象矩阵
                int coute = fontfamily.Length;
                //将所有字体+入到控件中
                for (int j = 0; j < coute; j++)
                {
                    string familyname = fontfamily[j].Name;
                    this.fontComboBox.Items.Add(familyname);
                }
                this.fontComboBox.Text = "宋体";
                //印章初始化        }        private void closeButton_Click(object sender, EventArgs e)
            {
                Form1.ActiveForm.Close();
            } 
            private void saveButton_Click(object sender, EventArgs e)
            {
                this.Refresh();
                Drawing();              //创建显示器DC   
                IntPtr dc1 = CreateDC("DISPLAY", null, null, (IntPtr)null);
                //创建Graphics对象   
                Graphics g1 = Graphics.FromHdc(dc1);
                //创建panel1大小的Bitmap对象   
                Bitmap img = new Bitmap(panel1.ClientSize.Width, panel1.ClientSize.Height, g1);
                Graphics g2 = Graphics.FromImage(img);
                //获得位图的句柄   
                IntPtr dc3 = g1.GetHdc();
                IntPtr dc2 = g2.GetHdc();
                //将panel坐标转成屏幕坐标   
                Point pos = panel1.PointToScreen(panel1.Location);
                pos.X -= panel1.Location.X;
                pos.Y -= panel1.Location.Y;
                //把当前屏幕捕获到位图对象中     
                BitBlt(dc2, 0, 0, panel1.ClientSize.Width, panel1.ClientSize.Height, dc3, pos.X, pos.Y, 13369376);
                //释放屏幕句柄   
                g1.ReleaseHdc(dc3);
                //释放位图句柄   
                g2.ReleaseHdc(dc2);
                SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                saveFileDialog1.InitialDirectory = "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures";
                saveFileDialog1.Filter = "Bitmap Image|*.bmp";
                saveFileDialog1.Title = "保存图像";
                saveFileDialog1.FileName = "*.bmp";
                saveFileDialog1.ShowDialog();             img.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);   
            }        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
            private static extern IntPtr CreateDC(
            string lpszDriver,
            string lpszDevice,
            string lpszOutput,
            IntPtr lpInitData
            );
            [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
            private static extern bool BitBlt(
            IntPtr hdcDest,
            int nXDest,
            int nYDest,
            int nWidth,
            int nHeight,
            IntPtr hdcSrc,
            int nXSrc,
            int nYSrc,
            System.Int32 dwRop
            );   
        
            private void SealChange(object sender, EventArgs e)
            {
                this.Refresh();
                Drawing();
            }        private void SealTextChange(object sender, EventArgs e)
            {
                if (this.textBox1.Text.Length > 20)
                {
                    this.textBox1.Text = this.textBox1.Text.Substring(0, 20);
                    this.textBox1.Select(20, 0);
                }
                this.Refresh();
                Drawing();
            }        private void PictureChanged(object sender, EventArgs e)
            {
                this.isPictureCheck = this.pictureCheckBox.Checked;
                this.pictureSizeDomainUpDown.Enabled = this.pictureCheckBox.Checked;
                this.SealChange(sender,e);
            }        private void Form1_Paint(object sender, PaintEventArgs e)
            {
                this.Refresh();
                Drawing();
            }
        }
    }几年前自己写过一个更强大的印章,比上面这个更方便些,但暂时找不到源码了。