0

The following snippet generates a pdf with text but the image is distorted like this

Seems the required image should be a .jpg. When I used a .png, not even a distorted image was showing in the pdf.

public static void createPdf() {
    try {
        //Image image = Image.createImage("/icon.png");
        Image image = Image.createImage("/Logo.jpg");
        EncodedImage encodedImage = EncodedImage.createFromImage(image, true);
        byte[] imageData = encodedImage.getImageData();

        StringBuilder content = new StringBuilder();
        content.append("%PDF-1.4\n");
        content.append("1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj\n");
        content.append("2 0 obj<</Type/Pages/Kids [3 0 R]/Count 1>>endobj\n");
        content.append("3 0 obj<</Type/Page/Parent 2 0 R/Resources 4 0 R/MediaBox [0 0 520 800]/Contents 6 0 R>>endobj\n");
        content.append("4 0 obj<</Font<</F1 5 0 R>>/XObject<</Im0 7 0 R>>>>endobj\n");
        content.append("5 0 obj<</Type/Font/Subtype/Type1/BaseFont/Helvetica>>endobj\n");
        content.append("6 0 obj<</Length 219>>stream\n");
        content.append("BT /F1 24 Tf 175 720 Td (Codename One)Tj ET\n");
        content.append("BT /F1 20 Tf 100 700 Td (Using one codebase, build)Tj ET\n");
        content.append("BT /F1 20 Tf 0 660 Td (Android apps)Tj ET\n");
        content.append("BT /F1 20 Tf 0 640 Td (iOS apps)Tj ET\n");
        content.append("BT /F1 20 Tf 0 600 Td (UWP apps)Tj ET\n");
        content.append("512 0 0 512 4 50 cm\n");
        content.append("/Im0 Do\n");
        content.append("endstream\n");
        content.append("endobj\n");
        content.append("7 0 obj<</Type/XObject/Subtype/Image/Width 512/Height 512/ColorSpace/DeviceRGB/BitsPerComponent 8/Filter/DCTDecode/Length 132959>>stream\n");
        
        content.append(new String(imageData));

        content.append("endstream\n");
        content.append("endobj\n");
        content.append("\n");
        content.append("xref\n");
        content.append("0 8\n");
        content.append("0000000000 65535 f \n");
        content.append("0000000009 00000 n \n");
        content.append("0000000052 00000 n \n");
        content.append("0000000102 00000 n \n");
        content.append("0000000197 00000 n \n");
        content.append("0000000255 00000 n \n");
        content.append("0000000316 00000 n \n");
        content.append("0000000609 00000 n \n");
        content.append("trailer\n");
        content.append("<</Size 8/Root 1 0 R>>\n");
        content.append("startxref\n");
        content.append("133724\n");
        content.append("%%EOF\n");
        
        FileSystemStorage fss = FileSystemStorage.getInstance();
        String pdfPath = fss.getAppHomePath() + "Test2.pdf";
        try (Writer w = new OutputStreamWriter(fss.openOutputStream(pdfPath))) {
            w.write(content.toString());
        } catch (Exception e) {
            Log.p("Error " + e);
        }
    } catch (Exception e) {
        Log.p("Error " + e);
    }
}

The generated pdf is saved in app storage directory home/.cn1

How can the required raw image data be extracted from the image so that image can show without distortion?

1
  • This question is similar to: How to insert an image into PDF source code. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem.
    – Luuk
    Commented 8 mins ago

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.