List of usage examples for com.itextpdf.text Document open
boolean open
To view the source code for com.itextpdf.text Document open.
Click Source Link
From source file:com.tomasz.drag.triballocommanderro.controller.MakerPDF.java
public static void printToPDFBoss(ArrayList<Person> workers, EventGig event) throws IOException, DocumentException { String filename = event.getName() + " Boss" + ".pdf"; String path = event.getData() + " " + event.getName() + "//"; //Rectangle rect = new Rectangle(0, 595, 8, 0); Document document = new Document(PageSize.A4.rotate(), 0, 0, 0, 0); //Document document = new Document(rect); //document.setMargins(0, 0, 0, 0); BaseFont bf;//from w w w .j a v a 2 s.c o m //BaseFont bf2; // bf = BaseFont.createFont("arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); bf = BaseFont.createFont("src\\main\\resources\\font\\arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); //bf2 = BaseFont.createFont("src\\main\\resources\\font\\arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); com.itextpdf.text.Font ft = new com.itextpdf.text.Font(bf, 9); //com.itextpdf.text.Font ft2 = new com.itextpdf.text.Font(bf2,9); PdfWriter.getInstance(document, new FileOutputStream(path + filename)); document.open(); Paragraph p = new Paragraph(event.getName() + " " + event.getData(), ft); p.setAlignment(Element.ALIGN_CENTER); document.add(p); document.add(new Paragraph(" ")); PdfPTable table = new PdfPTable(workers.size() + 1); table.setWidthPercentage(98); PdfPCell cell = new PdfPCell(new Phrase(" ")); table.addCell(cell); for (Person tempPerson : workers) { cell = new PdfPCell(new Paragraph(tempPerson.getName(), ft)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(cell); } if (event.isZaladunekScenaDach()) { cell = new PdfPCell(new Paragraph("Za. Sc./Dach", ft)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(cell); for (Person tempPerson : workers) { if (tempPerson.getZaladunekScenaDachWyplata() > 0) { cell = new PdfPCell( new Paragraph(String.format("%.2f", tempPerson.getZaladunekScenaDachWyplata()), ft)); cell.setVerticalAlignment(Element.ALIGN_CENTER); table.addCell(cell); } else { cell = new PdfPCell(new Phrase(" ")); table.addCell(cell); } } } if (event.isZaladunekTechniki()) { cell = new PdfPCell(new Phrase("Za. Tech.", ft)); table.addCell(cell); for (Person tempPerson : workers) { if (tempPerson.getZaladunekTechnikiWyplata() > 0) { cell = new PdfPCell( new Paragraph(String.format("%.2f", tempPerson.getZaladunekTechnikiWyplata()), ft)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(cell); } else { cell = new PdfPCell(new Phrase(" ")); table.addCell(cell); } } } if (event.isRozladunekScenaDach()) { cell = new PdfPCell(new Phrase("Roz Sc/Dach", ft)); table.addCell(cell); for (Person tempPerson : workers) { if (tempPerson.getRozladunekScenaDachWyplata() > 0) { cell = new PdfPCell( new Phrase(String.format("%.2f", tempPerson.getRozladunekScenaDachWyplata()), ft)); table.addCell(cell); } else { cell = new PdfPCell(new Phrase(" ")); table.addCell(cell); } } } if (event.isRozladunekTechniki()) { cell = new PdfPCell(new Phrase("Roz. Tech.", ft)); table.addCell(cell); for (Person tempPerson : workers) { if (tempPerson.getRozladunekTechnikiWyplata() > 0) { cell = new PdfPCell( new Phrase(String.format("%.2f", tempPerson.getRozladunekTechnikiWyplata()), ft)); table.addCell(cell); } else { cell = new PdfPCell(new Phrase(" ")); table.addCell(cell); } } } if (event.isMontazDachu()) { cell = new PdfPCell(new Phrase("Mon. Dach", ft)); table.addCell(cell); for (Person tempPerson : workers) { if (tempPerson.getMontazDachuWyplata() > 0) { cell = new PdfPCell(new Phrase(String.format("%.2f", tempPerson.getMontazDachuWyplata()), ft)); table.addCell(cell); } else { cell = new PdfPCell(new Phrase(" ")); table.addCell(cell); } } } if (event.isMontazDzwieku()) { cell = new PdfPCell(new Phrase("Mon. Dw.", ft)); table.addCell(cell); for (Person tempPerson : workers) { if (tempPerson.getMontazDzwiekuWyplata() > 0) { cell = new PdfPCell( new Phrase(String.format("%.2f", tempPerson.getMontazDzwiekuWyplata()), ft)); table.addCell(cell); } else { cell = new PdfPCell(new Phrase(" ")); table.addCell(cell); } } } if (event.isMontazSceny()) { cell = new PdfPCell(new Phrase("Mon. Sc.", ft)); table.addCell(cell); for (Person tempPerson : workers) { if (tempPerson.getMontazScenyWyplata() > 0) { cell = new PdfPCell(new Phrase(String.format("%.2f", tempPerson.getMontazScenyWyplata()), ft)); table.addCell(cell); } else { cell = new PdfPCell(new Phrase(" ")); table.addCell(cell); } } } if (event.isMontazSwiatla()) { cell = new PdfPCell(new Phrase("Mon. w.", ft)); table.addCell(cell); for (Person tempPerson : workers) { if (tempPerson.getMontazSwiatlaWyplata() > 0) { cell = new PdfPCell( new Phrase(String.format("%.2f", tempPerson.getMontazSwiatlaWyplata()), ft)); table.addCell(cell); } else { cell = new PdfPCell(new Phrase(" ")); table.addCell(cell); } } } if (event.isMontazTechniki()) { cell = new PdfPCell(new Phrase("Mon. Tech.", ft)); table.addCell(cell); for (Person tempPerson : workers) { if (tempPerson.getMontazTechnikiWyplata() > 0) { cell = new PdfPCell( new Phrase(String.format("%.2f", tempPerson.getMontazTechnikiWyplata()), ft)); table.addCell(cell); } else { cell = new PdfPCell(new Phrase(" ")); table.addCell(cell); } } } if (event.isDemontazDachu()) { cell = new PdfPCell(new Phrase("Dem. Dach", ft)); table.addCell(cell); for (Person tempPerson : workers) { if (tempPerson.getDemontazDachuWyplata() > 0) { cell = new PdfPCell( new Phrase(String.format("%.2f", tempPerson.getDemontazDachuWyplata()), ft)); table.addCell(cell); } else { cell = new PdfPCell(new Phrase(" ")); table.addCell(cell); } } } if (event.isDemontazDzwieku()) { cell = new PdfPCell(new Phrase("Dem. Dw.", ft)); table.addCell(cell); for (Person tempPerson : workers) { if (tempPerson.getDemontazDzwiekuWyplata() > 0) { cell = new PdfPCell( new Phrase(String.format("%.2f", tempPerson.getDemontazDzwiekuWyplata()), ft)); table.addCell(cell); } else { cell = new PdfPCell(new Phrase(" ")); table.addCell(cell); } } } if (event.isDemontazSceny()) { cell = new PdfPCell(new Phrase("Dem. Sc.", ft)); table.addCell(cell); for (Person tempPerson : workers) { if (tempPerson.getDemontazScenyWyplata() > 0) { cell = new PdfPCell( new Phrase(String.format("%.2f", tempPerson.getDemontazScenyWyplata()), ft)); table.addCell(cell); } else { cell = new PdfPCell(new Phrase(" ")); table.addCell(cell); } } } if (event.isDemontazSwiatla()) { cell = new PdfPCell(new Phrase("Dem. w.", ft)); table.addCell(cell); for (Person tempPerson : workers) { if (tempPerson.getDemontazSwiatlaWyplata() > 0) { cell = new PdfPCell( new Phrase(String.format("%.2f", tempPerson.getDemontazSwiatlaWyplata()), ft)); table.addCell(cell); } else { cell = new PdfPCell(new Phrase(" ")); table.addCell(cell); } } } if (event.isDemontazTechniki()) { cell = new PdfPCell(new Phrase("Dem. Tech.", ft)); table.addCell(cell); for (Person tempPerson : workers) { if (tempPerson.getDemontazTechnikiWyplata() > 0) { cell = new PdfPCell( new Phrase(String.format("%.2f", tempPerson.getDemontazTechnikiWyplata()), ft)); table.addCell(cell); } else { cell = new PdfPCell(new Phrase(" ")); table.addCell(cell); } } } if (event.isDyzur()) { cell = new PdfPCell(new Phrase("Dyur", ft)); table.addCell(cell); for (Person tempPerson : workers) { if (tempPerson.getDyzurWyplata() > 0) { cell = new PdfPCell(new Phrase(String.format("%.2f", tempPerson.getDyzurWyplata()), ft)); table.addCell(cell); } else { cell = new PdfPCell(new Phrase("")); table.addCell(cell); } } } if (event.isTechnikSceny()) { cell = new PdfPCell(new Phrase("Technik Sc.", ft)); table.addCell(cell); for (Person tempPerson : workers) { if (tempPerson.getTechnikScenyWyplata() > 0) { cell = new PdfPCell(new Phrase(String.format("%.2f", tempPerson.getTechnikScenyWyplata()), ft)); table.addCell(cell); } else { cell = new PdfPCell(new Phrase(" ")); table.addCell(cell); } } } if (event.isRealizacjaFOH()) { cell = new PdfPCell(new Phrase("Real. FOH", ft)); table.addCell(cell); for (Person tempPerson : workers) { if (tempPerson.getRealizacjaFOHWyplata() > 0) { cell = new PdfPCell( new Phrase(String.format("%.2f", tempPerson.getRealizacjaFOHWyplata()), ft)); table.addCell(cell); } else { cell = new PdfPCell(new Phrase(" ")); table.addCell(cell); } } } if (event.isRealizacjaMON()) { cell = new PdfPCell(new Phrase("Real. MON", ft)); table.addCell(cell); for (Person tempPerson : workers) { if (tempPerson.getRealizacjaMONWyplata() > 0) { cell = new PdfPCell( new Phrase(String.format("%.2f", tempPerson.getRealizacjaMONWyplata()), ft)); table.addCell(cell); } else { cell = new PdfPCell(new Phrase(" ")); table.addCell(cell); } } } if (event.isRealizacjaSwiatla()) { cell = new PdfPCell(new Phrase("Real. w.", ft)); table.addCell(cell); for (Person tempPerson : workers) { if (tempPerson.getRealizacjaSwiatlaWyplata() > 0) { cell = new PdfPCell( new Phrase(String.format("%.2f", tempPerson.getRealizacjaSwiatlaWyplata()), ft)); table.addCell(cell); } else { cell = new PdfPCell(new Phrase(" ")); table.addCell(cell); } } } if (true) { cell = new PdfPCell(new Phrase("Premia", ft)); table.addCell(cell); for (Person tempPerson : workers) { if (tempPerson.getPremia() > 0) { cell = new PdfPCell(new Phrase(String.format("%.2f", tempPerson.getPremia()), ft)); table.addCell(cell); } else { cell = new PdfPCell(new Phrase(" ")); table.addCell(cell); } } } Paragraph ph = new Paragraph("Razem", ft); //cell = new PdfPCell(new Phrase(ph)); cell.addElement(ph); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(cell); for (Person tempPerson : workers) { if (tempPerson.getSumaWyplata() >= 0) { cell = new PdfPCell(new Phrase(String.format("%.2f", tempPerson.getSumaWyplata()) + " z", ft)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(cell); } else { cell = new PdfPCell(new Phrase(" ")); table.addCell(cell); } } // for (Object obj : table.getChunks()) { // if (obj instanceof PdfPCell) { // System.out.println("jest PdfCell"); // PdfPCell tempCell = (PdfPCell) obj; // tempCell.setVerticalAlignment(Element.ALIGN_CENTER); // //tempCell.setVerticalAlignment(Element.ALIGN_MIDDLE); // } // // } table.setHorizontalAlignment(Element.ALIGN_CENTER); document.add(table); document.close(); }
From source file:com.tomasz.drag.triballocommanderro.controller.MakerPDF.java
public static void printToPDF(ArrayList<Person> workers, EventGig event) throws IOException, DocumentException { String filename = event.getName() + ".pdf"; String path = event.getData() + " " + event.getName() + "//"; Document document = new Document(); // step 2/*from ww w. j a v a 2s . co m*/ PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path + filename)); // step 3 document.open(); // step 4 ColumnText column = new ColumnText(writer.getDirectContent()); float[][] x = { { document.left(), document.left() + 230 }, { document.right() - 230, document.right() } }; for (Person person : workers) { column.addElement(MakerPDF.createTable(person, event)); } int count = 0; float height = 0; int status = ColumnText.START_COLUMN; while (ColumnText.hasMoreText(status)) { column.setSimpleColumn(x[count][0], document.bottom(), x[count][1], document.top() - height - 10); // render as much content as possible status = column.go(); // go to a new page if you've reached the last column if (++count > 1) { count = 0; document.newPage(); } } document.newPage(); document.close(); }
From source file:com.tommontom.pdfsplitter.PdfCombine.java
public List<File> combine(File folder) throws IOException, DocumentException { if (!folder.isDirectory()) { throw new IllegalArgumentException("Provided folder is not a directory: " + folder); }/*from w w w . j ava 2 s . c om*/ FileNameFilter FileFilter = new FileNameFilter(); File[] listOfFiles = folder.listFiles(FileFilter); /* * Stores the listing of the files */ List<File> newFileList = new ArrayList<>(); for (int i = 0; i < listOfFiles.length; i++) { File file = listOfFiles[i]; if (!file.isFile()) { continue; } // Split the source filename into its 2 parts String fileName = file.getName(); String fileNameWithoutExt = fileName.substring(0, fileName.lastIndexOf('.')); // Split on a space '\s' String[] fileNames = fileNameWithoutExt.split("\\s"); if (fileNames.length != 2) { throw new RuntimeException("File name format is not in right format"); } String fileNameFirst = fileNames[0]; String fileNameSecond = fileNames[1]; System.out.println("First lot number: " + fileNameFirst + " Second lot number: " + fileNameSecond); String[] fileNameFirstParts = fileNameFirst.split("-"); // Project num is always the 1st part String projectNum = fileNameFirstParts[0]; if (!projectNum.equals(fileNameFirstParts[0])) { throw new RuntimeException("Filename needs to be renamed to the correct format"); } // Strip off the first and second lot number, parse into integers int firstLotNum; int secondLotNum; firstLotNum = Integer.parseInt(fileNameFirstParts[1]); secondLotNum = Integer.parseInt(fileNameFirstParts[2]); // Determine number of pages by difference of lot numbers // Read in the source document // Example file name: 16034-212234 16034-212236.pdf > 16034-212234.pdf, // 16034-212235.pdf, 16034-212236.pdf PdfReader pdfFileReader = new PdfReader(file.getPath()); int mod = pdfFileReader.getNumberOfPages() % 2; if (pdfFileReader.equals(mod)) { throw new RuntimeException("File is not an even number of pages"); } Document document = new Document(); /* * instantiates a new document to be made */ int numPages = pdfFileReader.getNumberOfPages(); int p = 0; int j = 0; // Create a copy of the orignal source file. We will pick specific pages // out below document.open(); while (j < numPages) { if (p == numPages) { break; } if (j % 2 == 2) { j += 1; } j += 1; firstLotNum++; // Dynamic filename String dynamicFileName = projectNum + "-" + (firstLotNum - 1) + ".pdf"; document = new Document(); PdfCopy copy = new PdfCopy(document, new FileOutputStream(folder.getPath() + "\\" + dynamicFileName)); document.open(); p += 2; // Import pages from original doc copy.addPage(copy.getImportedPage(pdfFileReader, j)); copy.addPage(copy.getImportedPage(pdfFileReader, p)); document.close(); // Add to the list of compled file names newFileList.add(new File(dynamicFileName)); } System.out.println("Number of Documents Created:" + numPages); System.out.println("Number of Documents Created:" + listOfFiles[i]); } return newFileList; }
From source file:com.tommontom.pdfsplitter.PdfMerge.java
public static void doMerge(java.util.List<InputStream> list, String[] imageList, String[] listWordExcels, OutputStream outputStream) throws DocumentException, IOException { Document document = new Document(PageSize.LETTER, 0, 0, 0, 0); PdfWriter writer = PdfWriter.getInstance(document, outputStream); writer.setFullCompression();//w ww . ja v a 2 s .co m document.open(); PdfContentByte cb = writer.getDirectContent(); Image img; for (InputStream in : list) { PdfReader reader = new PdfReader(in); for (int i = 1; i <= reader.getNumberOfPages(); i++) { document.newPage(); //import the page from source pdf PdfImportedPage page = writer.getImportedPage(reader, i); //add the page to the destination pdf cb.addTemplate(page, 0, 0); } } for (int i = 0; i < imageList.length; i++) { document.newPage(); if (imageList[i] != null) { img = Image.getInstance(String.format("%s", imageList[i])); Rectangle one = new Rectangle(img.getPlainWidth(), img.getPlainHeight()); document.setPageSize(one); if (img.getScaledWidth() > img.getScaledHeight()) { img.rotate(); } if (img.getScaledWidth() > 792 || img.getScaledHeight() > 792) { img.scaleToFit(792, 792); } img.setDpi(150, 150); document.add(img); } } for (int i = 0; i < listWordExcels.length; i++) { if (imageList[i] != null) { File input = new File(listWordExcels[i]); File output = new File(listWordExcels[i] + ".pdf"); String outputS = listWordExcels[i] + ".pdf"; OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); connection.connect(); DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(input, output); PdfReader readerWord = new PdfReader(outputS); PdfImportedPage page = writer.getImportedPage(readerWord, readerWord.getNumberOfPages()); cb.addTemplate(page, 0, 0); } } outputStream.flush(); document.close(); outputStream.close(); }
From source file:com.tommontom.pdfsplitter.PdfSplit.java
public void pdfSplit(String path) throws IOException, DocumentException, InterruptedException { // TODO Instead of hard code path, pass in as argument File folder = new File(path); FileNameFilter FileFilter = new FileNameFilter(); File[] listOfFiles = folder.listFiles(FileFilter); /* Stores the listing of the files */ for (int i = 0; i < listOfFiles.length; i++) { File file = listOfFiles[i]; if (!file.isFile()) { continue; }/*from w w w. j av a2s . c o m*/ // Split the source filename into its 2 parts String fileName = file.getName(); String fileNameWithoutExt = fileName.substring(0, fileName.lastIndexOf(".")); PdfReader pdfFileReader = new PdfReader(file.getPath()); Document document = new Document(PageSize.LETTER, 0, 0, 0, 0); /* instantiates a new document to be made */ int k = 0; int numPages = pdfFileReader.getNumberOfPages(); // Split on a space '\s' // Determine number of pages by difference of lot numbers // Read in the source document // Example file name: 16034-212234 16034-212236.pdf > 16034-212234.pdf, 16034-212235.pdf, 16034-212236.pdf // Create a copy of the orignal source file. We will pick specific pages out below for (int j = 1; j < numPages + 1; j++) { String FileName = (fileNameWithoutExt); /* Dynamic file name */ PdfCopy copy = new PdfCopy(document, new FileOutputStream(path + "\\" + FileName + "(" + j + ")" + ".pdf")); document.open(); copy.addPage(copy.getImportedPage(pdfFileReader, j)); /* Import pages from original document */ deleteFile[k] = path + "\\" + FileName + "(" + j + 1 + ")" + ".pdf"; k++; if (j == 1) { newFileListing = ("Created File:" + path + FileName + "(" + j + ")" + ".pdf" + "\n"); } else if (j > 1) { newFileListing += ("Created File:" + path + FileName + "(" + j + ")" + ".pdf" + "\n"); } document.close(); } System.out.println("Number of Documents Created:" + numPages); pdfFileReader.close(); } }
From source file:com.tommontom.pdfsplitter.PdfSplit.java
public void pdfSplitCopy(String path) throws IOException, DocumentException { // TODO Instead of hard code path, pass in as argument File folder = new File(path); FileNameFilter FileFilter = new FileNameFilter(); File[] listOfFiles = folder.listFiles(FileFilter); /* Stores the listing of the files */ for (int i = 0; i < listOfFiles.length; i++) { File file = listOfFiles[i]; if (!file.isFile()) { continue; }/*from w w w.j a va2 s .co m*/ // Split the source filename into its 2 parts String fileName = file.getName(); String fileNameWithoutExt = fileName.substring(0, fileName.lastIndexOf(".")); PdfReader pdfFileReader = new PdfReader(file.getPath()); Document document = new Document(PageSize.LETTER, 0, 0, 0, 0); /* instantiates a new document to be made */ String[] fileNameNum = fileNameWithoutExt.split("-"); int fileNameNumOne = Integer.getInteger(fileNameNum[0]); int fileNameNumTwo = Integer.getInteger(fileNameNum[1]); int constant = 1; int k = 0; int numPages = fileNameNumTwo - fileNameNumOne; // Split on a space '\s' // Determine number of pages by difference of lot numbers // Read in the source document // Example file name: 16034-212234 16034-212236.pdf > 16034-212234.pdf, 16034-212235.pdf, 16034-212236.pdf // Create a copy of the orignal source file. We will pick specific pages out below document.open(); for (int j = 0; j < numPages + 1; j++) { String FileName = (fileNameWithoutExt); /* Dynamic file name */ PdfCopy copy = new PdfCopy(document, new FileOutputStream(path + "\\" + FileName + "(" + j + 1 + ")" + ".pdf")); deleteFile[k] = (path + "\\" + FileName + "(" + j + 1 + ")" + ".pdf"); k++; document.open(); copy.addPage( copy.getImportedPage(pdfFileReader, constant)); /* Import pages from original document */ if (j == 1) { newFileListing = ("Created File:" + path + FileName + "(" + j + ")" + ".pdf" + "\n"); } else if (j > 1) { newFileListing += ("Created File:" + path + FileName + "(" + j + ")" + ".pdf" + "\n"); } document.close(); copy.close(); } System.out.println("Number of Documents Created:" + numPages); pdfFileReader.close(); } }
From source file:com.tommontom.pdfsplitter.PdfSplit.java
public void pdfSplitSupplierDoc(String path) throws IOException, DocumentException { // TODO Instead of hard code path, pass in as argument File folder = new File(path); FileNameFilter FileFilter = new FileNameFilter(); File[] listOfFiles = folder.listFiles(FileFilter); /* Stores the listing of the files */ for (int i = 0; i < listOfFiles.length; i++) { File file = listOfFiles[i]; if (!file.isFile()) { continue; }//from w w w .j a v a2s . c o m // Split the source filename into its 2 parts String fileName = file.getName(); String fileNameWithoutExt = fileName.substring(0, fileName.lastIndexOf(".")); PdfReader pdfFileReader = new PdfReader(file.getPath()); Document document = new Document(PageSize.LETTER, 0, 0, 0, 0); /* instantiates a new document to be made */ String[] fileNames = fileNameWithoutExt.split("-"); /* * if (fileNames.length != 2) { throw new RuntimeException("File name format is not in right format"); } */ String fileNameFirst = fileNames[1]; String fileNameSecond = fileNames[2]; System.out.println("First lot number: " + fileNameFirst + " Second lot number: " + fileNameSecond); // Project num is always the 1st part String projectNum = fileNames[0]; if (!projectNum.equals(fileNames[0])) { throw new RuntimeException("Filename needs to have a project number"); } // Strip off the first and second lot number, parse into integers int firstLotNum; int secondLotNum; firstLotNum = Integer.parseInt(fileNames[1]); secondLotNum = Integer.parseInt(fileNames[2].substring(0, fileNames[2].lastIndexOf("."))); // Create a copy of the orignal source file. We will pick specific pages out below document.open(); int numPages = secondLotNum - firstLotNum; for (int j = 1; j < numPages + 1; j++) { String FileName = projectNum + "-" + (firstLotNum) + ".pdf"; /* Dynamic file name */ firstLotNum++; document = new Document(PageSize.LETTER, 0, 0, 0, 0); PdfCopy copy = new PdfCopy(document, new FileOutputStream(path + "\\" + FileName)); document.open(); copy.addPage(copy.getImportedPage(pdfFileReader, j)); /* Import pages from original document */ document.close(); copy.close(); } pdfFileReader.close(); } }
From source file:com.tommontom.pdfsplitter.PdfSplit.java
public void pdfSplitDrop(File[] files) throws IOException, DocumentException, InterruptedException { // TODO Instead of hard code path, pass in as argument String path;//w ww . j a v a 2 s. co m if (directoryField.getText().isEmpty() || directoryField.getText().equals(example)) { path = files[0].getParent(); } else { path = directoryField.getText(); } File[] listOfFiles = files; /* Stores the listing of the files */ for (int i = 0; i < listOfFiles.length; i++) { File file = listOfFiles[i]; if (!file.isFile()) { continue; } // Split the source filename into its 2 parts String fileName = file.getName(); String fileNameWithoutExt = fileName.substring(0, fileName.lastIndexOf(".")); PdfReader pdfFileReader = new PdfReader(file.getPath()); Document document = new Document(PageSize.LETTER, 0, 0, 0, 0); /* instantiates a new document to be made */ int numPages = pdfFileReader.getNumberOfPages(); // Determine number of pages by difference of lot numbers // Read in the source document // Example file name: 16034-212234 16034-212236.pdf > 16034-212234.pdf, 16034-212235.pdf, 16034-212236.pdf // Create a copy of the orignal source file. We will pick specific pages out below int k = 0; for (int j = 1; j < numPages + 1; j++) { String FileName = fileNameWithoutExt; /* Dynamic file name */ PdfCopy copy = new PdfCopy(document, new FileOutputStream(path + "\\" + FileName + "(" + j + ")" + ".pdf")); document.open(); copy.addPage(copy.getImportedPage(pdfFileReader, j)); /* Import pages from original document */ deleteFile[k] += path + "\\" + FileName + "(" + j + ")" + ".pdf"; k++; if (j == 1) { newFileListing = ("Created File:" + path + FileName + "(" + j + ")" + ".pdf" + "\n"); } else if (j > 1) { newFileListing += ("Created File:" + path + FileName + "(" + j + ")" + ".pdf" + "\n"); } document.close(); copy.close(); } pdfFileReader.close(); } }
From source file:com.tommontom.pdfsplitter.PdfSplit.java
public void pdfSplitDropCopy(File[] files) throws IOException, DocumentException { // TODO Instead of hard code path, pass in as argument String path;/*from ww w . jav a 2 s.c o m*/ path = files[0].getParent(); File[] listOfFiles = files; /* Stores the listing of the files */ System.out.println(directoryField); for (int i = 0; i < listOfFiles.length; i++) { File file = listOfFiles[i]; if (!file.isFile()) { continue; } // Split the source filename into its 2 parts String fileName = file.getName(); String fileNameWithoutExt = fileName.substring(0, fileName.lastIndexOf(".")); String[] fileNameNum = fileNameWithoutExt.split("-"); int fileNameNumOne = Integer.parseInt(fileNameNum[0]); int fileNameNumTwo = Integer.parseInt(fileNameNum[1]); int constant = 1; int numPages = fileNameNumTwo - fileNameNumOne; PdfReader pdfFileReader = new PdfReader(file.getPath()); Document document = new Document(PageSize.LETTER, 0, 0, 0, 0); ; /* instantiates a new document to be made */ // Determine number of pages by difference of lot numbers // Read in the source document // Example file name: 16034-212234 16034-212236.pdf > 16034-212234.pdf, 16034-212235.pdf, 16034-212236.pdf // Create a copy of the orignal source file. We will pick specific pages out below for (int j = 0; j < numPages + 1; j++) { String FileName = fileNameWithoutExt; /* Dynamic file name */ PdfCopy copy = new PdfCopy(document, new FileOutputStream(path + "\\" + FileName + "(" + j + 1 + ")" + ".pdf")); document.open(); copy.addPage( copy.getImportedPage(pdfFileReader, constant)); /* Import pages from original document */ if (j == 1) { newFileListing = ("Created File:" + path + FileName + "(" + j + ")" + ".pdf" + "\n"); } else if (j > 1) { newFileListing += ("Created File:" + path + FileName + "(" + j + ")" + ".pdf" + "\n"); } document.close(); } pdfFileReader.close(); } }
From source file:com.tommontom.pdfsplitter.PdfSplit.java
public void pdfSplitDropSupplierDoc(File[] files) throws IOException, DocumentException { // TODO Instead of hard code path, pass in as argument String path;/*from w w w . j a v a2s .com*/ if (directoryField.getText().isEmpty() || directoryField.getText().equals(example)) { path = files[0].getParent(); } else { path = directoryField.getText(); } File[] listOfFiles = files; /* Stores the listing of the files */ for (int i = 0; i < listOfFiles.length; i++) { File file = listOfFiles[i]; if (!file.isFile()) { continue; } // Split the source filename into its 2 parts String fileName = file.getName(); String fileNameWithoutExt = fileName.substring(0, fileName.lastIndexOf(".")); System.out.println(fileNameWithoutExt); PdfReader pdfFileReader = new PdfReader(file.getPath()); Document document = new Document(PageSize.LETTER, 0, 0, 0, 0); /* instantiates a new document to be made */ // Determine number of pages by difference of lot numbers // Read in the source document // Example file name: 16034-212234 16034-212236.pdf > 16034-212234.pdf, 16034-212235.pdf, 16034-212236.pdf // Create a copy of the orignal source file. We will pick specific pages out below // Split on a space '\s' String[] fileNames = fileNameWithoutExt.split("-"); String fileNameFirst = fileNames[1]; String fileNameSecond = fileNames[2]; System.out.println("First lot number: " + fileNameFirst + " Second lot number: " + fileNameSecond); // Project num is always the 1st part String projectNum = fileNames[0]; if (!projectNum.equals(fileNames[0])) { throw new RuntimeException("Filename needs to be renamed to the correct format"); } // Strip off the first and second lot number, parse into integers int firstLotNum; int secondLotNum; firstLotNum = Integer.parseInt(fileNameFirst); secondLotNum = Integer.parseInt(fileNameSecond); int numPages = secondLotNum - firstLotNum; // Create a copy of the orignal source file. We will pick specific pages out below document.open(); for (int j = 1; j < numPages + 1; j++) { String FileName = projectNum + "-" + (firstLotNum) + ".pdf"; /* Dynamic file name */ firstLotNum++; document = new Document(PageSize.LETTER, 0, 0, 0, 0); PdfCopy copy = new PdfCopy(document, new FileOutputStream(path + "\\" + FileName)); document.open(); copy.addPage(copy.getImportedPage(pdfFileReader, j)); /* Import pages from original document */ if (j == 1) { newFileListing = ("Created File:" + path + FileName + "(" + j + ")" + ".pdf" + "\n"); } else if (j > 1) { newFileListing += ("Created File:" + path + FileName + "(" + j + ")" + ".pdf" + "\n"); } document.close(); } } }