Converter rtf para pdf

12 respostas
K

Olá pessoal,

gostaria de saber como eu faço para converter um arquivo rtf em pdf usando itext 1.3.1 e itext-rtf 2.1.4
Estou usando o seguinte codigo:

Document document = new Document();

			String inputFile = "C:temp/Arquivo.rtf";

	        // create a PDF writer to save the new document to disk
	        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:temp/ArquivoModificado.pdf"));
	        // open the document for modifications
	        document.open();

	        // create a new parser to load the RTF file
	        RtfParser parser = new RtfParser(null);
	        // read the rtf file into a compatible document
	        parser.convertRtfDocument(new FileInputStream(inputFile), document);

	        // save the pdf to disk
	        document.close();

	        System.out.println("Finished");

Exibindo a seguinte mensagem:

The document is open; you can only add Elements with content.

Att

12 Respostas

X

KassiPretti:
Olá pessoal,

gostaria de saber como eu faço para converter um arquivo rtf em pdf usando itext 1.3.1 e itext-rtf 2.1.4
Estou usando o seguinte codigo:

Document document = new Document();

			String inputFile = "C:temp/Arquivo.rtf";

	        // create a PDF writer to save the new document to disk
	        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:temp/ArquivoModificado.pdf"));
	        // open the document for modifications
	        document.open();

	        // create a new parser to load the RTF file
	        RtfParser parser = new RtfParser(null);
	        // read the rtf file into a compatible document
	        parser.convertRtfDocument(new FileInputStream(inputFile), document);

	        // save the pdf to disk
	        document.close();

	        System.out.println("Finished");

Exibindo a seguinte mensagem:

The document is open; you can only add Elements with content.

Att

o problema esta aqui

String inputFile = "C:temp/Arquivo.rtf";
 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:temp/ArquivoModificado.pdf"));

esta faltando a barra

String inputFile = "C:/temp/Arquivo.rtf"; PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/temp/ArquivoModificado.pdf"));

K

avsouza foi erro de digitação msm
O erro continua =/

K

[atualizar]

X

testei seu codigo, porem com iText-rtf-2.1.5, gerou o pdf sem problema, só que errado.

G

Testei o código acima mas estou com o seguinte erro.

Exception in thread "main" java.lang.IllegalAccessError: tried to access method com.lowagie.text.Chunk.<init>()V from class com.lowagie.text.rtf.parser.destinations.RtfDestinationDocument
	at com.lowagie.text.rtf.parser.destinations.RtfDestinationDocument.handleCloseGroup(Unknown Source)
	at com.lowagie.text.rtf.parser.RtfParser.handleCloseGroup(Unknown Source)
	at com.lowagie.text.rtf.parser.RtfParser.tokenise(Unknown Source)
	at com.lowagie.text.rtf.parser.RtfParser.convertRtfDocument(Unknown Source)
	at com.aton.rtf.RtfToPdf.CopyOfApp.main(CopyOfApp.java:28)

Alguém sabe me dizer porque?

G

Resolvi, era a versão do itext. :slight_smile:

K

A questão de estar gerando errado o pdf não tem problema, pois eu não estava conseguindo executar.
Vou tentar usar a versão 2.1.5.

K

Pôxa, funcionou esse código?

Eu naõ consegui transformar um arquivo rtf em pdf!

F

Pessoal alguém sabe como setar o charset neste exemplo? Eu tenho vários rtf’s e eles possuem acentos que geram caracteres errados no pdf final… Alias esse itext-rtf foi descontinuado?
Se alguém puder ajudar eu agradeço!

S

o iText-rtf-2.1.5.jar esta implementado apenas para importar negrito? ou ele tambem importa italico e sublinhado?

Aqui só funcionou para o negrito.

M

No meu sistema está gerando um pdf em branco, alguém sabe dizer o por quê?

veja o código:

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.rtf.parser.RtfParser;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {

    public void gerarPdf(String inputFile, String outputFile) {

        // create a new document
        Document document = new Document();

        // create a PDF writer to save the new document to disk
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // open the document for modifications
        document.open();

        // create a new parser to load the RTF file
        RtfParser parser = new RtfParser(null);

        // read the rtf file into a compatible document
        try {
            parser.convertRtfDocument(new FileInputStream(inputFile), document);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // save the pdf to disk
        document.close();

        System.out.println("Finished");

    }

    public static void main(String[] args) {
        Main m = new Main();
        m.gerarPdf("D:/contrato.rtf", "D:/ContratoModificado.pdf");
    }
}

Na execução apresenta a seguinte mensagem:

run:
Exception in thread "main" java.lang.NullPointerException
at com.lowagie.text.rtf.parser.destinations.RtfDestinationFontTable.importSystemFonts(Unknown Source)
at com.lowagie.text.rtf.parser.destinations.RtfDestinationFontTable.init(Unknown Source)
at com.lowagie.text.rtf.parser.destinations.RtfDestinationFontTable.setParser(Unknown Source)
at com.lowagie.text.rtf.parser.destinations.RtfDestinationMgr.addDestination(Unknown Source)
at com.lowagie.text.rtf.parser.ctrlwords.RtfCtrlWordHandler.(Unknown Source)
at com.lowagie.text.rtf.parser.ctrlwords.RtfCtrlWordMap.(Unknown Source)
at com.lowagie.text.rtf.parser.ctrlwords.RtfCtrlWordMgr.(Unknown Source)
at com.lowagie.text.rtf.parser.RtfParser.init(Unknown Source)
at com.lowagie.text.rtf.parser.RtfParser.convertRtfDocument(Unknown Source)
at converterrtfempdf.Main.gerarPdf(Main.java:38)
at converterrtfempdf.Main.main(Main.java:56)
Java Result: 1
CONSTRUÍDO COM SUCESSO (tempo total: 4 segundos)

M

Arrumei uma solução com o JodConverter e OpenOffice, dica do vega! neste tópico: http://www.guj.com.br/posts/list/0/226779.java#1166271

Funciona perfeitamente, inclusive formatação.

Criado 6 de maio de 2009
Ultima resposta 15 de dez. de 2010
Respostas 12
Participantes 7