Bhailu
Bhailu

Reputation: 45

PDFBox Open PDF file into new browser tab

I am using the pdfbox library 2.0 version. I need to open PDF in new browser tab i.e. Print View.
As if we are migrating from iText to PDFBox below is the existing code with iText.

With below code, there is PDFAction class to achieve same. It is,

PdfAction action = new PdfAction(PdfAction.PRINTDIALOG);

and to apply print Javascript on doc,

copy.addJavaScript(action);

I need equivalent solution with PDFBox.

        Document document = new Document();
        try{
            outputStream=response.getOutputStream();
            // step 2
            PdfCopy copy = new PdfCopy(document, outputStream);
            
            // step 3
            document.open();
            // step 4
            PdfReader reader;
            int n;
            
            //add print dialog in Pdf Action to open file for preview. 
            PdfAction action = new PdfAction(PdfAction.PRINTDIALOG);
            
            // loop over the documents you want to concatenate
            Iterator i=mergepdfFileList.iterator();
            while(i.hasNext()){
                File f =new File((String)i.next());
                
                is=new FileInputStream(f);
                reader=new PdfReader(is);
                n = reader.getNumberOfPages();
                for (int page = 0; page < n; ) {
                    copy.addPage(copy.getImportedPage(reader, ++page));
                }
                copy.freeReader(reader);
                reader.close();
                is.close();
            }
            copy.addJavaScript(action);
            
            // step 5
            document.close();
            
        }catch(IOException io){
            
            throw io;
        }catch(DocumentException e){
            
            throw e;
        }catch(Exception e){
            
            throw e;
        }finally{           
            outputStream.close();
        }

I also tried with below reference but could not find print() method of PDDocument type.

Reference Link

Please guide me with this.

This is how file looks when display in browser tab: File View with iText

Upvotes: 0

Views: 792

Answers (1)

Tilman Hausherr
Tilman Hausherr

Reputation: 18926

This code reproduces what your file has, a JavaScript action in the name tree in the JavaScript entry in the name dictionary in the document catalog. ("When the document is opened, all of the actions in this name tree shall be executed, defining JavaScript functions for use by other scripts in the document" - PDF specification) There's probably an easier way to do this, e.g. with an OpenAction.

PDActionJavaScript javascript = new PDActionJavaScript("this.print(true);\n");
PDDocumentCatalog documentCatalog = document.getDocumentCatalog();
PDDocumentNameDictionary names = new PDDocumentNameDictionary(documentCatalog, new COSDictionary());
PDJavascriptNameTreeNode javascriptNameTreeNode = new PDJavascriptNameTreeNode();
Map<String, PDActionJavaScript> map = new HashMap<>();
map.put("0000000000000000", javascript);
javascriptNameTreeNode.setNames(map);
names.setJavascript(javascriptNameTreeNode);
document.getDocumentCatalog().setNames(names);

Upvotes: 1

Related Questions