Java - Read all pdf file in the folder [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
try
{
PdfReader reader = new PdfReader(RESULT1);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT2));
AcroFields form = stamper.getAcroFields();
String name = form.getField("Text1");//Check Box 1
stamper.close();
reader.close();
FileInputStream file = new FileInputStream(new File("//Users//"+ usr +"//Desktop//TNA//input//FR-OPS-030 Master Training Plan_Rev4.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheet("Sheet1");// getSheetAt(0);
HSSFRow row = sheet.createRow((short) 0);
HSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(HSSFColor.DARK_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFRow row1 = sheet.createRow(7);
HSSFCell name_c1 = row1.createCell(0);
name_c1.setCellValue(name);
name_c1.setCellStyle(style);
file.close();
FileOutputStream outFile =new FileOutputStream(new File("//Users//"+ usr +"//Desktop//TNA//output//FR-OPS-030 Master Training Plan_Rev41w.xls"));
workbook.write(outFile);
outFile.close();
}
catch(Exception ex)
{
System.out.println(ex);
}
I used the code to read PDF file from the folder. It's working fine for single document but I have multiple files in the folder. How do I read PDF multiple files. Please advice

You could start by taking a look at java.io.File which has methods for listing files...
File#listFiles which allows you to list ALL the files within the context of the given File instance
File#listFiles(FileFilter) which allows you to filter the list as it's being created...
Or, if you're using Java 7+, you could take a look at the enhanced File I/O API

Related

Is there a way to delete the PDF byte array and html file? [duplicate]

This question already has answers here:
How to delete a file from a directory using Java?
(6 answers)
Closed 1 year ago.
I'm trying to delete the byte array PDF and HTML file after it has been generated and the PDF is saved to a byte array Document to save server space usage. The Writer class and the pdf byte array do not have the delete methods. Appreciate it if anyone can help me out with this.
// File output
Writer file = new FileWriter (new File("src/" + "xyz.html"));
template.process(data, file);
file.flush();
file.close();
HtmlConverter.convertToPdf(new FileInputStream("src/" + "xyz.html"),new FileOutputStream("src/" + "XYZ.pdf"));
Path pdfPath = Paths.get("src/" + "XYZ.pdf");
byte[] pdf = Files.readAllBytes(pdfPath);
byteDocument = pdf;
//Delete pdf and html files.
If you have enough RAM to hold the PDF, it's likely you have enough for the source HTML at the same time. In that case, skip the file system altogether:
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
Writer writer = new OutputStreamWriter(buffer);
template.process(data, writer);
writer.flush();
ByteArrayInputStream input = new ByteArrayInputStream(buffer.toByteArray());
buffer.reset();
HtmlConverter.convertToPdf(input, buffer);
byteDocument = buffer.toByteArray();
If you need to use the file system due to memory constraints, use Files.delete() to remove your temporary files.

CSV to XLXS format with data in java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have an .csv file in which data is in below format
TEST;"TEST1";"TEST2";"TEST3";"TEST4" in each column.
I need to convert .csv file to .xlsx file in which each value should in different column. eg:See attached image.
I tried using Apache POI however, its just converting into .xlsx format but data remains in one column.
Can you anyone share sample code.
Sample input in csv
Below is the sample output result which should in xlsx format.
Here is a simple example (without exception handling, encoding, file paths, ...) that could handle CSV with semicolons (in that case csv translates to "character separated file") and creates a Xslx file:
//open input file
BufferedReader br = new BufferedReader(new FileReader("input.csv"));
//create sheet
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet();
//read from file
String line = br.readLine();
for (int rows=0; line != null; rows++) {
//create one row per line
Row row = sheet.createRow(rows);
//split by semicolon
String[] items = line.split(";");
//ignore first item
for (int i=1, col=0; i<items.length; i++) {
//strip quotation marks
String item = items[i].substring(1, items[i].length()-1);
Cell cell = row.createCell(col++);
//set item
cell.setCellValue(item);
}
//read next line
line = br.readLine();
}
//write to xlsx
FileOutputStream out = new FileOutputStream("Output.xlsx");
wb.write(out);
//close resources
br.close();
out.close();
Given an input.csv like this:
TEST;"TEST1";"TEST2";"TEST3";"TEST4"
TEST;"TEST5";"TEST6";"TEST7";"TEST8"
the Output.xlsx looks like this:

How can I open an existing *.xlsx file, make modifications, then save as new file (leaving original file untouched?)

I need to open an existing *.xlsx Excel file, make some modifications, and then save it as a new file (or stream it to the frontend without saving). The original file must remain unchanged.
For Memory reasons, I avoid using FileInputStream (as described here: http://poi.apache.org/spreadsheet/quick-guide.html#FileInputStream )
// XSSFWorkbook, File
OPCPackage pkg = OPCPackage.open(new File("file.xlsx"));
XSSFWorkbook wb = new XSSFWorkbook(pkg);
....
pkg.close();
JFileChooser fileOpen = new JFileChooser();
fileOpen.showOpenDialog(theFrame); // gives you an open file dialog
readFile(fileOpen.getSelectedFile()); // write you reading content in reaFile method
JFileChooser fileSave = new JFileChooser();
fileSave.showSaveDialog(Frame); //gives you a dialog box for saving
saveFile(fileSave.getSelectedFile()); // write your saving content in saveFile method
Here is how this can be done when using OPCPackage to read (try/catch/finally ommitted for readibility):
OPCPackage pkg = OPCPackage.open("existingFile.xlsx");
XSSFWorkbook wb = (XSSFWorkbook) WorkbookFactory.create(pkg);
make your modifications... XSSFSheet sheet = wb.getSheetAt(0); ...
fos = new FileOutputStream("outputFileName.xlsx");
wb.write(fos);
pkg.close();
fos.close();
Faces.sendFile(new File(outputFileName)
In order for this to work, it is important to use different file path for Input and for Output.
The last line sends the File to your Browser using Omnifaces.
See this question for more Information:
enter link description here
Here is my final solution, which I ended up using. This has the advantage, that no Files are saved anywhere. I also added the following line:
XSSFFormulaEvaluator.evaluateAllFormulaCells(workbook);
which will ensure that all formulas are updated.
Again, I ommitted all the try/catch/finally.
OPCPackage pkg = OPCPackage.open("existingFile.xlsx");
XSSFWorkbook wb = (XSSFWorkbook) WorkbookFactory.create(pkg);
// make your modifications...
XSSFSheet sheet = wb.getSheetAt(0);
//
XSSFFormulaEvaluator.evaluateAllFormulaCells(workbook);
ByteArrayOutputStream os = new ByteArrayOutputStream();
wb.write(os);
pkg.close();
os.close();
Faces.sendFile(os.toByteArray(), "file.xlsx", true);
In order for this to work, it is important to use different file path for Input and for Output.
The last line sends the File to your Browser using Omnifaces.

Remove page from PDF

I'm currently using iText and I'm wondering if there is a way to delete a page from a PDF file?
I have opened it up with a reader etc., and I want to remove a page before it is then saved back to a new file; how can I do that?
The 'better' way to 'delete' pages is doing
reader.selectPages("1-5,10-12");
Which means we only select pages 1-5, 10-12 effectively 'deleting' pages 6-9.
Get the reader of existing pdf file by
PdfReader pdfReader = new PdfReader("source pdf file path");
Now update the reader by
pdfReader.selectPages("1-5,15-20");
then get the pdf stamper object to write the changes into a file by
PdfStamper pdfStamper = new PdfStamper(pdfReader,
new FileOutputStream("destination pdf file path"));
close the PdfStamper by
pdfStamper.close();
It will close the PdfReader too.
Cheers.....
For iText 7 I found this example:
PdfReader pdfReader = new PdfReader(PATH + name + ".pdf");
PdfDocument srcDoc = new PdfDocument(pdfReader);
PdfDocument resultDoc = new PdfDocument(new PdfWriter(PATH + name + "_cut.pdf"));
resultDoc.initializeOutlines();
srcDoc.copyPagesTo(1, 2, resultDoc);
resultDoc.close();
srcDoc.close();
See also here: clone-reordering-pages
and here: clone-splitting-pdf-file
You can use a PdfStamper in combination with PdfCopy.
In this answer it is explained how to copy a whole document. If you change the criteria for the loop in the sample code you can remove the pages you don't need.
Here is a removing function ready for real life usage. Proven to work ok with itext 2.1.7. It does not use "strigly typing" also.
/**
* Removes given pages from a document.
* #param reader document
* #param pagesToRemove pages to remove; 1-based
*/
public static void removePages(PdfReader reader, int... pagesToRemove) {
int pagesTotal = reader.getNumberOfPages();
List<Integer> allPages = new ArrayList<>(pagesTotal);
for (int i = 1; i <= pagesTotal; i++) {
allPages.add(i);
}
for (int page : pagesToRemove) {
allPages.remove(new Integer(page));
}
reader.selectPages(allPages);
}

append data into xlsx file through java

I am using Apache POI for writing into .xlsx file. I can write into .xlsx file but I am unable to append new content. How can I append new content in the .xlsx file?
My Code is:
public static void write(){
try {
Workbook[] wbs = new Workbook[]{new XSSFWorkbook()};
Workbook workbook=wbs[0];
org.apache.poi.ss.usermodel.Sheet sheet = workbook.createSheet();
System.out.println(sheet.getSheetName());
Row row = sheet.createRow(2);
for(int i=0;i<10;i++){
Cell cell=row.createCell(i);
cell.setCellValue("Sun System");
}
FileOutputStream fout=new FileOutputStream("D:/Test.Xlsx");
workbook.write(fout);
fout.close();
} catch (Exception e) {
}
}
The first thing U've to do :
When you're working with Excel 2007 format, its more wise to use XSSF-Implementations, because you've used abstract implementations. Always remember this when using any implementation.
To append to an existing file you need to reach the end of the rows in that particular workbook sheet. This can be achieved by:
int rows = sheet.getPhysicalNumberOfRows(); // or sheet.getLastRowNum();
After that you can create new cells with the XSSF- Implementation classes. For more information refer to this page
You should open the existing file instead of creating a new one if you want to append, see also this stackoverflow question:
Edit existing excel files using jxl api / Apache POI
You are creating a new workbook each time this is run youd want to create a FileInputStream with a file path to the excel file and then use that input stream to get the XSSF workbook you want to edit
FileInputStream fis = new FileInputStream(filePath);
XSSFWorkbook workBook = new XSSFWorkbook(fis);
then in order to get a specific sheet you simply just use the .getSheet or getSheetAt methods that are apart of workBook. then create and set cells

Categories

Resources