2011-09-06 52 views
2

我正在寻找一个用monodroid创建pdf文件的解决方案。这也可能是我填写内容的pdf格式。我尝试了不同的库,如pdfsharp_on_mono或itextsharp,但它不起作用。创建一个新的空pdf文件是没有问题的。但是当我尝试填写内容时,总会有错误。用monodroid创建或填写PDF

我的目标是要有一个PDF文件,在稍后的时间应该通过一个XML文件填充。目前我会很高兴,如果我只是可以创建一个PDF并“写”一些东西。 有没有人提示,我怎么能实现它?我是一个非常喜欢monodroid的人。

如果您需要代码或错误消息,请说。我尝试过不同的解决方案。 欢呼声 anna

ps:抱歉我的英语不好。

回答

0

使用iText库我们可以在android应用程序中创建PDF文件。

import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import com.itextpdf.text.Anchor; 
import com.itextpdf.text.BadElementException; 
import com.itextpdf.text.Chapter; 
import com.itextpdf.text.Document; 
import com.itextpdf.text.DocumentException; 
import com.itextpdf.text.Element; 
import com.itextpdf.text.Font; 
import com.itextpdf.text.Paragraph; 
import com.itextpdf.text.Phrase; 
import com.itextpdf.text.Section; 
import com.itextpdf.text.pdf.PdfPCell; 
import com.itextpdf.text.pdf.PdfPTable; 
import com.itextpdf.text.pdf.PdfWriter; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.os.Environment; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
public class CreatePdf extends ActionBarActivity implements OnClickListener { 

private static String FILE = Environment.getExternalStorageDirectory() 
     .getAbsolutePath() + "/filename.pdf"; 
private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, 
     Font.BOLD); 
private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, 
     Font.BOLD); 
Button createPdf; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_create_pdf); 
    createPdf = (Button) findViewById(R.id.createBtn); 
    createPdf.setOnClickListener(this); 

} 

private void createPdf() { 
    try { 
     Document document = new Document(); 
     PdfWriter.getInstance(document, new FileOutputStream(FILE)); 
     document.open(); 
     addContent(document); 
     document.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (DocumentException e) { 
     e.printStackTrace(); 
    } 
} 

private static void addContent(Document document) throws DocumentException { 

    Anchor anchor = new Anchor("Anchor", catFont); 
    anchor.setName("Hello PDF"); 

    // Second parameter is the number of the chapter 
    Chapter catPart = new Chapter(0); 

    Paragraph subPara = new Paragraph("Android PDF Created", subFont); 
    addEmptyLine(subPara, 1); 
    Section subCatPart = catPart.addSection(subPara); 

    Paragraph paragraph = new Paragraph(); 
    addEmptyLine(paragraph, 5); 
    // subCatPart.add(paragraph); 
    // Add a table 
    createTable(subCatPart); 
    // Now add all this to the document 
    document.add(catPart); 

} 

private static void createTable(Section subCatPart) 
     throws BadElementException { 

    PdfPTable table = new PdfPTable(4); 
    PdfPCell c1 = new PdfPCell(new Phrase("Cell 1")); 
    c1.setHorizontalAlignment(Element.ALIGN_CENTER); 
    table.addCell(c1); 

    c1 = new PdfPCell(new Phrase("Cell 2")); 
    c1.setHorizontalAlignment(Element.ALIGN_CENTER); 
    table.addCell(c1); 

    c1 = new PdfPCell(new Phrase("Cell 3")); 
    c1.setHorizontalAlignment(Element.ALIGN_CENTER); 
    table.addCell(c1); 

    c1 = new PdfPCell(new Phrase("Cell 4")); 
    c1.setHorizontalAlignment(Element.ALIGN_CENTER); 
    table.addCell(c1); 
    subCatPart.add(table); 
} 

// method to add empty line 
private static void addEmptyLine(Paragraph paragraph, int number) { 
    for (int i = 0; i < number; i++) { 
     paragraph.add(new Paragraph(" ")); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.create_pdf, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.createBtn: 

     createPdf(); 
     break; 

    default: 
     break; 
    } 
} 
}