android - 在 Android 中使用 PdfJet 库在 Box 上绘制图像时遇到问题

标签 android pdf pdf-generation

我正在使用 Android 中的 PdfJet 库创建 Pdf 文件。一切顺利,但我在 Box 上绘制图像时遇到了一些问题。当我执行程序时,Pdf 被创建并且盒子也被创建但是图像没有放在盒子里。

这是我的代码。

   File file = new File(Environment.getExternalStorageDirectory(),
                "Images.pdf");

        FileOutputStream fos = new FileOutputStream(file);

        PDF pdf = new PDF(fos);
        Page page = new Page(pdf, A3.PORTRAIT);
        Font f1 = new Font(pdf, CoreFont.HELVETICA);
        f1.setSize(12.0f);

        InputStream is = getAssets().open("myImage.jpg");
        Image image1 = new Image(pdf, is, ImageType.JPG);

         Box bo = new Box();
         bo.setPosition(10,10);
         bo.setSize(page.getWidth()-50.0f, page.getHeight()-50.0f);
         image1.placeIn(bo);
         bo.drawOn(page);

         pdf.flush();
         fos.close();

欢迎任何对此问题有好的建议和答案的人。

最佳答案

请找到以下示例以使用 pdfJet 从 sdcard 和过去获取图像。当然对你有帮助

public class PdfDemo extends Activity {

    String exportDir;
    int SELECT_PICTURE = 0;
    String selectedImagePath;

    @SuppressLint("SdCardPath")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        exportDir = Environment.getExternalStorageDirectory() + File.separator
                + "firstPdf.pdf";

        ((Button) findViewById(R.id.btnChangeDate))
                .setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        getImage();
                    }
                });
        ((Button) findViewById(R.id.btn_gen_pdf))
                .setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        GeneratePdf();
                    }
                });

    }

    private void GeneratePdf() {
        try {
            FileOutputStream fos = new FileOutputStream(exportDir);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            PDF pdf = new PDF(bos);
            Page page = new Page(pdf, Letter.PORTRAIT);

            InputStream is = new FileInputStream(selectedImagePath);

            BufferedInputStream bis1 = new BufferedInputStream(is);

            Image image1 = new Image(pdf, bis1, ImageType.JPG);
            image1.setPosition(10, 52);
            // image1.scaleBy(.4)
            image1.scaleBy(0.3f, 0.4f);
            // image1.setRotateCW90(true);
            image1.drawOn(page);

            // Adding Text View
            Font f4 = new Font(pdf, CoreFont.HELVETICA_OBLIQUE);
            TextLine text = new TextLine(f4);
            text.setPosition(100.0, 100.0);
            text.setText("Even so, unemployment has remained at less than half the EU average.");
            text.setColor(Color.black);
            text.drawOn(page);

            Box flag = new Box();
            flag.setPosition(100.0, 100.0);
            flag.setSize(190.0, 100.0);
            flag.setColor(Color.white);
            flag.drawOn(page);

            double sw = 7.69; // stripe width
            Line stripe = new Line(0.0, sw / 2, 190.0, sw / 2);
            stripe.setWidth(sw);
            stripe.setColor(Color.oldgloryred);
            for (int row = 0; row < 7; row++) {
                stripe.placeIn(flag, 0.0, row * 2 * sw);
                stripe.drawOn(page);
            }

            Box union = new Box();
            union.setSize(76.0, 53.85);
            union.setColor(Color.oldgloryblue);
            union.setFillShape(true);
            union.placeIn(flag, 0.0, 0.0);
            union.drawOn(page);

            double h_si = 12.6; // horizontal star interval
            double v_si = 10.8; // vertical star interval
            Point star = new Point(h_si / 2, v_si / 2);
            star.setShape(Point.BOX);
            star.setRadius(3.0);
            star.setColor(Color.white);
            star.setFillShape(true);

            for (int row = 0; row < 6; row++) {
                for (int col = 0; col < 5; col++) {
                    star.placeIn(union, row * h_si, col * v_si);
                    star.drawOn(page);
                }
            }
            star.setPosition(h_si, v_si);
            for (int row = 0; row < 5; row++) {
                for (int col = 0; col < 4; col++) {
                    star.placeIn(union, row * h_si, col * v_si);
                    star.drawOn(page);
                }
            }

            pdf.flush();
            bos.close();

        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "" + e, Toast.LENGTH_SHORT)
                    .show();
            System.out.println("ERRORLOG::" + e);
            e.printStackTrace();
        }
    }

    private void getImage() {
        Intent i = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, SELECT_PICTURE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == SELECT_PICTURE && resultCode == RESULT_OK
                && data != null) {

            Uri pickedImage = data.getData();
            String[] filePath = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(pickedImage, filePath,
                    null, null, null);
            cursor.moveToFirst();

            selectedImagePath = cursor.getString(cursor
                    .getColumnIndex(filePath[0]));

            cursor.close();

        }
    }

    /**
     * 
     * @param uri
     * @return
     */
    public String getPathBelowOs(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

    /**
     * Getting image from Uri
     * 
     * @param contentUri
     * @return
     */
    public String getPathUpperOs(Uri contentUri) {// Will return "image:x*"
        String wholeID = DocumentsContract.getDocumentId(contentUri);

        // Split at colon, use second item in the array
        String id = wholeID.split(":")[1];

        String[] column = { MediaStore.Images.Media.DATA };

        // where id is equal to
        String sel = MediaStore.Images.Media._ID + "=?";

        Cursor cursor = getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel,
                new String[] { id }, null);

        String filePath = "";

        int columnIndex = cursor.getColumnIndex(column[0]);

        if (cursor.moveToFirst()) {
            filePath = cursor.getString(columnIndex);
        }

        cursor.close();
        return filePath;
    }

    public static InputStream bitmapToInputStream(Bitmap bitmap) {
        int size = bitmap.getHeight() * bitmap.getRowBytes();
        ByteBuffer buffer = ByteBuffer.allocate(size);
        bitmap.copyPixelsToBuffer(buffer);
        return new ByteArrayInputStream(buffer.array());
    }
}

关于android - 在 Android 中使用 PdfJet 库在 Box 上绘制图像时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21985629/

相关文章:

ios - 在 iPad 上将 UIView 作为矢量渲染到 PDF - 有时渲染为位图,有时渲染为矢量

php - 如何使用 PHP 使用 FPDF 制作这样的表格?

java - 使用 Java 更改/替换 PDF 中的文本

r - 在R markdown的pdf结果上添加 'DRAFT'水印

android - 如何将信息从浏览器传递到应用程序? (伊奥斯)

android - {"error": {"message" :"(#324) Requires upload file" ,"type" :"OAuthException" ,"code":324}}

java - 我需要采取哪些步骤才能将我的 Eclipse Android 项目转换为 Gradle 项目?

python - PDF Parsing Using Python - 提取格式化文本和纯文本

java - iText - 如果有可用空间,则在最后一页末尾添加表格

android - 如果 showPopup 仅适用于 API 11,我如何将菜单链接到具有 ABS 的 View ?