import java.lang.reflect.Method; // add sohid
//import urldecode.url_encode; // add sohid
import java.util.StringTokenizer;
import java.util.Properties;
import javax.print.PrintService;
import java.awt.print.PrinterJob;
import java.io.File;
import oracle.forms.engine.Main;
import oracle.forms.handler.IHandler;
import oracle.forms.properties.ID;
import oracle.forms.ui.VBean;
import org.apache.pdfbox.pdmodel.PDDocument;
//import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
/*
* @author : Shohidul islam( 23-may-2012 )
*
* Development Environment : NetBean 6.8
* Name of the File : DirectPrint.java
* This JavaBean is an attempt to eliminate the ORARRP client. Specificly in pos clients, as speed is main feature.
* This JavaBean will allow a pdf from a full URL or a partial URL to be printed directly to the default or named printer
* without a print dialog box.
*
*/
/**
* A object that enables pdf files to be printed directly with no user intervention.
*/
public class DirectPrint extends VBean {
//**********************
// FORMS STATIC IDs
//**********************
/**
* DIRECT_PRINT_PDF_URL - This is the full URL to the pdf. This for a report server that is on another server.
*
* Use this inplace of web.show_document(); in the PRINT_REPORT_OBJECT if the report server is not on the
* same server as the forms was launched.
*
* <p><b>Forms Example:</b></p>
* <code>set_custom_property('BeanArea',1,'DIRECT_PRINT_PDF_URL','http://www.domain.com/test.pdf');</code>
*/
protected static final ID pDirectPrintPDFURL = ID.registerProperty("DIRECT_PRINT_PDF_URL");
/**
* SET_TTF_FROM_FS - Used to set a TrueType Font (TTF) from the filesystem. Ussualy a font that is not
* by default loaded into this class.
* Value: FontName[$]PATH
* Note: The FontName has to be the exact font name listed in the PDF document.
* The Path is C:\Windows\fonts\fontname.ttf.
* Use ListFont method to see the names in document if needed.
*
* Note: All font set by URL and FS are loaded on each print job.
*
* <p><b>Forms Example:</b></p>
* <code>set_custom_property('BeanArea',1,'SET_TTF_FROM_FS','fontname$full_path');</code>
*/
protected static final ID pSetTTFFromFS = ID.registerProperty("SET_TTF_FROM_FS");
/*
* Variables
*/
/**
* Forms Handler.
*/
private IHandler mHandler = null; // Forms Handler
/**
* Forms Main Class.
*/
private Main formsMain = null; // Forms main class
/**
* Default Instance Name.
*/
private String this_instance_name = this.getDefaultName();
/**
* The protocol when a URL is built.
*/
private String protocol = null;
/**
* The domain when a URL is built.
*/
private String domain = null;
/**
* The port when a URL is built, the default value is 80.
*/
private int port = 80;
// Using Fonts
/**
* The external fonts to load from filesystem.
*/
private Properties extFSFonts = new Properties();
/**
* Named Printer - inited with the default printer
*/
/**
* Named Printer - inited with the default printer
*/
private PrintService currentPrintService = PrinterJob.getPrinterJob().getPrintService();
/**
* Default Constructor.
*/
public DirectPrint() {
}
/**
* Default init.
*/
public final void init(IHandler handler) {
mHandler = handler;
super.init(handler);
try {
Method method = handler.getClass().getMethod("getApplet", new Class[0]);
Object applet = method.invoke(handler, new Object[0]);
if (applet instanceof Main) {
formsMain = (Main) applet;
}
} catch (Exception ex) {
System.out.println("Exception init: " + ex.getMessage());
}
loadDefaultFSFonts(); // for load font
}
//*******************************
//* SET FORMS PROPERTIES
//*******************************
/**
* Uses to set properties of the ID object.
* @return The status of the set property
* @param _args Value passed in from the forms bean call.
* @param _ID The ID of the parameter that was called.
*/
public boolean setProperty(ID _ID, Object _args) {
/*=====================
* DIRECT_PRINT_PDF_URL
*=====================*/
if (_ID == pDirectPrintPDFURL) {
//check that argument is not null
if ((_args != null)) {
try {
//System.out.println("DIRECT_PRINT_PDF_URL: " + (String) _args);
//write_message("DIRECT_PRINT_PDF_URL: " + (String) _args);
//printPDFURL((String) _args);
// sendPDFURLToPrinter((String)_args);
} catch (Exception ex) {
System.out.println("Exception: " + ex.getMessage());
}
}
return true;
}
/*=========================
* SET_TTF_FROM_FS
* ========================*/
if (_ID == pSetTTFFromFS) {
if ((_args != null) && (((String) _args).length() != 0)) {
System.out.println("SET_TTF_FROM_FS: " + (String) _args);
this.setFSFontProperty((String) _args);
} else {
// not valid argument was passed, print message to console
System.out.println("Call to SET_TTF_FROM_FS can not be a null argument.");
}
return true;
}
return super.setProperty(_ID, _args);
}
/**
* This prints the URL to the currently set default printer.
* @param pdfURLStr Full URL to the PDF to print.
*/
private void sendPDFURLToPrinter() {
PDDocument document;
try {
// URL pdfFile = new URL(pdfURLStr); // comment by sohid because blank URL can not read
//URL pdfFile = new URL(pdfURLStr.replaceAll(" ", "%20"));
String pdfFile="E:\\green.pdf";
//write_message("Printing URL: " + pdfURLStr);
System.out.println(pdfFile.toString()); //sohid
document = PDDocument.load(pdfFile);
// PDFont currFont = PDTrueTypeFont.loadTTF(document, new File("C:/WINDOWS/Fonts/" + "CarolinaBar-B39-25F2.ttf"));
List pages = document.getDocumentCatalog().getAllPages();
// load fonts here iterate over pages
if (pages.isEmpty()) {
// write_message("Pages list is empty");
System.out.println("Pages list is empty"); //sohid
} else {
// System.out.println("sohid Pages list is NOT empty"); //sohid
Iterator it = pages.iterator();
while (it.hasNext()) {
PDPage currentPage = (PDPage)it.next();
Map map = currentPage.findResources().getFonts();
// System.out.println(" Sohid Map is: "+map.toString()); //sohid
Set mySet = map.keySet();
Iterator setInt = mySet.iterator();
while (setInt.hasNext()) {
String val = (String)setInt.next();
// System.out.println("Sohid Map Val: "+ val); //sohid
PDType0Font pdt = (PDType0Font)map.get(val);
// System.out.println("My Font is = " + pdt.getFontDescriptor().getFontName()); //sohid
// PDFont currFont = this.loadFontByName(document,pdt.getFontDescriptor().getFontName(),val);//pdt.getFontDescriptor().getFontName()
PDFont currFont = PDTrueTypeFont.loadTTF(document, new File("C:/WINDOWS/Fonts/" + "arial.ttf"));
if(currFont != null)
{
currentPage.findResources().getFonts().put(val,currFont);
}
}
}
}
// AccessPermission currentPermissions = document.getCurrentAccessPermission();
// if (!currentPermissions.canPrint()) {
// System.out.println("Permission denied to print document.");
// }
//
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintService(currentPrintService);
printJob.setPageable(document);
printJob.print();
// Close document
document.close();
} catch (Exception ex) {
// write_message(ex.getMessage());
System.out.println(ex.getMessage());
}
}
private void setFSFontProperty(String fontNameDelimLocation)
{
StringTokenizer st = new StringTokenizer(fontNameDelimLocation,"$");
String fontName = st.nextToken();
String fontLocation = st.nextToken();
// check that font name does not already exist
if(extFSFonts.getProperty(fontName) == null )
{
extFSFonts.setProperty(fontName,fontLocation);
System.out.println("extFSFonts added: "+fontName);
}
else
{
System.out.println("extFSFonts already set: "+fontName);
}
}
private void loadDefaultFSFonts()
{
// Example: extFSFonts.setProperty(fontName,fontLocation);
//extFSFonts.setProperty("CarolinaBar-B39-25F2-Normal.ttf","C:\\WINDOWS\\Fonts\\");
extFSFonts.setProperty("CarolinaBar-B39-25F2.ttf","C:\\WINDOWS\\Fonts\\");
//"C:/WINDOWS/Fonts/" + "CarolinaBar-B39-25F2-Normal.ttf"
}
public static void main(String args[]) {
DirectPrint dr = new DirectPrint();
dr.sendPDFURLToPrinter();
System.out.println("ashduashdja");
}
}
No comments:
Post a Comment