AlivePdf如何设置字体?

时间:2013-02-11 22:45:45

标签: flash flex actionscript alivepdf

我正在尝试使用Alivepdf库在flex中创建一个pdf。

我无法设置字体,可能是因为网站上的示例错误并且他们对代码进行了一些更改。

http://code.google.com/p/alivepdf/wiki/APINotes上有一些例子:

pdf.setFont( FontFamily.ARIAL , "", 32);

但是在文档中,setfont接受IFont作为第一个参数。

那么,我怎样才能设置这个IFont?

2 个答案:

答案 0 :(得分:1)

我会在你的情况下使用嵌入字体独立于用户PC上的字体。

您需要* .ttf和生成的* .afm文件(在网络中有一些工具可以执行此操作)。

如果你需要带有另一个字符集的文本块,那么它也很有用(这里有一个如何使用西里尔语的例子)

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" width="500" height="500">
<fx:Script>
    <![CDATA[
        import org.alivepdf.colors.RGBColor;
        import org.alivepdf.display.Display;
        import org.alivepdf.fonts.CodePage;
        import org.alivepdf.fonts.EmbeddedFont;
        import org.alivepdf.layout.Layout;
        import org.alivepdf.layout.Orientation;
        import org.alivepdf.layout.Unit;
        import org.alivepdf.pages.Page;
        import org.alivepdf.pdf.PDF;
        import org.alivepdf.saving.Method;

        private var myPDF:PDF;

        [Embed( source="/assets/fonts/times2.ttf", mimeType="application/octet-stream" )]
        public var timesRegularTtf:Class;
        [Embed( source="/assets/fonts/times2.afm", mimeType="application/octet-stream" )]
        public var timesRegularAfm:Class;
        private var timesRegularFont:EmbeddedFont = new EmbeddedFont(new timesRegularTtf() as ByteArray, new timesRegularAfm() as ByteArray, CodePage.CP1251);

        [Embed( source="/assets/fonts/timesbd3.ttf", mimeType="application/octet-stream" )]
        public var timesBoldTtf:Class;
        [Embed( source="/assets/fonts/timesbd3.afm", mimeType="application/octet-stream" )]
        public var timesBoldAfm:Class;
        private var timesBoldFont:EmbeddedFont = new EmbeddedFont(new timesBoldTtf() as ByteArray, new timesBoldAfm() as ByteArray, CodePage.CP1251);

        protected function savePDF(e:MouseEvent):void
        {
            var myPDF:PDF = new PDF( Orientation.PORTRAIT, Unit.MM); 
            myPDF.setDisplayMode( Display.FULL_PAGE, Layout.SINGLE_PAGE );

            var newPage:Page = new Page ( Orientation.PORTRAIT, Unit.MM);
            myPDF.addPage(newPage);

            myPDF.textStyle ( new RGBColor ( 0xff0000 ) );
            myPDF.setFont(timesRegularFont, 12);
            myPDF.setXY(10, 20);
            myPDF.writeText(1, "Hello World!");

            myPDF.textStyle ( new RGBColor ( 0x0000ff ) );
            myPDF.setFont(timesBoldFont, 20);
            myPDF.setXY(10, 40);
            myPDF.writeText(1, "Hello World!");

            myPDF.textStyle ( new RGBColor ( 0x00ff00 ) );
            myPDF.setFont(timesBoldFont, 24);
            myPDF.setXY(10, 60);
            myPDF.writeText(1, fromUtf8ToSomeCode("Привет, Мир!", "windows-1251"));

            var f:FileReference = new FileReference();
            var b:ByteArray = myPDF.save(Method.LOCAL);
            f.save(b, "test.pdf");
        }

        private function fromUtf8ToSomeCode(data:String, code:String):String
        {
            var b:ByteArray = new ByteArray();

            if (data.length > 0)
                b.writeMultiByte(data, code);

            return b.toString();
        }   
    ]]>
</fx:Script>    

<s:Button label="Save to PDF" click="savePDF(event)"/>
</s:Application>

答案 1 :(得分:0)

对于波兰人来说: 编辑文件org / alivepdf / pdf / PDF.as并找到代码行(4776)

buffer.writeMultiByte( content+"\n", "windows-1252" ); // jakiś idiota programista wstawił to na sztywno :/

更改为:

buffer.writeMultiByte( content+"\n", "windows-1250" );