How to launch a PDF File using PDFBox - pdfbox

I want to get the time elapsed while opening a pdf file . I am not able to find a way to do it using PDFBox.I created a PDDocument in my java program and want to use some API to launch the PDF file through my code .I am unable to find out which PDFBox API that would serve the purpose.
So it would be helpful if i can get some info on that.
Thanks.
Swati

Paste the file 04-Request-Headers.pdf in Documents folder in C drive.
If you execute the below java code it will opens the 04-Request-Headers.pdf file in Adobe Acrobat. Total time to be taken to open the PDF file is displayed in console.
Below one is example of how to open the PDF file using Java.
Code:
package com.pdf.pdfbox.test;
import java.awt.Desktop;
import java.io.File;
public class OpenPDFFileUsingJava {
public static void main(String[] args) {
try {
File file = new File("C:/Documents/04-Request-Headers.pdf");
if (file.exists()) {
long startTime = System.currentTimeMillis();
Desktop.getDesktop().open(file);
long endTime = System.currentTimeMillis();
System.out.println("Total time taken to open file -> "+ file.getName() +" in "+ (endTime - startTime) +" ms");
} else {
System.out.println("File not exits -> "+ file.getAbsolutePath());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output: Total time taken to open file -> 04-Request-Headers.pdf in 94 ms

Related

Changing screen shot name that is saved in /img folder on QAF framework

I have a requirement where I need to add the time stamp for the screenshot image that is saved in /img folder. When I see AssertionService.java(https://github.com/qmetry/qaf/blob/master/src/com/qmetry/qaf/automation/ui/selenium/AssertionService.java), I See it is adding some random string at the end.
How to remove this random string added and add time stamp? Thanks for the help in advance!
private String captureScreenShot() {
String filename = StringUtil.createRandomString(getTestCaseName()) + ".png";
try {
selenium.captureEntirePageScreenshot(getScreenShotDir() + filename, "");
} catch (Exception e) {
try {
selenium.windowFocus();
} catch (Throwable t) {
logger.error(t);
}
selenium.captureScreenshot(getScreenShotDir() + filename);
}
lastCapturedScreenShot = filename;
logger.info("Captured screen shot: " + lastCapturedScreenShot);
return filename;
}
Are you using selenium 1 or 2 api? Selenium 2 uses following code https://github.com/qmetry/qaf/blob/d58b1d1ca01b2df1a916bcd6d555df4f51a13b12/src/com/qmetry/qaf/automation/core/QAFTestBase.java#L351. Regardless of API, you can't change naming strategy for automatic screenshots. As alternate you may disable auto capturing of screenshot, capture as and when needed and set calling setLastCapturedScreenShot

Google Drive Android SDK can't write to App Folder

I would like to save my app data in Google Drive to share it between Devices of the User.
First if there is no file i create one in google Drive:
Drive.DriveApi.getAppFolder(driveApiClient)
.createFile(driveApiClient, changeSet, null)
In the callback i open the file:
driveFileResult.getDriveFile().open(driveApiClient, DriveFile.MODE_WRITE_ONLY, null)
And in that Callback i write to the File as described https://developers.google.com/drive/android/files#making_modifications:
DriveContents contents = driveContentsResult.getDriveContents();
try {
ParcelFileDescriptor parcelFileDescriptor = contents.getParcelFileDescriptor();
FileOutputStream fileOutputStream = new FileOutputStream(parcelFileDescriptor
.getFileDescriptor());
Writer writer = new OutputStreamWriter(fileOutputStream);
writer.write("hello world");
} catch (IOException e) {
e.printStackTrace();
}
contents.commit(driveApiClient, null, new ExecutionOptions.Builder().setConflictStrategy(ExecutionOptions.CONFLICT_STRATEGY_OVERWRITE_REMOTE).build())
In the status callback of the commit i get a success.
Next time i open the file the file is empty, and the size in the metadata is 0.
What am i doing wrong?
Just did a quick test. Definitely close the OutputStreamWriter - writer.close() before the commit; otherwise, you will get an empty file.

Import annotations (XFDF) to PDF

I have created a sample program to try to import XFDF to PDF using the Aspose library. The program can be run without exception, but the output PDF does not include any annotations. Any suggestions to solve this problem?
Update - 2014-12-12
I have also sent the issue to Aspose. They can reproduce the same problem and logged a ticket PDFNEWJAVA-34609 in their issue tracking system.
Following is my sample program:
public static void main(String[] args) {
final String ROOT = "C:\\PdfAnnotation\\";
final String sourcePDF = "hackermonthly-issue.pdf";
final String destPDF = "output.pdf";
final String sourceXFDF = "XFDFTest.xfdf";
try
{
// Specify the path of license file
License lic = new License();
lic.setLicense(ROOT + "Aspose.Pdf.lic");
//create an object of PdfAnnotationEditor class
PdfAnnotationEditor editor = new PdfAnnotationEditor();
//bind input PDF file
editor.bindPdf(ROOT + sourcePDF);
//create a file stream for input XFDF file to import annotations
FileInputStream fileStream = new FileInputStream(ROOT + sourceXFDF);
//create an enumeration of all the annotation types which you want to import
//int[] annType = {AnnotationType.Ink };
//import annotations of specified type(s) from XFDF file
//editor.importAnnotationFromXfdf(fileStream, annType);
editor.importAnnotationFromXfdf(fileStream);
//save output pdf file
editor.save(ROOT + destPDF);
} catch (Exception e) {
System.out.println("exception: " + e.getMessage());
}
}

Selenium Webdriver-- getting error while Taking screenshot

public void testTakeScreenshot()
{
try{
File fscreenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
System.out.println(fscreenshot.getPath());
File fdest = new File("E:/");
FileUtils.copyFile(fscreenshot,fdest);
System.out.println(fdest.getPath());
}catch(Exception e)
{
e.printStackTrace();
}
}
Generated Output at console :
C:\Users\Bunty\AppData\Local\Temp\screenshot1773089913844817102.png
java.io.IOException: Destination 'E:\' exists but is a directory
The test is running Ok but the file is created as shown in the console. while copying the link . I couldnt find any file for the same. Also the copy function is not working ; hence no file is present in E drive.
As the errormessage suggests, you shouldn't give the path to the directory ('E:\'), but the path to the file. Try:
File fdest = new File("E:/screenshot.png");

YUI-Compressor: result file is empty

I am using the YUI Compressor library to minify CSS and JavaScript files. I directly use the classes CssCompressor and JavaScriptCompressor.
Unfortunatly some of the resulting files are empty without any warnings or exceptions.
I already tried it with the versions:
yuicompressor-2.4.2.jar
yuicompressor-2.4.6.jar
yuicompressor-2.4.7pre.jar
My used code is:
public static void compress(File file) {
try {
long start = System.currentTimeMillis();
File targetFile = new File("results", file.getName() + ".min");
Writer writer = new FileWriter(targetFile);
if (file.getName().endsWith(".css")) {
CssCompressor cssCompressor = new CssCompressor(new FileReader(file));
cssCompressor.compress(writer, -1);
} else if (file.getName().endsWith(".js")) {
JavaScriptCompressor jsCompressor = new JavaScriptCompressor(new FileReader(file), new MyErrorReporter());
jsCompressor.compress(writer, -1, true, false, false, true);
}
long end = System.currentTimeMillis();
System.out.println("\t compressed " + file.getName() + " within " + (end - start) + " milliseconds");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Files which do not work (are empty afterwards) are e.g.
http://code.google.com/p/open-cooliris/source/browse/trunk/fancy/jquery.fancybox.css?r=2
http://nodejs.org/sh_main.js
I know there are some bugs within the YUICompressor using media but could this be in relation with the empty results?
I had the same problem.
In my case it stemmed from that my javascript code was not ECMA valid (we use a variable named double which is not allowed according to the ECMA rules).
I did not have the courage to check if your js is valid but trying to compress different parts of your js file can easily lead you to the problem if it exists.
Well, after a while of debugging I figured out a solution.
The problem was not the YUI Compressor it self but it was the FileWriter given to the method.
Flushing an closing the FileWriter should solve the problem with empty result files
since I only need the minified String for further processing I now use a StringWriter with closing and flushing