To manipulate PDF files have a look at PDF box. Its what alfresco uses itself to extract meta-data but you can use it to manipulate PDF documents. I had a quick look and there's a likely looking "addPage" method. And a "Splitter" class that can go in the other direction.
My thoughts are that you are over-using the "barcode". Yes its part of your solution but there's a bit of an issue with using it to group several pages together. For example there is also an issue of page ordering. Can you get any information from your scanner to batch together pages? Can you upload the pages into a folder and then run an action to merge them together? That could work automatically if you can have a simple upload stream but I suspect you will eventually want some sort of review process to check for missing and duplicated pages.