Add image to a specific location using iText in Android

无人久伴 提交于 2020-05-16 10:56:10

问题


I want to add image to a specific location in a PDF file using iText in Android. This is a fillable form and I have added textbox that is a place holder for the image and what I want to do is to get that textbox and image to it like this.

public class FormFill {
    public static void fill(AcroFields form, Person person) throws IOException, DocumentException{
        form.setField("firstname", person.getFirstName());
        form.setField("lastname", person.getLastName());
        form.setField("imagetextbox", "???");   

    }

I have the image uri like so

Uri imageUri = Uri.parse(person.getImagePath());

Any help would be appreciated.


回答1:


try this. i have used this function for add image in document.

public void addLogo(Document document) throws DocumentException {
    try { // Get user Settings GeneralSettings getUserSettings =

        Rectangle rectDoc = document.getPageSize();
        float width = rectDoc.getWidth();
        float height = rectDoc.getHeight();
        float imageStartX = width - document.rightMargin() - 315f;
        float imageStartY = height - document.topMargin() - 80f;

        System.gc();

        InputStream ims = getAssets().open("splashscreen.jpg");
        Bitmap bmp = BitmapFactory.decodeStream(ims);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();

        bmp.compress(Bitmap.CompressFormat.JPEG, 50, stream);

        byte[] byteArray = stream.toByteArray();
        // PdfImage img = new PdfImage(arg0, arg1, arg2)

        // Converting byte array into image Image img =
        Image img = Image.getInstance(byteArray); // img.scalePercent(50);
        img.setAlignment(Image.TEXTWRAP);
        img.scaleAbsolute(200f, 50f);
        img.setAbsolutePosition(imageStartX, imageStartY); // Adding Image
        document.add(img);

    } catch (Exception e) {
        e.printStackTrace();
    }

}



回答2:


I ended adding the image like so

                Image image = Image.getInstance(stream.toByteArray());
                PdfContentByte overContent = stamper.getOverContent(1);
                overContent.addImage(image);


来源:https://stackoverflow.com/questions/29910694/add-image-to-a-specific-location-using-itext-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!