【问题标题】:Add image to a specific location using iText in Android在 Android 中使用 iText 将图像添加到特定位置
【发布时间】:2015-04-28 05:03:27
【问题描述】:

我想在 Android 中使用 iText 将图像添加到 PDF 文件中的特定位置。这是一个可填写的表格,我添加了作为图像占位符的文本框,我想要做的是像这样获取该文本框和图像。

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", "???");   

    }

我有这样的图像 uri

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

任何帮助将不胜感激。

【问题讨论】:

标签: android itext


【解决方案1】:

试试这个。 我已经使用此功能在文档中添加图像。

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();
    }

}

【讨论】:

  • 哇,这个解决方案看起来很棒,我希望我可以让它工作,虽然我没有 Document 的实例,但我有一个 Stamper 的实例。我会试试看能不能成功。
【解决方案2】:

我像这样结束了添加图像

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    相关资源
    最近更新 更多