C#调用Illustrator CS4生成系统所有字体列表并保存为AI文件(Illustrator矢量图形编程之4)

本篇讲述C#调用Illustrator CS4生成系统所有字体列表并保存为AI文件,作为前几篇Illustrator矢量图形编程系列的续篇。

本文的重点在于如何取得字体列表,如何对文字输出的格式、大小进行控制,最后又是如何保存为Illustrator的不同版本的。

先看运行之后的效果图:

下面看看C#处理的代码:

private void btnChangeWords_Click(object sender, EventArgs e)
        {
            Illustrator.Application app = new Illustrator.Application();

            // 文档的尺寸,Illustrator默认为以Point(磅)为单位,所以下面的涉及尺寸度量的,都是Point为单位。做过印刷或广告的朋友应该知道,72Points=1英寸,即2.54cm。
            double myWidth = 888.0;
            double myHeight = 400.0;
            Illustrator.Document docRef = app.Documents.Add(null, myWidth, myHeight, null, null, null, null);

            // 边距
            double edgeSpacing = 14;

            // 栏间距
            double columnSpacing = 268;
            double x = edgeSpacing;
            double y = docRef.Height - edgeSpacing;
            double iCounter = 0;
            try
            {

                // 这里通过Illustrator.Application的实例app的TextFonts找到所有字体列表
                foreach (Illustrator.TextFont fontRef in app.TextFonts)
                {

                    // 通过Document实例的TextFrames.Add()方法增加文字
                    Illustrator.TextFrame textRef = docRef.TextFrames.Add();

                    // 文字的大小
                    textRef.TextRange.CharacterAttributes.Size = 14;

                    // 文字的内容
                    textRef.Contents = fontRef.Name + " | " + fontRef.Style.ToString();
                    textRef.Top = y; //垂直方向的位置
                    textRef.Left = x; //水平方向的位置

                    // 如果大于文档尺寸,则不输出,主要目的是想叙述控制的方法
                    if ((x + textRef.Width) > docRef.Width)
                    {
                        textRef.Delete();
                        continue;
                    }

                    // 设置字体
                    textRef.TextRange.CharacterAttributes.TextFont = app.TextFonts[fontRef.Name];

                    // 控制输出的总高度
                    y = y - textRef.Height;
                    if (y < 28)
                    {
                        y = docRef.Height - edgeSpacing;
                        x = x + columnSpacing;
                    }
                    iCounter = iCounter + 1;

                    // 这里只列出60条,如果需要更多,可以修改下句代码
                    if (iCounter > 60) break;
                }
            }
            catch (Exception excOuter)
            {
                excOuter = null;
            }

            // AI格式文档保存选项

            Illustrator.IllustratorSaveOptions saveOptions = new Illustrator.IllustratorSaveOptions();
            saveOptions.Compatibility = Illustrator.AiCompatibility.aiIllustrator11;//设置保存文件的版本,比如你可以使用aiIllustrator8等。
            saveOptions.FlattenOutput = Illustrator.AiOutputFlattening.aiPreserveAppearance;
           // 保存AI文件 
           docRef.SaveAs(@"F:/Johnson/IllustratorDemo/Fonts.ai", saveOptions);
           docRef = null;
        }

OK。

参考资源:

如果你对C#下的Illustrator编程感兴趣,可以再看看这两篇:
用C#对Illustrator矢量图形软件进行编程之1 http://blog.csdn.net/johnsuna/archive/2008/04/05/2252514.aspx
用C#对Illustrator矢量图形软件进行编程之2 http://blog.csdn.net/johnsuna/archive/2008/04/10/2279817.aspx

C#调用Illustrator生成矢量图的缩略图(Illustrator矢量图形编程之3) http://blog.csdn.net/johnsuna/archive/2009/01/03/3692188.aspx

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值