0
votes

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

1
Can you share the result PDF that you get with itext? - Tilman Hausherr
@TilmanHausherr Added file view in question. Also, this is the link for that file: drive.google.com/file/d/1UShCgqx1Kyx40C7v9ehU-KdMFnYxlKWn/… - Bhailu
Yes I need the file itself. The screenshot doesn't have a "print" field so I wonder if I have misunderstood the question. - Tilman Hausherr
As an aside: In your iText related code you use a PdfCopy where a PdfStamper would have created a more faithful result more economically. - mkl

1 Answers

1
votes

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);