Example usage for org.apache.pdfbox.pdmodel PDDocument close

List of usage examples for org.apache.pdfbox.pdmodel PDDocument close

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel PDDocument close.

Prototype

@Override
public void close() throws IOException 

Source Link

Document

This will close the underlying COSDocument object.

Usage

From source file:PDF.PDFTest.java

License:Apache License

public static void constrainText(String start, String end, File file) throws IOException {
    PDDocument doc = PDDocument.load(file);
    PDFTextStripper stripper = new PDFTextStripper();
    String text = stripper.getText(doc); // we get the text of the entire
    // document into a String
    String[] split_on_start = text.split(start); // split on the start
    // parameter, take upper
    // bound//  w  w w . j  av a  2s  .  c  o  m
    String[] split_on_end = split_on_start[1].split(end); // split on end
    // parameter,
    // take lower
    // bound
    String constrained_string = start;
    constrained_string += split_on_end[0]; // the final string will be the
    // area in between start and end
    doc.close();
    System.out.print(constrained_string);
}

From source file:pdf.PDFUtils.java

License:Open Source License

public static void showPreviewFrame(File pdfFile) throws IOException {
    PDDocument pdd = PDDocument.load(pdfFile);
    JPanel panel = createPanelWithAllPages(pdd);

    final JScrollPane scroll = new JScrollPane(panel);

    JFrame frame = new JFrame("Preview");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());

    frame.add(scroll, BorderLayout.CENTER);
    frame.setExtendedState(frame.getExtendedState() | Frame.MAXIMIZED_BOTH);
    frame.setVisible(true);/*from  w  w  w .j a v  a2s  .  c  o  m*/

    pdd.close();
}

From source file:pdf.PDFWindow.java

private void jButtonExtractActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonExtractActionPerformed
    // TODO add your handling code here:
    int from = (int) fromPage.getValue();
    int to = (int) toPage.getValue();
    PDDocument extract = Edit.split(actualFile, from, to);
    try {//from   ww w .  j  a v  a  2 s . c  o  m
        saveFile(extract);
    } catch (IOException ex) {
        Logger.getLogger(PDFWindow.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        extract.close();
    } catch (IOException ex) {
        Logger.getLogger(PDFWindow.class.getName()).log(Level.SEVERE, null, ex);
    }
    FrameJoin.setVisible(false);
}

From source file:pdf.PJpdf.java

public static void main(String[] argsv) throws IOException {
    PDDocument pd;
    BufferedWriter wr;/*  w w  w  .j av a2  s.co m*/
    try {
        File input = new File("Huober-2012-Higher-efficacy-of-l.pdf");
        File output = new File("SampleText.txt");
        pd = PDDocument.load(input);
        PDFTextStripper stripper = new PDFTextStripper();
        wr = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output)));
        stripper.writeText(pd, wr);
        if (pd != null) {
            pd.close();
        }
        wr.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

    BufferedReader br = new BufferedReader(new FileReader(new File("SampleText.txt")));
    String line = "";
    while ((line = br.readLine()) != null) {

        if (isUTF8MisInterpreted(line)) {
            System.out.println(line);
        }
    }

}

From source file:pdf.PrintKeyValue.java

public static void main(String argsv[]) throws FileNotFoundException, IOException, ParseException {
    HashMap<String, Integer> pKey = new HashMap<String, Integer>();
    HashMap<String, Integer> mKey = new HashMap<String, Integer>();
    HashMap<String, Integer> oKey = new HashMap<String, Integer>();
    HashMap<String, Integer> iKey = new HashMap<String, Integer>();
    //results_cochrane_HF.json
    System.out.println("reading");
    String fileName = "firstFile.json";
    //        String fileName = "results_cochrane_HF.json";
    FileReader fileReader = new FileReader(fileName);
    List<String> orderL = new ArrayList<String>();
    // Always wrap FileReader in BufferedReader.
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    String line = "";

    StringBuilder str = new StringBuilder();
    while ((line = bufferedReader.readLine()) != null) {
        str = str.append(line + " ");
        if (line.contains("\"Methods\":"))
            orderL.add("Methods");
        else if (line.contains("\"Interventions\":"))
            orderL.add("Interventions");
        else if (line.contains("\"Notes\":"))
            orderL.add("Notes");
        else if (line.contains("\"Outcomes\":"))
            orderL.add("Outcomes");
        else if (line.contains("\"Participants\":"))
            orderL.add("Participants");
        else if (line.contains("\"Label\":"))
            orderL.add("Label");
        else if (line.contains("\"Filename\":"))
            orderL.add("Filename");

    }/*from w w  w.j a  v  a2s  .  c  om*/
    String jsonText = str.toString();
    System.out.println("Done");

    JSONParser parser = new JSONParser();
    KeyFinder finder = new KeyFinder();

    //List<String> getValues(String lookFor,String jsonText)

    List<String> MethodsL = getValues("Methods", jsonText);
    List<String> InterventionsL = getValues("Interventions", jsonText);
    List<String> NotesL = getValues("Notes", jsonText);
    List<String> OutcomesL = getValues("Outcomes", jsonText);
    List<String> ParticipantsL = getValues("Participants", jsonText);
    List<String> LabelL = getValues("Label", jsonText);
    List<String> FilenameL = getValues("Filename", jsonText);

    List<fName> fnL = new ArrayList<fName>();
    List<studies> stuL = new ArrayList<studies>();
    studies currSt = new studies();
    fName currFN = new fName();
    for (String ptr : orderL) {
        if (ptr.equalsIgnoreCase("Methods")) {
            currSt.method = MethodsL.get(0);
            MethodsL.remove(0);
        } else if (ptr.equalsIgnoreCase("Interventions")) {
            currSt.interventions = InterventionsL.get(0);
            InterventionsL.remove(0);
        } else if (ptr.equalsIgnoreCase("Notes")) {
            currSt.notes = NotesL.get(0);
            NotesL.remove(0);
        } else if (ptr.equalsIgnoreCase("Outcomes")) {
            currSt.outcomes = OutcomesL.get(0);
            OutcomesL.remove(0);
        } else if (ptr.equalsIgnoreCase("Participants")) {
            currSt.participants = ParticipantsL.get(0);
            ParticipantsL.remove(0);
        } else if (ptr.equalsIgnoreCase("Label")) {
            currSt.label = LabelL.get(0);
            LabelL.remove(0);
            stuL.add(currSt);
            currSt = new studies();
        } else if (ptr.equalsIgnoreCase("Filename")) {
            currFN.fileName = FilenameL.get(0);
            FilenameL.remove(0);
            currFN.stuL = stuL;
            fnL.add(currFN);
            stuL = new ArrayList<studies>();
            currFN = new fName();
        }
    }

    for (fName tempFN : fnL) {
        //tempFN.printFName();
        // Write the PDF file text to a text file
        PDDocument pd;
        BufferedWriter wr;
        try {
            File input = new File(tempFN.fileName);
            File output = new File("SampleText.txt");
            pd = PDDocument.load(input);
            PDFTextStripper stripper = new PDFTextStripper();
            wr = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output)));
            stripper.writeText(pd, wr);
            if (pd != null) {
                pd.close();
            }
            wr.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

        // Read the text file and start processing
        boolean meth = false;
        boolean part = false;
        boolean inter = false;
        boolean out = false;
        boolean not = false;
        boolean lab = false;

        for (int i = 0; i < tempFN.stuL.size(); i++) {
            BufferedReader br = new BufferedReader(new FileReader(new File("SampleText.txt")));
            line = "";
            while ((line = br.readLine()) != null) {
                if (line.equalsIgnoreCase(tempFN.stuL.get(i).label)) {
                    lab = true;
                }
                if (i + 1 < tempFN.stuL.size()) {
                    if (line.equalsIgnoreCase(tempFN.stuL.get(i + 1).label)) {
                        lab = false;
                        break;
                    }
                }
                if (lab) {
                    if (line.startsWith("Methods ")) {
                        meth = true;
                        part = false;
                        inter = false;
                        out = false;
                        not = false;
                        String find = line.substring(line.indexOf(" ") + 1, line.length());
                        String nL = tempFN.stuL.get(i).method;
                        try {
                            //                                System.out.println("oldM " + nL);
                            //                                System.out.println("find " + find);
                            if (nL.contains(find))
                                tempFN.stuL.get(i).method = new StringBuffer(nL)
                                        .insert(nL.indexOf(find) + find.length(), "<>").toString();
                            //                                System.out.println("newL "+ tempFN.stuL.get(i).method);
                        } catch (Exception ex) {
                            System.out.println("** Error **");
                            System.out.println("Find - " + find);
                            System.out.println("nL - " + nL);
                        }

                    } else if (line.startsWith("Participants ")) {
                        part = true;
                        meth = false;
                        inter = false;
                        out = false;
                        not = false;
                        String find = line.substring(line.indexOf(" ") + 1, line.length());
                        String nL = tempFN.stuL.get(i).participants;
                        try {
                            if (nL.contains(find))
                                tempFN.stuL.get(i).participants = new StringBuffer(nL)
                                        .insert(nL.indexOf(find) + find.length(), "<>").toString();

                        } catch (Exception ex) {
                            System.out.println("** Error **");
                            System.out.println("Find - " + find);
                            System.out.println("nL - " + nL);
                        }
                    } else if (line.startsWith("Interventions ")) {
                        meth = false;
                        part = false;
                        inter = true;
                        out = false;
                        not = false;
                        String find = line.substring(line.indexOf(" ") + 1, line.length());
                        String nL = tempFN.stuL.get(i).interventions;
                        try {
                            if (nL.contains(find))
                                tempFN.stuL.get(i).interventions = new StringBuffer(nL)
                                        .insert(nL.indexOf(find) + find.length(), "<>").toString();
                        } catch (Exception ex) {
                            System.out.println("** Error **");
                            System.out.println("Find - " + find);
                            System.out.println("nL - " + nL);
                        }

                    } else if (line.startsWith("Outcomes ")) {
                        meth = false;
                        part = false;
                        inter = false;
                        out = true;
                        not = false;
                        String find = line.substring(line.indexOf(" ") + 1, line.length());
                        String nL = tempFN.stuL.get(i).outcomes;
                        try {
                            if (nL.contains(find))
                                tempFN.stuL.get(i).outcomes = new StringBuffer(nL)
                                        .insert(nL.indexOf(find) + find.length(), "<>").toString();
                        } catch (Exception ex) {
                            System.out.println("** Error **");
                            System.out.println("Find - " + find);
                            System.out.println("nL - " + nL);
                        }

                    } else if (line.startsWith("Notes ")) {
                        meth = false;
                        part = false;
                        inter = false;
                        out = false;
                        not = true;
                    }
                    if (meth) {

                        String find = line;
                        String nL = tempFN.stuL.get(i).method;
                        if (nL.contains(find)) {
                            //                                System.out.println("oldM "+ nL);
                            //                                System.out.println("find "+ find);
                            tempFN.stuL.get(i).method = new StringBuffer(nL)
                                    .insert(nL.indexOf(find) + find.length(), "<>").toString();
                            //                                System.out.println("newM "+ tempFN.stuL.get(i).method);
                        }
                    } else if (part) {
                        String find = line;
                        String nL = tempFN.stuL.get(i).participants;
                        if (nL.contains(find)) {
                            tempFN.stuL.get(i).participants = new StringBuffer(nL)
                                    .insert(nL.indexOf(find) + find.length(), "<>").toString();
                        }
                    } else if (out) {
                        String find = line;
                        String nL = tempFN.stuL.get(i).outcomes;
                        if (nL.contains(find)) {
                            tempFN.stuL.get(i).outcomes = new StringBuffer(nL)
                                    .insert(nL.indexOf(find) + find.length(), "<>").toString();
                        }
                    } else if (inter) {
                        String find = line;
                        String nL = tempFN.stuL.get(i).interventions;
                        if (nL.contains(find)) {
                            tempFN.stuL.get(i).interventions = new StringBuffer(nL)
                                    .insert(nL.indexOf(find) + find.length(), "<>").toString();
                        }
                    }
                }
            }
            br.close();
        }

        //tempFN.printFName();
    }

    //Printing the inner Keys and Values
    System.out.println("\n\n%$ Par %$\n\n");
    for (fName tempFN : fnL) {
        for (int i = 0; i < tempFN.stuL.size(); i++) {
            iKeyValues(tempFN.stuL.get(i).participants.replaceAll(";", "<>"));
            printIK();
            set = new ArrayList<String>();
        }

    }
    System.out.println("\n\n%$ out %$\n\n");
    for (fName tempFN : fnL) {
        for (int i = 0; i < tempFN.stuL.size(); i++) {
            iKeyValues(tempFN.stuL.get(i).outcomes);
            printIK();
            set = new ArrayList<String>();
        }
    }
    System.out.println("\n\n%$ met %$\n\n");
    for (fName tempFN : fnL) {
        for (int i = 0; i < tempFN.stuL.size(); i++) {
            iKeyValues(tempFN.stuL.get(i).method);
            printIK();
            set = new ArrayList<String>();
        }
    }
    System.out.println("\n\n%$ int %$\n\n");
    for (fName tempFN : fnL) {
        for (int i = 0; i < tempFN.stuL.size(); i++) {
            iKeyValues(tempFN.stuL.get(i).interventions);
            printIK();
            set = new ArrayList<String>();
        }
    }
    //Remove this if you don't want Inner Keys
    //Removed start of comment delimeter here

    // Printing in console
    System.out.println("First File. json Starts here ");

    for (fName tempFN : fnL) {
        //tempFN.printFName();
        System.out.println("\n=====\n");
        for (int i = 0; i < tempFN.stuL.size(); i++) {
            System.out.println("participants");
            System.out.println(tempFN.stuL.get(i).participants);
            printKeyValues(tempFN.stuL.get(i).participants, tempFN.stuL.get(i).pKeyV);
            System.out.println("\n++++++\n");
            System.out.println("outcomes");
            System.out.println(tempFN.stuL.get(i).outcomes);
            printKeyValues(tempFN.stuL.get(i).outcomes, tempFN.stuL.get(i).oKeyV);
            System.out.println("\n++++++\n");
            System.out.println("methods");
            System.out.println(tempFN.stuL.get(i).method);
            printKeyValues(tempFN.stuL.get(i).method, tempFN.stuL.get(i).mKeyV);
            System.out.println("\n++++++\n");
            System.out.println("interventions");
            System.out.println(tempFN.stuL.get(i).interventions);
            printKeyValues(tempFN.stuL.get(i).interventions, tempFN.stuL.get(i).iKeyV);
            System.out.println("\n++++++\n");
        }

        //            System.out.println("+++");
        //fillValues(tempFN.stuL.get(0).participants,tempFN.stuL.get(0).pKeyV);
    }

    //Printing in Data Structure
    for (fName tempFN : fnL) {
        //tempFN.printFName();
        for (int i = 0; i < tempFN.stuL.size(); i++) {
            fillValues(tempFN.stuL.get(i).participants, tempFN.stuL.get(i).pKeyV, "Participants", pKey);
            fillValues(tempFN.stuL.get(i).outcomes, tempFN.stuL.get(i).oKeyV, "Outcomes", oKey);
            fillValues(tempFN.stuL.get(i).method, tempFN.stuL.get(i).mKeyV, "Methods", mKey);
            fillValues(tempFN.stuL.get(i).interventions, tempFN.stuL.get(i).iKeyV, "Intervention", iKey);
        }
    }

    //Calculating keys and printing frequency
    List<Key> temp = new ArrayList<Key>();
    printFreqMap("Participants", pKey, temp);
    printFreqMap("Outcomes", oKey, temp);
    printFreqMap("Methods", mKey, temp);
    printFreqMap("Intervention", iKey, temp);

    //Grouping Keys
    System.out.println("\n*** Grouping ***\n");

    groupKeys gk = new groupKeys();
    temp = gk.gKeys(temp);

    System.out.println("\n\n*&^ Printing &$*\n\n");
    System.out.println("Key\tFrequency\tCategory");
    for (Key kk : temp) {
        StringBuilder sb = new StringBuilder(kk.name + "\t" + kk.freq + "\t" + kk.category + "\t");
        if (!kk.gKey.isEmpty()) {
            for (Key kx : kk.gKey)
                sb.append(kx.name + " ,");
            sb.deleteCharAt(sb.length() - 1);
            System.out.println(sb.toString());
        } else {
            System.out.println(sb.toString());
        }
    }
    // Just method to check if everything is parsed
    if ((MethodsL.size() + InterventionsL.size() + OutcomesL.size() + ParticipantsL.size()
            + FilenameL.size()) != 0)
        System.out.println("ERROR : Something not parsed");

    //Removed end of comment delimeter here
    //Remove this if you don't want Inner Keys
}

From source file:pdf.StockItemsPDF.java

public void createStockItemsPNG(String path) throws IOException {
    File file = new File(path);
    PDDocument doc = PDDocument.load(file);
    PDFRenderer renderer = new PDFRenderer(doc);
    BufferedImage image = renderer.renderImage(0);
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy.");
    ImageIO.write(image, "PNG",
            new File("pdf_docs\\stock_items_picture\\" + "sipic-" + sdf.format(dateOfWriteOff) + ".png"));
    doc.close();
}

From source file:pdf.StockItemsPDF.java

public String createStockItems() throws IOException {
    PDDocument document = PDDocument.load(template);
    //prva strana
    PDPage page = document.getPage(0);//  ww  w. ja v  a2 s  . co  m
    contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false);

    this.insertProducts();
    this.stockItemsInfo();
    contentStream.close();
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy.");
    String path = "pdf_docs\\stock_items\\" + "stock_item_" + dateOfWriteOff.getTime() + ".pdf";
    document.save(path);
    //createStockItemsPNG(path);
    document.close();

    return path;
}

From source file:pdf.SuppliesListPDF.java

public void createDailyReportPNG(String path) throws IOException {
    File file = new File(path);
    PDDocument doc = PDDocument.load(file);
    PDFRenderer renderer = new PDFRenderer(doc);
    BufferedImage image = renderer.renderImage(0);
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy.");
    ImageIO.write(image, "PNG",
            new File("pdf_docs\\lager_lists_picture\\" + "llpic-" + dateSuppliesList.getTime() + ".png"));
    doc.close();
}

From source file:pdf.SuppliesListPDF.java

public String createSuppliesList() throws IOException {
    PDDocument document = PDDocument.load(template);
    //prva strana
    PDPage page = document.getPage(0);//from  ww w  .  j a v a2  s  .  c o  m
    contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, false);

    this.insertSupplies();
    this.suppliesListInfo();
    contentStream.close();
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy.");
    String path = "pdf_docs\\lager_lists\\" + "supplies_list_" + dateSuppliesList.getTime() + ".pdf";
    document.save(path);
    //createDailyReportPNG(path);
    document.close();
    return path;

}

From source file:pdf.WorkOrderPDF.java

public static void main(String[] args) throws IOException {
    //DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    //System.out.println(dtf.format(java.time.LocalDateTime.now()));;

    File file = new File("pdf_docs\\work_orders\\work_order_template.pdf");

    PDDocument doc = PDDocument.load(file);
    PDFRenderer renderer = new PDFRenderer(doc);
    BufferedImage image = renderer.renderImage(0);
    ImageIO.write(image, "PNG", new File("pdf_docs\\work_orders_picture\\" + "wopic.png"));
    doc.close();
}