Skip to content

Image is not getting inserted in a existing PDF #575

@surojitsalt

Description

@surojitsalt

I have an existing PDF, and I want to insert an image at a specific location. There are no errors when using the PDF or executing the code, but the image is not appearing. However, it works with other PDF files.

private String FILE_NAME ="my.pdf";
private int FILE_ID = R.raw.sam;
private String FILE_NAME1 ="my1.pdf";
private String IMAGE_FILE_NAME ="myImage.png";

public void addImage(File pFile) {
try {

        if (!pFile.exists()) {
            pFile.createNewFile();
        }
        PDDocument vDocument = PDDocument.load(pFile);

        //get the number of pages
        PDPage vPage =  vDocument.getPage(0);
        PDPageContentStream vContentStream;

        File vFile = new File(getCacheDir(), IMAGE_FILE_NAME);
        File vFileNew = new File(getCacheDir(), FILE_NAME1);
        copyToCache(vFile, R.raw.sample1);

        // Define a content stream for adding to the PDF
        vContentStream = new PDPageContentStream(vDocument, vPage, PDPageContentStream.AppendMode.APPEND, true, true);


        // Draw the falcon base image
        PDImageXObject vImage = PDImageXObject.createFromFileByContent(vFile,vDocument);
        /*
        Bitmap vBitMap = BitmapFactory.decodeResource(getResources(), R.drawable.sample1);
        ByteArrayOutputStream vBAOS = new ByteArrayOutputStream();
        vBitMap.compress(Bitmap.CompressFormat.JPEG, 100, vBAOS);
        InputStream vInStream = new ByteArrayInputStream(vBAOS.toByteArray());
        PDImageXObject vImage = JPEGFactory.createFromStream(vDocument, vInStream);
        */
        vContentStream.drawImage(vImage, 50, 600,100,100);
        vContentStream.saveGraphicsState();
        vContentStream.restoreGraphicsState();
        vContentStream.close();

        // Save the final pdf document to a file
        vDocument.save(vFileNew);
        vDocument.close();

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

private void copyToCache(File pFile, @RawRes int pResourceID) throws IOException {
    //Get input stream object to read the pdf
    InputStream vInput = getResources().openRawResource(pResourceID);
    FileOutputStream vOutput = new FileOutputStream(pFile);
    byte[] vBuffer = new byte[1024];
    int vSize;

    // Copy the entire contents of the file
    while ((vSize = vInput.read(vBuffer)) != -1) {
        vOutput.write(vBuffer, 0, vSize);
    }

    //Close the buffer
    vInput.close();
    vOutput.close();
}

sam.pdf

Expected behavior
The image should be inserted at the correct location.

Actual behavior
No exception is occurring, but the image is not appearing.

Environment details:
implementation 'com.tom-roush:pdfbox-android:2.0.27.0'
compileSdk 34

defaultConfig {
    applicationId "com.sample.pdfwriter"
    minSdk 24
    //noinspection EditedTargetSdkVersion
    targetSdk 34
    versionCode 1
    versionName "1.0"

    multiDexEnabled true
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugExisting feature doesn't work correctly

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions