Create pdf files with pdBox

Asked

Viewed 28 times

0

Good afternoon,

I am trying to create a PDF file with pdBox, recording in it what he finds in the matcher of my application, but the created pdf comes out blank someone could help me in this doubt?

PDFParser parser = new PDFParser(new RandomAccessBufferedFileInputStream(f.getAbsolutePath()));
                    parser.parse();
                    COSDocument cosDoc = parser.getDocument();
                    PDFTextStripper pdfStripper = new PDFTextStripper();
                    PDDocument pdDoc = new PDDocument(cosDoc);
                    PrintWriter arq = new PrintWriter(new File("C:\\Tratados\\" + f.getName())); //Diretório de saida
                    //PrintWriter gravarArq = new PrintWriter(arq);
                    List<String> linhasGravadas = new ArrayList<>();

                    for (int i = 1; i <= pdDoc.getNumberOfPages(); i++) {
                        pdfStripper.setStartPage(i);
                        pdfStripper.setEndPage(i);
                        String parsedText = pdfStripper.getText(pdDoc);
                        String aut = "";
                        String hora = "";
                        String data = "";
                        int Aut = 0, Data = 0, Hora = 0;
                        System.out.println("Pagina: " + i);
                        arq.println("Pagina: " + i);
                        Matcher matcherData = Pattern.compile("([0-9]{2}\\/[0-9]{2}\\/[0-9]{4})"/*"\\b\\d{8}\\b"*/).matcher(parsedText);
                        Matcher matcherHora = Pattern.compile("\\d{2}[:|;]\\d{2}[:|;]\\d{2}").matcher(parsedText);
                        Matcher matcherAut = Pattern.compile(/*"\\s\\b\\d{9}\\b|*/"\\b\\d{9}\\b").matcher(parsedText);
                        int vAut = 0, vData = 0, vHora = 0;

                        if (!matcherAut.find()) {
                           System.out.println("Autorização: Não encontrado");
                           //arq.println("Autorização: Não encontrado");
                        } else {
                            //while (matcherAut.find()) {
                            Pattern pattern = Pattern.compile("\\s^[0126789].*|^[0126789].*"); // Segundo Filtro (Elimina os que não começam com 3|4)
                            Scanner scanner = new Scanner(matcherAut.group()).useDelimiter(pattern);
                            Matcher matcher2 = pattern.matcher(aut);
                            while (scanner.hasNext()) {
                                aut = scanner.next();
                                if (!linhasGravadas.contains(aut)) {
                                    System.out.println("Autorização: " + matcherAut.group());
                                    arq.println("Autorização: " + matcherAut.group());
                                    Aut = Aut + 1;
                                }
                            }
                            //}
                        }
  • This article can help you work with pdBox: https://drive.google.com/file/d/0BzcLdUlVTrBtMzAwNzc4ODYtNjdjZS00MTQ5LTg2YTgtZjI2MTdlNDEzYWFm/view?pli=1

  • The problem is that from what I saw the link only shows how to extract data and not how to create a file

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.