androidpdf-generationtextfieldandroid-droidtext

Remove appearance of TextField in droidText : Android


I am trying to create a PDF using droidText library. There I want to place some text content in the middle of the page but align to left. I was unable to do with Paragraph class and setAlignment() method. So I have decided to use TextField class. Below is the code I have used,

Document document = new Document(PageSize.A4);

try {

    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(android.os.Environment.getExternalStorageDirectory() + java.io.File.separator + "TextFields.pdf"));

    document.open();

    TextField tf = new TextField(writer, new Rectangle(100, 300, 200, 350), "Content");
    tf.setText("This is the content of text field");
    tf.setAlignment(Element.ALIGN_CENTER);
    tf.setOptions(TextField.MULTILINE | TextField.REQUIRED);

    PdfFormField field = tf.getTextField();
    writer.addAnnotation(field);

} catch (DocumentException de) {
    System.err.println(de.getMessage());
} catch (IOException ioe) {
    System.err.println(ioe.getMessage());
}

document.close();

Code works perfect, but the out put PDF file that I am getting has this TextField which can be editable and also change the font appearance when I am click on the TextField. I don't want that in to my final PDF. How can I remove that attribute? If it is not possible, is there any other way I can position some text in a PDF file using droidText library in Android?


Solution

    1. You're using a totally unsupported software library. See http://lowagie.com/iText2 There is an official Android port of the most recent iText version available at iText Software. It doesn't need Apache Harmony, and it uses SpongyCastle instead of BouncyCastle to fix issue with clashing BC versions. See http://lowagie.com/iText2
    2. Seems like you want to add a Watermark. In that case, you don't really need a TextField. The TextField object is used to create an interactive form (and you indicate that this isn't what you want). The easiest way to add a Phrase in the middle of a page, is to use ColumnText.showTextAligned(). See the examples of chapter 3 of "iText in Action — Second Edition" for more info.