GroupDocsGists
10/24/2017 - 1:12 PM

Main.java

public class Main {
    public static final String TEST_BUCKET = "";
    public static final String ACCESS_KEY = "";
    public static final String SECRET_KEY = "";

    public static void main(String[] args) throws Exception {


    	com.groupdocs.viewer.licensing.License lic = new com.groupdocs.viewer.licensing.License();
		lic.setLicense("D:/GroupDocs.Total.Java.lic");

        Program program = new Program(".docx"); // Input file extensions
        program.execute("files/", ".html", new Program.IDoIt() { // Output file extension
            public void doIt(String inPath, String baseOutPath, String extension, String outPath, int fileIndex) throws Exception {

                final String guid = new File(inPath).getName();

                ViewerConfig config = new ViewerConfig();
                config.setStoragePath(new File(inPath).getParentFile().getAbsolutePath());
                config.setUseCache(true);

                // Create html handler
                final AmazonS3Client amazonS3Client = new AmazonS3Client(new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY));

                createBucket(amazonS3Client, inPath);

                ViewerHtmlHandler htmlHandler = new ViewerHtmlHandler(config, null, new S3CacheHandler(amazonS3Client), new S3DataStore(amazonS3Client));

                List<PageHtml> pages = htmlHandler.getPages(guid);

                for (PageHtml page : pages) {
                    FileUtils.writeStringToFile(new File(outPath), page.getHtmlContent(), Charset.forName("UTF-8"));
                }
            }
        });
    }


    private static void createBucket(AmazonS3Client amazonS3Client, String inPath) {
        final List<Bucket> buckets = amazonS3Client.listBuckets();
        for (Bucket bucket : buckets) {
            if (TEST_BUCKET.equals(bucket.getName())) {
                return;
            }
        }
        final Bucket bucket = amazonS3Client.createBucket(TEST_BUCKET);
        if (bucket != null) {
            amazonS3Client.putObject(new PutObjectRequest(TEST_BUCKET, ACCESS_KEY, new File(inPath)));
        }
    }
}