我正在使用Android中的PdfJet Library创建Pdf文件。所有的事情都很顺利,但我在绘制盒子上的图像时遇到了一些问题。当我执行程序时,创建了Pdf并且还创建了框,但是Image不在Box中。
这是我的代码。
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();
对此问题提出好的建议和答案的任何人都欢迎光临。
答案 0 :(得分:2)
请使用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());
}
}