Here's the modified code, I have also upload it on http://pastebin.com/uDHdQD9a

I don't think it is a good idea to have different X and Y scales. This will look weird if you have photographs of objects whose shape are known, e.g. people.


            pd = PDDocument.load(input);

            pdAllPages = pd.getDocumentCatalog().getAllPages();

            //For proof of concept currently only converting first page.
//But once it works for first page correctly will loop through all pages of PDF.
            PDPage page = (PDPage) pdAllPages.get(0);

            InputStream is = page.getContents().createInputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            IOUtils.copy(is, baos);
            IOUtils.closeQuietly(is);

            /*
             * PDDocument pdNew = new PDDocument();
             * PDPage pageNew = new PDPage();
             * pdNew.addPage(pageNew);
             */
            PDRectangle cropBox = page.findCropBox();

            //The goal here is to keep a blank space of 50 pixels on top.
//So that for all PDFs processed through this code will leave same blank space at top. //In below code doing scaling of width too but in case it needs to be avoided can avoid. float scaleX = (cropBox.getUpperRightX() - 20) / cropBox.getUpperRightX(); float scaleY = (cropBox.getUpperRightY() - 50) / cropBox.getUpperRightY();

            System.out.println("cropBox: " + cropBox);
PDRectangle newCropBox = new PDRectangle(cropBox.getCOSArray()); // clone
            newCropBox.setLowerLeftX(cropBox.getLowerLeftX() * scaleX);
            newCropBox.setLowerLeftY(cropBox.getLowerLeftY() * scaleY);
            // don't do this, as it takes away the space
            //newCropBox.setUpperRightX(cropBox.getUpperRightX() * scaleX);
            //newCropBox.setUpperRightY(cropBox.getUpperRightY() * scaleY);
            System.out.println("newCropBox: " + newCropBox);
            page.setCropBox(newCropBox);

            // appendContent = false, compress = true
PDPageContentStream pageContentStream = new PDPageContentStream(pd,
                    page, false, true);
            pageContentStream.saveGraphicsState();
            System.out.println("scaleX: " + scaleX);
            System.out.println("scaleY: " + scaleY);

            // rectangle path
pageContentStream.appendRawCommands(String.format(Locale.US, "%f %f %f %f re\n",
                    newCropBox.getLowerLeftX(),
                    newCropBox.getLowerLeftY(),
cropBox.getUpperRightX() * scaleX - newCropBox.getLowerLeftX(), cropBox.getUpperRightY() * scaleY - newCropBox.getLowerLeftY()));

            // this is just a color fill test
            //pageContentStream.appendRawCommands("0 1 1 rg f\n");

            // modify clipping path; path no-op.
            pageContentStream.appendRawCommands("W n\n");

pageContentStream.appendRawCommands(scaleX + " 0 0 " + scaleY + " 0 0 cm\n"); pageContentStream.appendRawCommands("% start existing stuff\n");
            pageContentStream.appendRawCommands(baos.toByteArray());
            pageContentStream.appendRawCommands("\n");
            pageContentStream.restoreGraphicsState();
            //Commented out below code as currently
            //trying to work on leaving fix height blank
            //space on top for bar code
            /*
* BufferedImage bim = ImageIO.read(new File("C:\\workspaceRAD85_PDFBox_POC\\images.png"));
             * PDXObjectImage img = new PDPixelMap(pd, bim);
             * float x = cropBox.getUpperRightX() - bim.getWidth()-50;
             * float y = cropBox.getUpperRightY() - bim.getHeight();
             * pageContentStream.drawImage(img, x, y);
             */


            pageContentStream.close();

            IOUtils.closeQuietly(baos);


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to