2013-07-24 22 views
16
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.OutputStream; 
import com.itextpdf.text.Document; 
import com.itextpdf.text.Paragraph; 
import com.itextpdf.text.pdf.PdfWriter; 

public class GeneratePDF { 
    public static void main(String[] args) { 
     try { 

      String k = "<html><body> This is my Project </body></html>"; 

      OutputStream file = new FileOutputStream(new File("E:\\Test.pdf")); 

      Document document = new Document(); 
      PdfWriter.getInstance(document, file); 

      document.open(); 

      document.add(new Paragraph(k)); 

      document.close(); 
      file.close(); 

     } catch (Exception e) { 

      e.printStackTrace(); 
     } 
    } 
} 

Bu, HTML'yi PDF'ye dönüştürme kodum. Ben sadece dönüştürmek için mümkün ancak PDF dosyası içinde tüm HTML olarak kaydeder. <html><body> This is my Project </body></html>, sadece This is my Project'u kaydettirirken PDF'ye kaydedilir.HTML'yi PDF'ye dönüştürmek için nasıl kullanılır iText

+0

bu deneyin http://stackoverflow.com/questions/4712641/java-how-to-convert-a-html -de-pdf-olmadan-formatlama-formatlama – pratZ

+0

Ben java dönüştürmek gerekir –

+0

[Apache açık ofis API] (http://wiki.openoffice.org/wiki/API/Tutorials/PDF_export#How_to_use_it_from_Java) –

cevap

39

Yapabilirsin PDF HTML dönüştürmek verilir HTMLWorker sınıfıyla (onaylanmayan) bu gibi:

import com.itextpdf.text.html.simpleparser.HTMLWorker; 
//... 
try { 
    String k = "<html><body> This is my Project </body></html>"; 
    OutputStream file = new FileOutputStream(new File("C:\\Test.pdf")); 
    Document document = new Document(); 
    PdfWriter.getInstance(document, file); 
    document.open(); 
    HTMLWorker htmlWorker = new HTMLWorker(document); 
    htmlWorker.parse(new StringReader(k)); 
    document.close(); 
    file.close(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

veya XMLWorker kullanılarak (this jar indirin) Bu kodu kullanarak:

import com.itextpdf.tool.xml.XMLWorkerHelper; 
//... 
try { 
    String k = "<html><body> This is my Project </body></html>"; 
    OutputStream file = new FileOutputStream(new File("C:\\Test.pdf")); 
    Document document = new Document(); 
    PdfWriter writer = PdfWriter.getInstance(document, file); 
    document.open(); 
    InputStream is = new ByteArrayInputStream(k.getBytes()); 
    XMLWorkerHelper.getInstance().parseXHtml(writer, document, is); 
    document.close(); 
    file.close(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
+1

Ama Html Worker sınıfı kullanımdan kaldırılmıyor, bu yüzden lütfen Html Workerclass için hangi jar dosyasını ihtiyacımız olduğunu söyleyin? –

+0

ithalat – MaVRoSCy

+0

benim güncelleme göreceksiniz Ama yine Bu benim Proje ben sadece bu benim proje html –

1

Bu bağlantıların dönüştürülmesi yararlı olabilir.

https://code.google.com/p/flying-saucer/

https://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html

bir üniversite projesi, hatta bunlar için gidebilirsiniz ise, http://pd4ml.com/examples.htm

Örnek

+0

Projemde hangi jar dosyasını eklememiz gerekiyor? –

+0

Bağlantının kendisinde bu yardımı alacaksın, Hala bu linke git, zip'i kopyala, jar al, buildpath'i koy: https://code.google.com/p/flying-saucer/downloads/list – Jayesh

İlgili konular