我希望通过我的应用程序创建PDF文件,并希望通过电子邮件将PDF文件作为附件发送。在Android中有可能吗?如果是,请说明如何。
答案 0 :(得分:8)
这是代码
Document doc = new Document();
PdfWriter.getInstance(doc, new FileOutputStream("urgentz.pdf"));
doc.open();
Image image = Image.getInstance ("urgentzImageahslkdhaosd.jpg");
doc.add(new Paragraph("Your text blah bleh"));
doc.add(image);
doc.close();
将iText库与您的android项目一起使用
答案 1 :(得分:2)
iText图书馆使用。 请参阅答案Here。
iText样本很多。以下参考网站。 http://www.geek-tutorials.com/java/itext/insert_control_text.php
答案 2 :(得分:1)
创建pdf
非常简单首先下载droidText.0.2.jar并添加gradle文件和libs文件夹,如果你使用的是android studio。然后为PDF编写此代码
public void createPDF() {
Document doc = new Document();
try {
path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ADUREC";
File dir = new File(path);
if (!dir.exists())
dir.mkdirs();
Log.d("PDFCreator", "PDF Path: " + path);
//This is for random name
number = new ArrayList<Integer>();
for (int i = 1; i <= 10; ++i) number.add(i);
Collections.shuffle(number);
File file = new File(dir, "Document" + number + ".pdf");
FileOutputStream fOut = new FileOutputStream(file);
PdfWriter.getInstance(doc, fOut);
//open the document
doc.open();
Paragraph p1 = new Paragraph("TENANTS : " + tenants.getText().toString());
Font paraFont = new Font(Font.COURIER);
p1.setAlignment(Paragraph.ALIGN_CENTER);
p1.setFont(paraFont);
//add paragraph to document
doc.add(p1);
Paragraph p2 = new Paragraph("OFFFER NUMBER : " + offer_number.getText().toString());
Font paraFont2 = new Font(Font.COURIER, 14.0f, Color.GREEN);
p2.setAlignment(Paragraph.ALIGN_CENTER);
p2.setFont(paraFont2);
doc.add(p2);
Paragraph p3 = new Paragraph("OFFFER NUMBER : " + offer_number.getText().toString());
Font paraFont3 = new Font(Font.COURIER, 14.0f, Color.GREEN);
p3.setAlignment(Paragraph.ALIGN_CENTER);
p3.setFont(paraFont2);
doc.add(p3);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.logo);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
Image myImg = Image.getInstance(stream.toByteArray());
myImg.setAlignment(Image.MIDDLE);
// add image to document
doc.add(myImg);
//set footer
Phrase footerText = new Phrase("ADUREC DOCUMENT");
HeaderFooter pdfFooter = new HeaderFooter(footerText, true);
doc.setFooter(pdfFooter);
// Toast.makeText(getApplicationContext(), "success pdf", Toast
// .LENGTH_LONG).show();
} catch (DocumentException de) {
Log.e("PDFCreator", "DocumentException:" + de);
} catch (IOException e) {
Log.e("PDFCreator", "ioException:" + e);
} finally {
doc.close();
}
}
答案 3 :(得分:1)
implementation 'com.itextpdf:itextg:5.5.10'
中写以下依赖项<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
public class MainActivity extends AppCompatActivity {
Button save;
EditText text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
save=(Button)findViewById(R.id.save);
text=(EditText)findViewById(R.id.text);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(Build.VERSION.SDK_INT> Build.VERSION_CODES.M)
{
if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_DENIED)
{
String[] parmission={Manifest.permission.WRITE_EXTERNAL_STORAGE};
requestPermissions(parmission,1000);
}
else savepdf();
}
else savepdf();
}
});
}
private void savepdf()
{
Document doc=new Document();
String mfile=new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(System.currentTimeMillis());
String mfilepath= Environment.getExternalStorageDirectory()+"/"+mfile+".pdf";
Font smallBold=new Font(Font.FontFamily.TIMES_ROMAN,12,Font.BOLD);
try{
PdfWriter.getInstance(doc,new FileOutputStream(mfilepath));
doc.open();
String mtext=text.getText().toString();
doc.addAuthor("harikesh");
doc.add(new Paragraph(mtext,smallBold));
doc.close();
Toast.makeText(this, ""+mfile+".pdf"+" is saved to "+mfilepath, Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(this,"This is Error msg : " +e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode)
{
case 1000:
if(grantResults.length>0 && grantResults[0]== PackageManager.PERMISSION_GRANTED)
{
savepdf();
}
else Toast.makeText(this, "parmission denied..", Toast.LENGTH_SHORT).show();
}
}
}`
答案 4 :(得分:0)
您可以使用以下代码创建pdf页面并在画布上绘制任何内容。
fun createPdf(textToPdf: String) {
// create a new document
val document = PdfDocument()
// crate a page description
var pageInfo: PdfDocument.PageInfo = PdfDocument.PageInfo.Builder(300, 600, 1).create()
// start a page
var page: PdfDocument.Page = document.startPage(pageInfo)
var canvas = page.canvas
var paint = Paint()
paint.color = Color.RED
canvas.drawCircle(50F, 50F, 30F, paint)
paint.color = Color.BLACK
canvas.drawText(textToPdf, 80F, 50F, paint)
//canvas.drawt
// finish the page
document.finishPage(page)
// draw text on the graphics object of the page
// Create Page 2
pageInfo = PdfDocument.PageInfo.Builder(300, 600, 2).create()
page = document.startPage(pageInfo)
canvas = page.canvas
paint = Paint()
paint.color = Color.BLUE
canvas.drawCircle(100F, 100F, 100F, paint)
document.finishPage(page)
// write the document content
val directory_path = Environment.getExternalStorageDirectory().path + "/mypdf/"
val file = File(directory_path)
if (!file.exists()) {
file.mkdirs()
}
val targetPdf = directory_path + "test-2.pdf"
val filePath = File(targetPdf)
try {
document.writeTo(FileOutputStream(filePath))
Toast.makeText(mContext, "Done", Toast.LENGTH_LONG).show()
} catch (e: IOException) {
Log.e("main", "error " + e.toString())
Toast.makeText(mContext, "Something wrong: " + e.toString(), Toast.LENGTH_LONG).show()
}
// close the document
document.close()
//isPrinting = false
}