Javascript support added to AlivePDF Template

UPDATE: AlivePDF 1.5 will support Javascript!!!!! It seems that the method will be quite equivalent to the one that I use here, so hopefully no change will be needed. Way to go AlivePDF!!!!!

My second step in creating an application like PaperCrop was to display the PDF inside Adobe Air. As you may know, to do that you must have installed Adobe Acrobat Reader 9 or higher (or was it 8?). After installing the reader you can see the PDF using the browser of adobe air, by navigating to the page with the PDF embedded on it. A very nice example is “Cross-scripting PDF content in an Adobe Air application”

The main issue: you cannot control a PDF file unless it has the javascript code embedded. This was a problem, as there was no way to add javascript in a PDF file in Adobe Air…until now ^^

I added javascript support to AlivePDF Template, so now you can add the required scripts to control the file, as shown in the webpage above.

You can download the source code from the google code webpage of the project.

An example on how to use it is shown below. Enjoy!!!!

	var myPDF : PDFi = new PDFi ( Orientation.PORTRAIT, Unit.MM, Size.LETTER );
	myPDF.setViewerPreferences("true", "true", "true");
	myPDF.javascript = "" +
		"var msgHandlerObject = new Object();\n" +
		"msgHandlerObject.onMessage = myOnMessage;\n" +
		"msgHandlerObject.onError = myOnError;\n" +
		"msgHandlerObject.onDisclose = myOnDisclose;\n" +
		"\n" +
		"function myOnMessage(aMessage)\n" +
		"{\n" +
		"	if (aMessage.length==1) {\n" +
		"		switch(aMessage[0]) \n" +
		"		{\n" +
		"			case 'ZoomIn':  \n" +
		"			    zoom *= 2; \n" +
		"				break;\n" +
		"			case 'ZoomOut': \n" +
		"			    zoom /= 2; \n" +
		"				break;\n" +
		"			case 'PageUp': \n" +
		"			    pageNum--; \n" +
		"				break;\n" +
		"			case 'PageDn': \n" +
		"			    pageNum++; \n" +
		"				break;\n" +
		"			default: \n" +
		"				app.alert('Unknown message: ' + aMessage[0]);\n" +
		"		}\n" +
		"	}\n" +
		"	else\n" +
		"	{\n" +
		"	    app.alert('Message from hostContainer: \\n' + aMessage);\n" +
		"	}\n" +
		"}\n" +
		"\n" +
		"function myOnDisclose(cURL,cDocumentURL)\n" +
		"{\n" +
		"	return true;\n" +
		"}\n" +
		"\n" +
		"function myOnError(error, aMessage)\n" +
		"{\n" +
		"	app.alert(error);\n" +
		"}\n" +
		"\n" +
		"this.hostContainer.messageHandler = msgHandlerObject;\n" +
		"\n";

	var pagecount:Number = myPDF.setSourceFile(File.desktopDirectory.nativePath + File.separator + "PDFControlFlex" + File.separator + "src" + File.separator + "test.pdf" ); //"H:/readings/thesisStatement/p263-jain_challenges_environmental_design_for_pervasive_computing_systems.pdf");
	for(var i:Number = 1; i<=pagecount; i++) {
		var tplidx:Number = myPDF.importPage(i, '/MediaBox');
		myPDF.addPage();
		myPDF.useTemplate(tplidx, 0, 0, Math.ceil(myPDF.getTemplateSize(i).w));
	}

	var f : FileStream = new FileStream();
	var file : File = new File(File.desktopDirectory.nativePath + File.separator + "PDFControlFlex" + File.separator + "src" + File.separator + "MyPDFi.pdf");
	f.open( file, FileMode.WRITE);
	var bytes : ByteArray = myPDF.save(Method.LOCAL);
	f.writeBytes(bytes);
	f.close();
  1. No comments yet.

  1. No trackbacks yet.

Spam Protection by WP-SpamFree