I did a lot of researches on this site but i didn't find anything connected to my situation, so I'm kindly looking for an advice in order to reach the following result:
I'm working on a Java GUI and I developed an application to create an invoice.
On my project's folder i uploaded a blank pdf (that includes forms named: "nr_invoice", "client", "service", "price", "quantity", "tax" and "tot"), this pdf is named "invoice.pdf". Through the following code, by clicking a button on the GUI, the datas in the GUI are reported into the selected pdf's fields and the pdf is saved into the folder "src/pdf" with the name "new_invoice.pdf".
The code works, but what I'd like to do is to show a save file dialog (using JFileChooser) when the user click on the button "btnSave". So, by clicking on this button, the user is able to choose the save path and also the name of the file (but will be possible to save only a .pdf format).
Thank you very much for your help!
public class PdfGenerator extends JFrame implements ActionListener {
public static final String SRC = "src/pdf/invoice.pdf";
public static final String DEST = "src/pdf/new_invoice.pdf";
PdfDocument pdf = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
Map<String, PdfFormField> fields = form.getFormFields();
fields.get("nr_invoice").setValue(invoice.getText());
fields.get("client").setValue(cl_name.getText());
fields.get("service").setValue(description.getText());
fields.get("price").setValue(amount.getText());
fields.get("quantity").setValue(quantity.getText());
fields.get("tax").setValue(tax_amount.getText());
fields.get("tot").setValue(total_price.getText());
form.flattenFields();
pdf.close();
}
});
}
Related
I am making a Java-SQL database storing app on Eclipse and MySQL. In this app, I have to upload image to the file directory. Currently while making this app, I am using an image upload path and storing all the uploaded images there. But when I'm finished with the app, and if I'm to work it on someone else's computer the image upload path in the code obviously will not work on that computer. What shall I do to make it work on other computer as well? Should I make a prompt which asks for image upload path every time the app opens and store that, or something else?? please help.
private String imageUploadPath = "/home/tsoprano/Documents/eclipse-workspace/enable/src/com/enable/regis/imgupload/";
File file1;
picLabel = new JLabel(" Upload Photo");
picLabel.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
JFileChooser imageChooser= new JFileChooser();
if(imageChooser.showOpenDialog(picLabel)==JFileChooser.APPROVE_OPTION) {
file1 = imageChooser.getSelectedFile();
ImageIcon icon= new ImageIcon(imageChooser.getSelectedFile().getPath());
Image img=icon.getImage().getScaledInstance(130, 150, Image.SCALE_SMOOTH);
icon= new ImageIcon(img);
picLabel.setIcon(icon);
}
}
});
//inside the submit button action
String filePath = imageUploadPath + file1.getName();
try {
BufferedImage bi = ImageIO.read(file1);
ImageIO.write(bi, "jpg", new File(filePath));
} catch (IOException e1) {
e1.printStackTrace();
}
rdto.setPicUrl(filePath);
I would not suggest doing this via a config file, because this is not very user-friendly. Instead, get the current directory (i. e. the one that you executed the java command) using System.getProperty("user.dir"). Then create a new folder inside there, and use it as your upload folder. This will make sure you always have a useable path without bothering the user with specifying it.
This code is to view a PDF in swing, but as I open any PDF the content is not visible and blank pages appear. I am using icepdf core and viewer jar files.
public class PDFView{
public static void main(String[]args) {
SwingController controller = new SwingController();
SwingViewBuilder factory = new SwingViewBuilder(controller);
controller.setIsEmbeddedComponent(true);
DocumentViewController viewController = controller.getDocumentViewController();
JPanel viewerComponentPanel = factory.buildViewerPanel();
ComponentKeyBinding.install(controller, viewerComponentPanel);
controller.getDocumentViewController().setAnnotationCallback(
new org.icepdf.ri.common.MyAnnotationCallback(
controller.getDocumentViewController()));
JFrame applicationFrame = new JFrame();
applicationFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
applicationFrame.getContentPane().setLayout(new BorderLayout());
applicationFrame.getContentPane().add(viewerComponentPanel,BorderLayout.CENTER);
applicationFrame.getContentPane().add(factory.buildCompleteMenuBar(),BorderLayout.NORTH);
controller.setPageViewMode(DocumentViewControllerImpl.ONE_PAGE_VIEW, false);
applicationFrame.pack();
applicationFrame.setVisible(true);
}}
Find the screenshots here..
Errors and warnings in the code
[]
Output-view of PDF
java.lang.noClassDefFoundError : com.sun.image.codec.jpeg.JPEGImageDecoder
Its seems you need JPEGImageDecoder for it
http://www.java2s.com/Code/Jar/s/Downloadsunjaicodecjar.htm
Download the JAR and add it to your build path libraries
Am using icepdf to show pdf on my swing application. I have external buttons for pdf navigation. I need to navigate so many pdfs, but inside the same panel without closing.
My code for Icepdf:
public static void pdfviewerICE(currentpdfurl) {
filePath = currentpdfurl;
// build a controller
SwingController controller = new SwingController();
// Build a SwingViewFactory configured with the controller
SwingViewBuilder factory = new SwingViewBuilder(controller);
PropertiesManager properties = new PropertiesManager(
System.getProperties(),
ResourceBundle.getBundle(PropertiesManager.DEFAULT_MESSAGE_BUNDLE));
properties.set(PropertiesManager.PROPERTY_DEFAULT_ZOOM_LEVEL, "1.75");
JSplitPane jSplitPane_PDF = factory.buildUtilityAndDocumentSplitPane(true);
controller.openDocument(filePath);
if ((internalFrame.getComponents()!= null) || (internalFrame.isClosed())) {
internalFrame.add(jSplitPane_PDF);
internalFrame.show();
}
}
This is loading same pdf everytime.
Prasath Bala,
you have to make "controller" as public. Then call your controller when you pdf page is changed
Code:
controller.openDocument(currentpdfurl);
I too had the same issue, this will solve your query for sure.
I am new in GWT and I am working on a project where GWT is used to generate a form. In this form I have added a table in a Vertical panel. Now I want to export this table in excel or CSV. Wondering if someone can help me on this issue.
This is sample code of the entry point class:
public class ItemListEditor implements EntryPoint
{
private final Button addRow = new Button("Add Row");
private final Button save = new Button("Save");
private final Button next = new Button("Next");
private final Button upload = new Button("Upload");
private final Button export = new Button("Export");
private ItemTable table = null;
}
Again, my problem is only to export this table- like when click on export button, system will ask for the location.
I can provide more information if needed.
Please help on this issue
Create a HTTPServlet class, inside the doGet() method of the servlet create a HSSFWorbook using the Apache Poi jar, write your data in the sheet, write the workbook in the response part of servlet.
Map the servlet in your web.xml file and finally, use this servlet url inside your button handler..
EDITED ----->
button.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent arg0) {
exlGenerationServiceAsync.excelReportObjects(new AsyncCallback() {
#Override
public void onFailure(Throwable arg0) {
arg0.printStackTrace();
}
#Override
public void onSuccess(Object arg0) {
Window.open(GWT.getModuleBaseURL() + "url", "", "");
}
});
}
}
Servlet mapping inside the web.xml file----->
<servlet>
<servlet-name>excelFile</servlet-name>
<servlet-class>com.company.server.excelFileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>excelFile</servlet-name>
<url-pattern>/yourUrl</url-pattern>
</servlet-mapping>
Where excelFileServlet is my servlet class.
Inside Servlet class---->
File file = new File("Path Where you want to write your excel file");
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition", "attachment; filename=" + "Excel_Report_Name.xls");
response.setContentLength((int) file.length());
FileInputStream fileInputStream = new FileInputStream(file);
try{
OutputStream responseOutputStream = response.getOutputStream();
int bytes;
while ((bytes = fileInputStream.read()) != -1) {
responseOutputStream.write(bytes);
}
responseOutputStream.close();
}catch(Exception e){
e.printStackTrace();
System.err.println("Inside Try/catch");
If you're looking for some quick and raw exportation, you can give a try to https://code.google.com/p/gwt-table-to-excel/. The project is not very active.
Otherwise, if you want to have more control on the formatting and data you have to send the content of the table to the server side using RPC or other, and then you can use Apache POI to export it as you do usually in Java.
I have a repository storing many images somewhere on the server.
I want to be able to create a dynamic Image object with one of the images stored in my repository.
I am using wicket 1.5.7. I saw this example somewhere
1) Created the FileResource class:
public class FileResource extends WebResource {
private static final long serialVersionUID = 1L;
private File file;
public FileResource(File file) {
this.file = file;
}
#Override
public IResourceStream getResourceStream() {
return new FileResourceStream(file);
}
}
2) In MyPage.java:
File imageFile = new File("local_path_to_image");
Image myImage = new Image("myImage", new FileResource(imageFile));
add(myImage);
3) In MyPage.html:
<i-m-g wicket:id="myImage" />
But this is not working in my case because WebResource is not available in my wicket 1.5.
I have also studied this link in wicket action. But I am a wicket bignner i could not understand much.
I am making a project in which user when click on a product a modal window open with the product name. I also want to include the product image on my modal window inside a panel. Images are stored on my server in a directory.
Any help and advices appreciated! Thanks in advance.
finally i settled on this code. I am passing image file name and creating image.
add(new NonCachingImage("imgPlc", new AbstractReadOnlyModel<DynamicImageResource>(){
#Override public DynamicImageResource getObject() {
DynamicImageResource dir = new DynamicImageResource() {
#Override protected byte[] getImageData(Attributes attributes) {
StringValue name = parameters.get("name");
byte[] imageBytes = null;
if(name.isEmpty() == false)
imageBytes = getImageAsBytes(name.toString());
return imageBytes;
}
};
dir.setFormat("image/png");
return dir;
}
}));