I'm trying to create a pdf in flex, using Alivepdf library.
I cannot set font, probably because the example on the site are wrong and they made some change to the code.
On http://code.google.com/p/alivepdf/wiki/APINotes there are some example like:
pdf.setFont( FontFamily.ARIAL , "", 32);
But in the documentation the setfont accept IFont as first parameter.
Well, how can I set this IFont?
I would use embeded fonts in your case to be independent of the fonts that are on the users PC.
You need a *.ttf and a generated *.afm files (In the network there are some tools to do it).
If you need text blocks with another charset it can be useful as well (here is an example how to use cyrillic)
<?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>