Multipage TIF to PDF
by Alessandro Crugnola on February 17, 2011
in purePDF
Recently I’ve continued the porting of iText adding the support for a new image type: multipage tif.
The only problem with this update is that actually it’s really slow and probably with very complex image files it can hang the player for too long. Probably I should modify the ImageElement.getInstance method and make it asynchronous in order to prevent flash player to freeze…
Anyway, the current trunk version of purePDF can handle TIF image and here a simple code example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | package { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.events.TimerEvent; import flash.net.FileReference; import flash.utils.ByteArray; import flash.utils.Timer; import flash.utils.getQualifiedClassName; import org.purepdf.elements.RectangleElement; import org.purepdf.elements.images.ImageElement; import org.purepdf.io.RandomAccessFileOrArray; import org.purepdf.pdf.PageSize; import org.purepdf.pdf.PdfDocument; import org.purepdf.pdf.PdfViewPreferences; import org.purepdf.pdf.PdfWriter; import org.purepdf.pdf.codec.TiffImage; public class TestTIF extends Sprite { [Embed( source="assets/foxdog_multiplepages.tif", mimeType="application/octet-stream" )] private var cls1: Class; private var document: PdfDocument; private var writer: PdfWriter; private var buffer: ByteArray; private var filename: String; private var index: int; private var pages: int; private var stream: RandomAccessFileOrArray; public function TestTIF() { super(); addEventListener( Event.ADDED_TO_STAGE, onAdded ); } private function onAdded( event: Event ): void { stage.addEventListener( MouseEvent.CLICK, onClick ); } private function createDocument( subject: String = null, rect: RectangleElement = null ): void { buffer = new ByteArray(); if ( rect == null ) rect = PageSize.A4; writer = PdfWriter.create( buffer, rect ); document = writer.pdfDocument; document.addTitle( getQualifiedClassName( this ) ); if ( subject ) document.addSubject( subject ); document.setViewerPreferences( PdfViewPreferences.FitWindow ); } private function onClick( event: Event ): void { filename = getQualifiedClassName( this ).split( "::" ).pop() + ".pdf"; var byte: ByteArray = new cls1(); stream = new RandomAccessFileOrArray( byte ); var image: ImageElement = ImageElement.getInstance( byte ); createDocument( "Multi page TIFF Image Example", PageSize.A4 ); document.open(); // add the first page to the document document.add( image ); // get the total number of pages pages = TiffImage.getNumberOfPages( stream ); trace("number of pages: " + pages ); // next page index to add to document (first page is 1) index = 2; var timer: Timer = new Timer( 100, 1 ); timer.addEventListener( TimerEvent.TIMER, onTimerComplete ); timer.start(); } private function onComplete(): void { stream.close(); document.close(); save(); } private function onTimerComplete( event: TimerEvent ): void { if ( index > pages ) { onComplete(); return; } document.add( TiffImage.getTiffImage( stream, index ) ); index++; Timer( event.target ).reset(); Timer( event.target ).start(); } private function save( e: * = null ): void { var f: FileReference = new FileReference(); f.save( buffer, filename ); } } } |
Here you can see the result PDF file.
And here the input TIF image.
Tagged as: multipage, pdf, purePDF, tif
{ 0 comments }
purePDF and transparent BitmapData
by Alessandro Crugnola on January 14, 2011
in actionscript,purePDF
Recently I’ve updated purePDF to a new version fixing one bug which caused problems with PNG images.
However when you’re trying to add BitmapData with transparency to a pdf document you probably get black backgrounds to your images. This is because internaly purePDF converts bitmapdata into 24bit tiff images, so no alpha informations.
This is a script you can use to convert your BitmapData images into transparent pdf ImageElements:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | /** * Create a transparent ImageElement * * An ImageElement with the input bitmapdata RGB informations will be * created and an ImageElement will be used as mask ( using the alpha info from the bitmapdata ) * If the input bitmapdata is not transparent a regular ImageElement will be returned. */ protected function createTransparentImageElement( bitmap: BitmapData ): ImageElement { var output: ByteArray = new ByteArray(); var transparency: ByteArray = new ByteArray(); var input: ByteArray = bitmap.getPixels( bitmap.rect ); input.position = 0; while( input.bytesAvailable ){ const pixel: uint = input.readInt(); // write the RGB informations output.writeByte( (pixel >> 16) & 0xff ); output.writeByte( (pixel >> 8) & 0xff ); output.writeByte( (pixel >> 0) & 0xff ); // write the alpha informations transparency.writeByte( (pixel >> 24) & 0xff ); } output.position = 0; transparency.position = 0; var mask: ImageElement = ImageElement.getRawInstance( bitmap.width, bitmap.height, 1, 8, transparency, null ); var image: ImageElement = ImageElement.getRawInstance( bitmap.width, bitmap.height, 3, 8, output, null ); if( bitmap.transparent ) { mask.makeMask(); image.imageMask = mask; } return image; } |
and here there’s a demo app: bit.ly/dOT2ob
{ Comments on this entry are closed }
Android experiments
9 January 2011As you probably understood during last days I’m completely absorbed in Android development. While experimenting new things ( it remembers to me the first days with Flash ) I did a couple of applications and I’ve published them to the market ( it takes less than one hour to be published on the market, just [...]
How-to debug native code with Android
14 December 2010This is a step by step guide for executing and debugging native code on Android with Eclipse. 1. Prerequisites The SDK version used for this guide is Froyo with the NDK r4b ( crystax release ). Also Eclipse CDT plugin it’s very useful for our purposes, so install it. Last plugin to install it’s the [...]
Tagged as: android, debug, eclipse, native, ndk, sdk, sequoyah
Read the full article →Playing with a LocalActivityManager bug
26 November 2010If you already used the ActivityGroup class you probably used also the LocalActivityManager instance to manage your group’s activities. I was creating my activity which should also manages its internal history ( using a ViewFlipper for animating the activity views ). The problem came out once I tried to destroy an activity from the history using [...]
Tagged as: android, bug, localactivitymanager, reflection
Read the full article →Reading resource files from native code
24 October 2010If you’re working with android-ndk and you want to open an asset included in your apk package, you should make a request via jni to the Resource manager in your java code. Something like: 12345678910111213141516public int[] openAsset( String path ) { AssetFileDescriptor ad = null; try { ad = [...]
Tagged as: android, assets, native, ndk, resource, zip
Read the full article →Disable asset compression with Android aapt
9 October 2010Trying to embed all the assets resources into my final apk was not so easy as I tought.. at least using the ADT Eclipse plugin for building project. In fact, using AssetManager.openFd to return to the C++ code a FileDescriptor does not work with compressed sources. When aapt compile the resources into the apk file [...]
Tagged as: aapt, adt, android, ant, eclipse
Read the full article →Android: Create your own sharing app
18 September 2010One of the first applications I did for Android was a very simple application which simply takes a picture using Camera and then send it to a remote server. Very simple, but it introduced to me into the android world and the notion of activities and communication between different applications. A little addition to this [...]
Tagged as: activity, android, share
Read the full article →← Previous Entries