Click here to Skip to main content
15,894,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,
i am new to flex, i have no idea to convert pdf file into byte array.and also i tried in google also but no results yet.can u prefer how to convert pdf file into byte array and retrieve byte array into pdf file in flex application.

it is urgent....

Thanks in advance.(nothing is impossible)
Posted

1 solution

You need to read the PDF (filereader), then you can convert that File-object into a byte Array. That is pretty simple, because any object can be converted int a byte array.

Reading, Writing, and Creating Files @ Java Tutorials[^]


Java
public static byte[] getBytes(Object obj) throws java.io.IOException{
      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      ObjectOutputStream oos = new ObjectOutputStream(bos); 
      oos.writeObject(obj);
      oos.flush(); 
      oos.close(); 
      bos.close();
      byte [] data = bos.toByteArray();
      return data;
  }
 
Share this answer
 
Comments
Ram Sam 2 28-Mar-12 0:36am    
k but i need code in flex .can u post code in that language?
TorstenH. 28-Mar-12 1:21am    
...you want me to add

<mx:script xmlns:mx="#unknown">
<![CDATA[

// code goes here
].]>


...or where is the problem?
Ram Sam 2 28-Mar-12 2:29am    
public var fileStream:FileStream;
public function browse_file(eve:MouseEvent):void{
file1 = File.documentsDirectory;
file1.addEventListener(Event.SELECT, handleSelectFile);
file1.browseForOpen("Select Picture");
}
public function handleSelectFile(event:Event):void
{
file_name.text = event.target.nativePath;
//stream=event.target.type;
//Alert.show(stream);
var fileloader:Loader = new Loader();
fileloader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleLoadFile);
fileloader.load(new URLRequest(event.target.url));
}

public function handleLoadFile(event:Event):void
{


}
// upload button action
public function fileuploads(event:MouseEvent):void{

var file:File = new File(file_name.text);
if ( file.extension == "txt" )//we check to see if file extension is txt if not we don't do anything otherwise we execute the desired action
{
readfiles(file.url,file.name);


}else if(file.extension=="doc" || file.extension=='pdf'){
readpdffiles(file.url,file.name);
}
}

public function readpdffiles(target_url:String,fname:String,startIndex:int = 0,endIndex:int = int.MAX_VALUE):void
{
var file:File=new File(target_url);
var fs:FileStream=new FileStream();
fs.open(file,FileMode.READ);
var data:ByteArray=new ByteArray();
data.writeObject(fs.readByte());

}
note :here i am able to read txt file as string and able to store .but i dnt know for pdf,doc file?can u check?
TorstenH. 28-Mar-12 7:33am    
seems like you have them as a File object there.

Now write the file temporary and throw that one at the Operating system:

final Program p = Program.findProgram("pdf");
p.execute(strFilePath);

so the OS takes care for opening the DOC or PDF File. That is the best and also simplest way to do this, because you do not insist on a certain PDF reader or office program. You just say "open this thingy with whatever you think is capable to do so".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900