I am attempting to print Word documents generated using docx4j that I convert to PDF and print using PDFBox. The document I'm attempting to print just contains a simple table with basic English text.
This is the method I use:
private void printDocument(){
PrinterJob job = PrinterJob.getPrinterJob();
try {
PDDocument document;
document = PDDocument.load("files\\textDocs\\OITScanSheet.pdf");
job.setPageable(new PDPageable(document, job));
job.setJobName("OIT Scan Sheet");
job.print();
} catch (NullPointerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (PrinterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This is what I'm trying to print:
Before print
And this is what comes out when I print (Sorry for the terrible quality):
After print
The table is intact with the correct number of rows, but the text is not there. When I print the document from Adobe Reader it prints as it should.
I'm using PDFBox 2.0.0-snapshot right now. I switched from version 1.8.4 because I was running into this problem, where it was printing the table correctly but all of the text was junk.
I'm fairly certain the problem has something to do with some sort of font problem behind the scenes, but I have no idea how to go about fixing it. Any help would be greatly appreciated.
If you really need the printDialog, this is a ready to use code, assuming you have PDFBox 2.0 and font 2.0 in your classpath
public static void PDFPrintingServices(String filePath) throws IOException, PrinterException {
PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();
attr_set.add(MediaSizeName.ISO_A4);
attr_set.add(Sides.ONE_SIDED);
PDDocument pdf = PDDocument.load(new File(filePath));
PDFPageable p = new PDFPageable(pdf);
PDFPrintable printable = new PDFPrintable(pdf, Scaling.SCALE_TO_FIT);
PrinterJob job = PrinterJob.getPrinterJob();
job.setJobName("My Printer");
job.setPageable(p);
job.setPrintable(printable);
if(job.printDialog()) {
job.print();
job.print(attr_set);
}
}
Related
I have a for loop which i would like to run parallelly.
if (platform != null) {
for (final String p : platform) {
log.info("Platform " + p);
executorService.submit(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
runService(p, config, url, title, report);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
executorService.shutdown();
executorService.awaitTermination(Long.MAX_VALUE,TimeUnit.NANOSECONDS);
}
The runService method for each platform creates a new sheet for the same workbook.
I have read on other stackoverflow questions that apache poi workbook does not support multithreading on the same workbook object .I have made those methods as synchronized which are updating the sheet of the workbook. This solves all the apache-poi errors which I was getting but still would like to know is there a better way to handle this situation.
I want to rename a csv file in java using following code segment, but file is not getting renamed.
public static void main(String[] args) {
File fileToBeRenamed = new File("C:/abc/a.txt");
File newFileName = new File("C:/abcd/b.txt");
try {
fileToBeRenamed.createNewFile();
newFileName.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
boolean isRenamed = fileToBeRenamed.renameTo(newFileName);
if(isRenamed)
System.out.println("File renamed successfully");
else
System.out.println("File could not be renamed");
}
Its not throwing any error. but file is not getting renamed.So please help me to do so.
let's suppose you have a file A(fileToBeRenamed) and you want to rename it to B(newFileName). Then , no need to create "newFileName" file. your code is fine , except the file creation part.
so comment out the lines:
try {
fileToBeRenamed.createNewFile();
newFileName.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
And it will work.
Note: I don't think it has anything to do with file extension(csv/text etc), when both are the same.
I think you want to rename a.txt to b.txt, So you don't need create b.txt. If you remove newFileName.createNewFile() will work
I am creating java files from json Objects using a library called jsonschema2pojo-core.jar. It successfully creates the required files for me. Now I need to access the newly(dynamically) created file and creates its instance to use it further.
But as the newly created class is still not in the classpath I am unable to do this. Tried to do my part of research and figured out that Eclipse jars allows such refresh only in plugin projects. Can anyone suggest some thing for this?
public static void main(String[] args){
String fileName = "MyJavaFile";
POJOBuilder pojo = new POJOBuilder();
pojo.buildPOJO("file:///C:/mypath/myJSON.json", fileName); //generates the java file MyJavaFile.java
Object obj = null;
try {
obj = Class.forName("com.mypackage."+fileName).newInstance(); // Java file not available yet
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Can this be done through threads? I mean wait until the creation of the POJO is done and then start with the rest after that?
I want to include a font called Fixedsys in my game and this is the code I use :
try{
Font myFont = null;
File fontFile = new File("Fixedsys.ttf");
if(fontFile.exists()){
myFont = Font.createFont(Font.TRUETYPE_FONT, fontFile).deriveFont(Font.PLAIN, 22f);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(myFont);
System.out.println("Not null");
}else{
System.out.println("FILE DOES NOT EXIST");
}
} catch (FontFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
For some reason, Java thinks the file DOES NOT exist and prints out the FILE DOES NOT EXIST line. I have searched through google and stackoverflow but none of then work when I use :
myComponent.setFont(myFont);
I get an error saying:
cannot find variable myFont
I have checked over and over and over but nothing seems wrong.
EDIT : I removed the if(file.exists()) line and i get a different error. I get :
Cannot read Fixedsys.ttf !
EDIT 2 : ug_'s comment proved right. Java was looking in the wrong folder for the file. Thanks.
The myFont variable is a local variable inside the catch block and therefore doesn't exist anywhere else.
You have to make it a class variable to use it outside the catch block.
Like so:
class SomeClass {
// declare here
private Font myFont;
public SomeClass() {
try{
// initialize here
File fontFile = new File("Fixedsys.ttf");
if(fontFile.exists()){
myFont = Font.createFont(Font.TRUETYPE_FONT, fontFile).deriveFont(Font.PLAIN, 22f);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(myFont);
System.out.println("Not null");
}else{
System.out.println("FILE DOES NOT EXIST");
}
} catch (FontFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// somewhere else:
myComponent.setFont(myFont);
}
The real answer is that the font file does not exist at that path location. Look in Windows\Fonts or wherever the file really is.
I am developing an Eclipse plugin and I want to take input from the user. I had a problem before with dealing with the console as I have 2 consoles 1 in the original Eclipse application and the other in the new opened Eclipse plugin window. I used the method shown below to print in the Console of the Plugin
public static void writeToConsole(String output) {
MessageConsole console = new MessageConsole("My Console", null);
console.activate();
ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[]{ console });
MessageConsoleStream stream = console.newMessageStream();
stream.println(output);
}
I just don't know how to write in the console and take what's written in a variable
N.B.
The normal methods like
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
String s = bufferRead.readLine();
System.out.println(s);
are working for the original Eclipse not the plugin
You can't tie up the UI waiting for user input so you have to handle user input when it is received. One way to do this is with a document listener. Here is an example that prints the last line typed:
console.getDocument().addDocumentListener(new IDocumentListener() {
#Override
public void documentChanged(final DocumentEvent event) {
if ("\r\n".equals(event.getText())) {
final IDocument doc = event.getDocument();
try {
final IRegion region = doc.getLineInformationOfOffset(event.getOffset());
try {
final String line = doc.get(region.getOffset(), region.getLength());
System.out.println(line);
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
#Override
public void documentAboutToBeChanged(final DocumentEvent event) {
// TODO Auto-generated method stub
}
});