Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
prakash86
Posts: 2
Registered: ‎08-23-2012
My Device: 9800
My Carrier: Airtel

PDF creation in Blackberry

Hi friends ,

I am newbie here, referred ... http://supportforums.blackberry.com/t5/Java-Development/Save-set-of-images-to-PDF-file-from-Java-App...   this post and do the same thing, but I can create only the empty PDF file.

 

In my application I want to convert the image file to PDF. How can I achieve it.

Please use plain text.
Developer
BrajeshSanodiya
Posts: 130
Registered: ‎08-03-2011
My Device: torch, Z10
My Carrier: Vodafone, Airtel

Re: PDF creation in Blackberry

check this link http://bbx.codeplex.com/SourceControl/list/changesets
-----------------------------------------------------------------------
"Like" if you liked the post.
"Accept as Solution" if the post solves your question.
-----------------------------------------------------------------------
Please use plain text.
Developer
kamal_nigam
Posts: 434
Registered: ‎07-23-2012
My Device: Blackberry 10
My Carrier: Orange

Re: PDF creation in Blackberry

i think above link realted to BB10 not with the BB Java. i feel BB does not supported any PDF related API.

 

Thanks
-------------------------------------------------------------------------------------
Press the Accept as solution Button when u got the Solution
Press Kudo to say thank to developer.
-------------------------------------------------------------------------------------.
Please use plain text.
Developer
simon_hain
Posts: 16,010
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: PDF creation in Blackberry

no, BBX is a library created for BB OS, not for BB10.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
New Contributor
prakash86
Posts: 2
Registered: ‎08-23-2012
My Device: 9800
My Carrier: Airtel

Re: PDF creation in Blackberry

hi friends ..

i followed this link. http://bbx.codeplex.com/SourceControl/changeset/d29c5b511ed4 and also created the sample , but i can create the empty pdf only. i can not see the image on that pdf file. if any one tried please help me.

 

this is my code

 

OutputStream outStream = null;
        Image2PDFBuilder ipdf = new Image2PDFBuilder();
        Bitmap img = Bitmap.getBitmapResource("screenCap.png");
        ipdf.addImage(img);
        try {
            FileConnection fc = (FileConnection) Connector
                    .open("file:///SDCard/newfile.pdf");
            // If no exception is thrown, then the URI is valid, but the file
            // may or may not exist.
            if (!fc.exists()) {
                fc.create(); // create the file if it doesn't exist
            }
            outStream = fc.openOutputStream();
            try {
                ipdf.build(outStream);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            outStream.close();
            fc.close();
        } catch (Exception ioe) {
            System.out.println(ioe.getMessage());
        }

 

and this is my Image2PDFBuilder class...

 

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Hashtable;
import java.util.Vector;

import net.rim.device.api.system.Bitmap;

public class Image2PDFBuilder
{
    private Vector images;
    
    public Image2PDFBuilder()
    {
        images = new Vector();
    }
    
    public Image2PDFBuilder addImage(Bitmap img)
    {
        if(img != null)
        {
            images.addElement(img);
        }
        return this;
    }
    
    public Image2PDFBuilder removeImage(Bitmap img)
    {
        if(img != null)
        {
            images.removeElement(img);
        }
        return this;
    }
    
    public void reset()
    {
        images.removeAllElements();
    }
    
    public void build(OutputStream os) throws IOException
    {
        int imageCount = images.size();
        
        if (imageCount > 0)
        {
            //Default width/height will be a 8.5x11 paper at 96dpi
            int width = 816;
            int height = 1056;
            
            //Find the desired size of the PDF
            for (int i = 0; i < imageCount; i++)
            {
                Bitmap bmp = (Bitmap)images.elementAt(i);
                width = Math.max(width, bmp.getWidth());
                height = Math.max(height, bmp.getHeight());
            }
            
            int[] offX = new int[imageCount];
            int[] offY = new int[imageCount];
            
            //Determine the positions of all the images (center each)
            for (int i = 0; i < imageCount; i++)
            {
                //Center of page
                offX[i] = width / 2;
                offY[i] = height / 2;
                
                //Offset
                Bitmap bmp = (Bitmap)images.elementAt(i);
                offX[i] -= bmp.getWidth() / 2;
                offY[i] -= bmp.getHeight() / 2;
            }
            
            PDF pdf = new PDF();
            
            //Make the objects (backwards so we can use them correctly, indirect objects)
            
            PDFObject[] contents = new PDFObject[imageCount];
            PDFObject[] xobjectImages = new PDFObject[imageCount];
            PDFObject[] resources = new PDFObject[imageCount];
            PDFObject[] pages = new PDFObject[imageCount];
            
            int totalObjectCount = 3 + (4 * imageCount);
            Hashtable table;
            
            for (int i = imageCount - 1; i >= 0; i--)
            {
                //Contents
                ByteArrayOutputStream mem = new ByteArrayOutputStream();
                StringBuffer sb = new StringBuffer();
                {
                    sb.append("q% Save graphics state\n");
                    sb.append("1 0 0 1 " + offX[i] + " " + offY[i] + " cm% Translate to (" + offX[i] + "," + offY[i] + ")\n");
                    sb.append("/Im1 Do% Paint image\n");
                    sb.append("Q% Restore graphics state\n");
                    
                    try
                    {
                        mem.write(sb.toString().getBytes("US-ASCII"));
                    }
                    catch(UnsupportedEncodingException e)
                    {
                    }
                    mem.flush();
                }
                pdf.addObject(contents[i] = new PDFObject(totalObjectCount--, 0, mem));
                
                //Images
                Bitmap bmp = (Bitmap)images.elementAt(i);
                table = new Hashtable();
                table.put("Type", new PDFName("XObject"));
                table.put("Subtype", new PDFName("Image"));
                table.put("Width", new Integer(bmp.getWidth()));
                table.put("Height", new Integer(bmp.getHeight()));
                table.put("ColorSpace", new PDFName("DeviceRGB"));
                table.put("BitsPerComponent", new Integer(8));
                
                //-Get image data
                int[] idata = new int[bmp.getWidth() * bmp.getHeight()];
                bmp.getARGB(idata, 0, bmp.getWidth(), 0, 0, bmp.getWidth(), bmp.getHeight());
                mem = new ByteArrayOutputStream();
                for (int k = 0; k < idata.length; k++)
                {
                    //Slow, but this isn't a real time game that needs speed (also, alpha is not used as PDF doesn't really "have" alpha for images)
                    mem.write((idata[k] & 0x00FF0000) >> 16);
                    mem.write((idata[k] & 0x0000FF00) >> 8);
                    mem.write(idata[k] & 0x000000FF);
                }
                mem.flush();
                pdf.addObject(xobjectImages[i] = new PDFObject(totalObjectCount--, 0, new PDFStream(mem, table)));
                
                //Resources
                Hashtable xobject = table = new Hashtable();
                xobject.put("Im1", xobjectImages[i]);
                
                table = new Hashtable();
                table.put("ProcSet", new Object[] { new PDFName("PDF"), new PDFName("ImageC") });
                table.put("XObject", xobject);
                pdf.addObject(resources[i] = new PDFObject(totalObjectCount--, 0, table));
                
                //Page
                table = new Hashtable();
                table.put("Type", new PDFName("Page"));
                table.put("MediaBox", new Object[] { new Integer(0), new Integer(0), new Integer(width), new Integer(height) });
                table.put("Contents", contents[i]);
                table.put("Resources", resources[i]);
                pdf.addObject(pages[i] = new PDFObject(totalObjectCount--, 0, table));
            }
            
            //Parent page
            PDFObject pPages;
            table = new Hashtable();
            table.put("Type", new PDFName("Pages"));
            table.put("Kids", pages);
            table.put("Count", new Integer(imageCount));
            pdf.addObject(pPages = new PDFObject(totalObjectCount--, 0, table));
            
            //-Set each page's parent
            for (int i = 0; i < imageCount; i++)
            {
                ((Hashtable)pages[i].getValue()).put("Parent", pPages);
            }
            
            //Outlines
            PDFObject outlines;
            table = new Hashtable();
            table.put("Type", new PDFName("Outlines"));
            table.put("Count", new Integer(0));
            pdf.addObject(outlines = new PDFObject(totalObjectCount--, 0, table));
            
            //The PDF catalog
            table = new Hashtable();
            table.put("Type", new PDFName("Catalog"));
            table.put("Outlines", outlines);
            table.put("Pages", pPages);
            pdf.addObject(new PDFObject(totalObjectCount--, 0, table));
            
            //Write
            pdf.write(os);
        }
    }
}

Please use plain text.
Developer
simon_hain
Posts: 16,010
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: PDF creation in Blackberry

you would have to contact the creator of the library there.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.