DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
  • submit to reddit
Tinu Awopetu07/27/09
35069 views
6 replies

NetBeans IDE 6.7.1 with JavaFX Now Available for Download!

The NetBeans Team is pleased to announce the release of NetBeans IDE 6.7.1, which supports the latest JavaFX SDK. Download NetBeans 6.7.1 (A JavaFX-only download bundle is also available.) NetBeans IDE 6.7.1 is an update to NetBeans IDE 6.7 and includes the...

Rick Ross02/14/08
14145 views
16 replies

Welcome to DZone's new IndicZone community for Indian IT Pros

Let me welcome all of you to the new DZone community for one of the most important and fastest growing groups in the worldwide IT scene: Indian developers and IT professionals. No matter where you look, if something interesting is happening on the IT and...

Adi Gaskell11/27/14
209 views
0 replies

Can an algorithm show you who to collaborate with?

Collaboration is widely seen as a good thing (when it’s done well at least), but knowing who can add value to your work is often tricky. There have been some attempts to employ serendipity to the occasion, with tools such as Lunch Roulette aiming to match employees up at random for a lunch date.

Bloomfire Marketing11/27/14
136 views
0 replies

Bloomfire premiers Advanced Reporting for customer insights

Bloomfire has launched an Advanced Reporting offering, which the company says is designed to give organizations detailed, easily consumed data that moves beyond views, likes and follows to show the true engagement of collaboration initiatives.

Adi Gaskell11/27/14
239 views
0 replies

Research explores how we can change the behaviour of our groups

A fundamental part of being a sense and respond organization is the ability for change to occur from throughout the organization. Suffice to say however, few have really achieved that hallowed place, with most change projects still requiring an executive sponsor to help disseminate things widely.

Daniela Castrataro11/27/14
132 views
0 replies

The CrowdAsset Framework in the EC Open Innovation Handbook

The European Commission is very supportive of the idea of Open Innovation. They promote it, research it , and endeavour to utilise it with projects like FI WARE and similar initiatives, and they also bring together examples of creative and inspiring cases and thinking.

Madhuri Challa11/27/14
9 views
0 replies

DataBase Testing Online Training

We are offering job oriented Database Testing online Training with 100% placement assistance. Our training includes latest syllabus topics which may helpful to all students. These topics will help you to prepare database testing and which may helpful to you...

Madhuri Challa11/27/14
9 views
0 replies

Advanced Java Online Training with Placement Assistance

We are offering advanced java online training with job placement assistance. We are having real time experienced java faculties which may help you and assist you all the time. Our java training will helps you and after attending our training sessions you will...

Andries Mortier11/27/14
74 views
0 replies

Toc.ui is now pre-released!

Introduction The TOC tool  is mainly a link manager. It displays your links in a sort of grouped project overview, but with executable actions behind it, which is very handy to use as a shortcut tool. However it is the architecture behind it which...

Oliver White11/26/14
28 views
0 replies

Secure Containers for the Cloud

Let’s take a look at the offering in a bit of detail.

Theo England11/26/14
11 views
0 replies

Scala eXchange 2014 - December 8th-9th

Scala eXchange is Europe's largest annual Scala conference and will attract over 500 attendees across the continent to London. For two days and over four tracks of talks, Scala developers can hear the latest happenings in the Scala ecosystem. Our four keynote...

package ImageToPdf;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URI;

import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.BreakType;
import com.aspose.words.PageSetup;
import com.aspose.words.ConvertUtil;
import com.aspose.words.RelativeHorizontalPosition;
import com.aspose.words.RelativeVerticalPosition;
import com.aspose.words.WrapType;


class Program
{
    public static void main(String[] args) throws Exception
    {
        // Sample infrastructure.
        URI exeDir = Program.class.getResource("").toURI();
        String dataDir = new File(exeDir.resolve("../../Data")) + File.separator;

        convertImageToPdf(dataDir + "Test.jpg", dataDir + "TestJpg Out.pdf");
        convertImageToPdf(dataDir + "Test.png", dataDir + "TestPng Out.pdf");
        convertImageToPdf(dataDir + "Test.bmp", dataDir + "TestBmp Out.pdf");
        convertImageToPdf(dataDir + "Test.gif", dataDir + "TestGif Out.pdf");
    }

    /**
     * Converts an image to PDF using Aspose.Words for Java.
     *
     * @param inputFileName File name of input image file.
     * @param outputFileName Output PDF file name.
     */
    public static void convertImageToPdf(String inputFileName, String outputFileName) throws Exception
    {
        // Create Aspose.Words.Document and DocumentBuilder.
        // The builder makes it simple to add content to the document.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);

        // Load images from the disk using the approriate reader.
        // The file formats that can be loaded depends on the image readers available on the machine.
        ImageInputStream iis = ImageIO.createImageInputStream(new File(inputFileName));
        ImageReader reader = ImageIO.getImageReaders(iis).next();
        reader.setInput(iis, false);

        try
        {
            // Get the number of frames in the image.
            int framesCount = reader.getNumImages(true);

            // Loop through all frames.
            for (int frameIdx = 0; frameIdx < framesCount; frameIdx++)
            {
                // Insert a section break before each new page, in case of a multi-frame image.
                if (frameIdx != 0)
                    builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);

                // Select active frame.
                BufferedImage image = reader.read(frameIdx);

                // We want the size of the page to be the same as the size of the image.
                // Convert pixels to points to size the page to the actual image size.
                PageSetup ps = builder.getPageSetup();

                ps.setPageWidth(ConvertUtil.pixelToPoint(image.getWidth()));
                ps.setPageHeight(ConvertUtil.pixelToPoint(image.getHeight()));

                // Insert the image into the document and position it at the top left corner of the page.
                builder.insertImage(
                    image,
                    RelativeHorizontalPosition.PAGE,
                    0,
                    RelativeVerticalPosition.PAGE,
                    0,
                    ps.getPageWidth(),
                    ps.getPageHeight(),
                    WrapType.NONE);
            }
        }

        finally {
            if (iis != null) {
                iis.close();
                reader.dispose();
            }
        }

        // Save the document to PDF.
        doc.save(outputFileName);
    }
} 

//Converts large size image into a PDF document

//When the size of image is bigger than max allowed size of MS Word page, an exception is thrown or image is not visible in output document. The measurement for images must be between 0 and 55.88 cm (55.88 cm = 1584 points). In this case, use the following code example.
package ImageToPdf;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URI;

import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.BreakType;
import com.aspose.words.PageSetup;
import com.aspose.words.ConvertUtil;
import com.aspose.words.RelativeHorizontalPosition;
import com.aspose.words.RelativeVerticalPosition;
import com.aspose.words.WrapType;


class Program
{
    public static void main(String[] args) throws Exception
    {
        // Sample infrastructure.
        URI exeDir = Program.class.getResource("").toURI();
        String dataDir = new File(exeDir.resolve("../../Data")) + File.separator;

        convertImageToPdf(dataDir + "Test.jpg", dataDir + "TestJpg Out.pdf");
        convertImageToPdf(dataDir + "Test.png", dataDir + "TestPng Out.pdf");
        convertImageToPdf(dataDir + "Test.bmp", dataDir + "TestBmp Out.pdf");
        convertImageToPdf(dataDir + "Test.gif", dataDir + "TestGif Out.pdf");
    }

    /**
     * Converts an image to PDF using Aspose.Words for Java.
     *
     * @param inputFileName File name of input image file.
     * @param outputFileName Output PDF file name.
     */
    	public static void convertImageToPdf(String inputFileName, String outputFileName) throws Exception
	{
	    // Create Aspose.Words.Document and DocumentBuilder.
	    // The builder makes it simple to add content to the document.
	    Document doc = new Document();
	    DocumentBuilder builder = new DocumentBuilder(doc);

	    // Load images from the disk using the approriate reader.
	    // The file formats that can be loaded depends on the image readers available on the machine.
	    ImageInputStream iis = ImageIO.createImageInputStream(new File(inputFileName));
	    ImageReader reader = ImageIO.getImageReaders(iis).next();
	    reader.setInput(iis, false);

	    try
	    {
	        // Get the number of frames in the image.
	        int framesCount = reader.getNumImages(true);

	        // Loop through all frames.
	        for (int frameIdx = 0; frameIdx < framesCount; frameIdx++)
	        {
	            // Insert a section break before each new page, in case of a multi-frame image.
	            if (frameIdx != 0)
	                builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);

	            // Select active frame.
	            BufferedImage image = reader.read(frameIdx);

	            // Max page size
	            double maxPageHeight = 1584;
	            double maxPageWidth = 1584;

	            double currentImageHeight = ConvertUtil.pixelToPoint(image.getHeight());
	            double currentImageWidth = ConvertUtil.pixelToPoint(image.getWidth());

	            if (currentImageWidth >= maxPageWidth || currentImageHeight >= maxPageHeight)
	            {

	                // Get max image size.
	            	 double[] size = CalculateImageSize(image, maxPageHeight, maxPageWidth, currentImageHeight, currentImageWidth);
	            	 currentImageWidth = size[0];
	            	 currentImageHeight = size[1];
	            }

	            // We want the size of the page to be the same as the size of the image.
	            // Convert pixels to points to size the page to the actual image size.
	            PageSetup ps = builder.getPageSetup();

	            ps.setPageWidth(currentImageWidth);
	            ps.setPageHeight(currentImageHeight);

	            // Insert the image into the document and position it at the top left corner of the page.
	            Shape shape = builder.insertImage(
	                image,
	                RelativeHorizontalPosition.PAGE,
	                0,
	                RelativeVerticalPosition.PAGE,
	                0,
	                ps.getPageWidth(),
	                ps.getPageHeight(),
	                WrapType.NONE);

	            resizeLargeImage(shape);
	        }
	    }

	    finally {
	        if (iis != null) {
	            iis.close();
	            reader.dispose();
	        }
	    }

	    // Save the document to PDF.
	    doc.save(outputFileName);
	}

	public static double[] CalculateImageSize(BufferedImage img, double containerHeight,  double containerWidth, double targetHeight, double targetWidth) throws Exception {

	 targetHeight = containerHeight;
	 targetWidth = containerWidth;

	  //Get size of an image
	    double imgHeight = ConvertUtil.pixelToPoint(img.getHeight());
	    double imgWidth = ConvertUtil.pixelToPoint(img.getWidth());

	    if (imgHeight < targetHeight && imgWidth < targetWidth)
	    {
	        targetHeight = imgHeight;
	        targetWidth = imgWidth;
	    }
	    else
	    {
	        //Calculate size of an image in the document
	        double ratioWidth = imgWidth / targetWidth;
	        double ratioHeight = imgHeight / targetHeight;
	        if (ratioWidth > ratioHeight)
	            targetHeight = (targetHeight * (ratioHeight / ratioWidth));
	        else
	            targetWidth = (targetWidth * (ratioWidth / ratioHeight));
	    }

	    double[] size = new double[2];

	    size[0] = targetWidth; //width
	    size[1] = targetHeight; //height

	    return(size);
	}

    public static void resizeLargeImage(Shape image) throws Exception
    {
        // Return if this shape is not an image.
        if (!image.hasImage())
            return;

        // Calculate the free space based on an inline or floating image. If inline we must take the page margins into account.
        PageSetup ps = image.getParentParagraph().getParentSection().getPageSetup();
        double freePageWidth = image.isInline() ? ps.getPageWidth() - ps.getLeftMargin() - ps.getRightMargin() : ps.getPageWidth();
        double freePageHeight = image.isInline() ? ps.getPageHeight() - ps.getTopMargin() - ps.getBottomMargin() : ps.getPageHeight();

        // Is one of the sides of this image too big for the page?
        ImageSize size = image.getImageData().getImageSize();
        boolean exceedsMaxPageSize = size.getWidthPoints() > freePageWidth || size.getHeightPoints() > freePageHeight;

        if (exceedsMaxPageSize)
        {
            // Calculate the ratio to fit the page size based on which side is longer.
            boolean widthLonger = (size.getWidthPoints() > size.getHeightPoints());
            double ratio = widthLonger ? freePageWidth / size.getWidthPoints() : freePageHeight / size.getHeightPoints();

            // Set the new size.
            image.setWidth(size.getWidthPoints() * ratio);
            image.setHeight(size.getHeightPoints() * ratio);
        }
    }
}
Victor Savkin11/26/14
800 views
0 replies

Why JavaScript Needs Types

The JavaScript community is embracing the idea of optional types. TypeScript has become a robust tool used in production at many companies. AtScript, and Flow are new projects in this area. I am really excited to see what typed JavaScript community will look like in a few years.

Adi Gaskell11/26/14
508 views
0 replies

GE reveal the fruits of their Quirky partnership

Quirky and GE have had a very close relationship for a little while now. The first partnership between the pair emerged around 18 months ago when GE opened up a number of patents to the Quirky community for their pool of innovators and inventors to go to work on.

Interact Intran...11/26/14
296 views
0 replies

12 signs you need an intranet

12 signs that your company is crying out for an intranet

Adi Gaskell11/26/14
231 views
0 replies

Babylon provides another step towards remote healthcare

Earlier this month I attended the Health 2.0 conference in London, and there were a fascinating array of projects covered from around the world.  I’ll be reviewing some of the better products over the coming days, but I think the biggest trend...

Highq Collaborate11/26/14
225 views
0 replies

Deloitte Technology Fast 50 lists HighQ in top UK tech firms

We’re delighted to announce that HighQ has been ranked at number 50 in the Deloitte Technology Fast 50, a ranking of the top 50 fastest growing UK technology companies.

Jim Bird11/26/14
5804 views
0 replies

Different Ways of Scaling Agile

The major advantage of Agile at scale isn't the ability to react to continuous changes or even to deliver faster or cheaper. It's knowing sooner whether you should keep going, or if you need to keep going, or if you should stop and do something else instead.

Sanjay Patel11/26/14
6 views
0 replies

Git Rapid Tutorial: Git In Practice Using Eclipse Or Any IDE

I am pleased to announce the release of my video tutorial "Git Rapid Tutorial: Git In Practice Using Eclipse Or Any IDE." Visit http://naturalprogrammer.com/2014/11/26/git-rapid-tutorial-using-eclipse-egit/  to know more about it and for FREE...

Johanna Rothman11/26/14
1989 views
0 replies

Three Alternatives for Making Smaller Stories

You want to deliver value in your projects. Short stories allow you to do this. Long stories stop your momentum. The longer your project, and the more teams (if you work on a program), the more you need to keep your stories short. Try these alternatives.