Results 1 to 2 of 2
  1. #1
    gayasubbu is offline Member
    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Default Scaling an image using PdfBox

    My requirement is to convert a PDF (multiple pages) to individual images and again construct a PDF with all these image embedded into a new PDF.

    Method that converts PDF to Images:
    static ImageInfo convertPDFtoImageUsingPdfBox(String fileName,
    String fileFolder) throws IOException {
    System.out.println("\nPDF to Image conversion starts...");
    String name = null;
    String suffix = null;
    ImageInfo info = new ImageInfo();
    PDDocument document = null;
    int noOfPages = 0;
    try {
    document = PDDocument.load(fileName);
    } catch (IOException ex) {
    System.out.println("" + ex);
    }
    List<PDPage> pages = document.getDocumentCatalog().getAllPages();
    if (pages != null) {
    noOfPages = pages.size();
    }


    int pageRotation = 0;
    Iterator iter = pages.iterator();
    int i = 1;

    while (iter.hasNext()) {
    PDPage page = (PDPage) iter.next();
    PDResources resources = page.getResources();
    if (page.getRotation() != null) {
    pageRotation = page.getRotation();
    }

    Map pageImages = resources.getImages();
    if (pageImages != null) {
    Iterator imageIter = pageImages.keySet().iterator();
    while (imageIter.hasNext()) {
    String key = (String) imageIter.next();
    PDXObjectImage image = (PDXObjectImage) pageImages.get(key);
    image.getSuffix();
    image.setHeight(600);
    image.setWidth(600);
    if (suffix == null) {
    suffix = image.getSuffix();
    }
    image.write2file(fileFolder + i);
    i++;
    }
    }
    }
    if (suffix == null) {
    Iterator iter1 = pages.iterator();
    while (iter1.hasNext()) {
    PDPage page = (PDPage) iter1.next();
    BufferedImage img = page.convertToImage();
    if (img != null) {
    ImageIO.write(img, "png", new File(fileFolder + i + ".png"));
    }
    i++;
    }
    }
    document.close();
    info.noOfPages = noOfPages;
    if (suffix != null) {
    info.imageSuffix = suffix;
    } else {
    info.imageSuffix = "png";
    }
    info.rotationNeeded = pageRotation;
    System.out.println("\nPDF to Image Conversion information....");
    System.out.println("No of Pages:" + info.noOfPages);
    System.out.println("Suffix:" + info.imageSuffix);
    System.out.println("Page Rotation:" + info.rotationNeeded);
    return info;
    }

    Method to convert Image to PDF
    static void convertImagetoPDFUsingPdfBox(int number, String fileName,
    String folderName, String imageSuffix, int pageRotation) throws IOException, COSVisitorException {

    PDDocument doc = new PDDocument();
    try {
    for (int i = 1; i <= number; i++) {
    addImageForPdfBox(doc, folderName + i + "." + imageSuffix, pageRotation);
    }
    doc.save(fileName);
    } catch (Exception e) {
    e.printStackTrace();
    }
    finally
    {
    if( doc != null )
    {
    doc.close();
    }
    }
    }
    Method to add an image
    static void addImageForPdfBox(PDDocument doc, String imageName, int pageRotation) throws IOException, COSVisitorException {
    PDPage page = new PDPage();
    doc.addPage( page );

    PDXObjectImage ximage = null;
    if(imageName.toLowerCase().endsWith( ".jpg" ) )
    {
    ximage = new PDJpeg(doc, new FileInputStream( imageName ) );
    }
    else if (imageName.toLowerCase().endsWith(".tif") || imageName.toLowerCase().endsWith(".tiff"))
    {
    ximage = new PDCcitt(doc, new RandomAccessFile(new File(imageName),"r"));
    }
    else
    {
    throw new IOException( "Image type not supported:" + imageName );
    }
    PDPageContentStream contentStream = new PDPageContentStream(doc, page);

    contentStream.drawImage( ximage, 20, 20 );

    contentStream.close();
    }

    The issue I face the image is added as is and so looks truncated, I would like to scale the image to fit the page.
    Any help on how to do in PdfBox?

    In Itext I'm able to achieve this but I'm suggested to use PdfBox.

    Any help or directions much appreciated.

  2. #2
    forus is offline Member
    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Default Re: Scaling an image using PdfBox

    void org.apache.pdfbox.pdmodel.graphics.xobject.PDXObje ctImage.setBitsPerComponent(int bpc) - Set the number of bits per component.

    This method should help

Similar Threads

  1. movement during scaling
    By PRW56 in forum New To Java
    Replies: 4
    Last Post: 07-12-2012, 02:10 PM
  2. pdfbox to overwrite image on a pdf
    By vammpiro in forum Advanced Java
    Replies: 1
    Last Post: 03-09-2012, 04:54 PM
  3. Scaling VS. Clipping
    By JojoDiaz in forum New To Java
    Replies: 0
    Last Post: 11-14-2011, 05:10 AM
  4. PDFBox: Problem with converting pdf page into image
    By artfhc in forum Advanced Java
    Replies: 1
    Last Post: 01-03-2011, 08:37 PM
  5. scaling image
    By OmerHalit in forum Advanced Java
    Replies: 3
    Last Post: 05-07-2010, 02:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •