dialog to select pdf to import

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

dialog to select pdf to import

joe smith
I have a dialog that allows a user to select a file and then I place in into a PDF I am creating.

In the examples  to import a pdf you use:

public static final String SOURCE = "C:\\myTest.pdf";

to read the pdf you use:

PdfReader reader = new PdfReader(SOURCE);

This works fine but when I pass the selection made in the dialog it does not work.

So in my dialog class I have the following line that passes the selection made by the user.

jobTicket.setP((dlg.getFilterPath()+File.separatorChar+files[i].toString()).replaceAll("\\\\", "\\\\\\\\" ));
String p = jobTicket.getP();
System.out.println("JT=" + p); //this returns JT=C:\\myTest.pdf

In my create pdf class I have the following lines which should work but return a error

public static void setP(String p) {
        jobTicket.p = p;
    }

public static final String SOURCE = p;

PdfReader reader = new PdfReader(SOURCE); //error below.

if SOURCE is returning "C:\\myTest.pdf" why is this not working?

Any suggestions would be appreciated.

Error below:

java.lang.NullPointerException
    at java.io.File.<init>(Unknown Source)
    at com.itextpdf.text.pdf.RandomAccessFileOrArray.<init>(RandomAccessFileOrArray.java:92)
    at com.itextpdf.text.pdf.RandomAccessFileOrArray.<init>(RandomAccessFileOrArray.java:80)
    at com.itextpdf.text.pdf.PRTokeniser.<init>(PRTokeniser.java:112)
    at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:171)
    at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:161)
    at org.HelloWorld.jobTicket.createPdf(jobTicket.java:41)
    at org.HelloWorld.FileDialogSWT$1.widgetSelected(FileDialogSWT.java:109)
    at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:240)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4165)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3754)
    at org.HelloWorld.FileDialogSWT.open(FileDialogSWT.java:55)
    at org.HelloWorld.FileDialogSWT.main(FileDialogSWT.java:40)


------------------------------------------------------------------------------
Special Offer -- Download ArcSight Logger for FREE!
Finally, a world-class log management solution at an even better
price-free! And you'll get a free "Love Thy Logs" t-shirt when you
download Logger. Secure your free ArcSight Logger TODAY!
http://p.sf.net/sfu/arcsisghtdev2dev
_______________________________________________
iText-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php
Reply | Threaded
Open this post in threaded view
|

Re: dialog to select pdf to import

iText mailing list
On 31/08/2011 2:15, joe smith wrote:
> I have a dialog that allows a user to select a file and then I place
> in into a PDF I am creating.
If you want an answer to your question, I suggest you rephrase it.
I fear very little people understand what you're asking.

"I have a dialog" => Where? In a browser? In an app? In a PDF?
A user can select a file => Where? From his operating system? From a
list of precanned files?
I place in a PDF I am creating => Huh? Are you talking about text? About
an image?
"In the examples to import a pdf you use C:\\myTest.pdf" => No, I don't!
What examples are you talking about?
"This works fine" => OK
"when I pass the selection made in the dialog" => Huh? How do you pass
the selection?
Are you passing bytes? are you passing a path? are you talking about a
file upload? I have no clue.
"it does not work" => Your question doesn't work either! Please fix your
question.
You introduce a class jobTicket, what is that all about?

------------------------------------------------------------------------------
Special Offer -- Download ArcSight Logger for FREE!
Finally, a world-class log management solution at an even better
price-free! And you'll get a free "Love Thy Logs" t-shirt when you
download Logger. Secure your free ArcSight Logger TODAY!
http://p.sf.net/sfu/arcsisghtdev2dev
_______________________________________________
iText-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php
Reply | Threaded
Open this post in threaded view
|

Re: dialog to select pdf to import

joe smith
OK I'll try again.

I've been working my way through the examples at  http://itextpdf.com/book/examples.php and part2.chapter06.ImportingPages1 for example.

What I have is a java application.
The dialog class which allows a user to select a pdf file to import and place into a new pdf document (FileDialogSWT.java). Once the files is selected the second class imports the selected pdf and places it scaled on a new pdf document (jobTicket.java). The issue I'm having is that on line 26 of the jobTicket.java if I comment out the 26 and use line 25 the application works. Now line 25 has public static final String SOURCE = "C:\\myTest.pdf"; SOURCE is a string clearly so on line 26 where I have public static final String SOURCE = p; I set and get p with:

private static String p = null;

    public static String getP() {
        return p;
    }
    public static void setP(String p){
        jobTicket.p = p;

In my dialog class I use
  jobTicket.setP((dlg.getFilterPath()+File.separatorChar+files[i].toString()).replaceAll("\\\\", "\\\\\\\\" )); to take the selected file and format it in the same way as line 25 which is "C:\\myTest.pdf"

when I use

String p = jobTicket.getP();
System.out.println("JT=" + p);


the console gives me JT=C:\\myTest.pdf so if p is a string and equals
"C:\\myTest.pdf"  then why doesn't  public static final String SOURCE = p; work like  public static final String SOURCE = "C:\\myTest.pdf";

I've added the code below if that helps.

FileDialogSWT.java:


package org.HelloWorld;

import java.io.File;

public class FileDialogSWT {

    protected Shell shell;
    // These filter names are displayed to the user in the file dialog. Note that
    // the inclusion of the actual extension in parentheses is optional, and
    // doesn't have any effect on which files are displayed.
    private static final String[] FILTER_NAMES = {
        "Acrobat Files (*.pdf)",
        "Postscript Files (*.ps)",
         "All Files (*.*)"};

    // These filter extensions are used to filter which files are displayed.
    private static final String[] FILTER_EXTS = { "*.pdf", "*.ps", "*.*"};
    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        try {
            FileDialogSWT window = new FileDialogSWT();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    /**
     * Create contents of the window.
     */
    protected void createContents() {
        shell = new Shell();
        shell.setSize(450, 300);
        shell.setText("SWT Application");
       
        Button btnOpen = new Button(shell, SWT.NONE);
       
        btnOpen.setBounds(10, 10, 68, 23);
        btnOpen.setText("Open");
       
        ListViewer listViewer = new ListViewer(shell, SWT.BORDER | SWT.V_SCROLL);
        final List list = listViewer.getList();
        list.setBounds(10, 39, 422, 187);
       
        Button btnCancel = new Button(shell, SWT.NONE);
        btnCancel.setBounds(364, 240, 68, 23);
        btnCancel.setText("Cancel");
       
        Button btnOK = new Button(shell, SWT.NONE);
        btnOK.setBounds(290, 240, 68, 23);
        btnOK.setText("OK");
       
        btnOpen.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                // User has selected to open multiple files
                  FileDialog dlg = new FileDialog(shell, SWT.MULTI);
                  dlg.setFilterNames(FILTER_NAMES);
                  dlg.setFilterExtensions(FILTER_EXTS);
                  String fn = dlg.open();
                  if (fn != null) {
                    // Append all the selected files. Since getFileNames() returns only
                    // the names, and not the path, prepend the path, normalizing
                    // if necessary
                    //StringBuffer buf = new StringBuffer();
                    String[] files = dlg.getFileNames();
                    for (int i = 0, n = files.length; i < n; i++) {
                        list.add(dlg.getFilterPath()+File.separatorChar+files[i].toString());
                        System.out.println(dlg.getFilterPath()+File.separatorChar+files[i].toString());
                        //jobTicket.setP((dlg.getFilterPath()+File.separatorChar+files[i].toString()).replaceAll("\\\\", "/" ));
                        jobTicket.setP((dlg.getFilterPath()+File.separatorChar+files[i].toString()).replaceAll("\\\\", "\\\\\\\\" ));
                        String p = jobTicket.getP();
                        System.out.println("JT=" + p);
                        try {
                            jobTicket.createPdf(files[i].toString());
                        } catch (DocumentException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                       
                       
//                      buf.append(dlg.getFilterPath());
//                      if (buf.charAt(buf.length() - 1) != File.separatorChar) {
//                        buf.append(File.separatorChar);
//                      }
//                      buf.append(files[i]);
//                      buf.append(" ");
                    }
//                    list.add(buf.toString());
                  }       
           
            }
        });
        btnOK.addListener(SWT.Selection, new Listener() {
           
            @Override
            public void handleEvent(Event arg0) {
                list.selectAll();
                int ftxt = list.getItemCount();
                System.out.println(ftxt);
                String[] tlist = list.getItems();
                System.out.println(tlist[0]);
            }
        });
       
        btnCancel.addListener(SWT.Selection, new Listener() {
              public void handleEvent(Event event) {
                //value = null;
                shell.dispose();
                System.out.println("Cancel");
              }
            });
       
       

    }
}

jobTicket.java:

package org.HelloWorld;

import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;

public class jobTicket {
   
    private static String p = null;

    public static String getP() {
        return p;
    }
    public static void setP(String p){
        jobTicket.p = p;

    }
    //public static final String SOURCE = "C:/myTest.pdf";
   // public static final String SOURCE = "C:\\myTest.pdf";
   public static final String SOURCE = p;                                  ////<-------------------------line 26

     /**
     * Creates a PDF document.
     * @param filename the path to the new PDF document
     * @throws    DocumentException
     * @throws    IOException
     */
    public static void createPdf(String filename) throws DocumentException, IOException {
       
        PdfReader reader = new PdfReader(SOURCE);
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(System.getProperty("user.home") + "/Desktop/JobTic_"+filename));
        // step 3
        document.open();
        // step 4      
       
        PdfContentByte cb = writer.getDirectContent();
        PdfImportedPage page = writer.getImportedPage(reader, 1); //page #1
       
        if(reader.getPageSize(1).getWidth() > reader.getPageSize(1).getHeight()){
            float Scale = 560/reader.getPageSize(1).getWidth();
            cb.addTemplate(page, Scale, 0, 0, Scale, 18, 18);
        }else{
            float Scale = 680/reader.getPageSize(1).getHeight();
            cb.addTemplate(page, Scale, 0, 0, Scale, 18, 18);
        }
       
     
        document.close();
    }
   
  
}








> Date: Wed, 31 Aug 2011 09:20:43 +0200

> From: [hidden email]
> To: [hidden email]
> Subject: Re: [iText-questions] dialog to select pdf to import
>
> On 31/08/2011 2:15, joe smith wrote:
> > I have a dialog that allows a user to select a file and then I place
> > in into a PDF I am creating.
> If you want an answer to your question, I suggest you rephrase it.
> I fear very little people understand what you're asking.
>
> "I have a dialog" => Where? In a browser? In an app? In a PDF?
> A user can select a file => Where? From his operating system? From a
> list of precanned files?
> I place in a PDF I am creating => Huh? Are you talking about text? About
> an image?
> "In the examples to import a pdf you use C:\\myTest.pdf" => No, I don't!
> What examples are you talking about?
> "This works fine" => OK
> "when I pass the selection made in the dialog" => Huh? How do you pass
> the selection?
> Are you passing bytes? are you passing a path? are you talking about a
> file upload? I have no clue.
> "it does not work" => Your question doesn't work either! Please fix your
> question.
> You introduce a class jobTicket, what is that all about?
>
> ------------------------------------------------------------------------------
> Special Offer -- Download ArcSight Logger for FREE!
> Finally, a world-class log management solution at an even better
> price-free! And you'll get a free "Love Thy Logs" t-shirt when you
> download Logger. Secure your free ArcSight Logger TODAY!
> http://p.sf.net/sfu/arcsisghtdev2dev
> _______________________________________________
> iText-questions mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/itext-questions
>
> iText(R) is a registered trademark of 1T3XT BVBA.
> Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/
> Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php

------------------------------------------------------------------------------
Special Offer -- Download ArcSight Logger for FREE!
Finally, a world-class log management solution at an even better
price-free! And you'll get a free "Love Thy Logs" t-shirt when you
download Logger. Secure your free ArcSight Logger TODAY!
http://p.sf.net/sfu/arcsisghtdev2dev
_______________________________________________
iText-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php
Reply | Threaded
Open this post in threaded view
|

Re: dialog to select pdf to import

iText mailing list
Hello,
as I suspected, this question is off-topic on the mailing list.

On 1/09/2011 1:02, joe smith wrote:
I comment out the 26 and use line 25 the application works. Now line 25 has public static final String SOURCE = "C:\\myTest.pdf"; SOURCE is a string clearly so on line 26 where I have public static final String SOURCE = p; I set and get p with:

private static String p = null;

    public static String getP() {
        return p;
    }
    public static void setP(String p){
        jobTicket.p = p;

In my dialog class I use
  jobTicket.setP((dlg.getFilterPath()+File.separatorChar+files[i].toString()).replaceAll("\\\\", "\\\\\\\\" )); to take the selected file and format it in the same way as line 25 which is "C:\\myTest.pdf"

You define a "static FINAL String"; FINAL means that you can't change its value. When the JVM starts, you assign the value of p, the value of p = null, so SOURCE = null.

You may try to change the value of p all you want, but the value of SOURCE will remain null.
Hence the NullPointerException. This is a Java programming problem, not an iText problem!

------------------------------------------------------------------------------
Special Offer -- Download ArcSight Logger for FREE!
Finally, a world-class log management solution at an even better
price-free! And you'll get a free "Love Thy Logs" t-shirt when you
download Logger. Secure your free ArcSight Logger TODAY!
http://p.sf.net/sfu/arcsisghtdev2dev
_______________________________________________
iText-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php