【问题标题】:Facing issue to Draw the image on Box using PdfJet library in Android在 Android 中使用 PdfJet 库在 Box 上绘制图像面临的问题
【发布时间】:2014-03-25 23:45:16
【问题描述】:

我正在使用 Android 中的 PdfJet 库创建 Pdf 文件。所有的事情都进展顺利,但我在 Box 上绘制 Image 时遇到了一些问题。当我执行程序时,创建了 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();

欢迎任何对此问题提出好的建议和答案的人来这里。

【问题讨论】:

    标签: android pdf pdf-generation


    【解决方案1】:

    请找到以下示例以使用 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());
        }
    }
    

    【讨论】:

    • 互联网上很少有与 PdfJet 相关的帮助,我在 2014 年 2 月 24 日提出了这个问题,很久很久了:P,任何方式谢谢你的回答。
    • 我也尝试在 Box 中传递图像,但即使在网上也找不到解决方案,所以我所做的是“image1.scaleBy(0.3f, 0.4f)”此代码自动处理图像大小...
    • 嗨,我找到了将图像放在盒子里的解决方案 Box flag = new Box(); flag.setPosition(10.0, 30.0); flag.setSize(190.0, 100.0); flag.setColor(Color.white); flag.drawOn(page); image1.scaleBy(0.3f, 0.3f); image1.placeIn(标志); image1.drawOn(page);
    • 这是面糊解决方案。继续加油。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    • 2012-11-24
    • 2014-11-18
    • 1970-01-01
    相关资源
    最近更新 更多