Welcome to the Official BlackBerry® Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Adobe AIR Development

Reply
Regular Contributor
mahesh_DT
Posts: 65
Registered: 11-21-2011
My Carrier: developer

Path to save PDF files in playbook device.

Hello every one,

I am trying to save a PDF file to the blackberry device.

package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.printing.PrintJob;
	import flash.text.TextField;
	import flash.utils.ByteArray;
	
	import org.alivepdf.display.Display;
	import org.alivepdf.images.ImageFormat;
	import org.alivepdf.layout.Orientation;
	import org.alivepdf.layout.Size;
	import org.alivepdf.layout.Unit;
	import org.alivepdf.pdf.PDF;
	import org.alivepdf.saving.Method;
	
	import qnx.ui.display.Image;

	public class pdfgen extends Sprite
	{
		private var _mySprite:Sprite;
		private var myPDF:PDF;
		private var _pdfFilename:String = "test.pdf";
		private var file:File = File.documentsDirectory;
		private var fstream:FileStream;
		private var pdftext:TextField;
		private var image:Image;
		private var bitmapnew:Bitmap;
		private var mycontainer:Sprite;
		private var myImage:Bitmap;		
		public function pdfgen()
		{
			super();
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
			// support autoOrients			
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
		}	
		
		private function init(e:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);			
			_mySprite = new Sprite();			
			//drawCrossBox(_mySprite, 540, 790, 0, 0, 0xCCCCCC, 1, 1, 0x000000, 1);				
			pdftext = new TextField();
			pdftext.text = "HELLO TO PDF";
			pdftext.x = 20;
			pdftext.y = 20;
			addChild(pdftext);
			var loader:Loader = new Loader(); 
			loader.load(new URLRequest("assets/small.jpg"));
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);						
		}		
		private function onComplete (event:Event):void
		{
			myImage = Bitmap(LoaderInfo(event.target).content);
			//adding the image to a container (  sprite  )
			mycontainer = new Sprite();
			mycontainer.addChild( myImage );
			// adding the container to a stage
			addChild(mycontainer);
			generatePDF();
		}  		
		private function generatePDF():void
		{
			var numPages:int = 0;
			var printJob:PrintJob = new PrintJob();
			
			var printY:int = 0;
			var printHeight:Number;
			numPages = Math.ceil(mycontainer.height / printJob.pageHeight);			
			myPDF = new PDF(Orientation.PORTRAIT, Unit.MM, Size.LETTER);
			myPDF.setDisplayMode(Display.REAL);
			for(var k:int=0;k<2;k++)
			{
				myPDF.addPage();
				for(var i:int = 0;i<2;i++)
				{
					if(k == 0)
					{
						 myPDF.addImage(mycontainer,null,0,i*100,100,100,0,1,true,"JPG",50,"Normal",null);
					}					
				}				
			}
			myPDF.setMargins(10,10,10,10);
			myPDF.addText("Hello to pdf" , 20,20);			
			//myPDF.addImage(mycontainer,null,0,100,100,100,0,1,false,"bmp",50);
			var fileReference:FileReference = new FileReference();
			var byteArray:ByteArray = myPDF.save(Method.LOCAL);
			fileReference.save(byteArray, _pdfFilename);
			fstream = new FileStream();
			fstream.openAsync(file.resolvePath("\\playboo_media\Camera\testpdf" + ".pdf"), FileMode.WRITE);		
			//byteArray = new ByteArray();
			//byteArray = e.ImageData;			
			byteArray.position = 0;
			fstream.writeBytes(byteArray);
			fstream.close();			
		}		
		private function drawCrossBox(sprite:Sprite, $width:int=100, $height:int=100, $x:int=0, $y:int=0, $bgColor:uint=0xFFFFFF, $bgAlpha:Number=1, $lineThickness:Number=1, $lineColor:uint=0x000000, $lineAlpha:Number=1):void
		{
			sprite.graphics.beginFill($bgColor, $bgAlpha);
			sprite.graphics.drawRect($x, $y, $width, $height);
			sprite.graphics.endFill();
			
			sprite.graphics.lineStyle($lineThickness, $lineColor, $lineAlpha);
			sprite.graphics.moveTo($x,$y);
			sprite.graphics.lineTo($x+$width, $y);
			sprite.graphics.lineTo($x+$width, $y+$height);
			sprite.graphics.lineTo($x, $y+$height);
			sprite.graphics.lineTo($x, $y);
			sprite.graphics.lineTo($x+$width, $y+$height);
			sprite.graphics.moveTo($x+$width, $y);
			sprite.graphics.lineTo($x, $y+$height);
		}
		public function addtext1(str:String,_x:Number,_y:Number):void
		{
			pdftext = new TextField();
			pdftext.text = str;
			pdftext.x = _x;
			pdftext.y = _y;
			this.addChild(pdftext);			
		}		
	}		
}

 

I have been using this code till now. After running this app in playbook ,The save alertbox is also appearing and also i have gave the static path to save the file, but after saving the file, i can't find the file anywhere in the playbook.

Can Any one please tell me the correct path to save the PDF file in playbook and to access that file.

Your help will be really appreciated,

thank you in advance.

Please use plain text.
Developer
jtegen
Posts: 4,247
Registered: 10-27-2010
My Carrier: Verizon

Re: Path to save PDF files in playbook device.

Might want to check your path. It dont see a path like "\\playboo_medi​a\Camera". Assuming you have permission to write to the shared area, just try writing to the documents directory using the File static functions.
Please use plain text.
Regular Contributor
mahesh_DT
Posts: 65
Registered: 11-21-2011
My Carrier: developer

Re: Path to save PDF files in playbook device.

Thank you very  much to your response,

I have tried in that , but bad luck i could not find any file saving in that directory.Is there any sample or any other way to save the PDF file onto playbook device.

Please use plain text.
Developer
jtegen
Posts: 4,247
Registered: 10-27-2010
My Carrier: Verizon

Re: Path to save PDF files in playbook device.

So if you cannot write any file into the documents directory, then you must not have permission to do so. Have you tried checking the device preferences to verifty that your app has access to that directory?
Please use plain text.