I am using the IText pdf library (itextpdf.com/) to create pdf files for my project written in java.
The problem is: I create 2 pdfs at the result of my method and i want to delete the first, but seems like my first pdf file cannot be deleted for some reason. I have tried using File.delete(), putting File.delete() inside a "finally{}" block... nothing seems to work.
I am sure that i close my FileOutsputStream and do document.close() too! What can i do to remove this file?
public boolean gerarPDFDeStringVariosArquivosSemNumeroDePaginasComId(LinkedList < String > textosLidos, LinkedList < String > nomesDosArquivosLidos, File arquivoPdfOutput) {
try {
nomesDosArquivosLidosESeusIds = new HashMap < String, String > ();
FileOutputStream fos = new FileOutputStream(arquivoPdfOutput);
Document document = new Document();
PdfWriter.getInstance(document, fos);
document.open();
addMetaData(document);
addTitlePage(document);
for (int i = 0; i < textosLidos.size(); i++) {
String umTextoLido = textosLidos.get(i);
String umNomeArquivoLido = nomesDosArquivosLidos.get(i);
String idUmNomeArquivoLido = "#%&#" + "id_" + i + "#%&#";
this.nomesDosArquivosLidosESeusIds.put(umNomeArquivoLido, idUmNomeArquivoLido);
String umNomeArquivoLidoEIdDele = idUmNomeArquivoLido + " \n" + umNomeArquivoLido; //o id servirah para sabermos quantas paginas o arquivo possui no pdf
String textoLido2 = umTextoLido.replaceAll("\\t", " ");
addContent(document, textoLido2, umNomeArquivoLidoEIdDele);
}
document.close();
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean gerarPDFDeStringVariosArquivosComNumeroDePaginas(LinkedList < String > textosLidos, LinkedList < String > nomesDosArquivosLidos, File arquivoPdfOutput, File arquivoPdfOutputComNumeroDePaginas) {
/*primeiro vou executar gerarPDFDeStringVariosArquivosSemNumeroDePaginas para gerar um pdf com os
* ids de cada arquivo, seus textos, mas sem o numero de paginas e vou alterar a variavel local this.nomesDosArquivosLidosESeusIds
*/
boolean conseguiGerarPrimeiroPdf = gerarPDFDeStringVariosArquivosSemNumeroDePaginasComId(textosLidos, nomesDosArquivosLidos, arquivoPdfOutput);
if (conseguiGerarPrimeiroPdf == true) {
//agora vou pegar quantas paginas os arquivos tem
VerificaNumeroDePaginasDeCadaArquivoNoPdfGerado verificaNumeroDePaginas = new VerificaNumeroDePaginasDeCadaArquivoNoPdfGerado();
HashMap < String, Integer > arquivosEQuantasPaginasElesTem = verificaNumeroDePaginas.pegarNumeroDePaginasNoPdfDeCadaArquivo(this.nomesDosArquivosLidosESeusIds, nomesDosArquivosLidos, Main.FILE);
//agora comeco a criar o segundo pdf que terah o numero de paginas de cada arquivo
try {
FileOutputStream fos = new FileOutputStream(arquivoPdfOutputComNumeroDePaginas);
Document document = new Document();
PdfWriter.getInstance(document, fos);
document.open();
addMetaData(document);
addTitlePage(document);
for (int i = 0; i < textosLidos.size(); i++) {
String umTextoLido = textosLidos.get(i);
String umNomeArquivoLido = nomesDosArquivosLidos.get(i);
int quantasPaginasTemOArquivoLido = arquivosEQuantasPaginasElesTem.get(umNomeArquivoLido);
String umNomeArquivoLidoEPaginas;
if (quantasPaginasTemOArquivoLido > 1) {
umNomeArquivoLidoEPaginas = umNomeArquivoLido + " (" + quantasPaginasTemOArquivoLido + " páginas)";
} else {
umNomeArquivoLidoEPaginas = umNomeArquivoLido + " (" + quantasPaginasTemOArquivoLido + " página)";
}
String textoLido2 = umTextoLido.replaceAll("\\t", " ");
addContent(document, textoLido2, umNomeArquivoLidoEPaginas);
}
document.close();
fos.close();
arquivoPdfOutput.delete();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
} else {
return false;
}
}
I do this to test:
File arquivoPdfGerar = new File(Main.FILE);
File arquivopdfGerarComNumeroDePaginas = new File(Main.FILE2);
/*PrintStream ps = new PrintStream(fileOutputStream);
System.setOut(ps);*/
LinkedList < String > nomesArquivosLidos = new LinkedList < String > ();
LinkedList < String > textosArquivosLidos = new LinkedList < String > ();
String url = "C:/Users/fábioandrews/Documents/git/PdfGeneratorForSoftwareRegistration/PdfGeneratorForSoftwareRegistration/src/br/ufrn/pairg/pdfgenerator/FirstPDF.java";
String nomeProjeto = "PdfGeneratorForSoftwareRegistration";
String arquivoLido = LeitorArquivoTexto.lerArquivoQualquerDeTexto(url);
String nomeArquivoLido = LeitorArquivoTexto.pegarNomeArquivo(url, nomeProjeto);
nomesArquivosLidos.add(nomeArquivoLido);
textosArquivosLidos.add(arquivoLido);
url = "C:/Users/fábioandrews/Documents/git/PdfGeneratorForSoftwareRegistration/PdfGeneratorForSoftwareRegistration/src/br/ufrn/pairg/pdfgenerator/Main.java";
nomeProjeto = "PdfGeneratorForSoftwareRegistration";
arquivoLido = LeitorArquivoTexto.lerArquivoQualquerDeTexto(url);
nomeArquivoLido = LeitorArquivoTexto.pegarNomeArquivo(url, nomeProjeto);
nomesArquivosLidos.add(nomeArquivoLido);
textosArquivosLidos.add(arquivoLido);
GeraPDFDeStringVariosArquivos geradorPdf = new GeraPDFDeStringVariosArquivos();
geradorPdf.gerarPDFDeStringVariosArquivosComNumeroDePaginas(textosArquivosLidos, nomesArquivosLidos, arquivoPdfGerar, arquivopdfGerarComNumeroDePaginas);
What about the following line ?
PdfWriter.getInstance(document, fos);
IMHO, this method/line
looks useless in you code as I don't find any reference to the object (PdfWriter) returned by it.
if this line can be removed, just do it ;-)
if not, you have to
hold PdfWriter object returned.
and close it (in a finally block as it should be done for the FileOutputStream and `Document' instances too).
Note: this remarks are done according to the itext version 5.5.6 I am using.
If you still have issues, you may plug this little tool (created by the autor of Jenkins). He saved me on an ooooold program.
Thanks for all your answers. The solution was right where Bruno Lowagie said: When i was reading the pdf files to count how many pages where there, i was not closing the pdfreader and therefore the file was still in use.
Thank you all for the answers ^^
Related
I have a java web project (jsp + servlets) that the local code works the property registry, if I commit to git and then give a git pull or git clone the property registry stops working, but none appear error, it simply does not record the data and only returns to the success page, this problem only occurs when I try to commit this version.
I tried to create a new repository and upload it as a new project but the problem continues
this is the DAO class
public boolean cadastrar(Imovel imovel) throws SQLException {
Connection connection = ConnectionFactory.getConexao();
//Endereço
String INSERTIMOVEL = "INSERT INTO Imovel VALUES (DEFAULT,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
String INSERTENDERECO = "INSERT INTO Endereco VALUES (DEFAULT,?,?,?,?,?,?,?)";
smt = connection.prepareStatement(INSERTENDERECO, Statement.RETURN_GENERATED_KEYS);
smt.setString(1, imovel.getEndereco().getLogradouro());
smt.setString(2, imovel.getEndereco().getComplemento());
smt.setInt(3, imovel.getEndereco().getNumero());
smt.setString(4, imovel.getEndereco().getCidade());
smt.setString(5, imovel.getEndereco().getCep());
smt.setString(6, imovel.getEndereco().getBairro());
smt.setString(7, imovel.getEndereco().getEstado());
smt.execute();
rs = smt.getGeneratedKeys();
rs.next();
//imovel
smt = connection.prepareStatement(INSERTIMOVEL);
smt.setString(1, imovel.getTitulo());
smt.setString(2, imovel.getDescricao());
smt.setString(3, "Em Análise");
smt.setString(4, "Ativo");
smt.setDouble(5, imovel.getValor());
smt.setDouble(6, imovel.getArea_total());
smt.setDouble(7, imovel.getArea_edificada());
smt.setInt(8, imovel.getComodos());
smt.setInt(9, imovel.getVagas_garagem());
smt.setInt(10, imovel.getBanheiros());
smt.setTimestamp(11, timestamp);
smt.setString(12, imovel.getDiretorio_imagem());
smt.setString(13, imovel.getTipo_imovel());
smt.setInt(14, imovel.getUsuario().getId_usuario());
smt.setInt(15, rs.getInt(1));
boolean rowInserted = smt.executeUpdate() > 0;
rs.close();
smt.close();
connection.close();
return rowInserted;
}
and this is the controller (i use command and factory patterns)
#Override
public String executar(HttpServletRequest request, HttpServletResponse response) {
try {
HttpSession usuarioLogado = request.getSession();
Sessao sessao = (Sessao) usuarioLogado.getAttribute("usuarioLogado");
Part filePart = request.getPart("uploadFile"); //
String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); //
InputStream fileContent = filePart.getInputStream();
imovel.setDiretorio_imagem(sessao.getId_usuario() + File.separator + fileName);
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = fileContent.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
byte[] bytes = os.toByteArray();
// cria o diretorio de upload
// esse caminho e relativo ao diretorio da aplicacao
ServletContext context = request.getServletContext();
String uploadPath = context.getRealPath("/") + "Resources\\upload" + File.separator + sessao.getId_usuario();
// caso o diretorio nao exista o bloco abaixo cria o mesmo
File uploadDir = new File(uploadPath);
if (!uploadDir.exists()) {
uploadDir.mkdir();
}
//converte o array de bytes em file e grava no diretorio
File f = new File(uploadPath + File.separator + fileName);
try (FileOutputStream fos = new FileOutputStream(f)) {
fos.write(bytes);
}
//Imovel Requests
String titulo = request.getParameter("titulo");
String descricao = request.getParameter("descricao");
int comodos = Integer.parseInt(request.getParameter("comodos"));
int banheiro = Integer.parseInt(request.getParameter("banheiro"));
int garagem = Integer.parseInt(request.getParameter("garagem"));
Double valor = Double.parseDouble(request.getParameter("valorimovel"));
Double areatotal = Double.parseDouble(request.getParameter("areatotal"));
Double areaedificada = Double.parseDouble(request.getParameter("areaedificada"));
String tpimovel = request.getParameter("tpimovel");
imovel.setTitulo(titulo);
imovel.setDescricao(descricao);
imovel.setComodos(comodos);
imovel.setBanheiros(banheiro);
imovel.setVagas_garagem(garagem);
imovel.setValor(valor);
imovel.setArea_total(areatotal);
imovel.setArea_edificada(areaedificada);
imovel.setTipo_imovel(tpimovel);
imovel.getUsuario().setId_usuario(sessao.getId_usuario());
//Endereço Requests
String logradouro = request.getParameter("logradouro");
int numero = Integer.parseInt(request.getParameter("numero"));
String complemento = request.getParameter("complemento");
String cidade = request.getParameter("cidade");
String estado = request.getParameter("estado");
String cep = request.getParameter("cep");
String bairro = request.getParameter("bairro");
//Endereço Set's
imovel.getEndereco().setLogradouro(logradouro);
imovel.getEndereco().setNumero(numero);
imovel.getEndereco().setComplemento(complemento);
imovel.getEndereco().setCidade(cidade);
imovel.getEndereco().setEstado(estado);
imovel.getEndereco().setCep(cep);
imovel.getEndereco().setBairro(bairro);
ImovelDAO dao = new ImovelDAO();
if (dao.cadastrar(imovel)) {
request.setAttribute("msg", "Seu imóvel foi cadastrado e passará por uma análise, fique de olho no seu email!");
return "index.jsp";
} else {
request.setAttribute("msgerro", "Ocorreu um erro ao tentar cadastrar o imóvel, tente novamente");
return "index.jsp";
}
} catch (SQLException | NumberFormatException | IOException | ServletException | MessagingException ex) {
request.setAttribute("msgerro", ex.getMessage());
return "index.jsp";
}
}
This line smt = connection.prepareStatement(INSERTIMOVEL); closes the previous statement and ResultSet. Get the value before that line.
int val = rs.getInt(1);
smt = connection.prepareStatement(INSERTIMOVEL);
// ...
smt.setInt(15, val);
If that is not the issue, please edit your question to include any exceptions or a diff with the last working version.
Im scraping data from multiple web pages using Jsoup, how can I get the scraped data to save to file without it overwriting the previous webpage that got scraped
I've tried searching on stack overflow and Jsoup docs for a solution.
int j = 0;
int i = 0;
String URL = ("https://www.ufc.com/athletes/all?gender=All&search=&page="+j);
Document doc = Jsoup.connect(URL).userAgent("mozilla/70.0.1").get();
Elements temp = doc.select("div.c-listing-athlete__text");
for (Element fighterList:temp) {
i++;
System.out.println(i + " " + fighterList.getElementsByClass("c-listing-athlete__name").first().text());
}
j++;
URL = ("https://www.ufc.com/athletes/all?gender=All&search=&page="+j);
doc = Jsoup.connect(URL).userAgent("mozilla/70.0.1").get();
temp = doc.select("div.c-listing-athlete__text");
for (Element fighterList:temp) {
i++;
System.out.println(i + " " + fighterList.getElementsByClass("c-listing-athlete__name").first().text());
}
If you need to save the data from code, just check this, maybe it can help you:
int i = 0;
int pagesNumber = 10;
String URL = "";
Document doc = null;
Elements temp = null;
try {
// Create file
FileWriter fstream = new FileWriter(System.currentTimeMillis() + "out.txt");
BufferedWriter out = new BufferedWriter(fstream);
for (i=0; i<pagesNumber; i++) {
URL = ("https://www.ufc.com/athletes/all?gender=All&search=&page="+i);
doc = Jsoup.connect(URL).userAgent("mozilla/70.0.1").get();
temp = doc.select("div.c-listing-athlete__text");
for (Element fighter : temp) {
out.write(i + " " + fighter.getElementsByClass("c-listing-athlete__name").first().text());
}
}
//Close the output stream
out.close();
} catch (Exception e) { // Catch exception if any
System.err.println("Error: " + e.getMessage());
}
Hope it helps :)
I have a problem reading an .xml file in an FTP, and the following message appears: "ftp:\cifpag\FilesNotes\Maxdata_Venda_209016.XML (The syntax of the file name, directory name, or volume label is incorrect) ", in the code this commenting on the tests that already were done and what happens in each step, it follows the code below:
public class ImportXmlFromFTP {
private static Connection conexao;
private String endereco_ftp;
private String usuario;
private String senha;
private String caminho_ftp;
private String caminho_local;
private String valor;
public ImportXmlFromFTP(){
}
public void inicia() throws SocketException, IOException, SQLException {
//DADOS CONEXAO
this.conexao = ConexaoBancoDeDados.getConexao();
String sql = "SELECT daea_endereco_ftp, daea_usuario, daea_senha, daea_caminho_ftp, daea_caminho_local FROM sistema.dados_envio_arquivos";
PreparedStatement stmtSelect = this.conexao.prepareStatement(sql);
ResultSet rs = stmtSelect.executeQuery();
List<DadosEnviaArquivos> listaCliente = new ArrayList<DadosEnviaArquivos>();
while(rs.next()){
DadosEnviaArquivos dados = new DadosEnviaArquivos();
endereco_ftp = rs.getString("daea_endereco_ftp");
usuario = rs.getString("daea_usuario");
senha = rs.getString("daea_senha");
caminho_ftp = rs.getString("daea_caminho_ftp");
caminho_local = rs.getString("daea_caminho_local");
}
//CONEXAO COM FTP
FTPClient ftp = new FTPClient();
ftp.connect(endereco_ftp);
ftp.login( usuario, senha );
ftp.changeWorkingDirectory (caminho_ftp);
//PASTA COM OS ARQUIVOS
int m = 0;
String nomeArquivo = "";
File caminhoParaFTP = new File(caminho_local + "/");
File arquivos[] = caminhoParaFTP.listFiles();
while (m != arquivos.length){
nomeArquivo = arquivos[m].getName();
FileInputStream arqEnviar = new FileInputStream(caminhoParaFTP + "/" + nomeArquivo);
//IMPORTA PARA O FTP
if (ftp.storeFile (nomeArquivo, arqEnviar)) {
System.out.println("Arquivo armazenado com sucesso!");
arqEnviar.close();
//APAGA ARQUIVO DA PASTA LOCAL
File file = new File(caminhoParaFTP+"/"+nomeArquivo);
file.delete();
m++;
}else{
System.out.println ("Erro ao armazenar o arquivo.");
}
}
ftp.disconnect();
//LER ARQUIVO DO FTP E IMPORTAR PARA O SISTEMA
//PEGAR PASTA FTP
classe_FTP ClienteFTP = new classe_FTP();
ClienteFTP.Conectar(endereco_ftp, usuario, senha, 21);
String caminho = caminho_ftp;
ArrayList<String> nomes=new ArrayList<String>();
FTPFile[] arquivosFTP = ClienteFTP.Dir(caminho);
if (arquivosFTP != null) {
int length = arquivosFTP.length;
for (int g = 0; g < length; ++g) {
FTPFile p = arquivosFTP[g];
if (p.isFile()) {
String arquivoNominal = p.getName();
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
try {
//BEGIN TESTES
FileInputStream stream = new FileInputStream("ftp:\\\\cifensa.com.br\\ArquivosNotas\\" + arquivoNominal);
//InputStream is = new FileInputStream("ftp:\\\\" +"\\\\cifensa.com.br//ArquivosNotas//" + arquivoNominal);
//InputStream stream = ftp.retrieveFileStream(p.getName());
//System.out.println("Caminho: "+stream.toString());
//END TO TESTE
Document doc = builder.parse(stream);
NodeList listaDeVenda = doc.getElementsByTagName("venda");
int tamanhoDaLista = listaDeVenda.getLength();
NodeList listaDeProdutos = doc.getElementsByTagName("item");
int tamanhoDaListaDeProdutos = listaDeProdutos.getLength();
for(int k = 0; k < tamanhoDaListaDeProdutos; k++){
Pedido pedido = new Pedido();
String slq = "INSERT INTO sistema.pedido(pedi_produto, pedi_quantidade, pedi_preco_unidade_produto, pedi_cliente, pedi_numero_documento, pedi_data_documento, pedi_cliente_cpf, " +
"pedi_numero_documento_fiscal, pedi_unidade, pedi_total_desconto_produto, pedi_valor_desconto_produto, pedi_porcetagem_desconto_produto, " +
"pedi_empresa, pedi_vendedor, pedi_operacao ) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement stmt = conexao.prepareStatement(slq);
Node noDeProdutos = listaDeProdutos.item(k);
if(noDeProdutos.getNodeType() == Node.ELEMENT_NODE){
Element elementoProduto = (Element) noDeProdutos;
NodeList listaNosFilhosProdutos = elementoProduto.getChildNodes();
int tamandoListaNosFilhosProdutos = listaNosFilhosProdutos.getLength();
for(int l = 0; l < tamandoListaNosFilhosProdutos; l++){
Node nosFilhoProdutos = listaNosFilhosProdutos.item(l);
if(nosFilhoProdutos.getNodeType() == Node.ELEMENT_NODE){
Element elementoNoProduto = (Element) nosFilhoProdutos;
switch(elementoNoProduto.getTagName()){
case "VDIPRONOME":
stmt.setString(1, elementoNoProduto.getTextContent());
break;
case "VDIQTDE":
stmt.setDouble(2, Double.parseDouble(elementoNoProduto.getTextContent()));
break;
case "VDIVALOR":
stmt.setDouble(3, Double.parseDouble(elementoNoProduto.getTextContent()));
break;
}
}
}
}
for(int i = 0; i < tamanhoDaLista; i++ ){
Node noDeVenda = listaDeVenda.item(i);
if(noDeVenda.getNodeType() == Node.ELEMENT_NODE){
Element elementoVenda = (Element) noDeVenda;
NodeList listaNosFilhosVenda = elementoVenda.getChildNodes();
int tamanhoListaNosFilhosVenda = listaNosFilhosVenda.getLength();
for(int j = 0; j < tamanhoListaNosFilhosVenda; j++){
Node noFilhosVenda = listaNosFilhosVenda.item(j);
if(noFilhosVenda.getNodeType() == Node.ELEMENT_NODE){
Element elementoNoVenda = (Element) noFilhosVenda;
switch(elementoNoVenda.getTagName()){
case "VEDCLINOME":
stmt.setString(4, elementoNoVenda.getTextContent());
break;
case "VEDID":
stmt.setInt(5, Integer.parseInt(elementoNoVenda.getTextContent()));
break;
case "VEDABERTURA":
try{
String dataSemFormatacao = elementoNoVenda.getTextContent();
Timestamp ts = Timestamp.valueOf(dataSemFormatacao);
stmt.setTimestamp(6, ts);
} catch (Exception e) {
e.printStackTrace();
}
break;
case "CLICPFCGC":
stmt.setString(7, elementoNoVenda.getTextContent());
break;
}
}
}
}
stmt.setInt(8, 123);
stmt.setString(9, "UN");
stmt.setDouble(10, 23.4);
stmt.setDouble(11, 14.3);
stmt.setDouble(12, 14.5);
stmt.setInt(13, 1);
stmt.setInt(14, 1);
stmt.setInt(15, 1);
stmt.execute();
stmt.close();
System.out.println("Importado com sucesso!");
}//FIM DO FOR DE DADOS USUARIO
}//FIM DO FOR DE PRODUTOS
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
}
}
}
public static String getFileExtensionName(File f) {
if (f.getName().indexOf(".") == -1) {
return "";
} else {
return f.getName().substring(f.getName().length() - 3, f.getName().length());
}
}
public void para(){
}
}
You can read this example. https://www.codejava.net/java-se/networking/ftp/java-ftp-file-download-tutorial-and-example
Step 1: Download file to local store.
Step 2: Read file XML.
Good luck,
I was able to execute the code by transforming the XML file into a string and then returning to be an xml, I hope it helps but someone.
public class MyFTPClass {
private static FTPFile[] obterArquivosDiretorio(FTPClient ftp, String dirPath) throws IOException {
String cwd = ftp.printWorkingDirectory();
ftp.changeWorkingDirectory(dirPath);
FTPFile[] files = ftp.listFiles();
ftp.changeWorkingDirectory(cwd);
return files;
}
public static void main(String args[]) throws SAXException, ParserConfigurationException {
// Create an instance of FTPClient
FTPClient ftp = new FTPClient();
try {
// Establish a connection with the FTP URL
ftp.connect("caminho_ftp");
// Enter user details : user name and password
boolean isSuccess = ftp.login("usuario", "senha");
if (isSuccess) {
// Fetch the list of names of the files. In case of no files an
// empty array is returned
String path = "ArquivosNotas";
FTPFile[] listedDirectories = obterArquivosDiretorio(ftp, path);
int countXml = 1;
// Iterate on the returned list to obtain name of each file
for (FTPFile file : listedDirectories) {
if (file.getName().toLowerCase().contains("xml")) {
System.out.println();
System.out.println("Lendo " + countXml + " xml");
System.out.println();
InputStream stream = ftp.retrieveFileStream("ArquivosNotas/" + file.getName());
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
// xml vai virar uma string para depois fazer o parse para o document
StringBuilder sb = new StringBuilder();
String inline = "";
while ((inline = reader.readLine()) != null) {
sb.append(inline);
}
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
// convert string do xml para xml em document
Document doc = (Document) builder.parse(new ByteArrayInputStream(sb.toString().getBytes()));
NodeList listaDeVenda = doc.getElementsByTagName("venda");
int tamanhoDaLista = listaDeVenda.getLength();
NodeList listaDeProdutos = doc.getElementsByTagName("item");
int tamanhoDaListaDeProdutos = listaDeProdutos.getLength();
System.out.println();
System.out.println("Qtde itens xml " + tamanhoDaListaDeProdutos);
System.out.println("Finalizado " + countXml + " xml");
System.out.println();
countXml++;
stream.close();
reader.close();
ftp.completePendingCommand();
}
}
}
ftp.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ftp.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
How to merge multiple pdf files (generated on run time) through ItextSharp then printing them.
I found the following link but that method requires the pdf names considering that the pdf files stored and this is not my case .
I have multiple reports i'll convert them to pdf files through this method :
private void AddReportToResponse(LocalReport followsReport)
{
string mimeType;
string encoding;
string extension;
string[] streams = new string[100];
Warning[] warnings = new Warning[100];
byte[] pdfStream = followsReport.Render("PDF", "", out mimeType, out encoding, out extension, out streams, out warnings);
//Response.Clear();
//Response.ContentType = mimeType;
//Response.AddHeader("content-disposition", "attachment; filename=Application." + extension);
//Response.BinaryWrite(pdfStream);
//Response.End();
}
Now i want to merge all those generated files (Bytes) in one pdf file to print it
If you want to merge source documents using iText(Sharp), there are two basic situations:
You really want to merge the documents, acquiring the pages in their original format, transfering as much of their content and their interactive annotations as possible. In this case you should use a solution based on a member of the Pdf*Copy* family of classes.
You actually want to integrate pages from the source documents into a new document but want the new document to govern the general format and don't care for the interactive features (annotations...) in the original documents (or even want to get rid of them). In this case you should use a solution based on the PdfWriter class.
You can find details in chapter 6 (especially section 6.4) of iText in Action — 2nd Edition. The Java sample code can be accessed here and the C#'ified versions here.
A simple sample using PdfCopy is Concatenate.java / Concatenate.cs. The central piece of code is:
byte[] mergedPdf = null;
using (MemoryStream ms = new MemoryStream())
{
using (Document document = new Document())
{
using (PdfCopy copy = new PdfCopy(document, ms))
{
document.Open();
for (int i = 0; i < pdf.Count; ++i)
{
PdfReader reader = new PdfReader(pdf[i]);
// loop over the pages in that document
int n = reader.NumberOfPages;
for (int page = 0; page < n; )
{
copy.AddPage(copy.GetImportedPage(reader, ++page));
}
}
}
}
mergedPdf = ms.ToArray();
}
Here pdf can either be defined as a List<byte[]> immediately containing the source documents (appropriate for your use case of merging intermediate in-memory documents) or as a List<String> containing the names of source document files (appropriate if you merge documents from disk).
An overview at the end of the referenced chapter summarizes the usage of the classes mentioned:
PdfCopy: Copies pages from one or more existing PDF documents. Major downsides: PdfCopy doesn’t detect redundant content, and it fails when concatenating forms.
PdfCopyFields: Puts the fields of the different forms into one form. Can be used to avoid the problems encountered with form fields when concatenating forms using PdfCopy. Memory use can be an issue.
PdfSmartCopy: Copies pages from one or more existing PDF documents. PdfSmartCopy is able to detect redundant content, but it needs more memory and CPU than PdfCopy.
PdfWriter: Generates PDF documents from scratch. Can import pages from other PDF documents. The major downside is that all interactive features of the imported page (annotations, bookmarks, fields, and so forth) are lost in the process.
I used iTextsharp with c# to combine pdf files. This is the code I used.
string[] lstFiles=new string[3];
lstFiles[0]=#"C:/pdf/1.pdf";
lstFiles[1]=#"C:/pdf/2.pdf";
lstFiles[2]=#"C:/pdf/3.pdf";
PdfReader reader = null;
Document sourceDocument = null;
PdfCopy pdfCopyProvider = null;
PdfImportedPage importedPage;
string outputPdfPath=#"C:/pdf/new.pdf";
sourceDocument = new Document();
pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
//Open the output file
sourceDocument.Open();
try
{
//Loop through the files list
for (int f = 0; f < lstFiles.Length-1; f++)
{
int pages =get_pageCcount(lstFiles[f]);
reader = new PdfReader(lstFiles[f]);
//Add pages of current file
for (int i = 1; i <= pages; i++)
{
importedPage = pdfCopyProvider.GetImportedPage(reader, i);
pdfCopyProvider.AddPage(importedPage);
}
reader.Close();
}
//At the end save the output file
sourceDocument.Close();
}
catch (Exception ex)
{
throw ex;
}
private int get_pageCcount(string file)
{
using (StreamReader sr = new StreamReader(File.OpenRead(file)))
{
Regex regex = new Regex(#"/Type\s*/Page[^s]");
MatchCollection matches = regex.Matches(sr.ReadToEnd());
return matches.Count;
}
}
Here is some code I pulled out of an old project I had. It was a web application but I was using iTextSharp to merge pdf files then print them.
public static class PdfMerger
{
/// <summary>
/// Merge pdf files.
/// </summary>
/// <param name="sourceFiles">PDF files being merged.</param>
/// <returns></returns>
public static byte[] MergeFiles(List<Stream> sourceFiles)
{
Document document = new Document();
MemoryStream output = new MemoryStream();
try
{
// Initialize pdf writer
PdfWriter writer = PdfWriter.GetInstance(document, output);
writer.PageEvent = new PdfPageEvents();
// Open document to write
document.Open();
PdfContentByte content = writer.DirectContent;
// Iterate through all pdf documents
for (int fileCounter = 0; fileCounter < sourceFiles.Count; fileCounter++)
{
// Create pdf reader
PdfReader reader = new PdfReader(sourceFiles[fileCounter]);
int numberOfPages = reader.NumberOfPages;
// Iterate through all pages
for (int currentPageIndex = 1; currentPageIndex <=
numberOfPages; currentPageIndex++)
{
// Determine page size for the current page
document.SetPageSize(
reader.GetPageSizeWithRotation(currentPageIndex));
// Create page
document.NewPage();
PdfImportedPage importedPage =
writer.GetImportedPage(reader, currentPageIndex);
// Determine page orientation
int pageOrientation = reader.GetPageRotation(currentPageIndex);
if ((pageOrientation == 90) || (pageOrientation == 270))
{
content.AddTemplate(importedPage, 0, -1f, 1f, 0, 0,
reader.GetPageSizeWithRotation(currentPageIndex).Height);
}
else
{
content.AddTemplate(importedPage, 1f, 0, 0, 1f, 0, 0);
}
}
}
}
catch (Exception exception)
{
throw new Exception("There has an unexpected exception" +
" occured during the pdf merging process.", exception);
}
finally
{
document.Close();
}
return output.GetBuffer();
}
}
/// <summary>
/// Implements custom page events.
/// </summary>
internal class PdfPageEvents : IPdfPageEvent
{
#region members
private BaseFont _baseFont = null;
private PdfContentByte _content;
#endregion
#region IPdfPageEvent Members
public void OnOpenDocument(PdfWriter writer, Document document)
{
_baseFont = BaseFont.CreateFont(BaseFont.HELVETICA,
BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
_content = writer.DirectContent;
}
public void OnStartPage(PdfWriter writer, Document document)
{ }
public void OnEndPage(PdfWriter writer, Document document)
{ }
public void OnCloseDocument(PdfWriter writer, Document document)
{ }
public void OnParagraph(PdfWriter writer,
Document document, float paragraphPosition)
{ }
public void OnParagraphEnd(PdfWriter writer,
Document document, float paragraphPosition)
{ }
public void OnChapter(PdfWriter writer, Document document,
float paragraphPosition, Paragraph title)
{ }
public void OnChapterEnd(PdfWriter writer,
Document document, float paragraphPosition)
{ }
public void OnSection(PdfWriter writer, Document document,
float paragraphPosition, int depth, Paragraph title)
{ }
public void OnSectionEnd(PdfWriter writer,
Document document, float paragraphPosition)
{ }
public void OnGenericTag(PdfWriter writer, Document document,
Rectangle rect, string text)
{ }
#endregion
private float GetCenterTextPosition(string text, PdfWriter writer)
{
return writer.PageSize.Width / 2 - _baseFont.GetWidthPoint(text, 8) / 2;
}
}
I didn't write this, but made some modifications. I can't remember where I found it. After I merged the PDFs I would call this method to insert javascript to open the print dialog when the PDF is opened. If you change bSilent to true then it should print silently to their default printer.
public Stream addPrintJStoPDF(Stream thePDF)
{
MemoryStream outPutStream = null;
PRStream finalStream = null;
PdfDictionary page = null;
string content = null;
//Open the stream with iTextSharp
var reader = new PdfReader(thePDF);
outPutStream = new MemoryStream(finalStream.GetBytes());
var stamper = new PdfStamper(reader, (MemoryStream)outPutStream);
var jsText = "var res = app.setTimeOut('this.print({bUI: true, bSilent: false, bShrinkToFit: false});', 200);";
//Add the javascript to the PDF
stamper.JavaScript = jsText;
stamper.FormFlattening = true;
stamper.Writer.CloseStream = false;
stamper.Close();
//Set the stream to the beginning
outPutStream.Position = 0;
return outPutStream;
}
Not sure how well the above code is written since I pulled it from somewhere else and I haven't worked in depth at all with iTextSharp but I do know that it did work at merging PDFs that I was generating at runtime.
Tested with iTextSharp-LGPL 4.1.6:
public static byte[] ConcatenatePdfs(IEnumerable<byte[]> documents)
{
using (var ms = new MemoryStream())
{
var outputDocument = new Document();
var writer = new PdfCopy(outputDocument, ms);
outputDocument.Open();
foreach (var doc in documents)
{
var reader = new PdfReader(doc);
for (var i = 1; i <= reader.NumberOfPages; i++)
{
writer.AddPage(writer.GetImportedPage(reader, i));
}
writer.FreeReader(reader);
reader.Close();
}
writer.Close();
outputDocument.Close();
var allPagesContent = ms.GetBuffer();
ms.Flush();
return allPagesContent;
}
}
To avoid the memory issues mentioned, I used file stream instead of memory stream(mentioned in ITextSharp Out of memory exception merging multiple pdf) to merge pdf files:
var parentDirectory = Directory.GetParent(SelectedDocuments[0].FilePath);
var savePath = parentDirectory + "\\MergedDocument.pdf";
using (var fs = new FileStream(savePath, FileMode.Create))
{
using (var document = new Document())
{
using (var pdfCopy = new PdfCopy(document, fs))
{
document.Open();
for (var i = 0; i < SelectedDocuments.Count; i++)
{
using (var pdfReader = new PdfReader(SelectedDocuments[i].FilePath))
{
for (var page = 0; page < pdfReader.NumberOfPages;)
{
pdfCopy.AddPage(pdfCopy.GetImportedPage(pdfReader, ++page));
}
}
}
}
}
}
****/*For Multiple PDF Print..!!*/****
<button type="button" id="btnPrintMultiplePdf" runat="server" class="btn btn-primary btn-border btn-sm"
onserverclick="btnPrintMultiplePdf_click">
<i class="fa fa-file-pdf-o"></i>Print Multiple pdf</button>
protected void btnPrintMultiplePdf_click(object sender, EventArgs e)
{
if (ValidateForMultiplePDF() == true)
{
#region Declare Temp Variables..!!
CheckBox chkList = new CheckBox();
HiddenField HidNo = new HiddenField();
string Multi_fofile, Multi_listfile;
Multi_fofile = Multi_listfile = "";
Multi_fofile = Server.MapPath("PDFRNew");
#endregion
for (int i = 0; i < grdRnew.Rows.Count; i++)
{
#region Find Grd Controls..!!
CheckBox Chk_One = (CheckBox)grdRnew.Rows[i].FindControl("chkOne");
Label lbl_Year = (Label)grdRnew.Rows[i].FindControl("lblYear");
Label lbl_No = (Label)grdRnew.Rows[i].FindControl("lblCode");
#endregion
if (Chk_One.Checked == true)
{
HidNo .Value = llbl_No .Text.Trim()+ lbl_Year .Text;
if (File.Exists(Multi_fofile + "\\" + HidNo.Value.ToString() + ".pdf"))
{
#region Get Multiple Files Name And Paths..!!
if (Multi_listfile != "")
{
Multi_listfile = Multi_listfile + ",";
}
Multi_listfile = Multi_listfile + Multi_fofile + "\\" + HidNo.Value.ToString() + ".pdf";
#endregion
}
}
}
#region For Generate Multiple Pdf..!!
if (Multi_listfile != "")
{
String[] Multifiles = Multi_listfile.Split(',');
string DestinationFile = Server.MapPath("PDFRNew") + "\\Multiple.Pdf";
MergeFiles(DestinationFile, Multifiles);
Response.ContentType = "pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + DestinationFile + "\"");
Response.TransmitFile(DestinationFile);
Response.End();
}
else
{
}
#endregion
}
}
private void MergeFiles(string DestinationFile, string[] SourceFiles)
{
try
{
int f = 0;
/**we create a reader for a certain Document**/
PdfReader reader = new PdfReader(SourceFiles[f]);
/**we retrieve the total number of pages**/
int n = reader.NumberOfPages;
/**Console.WriteLine("There are " + n + " pages in the original file.")**/
/**Step 1: creation of a document-object**/
Document document = new Document(reader.GetPageSizeWithRotation(1));
/**Step 2: we create a writer that listens to the Document**/
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(DestinationFile, FileMode.Create));
/**Step 3: we open the Document**/
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;
int rotation;
/**Step 4: We Add Content**/
while (f < SourceFiles.Length)
{
int i = 0;
while (i < n)
{
i++;
document.SetPageSize(reader.GetPageSizeWithRotation(i));
document.NewPage();
page = writer.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
}
else
{
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
/**Console.WriteLine("Processed page " + i)**/
}
f++;
if (f < SourceFiles.Length)
{
reader = new PdfReader(SourceFiles[f]);
/**we retrieve the total number of pages**/
n = reader.NumberOfPages;
/**Console.WriteLine("There are"+n+"pages in the original file.")**/
}
}
/**Step 5: we Close the Document**/
document.Close();
}
catch (Exception e)
{
string strOb = e.Message;
}
}
private bool ValidateForMultiplePDF()
{
bool chkList = false;
foreach (GridViewRow gvr in grdRnew.Rows)
{
CheckBox Chk_One = (CheckBox)gvr.FindControl("ChkSelectOne");
if (Chk_One.Checked == true)
{
chkList = true;
}
}
if (chkList == false)
{
divStatusMsg.Style.Add("display", "");
divStatusMsg.Attributes.Add("class", "alert alert-danger alert-dismissable");
divStatusMsg.InnerText = "ERROR !!...Please Check At Least On CheckBox.";
grdRnew.Focus();
set_timeout();
return false;
}
return true;
}
I am new to Docx4j ,Need help to split docx file based on string using docx4j Java,So that it writes output into multiple files.
I tried to do the same using Apache POI and got the output,however when tried to convert it into HTML, got issues on style missing,also added styles later, still facing the same issue.
Below is the code using apache poi:
public static int pos = 0;
public static int posc = 0;
public static String ind = "n";
final static int DEFAULT_FONT_SIZE = 10;
public static void main(String[] args) throws FileNotFoundException,
IOException, XmlException {
File file = null;
File outfilep = null;
File outfilec = null;
File dir = new File(PropertyUtils.getProperty("INPUT_DIR"));
String[] files = dir.list();
if (files.length == 0) {
System.out.println("The directory is empty");
} else {
for (String aFile : files) {
System.out.println(aFile);
file = new File(PropertyUtils.getProperty("INPUT_DIR") + aFile
+ "/" + aFile + ".docx");
outfilep = new File(PropertyUtils.getProperty("INPUT_DIR")
+ aFile + "/" + aFile + "-Product.docx");
outfilec = new File(PropertyUtils.getProperty("INPUT_DIR")
+ aFile + "/" + aFile + "-Component.docx");
// Write Soruce file
}
}
XWPFDocument doc = new XWPFDocument(new FileInputStream(file));
XWPFDocument destDoc = new XWPFDocument();
copyLayout(doc, destDoc);
XWPFDocument destDocc = new XWPFDocument();
OutputStream out = new FileOutputStream(outfilep);
OutputStream outc = new FileOutputStream(outfilec);
for (IBodyElement bodyElement : doc.getBodyElements()) {
BodyElementType elementType = bodyElement.getElementType();
if (elementType.name().equals("PARAGRAPH")) {
XWPFParagraph pr = (XWPFParagraph) bodyElement;
if (pr.getText().contains("CONSTRUCTION DETAILS:"))
{
ind = "y";
System.out.println("ind is Y++++++++++++");
}
if (ind == "n")
{
copyStyle(doc, destDoc,
doc.getStyles().getStyle(pr.getStyleID()));
XWPFParagraph dstPr = destDoc.createParagraph();
dstPr.createRun();
pos = destDoc.getParagraphs().size() - 1;
CTPPr ppr = pr.getCTP().getPPr();
if (ppr == null) ppr = pr.getCTP().addNewPPr();
CTSpacing spacing = ppr.isSetSpacing()? ppr.getSpacing() : ppr.addNewSpacing();
spacing.setAfter(BigInteger.valueOf(0));
spacing.setBefore(BigInteger.valueOf(0));
spacing.setLineRule(STLineSpacingRule.AUTO);
spacing.setLine(BigInteger.valueOf(240));
destDoc.setParagraph(pr, pos);
// System.out.println("prod "
// + destDoc.getParagraphArray(pos).getParagraphText());
}
else {
copyStyle(doc, destDocc,
doc.getStyles().getStyle(pr.getStyleID()));
XWPFParagraph dstPrr = destDocc.createParagraph();
dstPrr.createRun();
pos = destDocc.getParagraphs().size() - 1;
CTPPr ppr = pr.getCTP().getPPr();
if (ppr == null) ppr = pr.getCTP().addNewPPr();
CTSpacing spacing = ppr.isSetSpacing()? ppr.getSpacing() : ppr.addNewSpacing();
spacing.setAfter(BigInteger.valueOf(0));
spacing.setBefore(BigInteger.valueOf(0));
spacing.setLineRule(STLineSpacingRule.AUTO);
spacing.setLine(BigInteger.valueOf(240));
destDocc.setParagraph(pr, pos);
//// System.out.println("comp "
//// + destDoc.getParagraphArray(pos).getParagraphText());
}
} else if (elementType.name().equals("TABLE")) {
XWPFTable table = (XWPFTable) bodyElement;
if (ind == "n")
{
copyStyle(doc, destDoc,
doc.getStyles().getStyle(table.getStyleID()));
destDoc.createTable();
pos = destDoc.getTables().size() - 1;
destDoc.setTable(pos, table);
// System.out.println("prodtable " + destDoc.getParagraphArray(pos).getParagraphText());
}
else {
copyStyle(doc, destDocc,
doc.getStyles().getStyle(table.getStyleID()));
destDocc.createTable();
pos = destDocc.getTables().size() - 1;
destDocc.setTable(pos, table);
// System.out.println("comptable " + destDoc.getParagraphArray(pos).getParagraphText());
}
}
}
destDoc.write(out);
destDocc.write(outc);
}
// Copy Styles of Table and Paragraph.
private static void copyStyle(XWPFDocument srcDoc, XWPFDocument destDoc,
XWPFStyle style) {
if (destDoc == null || style == null)
return;
if (destDoc.getStyles() == null) {
destDoc.createStyles();
}
List<XWPFStyle> usedStyleList = srcDoc.getStyles().getUsedStyleList(
style);
for (XWPFStyle xwpfStyle : usedStyleList) {
destDoc.getStyles().addStyle(xwpfStyle);
}
}
private static void copyLayout(XWPFDocument srcDoc, XWPFDocument destDoc)
{
CTPageMar pgMar = srcDoc.getDocument().getBody().getSectPr().getPgMar();
BigInteger bottom = pgMar.getBottom();
BigInteger footer = pgMar.getFooter();
BigInteger gutter = pgMar.getGutter();
BigInteger header = pgMar.getHeader();
BigInteger left = pgMar.getLeft();
BigInteger right = pgMar.getRight();
BigInteger top = pgMar.getTop();
CTPageMar addNewPgMar = destDoc.getDocument().getBody().addNewSectPr().addNewPgMar();
addNewPgMar.setBottom(bottom);
addNewPgMar.setFooter(footer);
addNewPgMar.setGutter(gutter);
addNewPgMar.setHeader(header);
addNewPgMar.setLeft(left);
addNewPgMar.setRight(right);
addNewPgMar.setTop(top);
CTPageSz pgSzSrc = srcDoc.getDocument().getBody().getSectPr().getPgSz();
BigInteger code = pgSzSrc.getCode();
BigInteger h = pgSzSrc.getH();
Enum orient = pgSzSrc.getOrient();
BigInteger w = pgSzSrc.getW();
CTPageSz addNewPgSz = destDoc.getDocument().getBody().addNewSectPr().addNewPgSz();
addNewPgSz.setCode(code);
addNewPgSz.setH(h);
addNewPgSz.setOrient(orient);
addNewPgSz.setW(w);
}
Splitting a docx is easy enough to do in a brute force kind of a way: you can delete the content (paragraphs etc) you don't want, then save the result.
This way, the original relationships will stay intact, but your docx container may be bigger than necessary, since it might have images etc which are no longer used.
Done this way, there are still things you need to look out for:
splitting between a bookmark start and end tag (same for comments)
automatic numbering might give the wrong start number, unless you set start at
Obviously you could write code to address such issues.
Alternatively, with our commercial Enterprise edition of docx4j, you can use its "merge" code to say you want say paragraphs X to Y, and it'll give you a docx containing only that (ie no extraneous images in the docx container, split bookmarks taken care of etc).
I hope this will solve the issue.
public class SplitUsingDocx4j {
/**
* #param args
* #throws Docx4JException
* #throws FileNotFoundException
*/
public static void main(String[] args) throws Docx4JException,
FileNotFoundException {
File dir = new File(PropertyUtils.getProperty("INPUT_DIR"));
String[] files = dir.list();
File file = null;
if (files.length == 0) {
System.out.println("The directory is empty");
} else {
for (String aFile : files) {
System.out.println(aFile);
file = new File(PropertyUtils.getProperty("INPUT_DIR") + aFile
+ "/" + aFile + ".docx");
}
}
// Creating new documents
WordprocessingMLPackage doc1 = WordprocessingMLPackage.createPackage();
WordprocessingMLPackage doc2 = WordprocessingMLPackage.createPackage();
// loading existing document
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.load(new java.io.File(file.getPath()));
MainDocumentPart tempDocPart = wordMLPackage.getMainDocumentPart();
List<Object> obj = wordMLPackage.getMainDocumentPart().getContent();
// for copying styles from existing doc to new docs
StyleDefinitionsPart sdp = tempDocPart.getStyleDefinitionsPart();
Styles tempStyle = sdp.getJaxbElement();
doc1.getMainDocumentPart().getStyleDefinitionsPart()
.setJaxbElement(tempStyle);
doc2.getMainDocumentPart().getStyleDefinitionsPart()
.setJaxbElement(tempStyle);
boolean flag = false;
for (Object object : obj) {
if (!flag) {
if (object.toString().equalsIgnoreCase("CONSTRUCTION DETAILS:")) {
flag = true;
}
doc1.getMainDocumentPart().addObject(object);
} else {
doc2.getMainDocumentPart().addObject(object);
}
}
String fileName = file.getName().toString().replace(".docx", "");
doc1.save(new File(fileName + "-1.docx"));
doc2.save(new File(fileName + "-2.docx"));
}}