在应用程序的点击我已通过相机单击图像并存储在SD卡现在我想将其转换为PDF,我不知道该怎么做它。任何人都可以帮助我。 我想点击button.is它有可能将.jpg转换为pdf
-2
A
回答
4
这是一个代码,应该做的工作。我试着评论最多的行数,但如果你不明白,请告诉我。
class JpgToPdfActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.jpg_to_pdf_activity);
// Get button
Button convertButton = (Button) findViewById(R.id.convert_button);
convertButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick()
{
// Will run the conversion in another thread to avoid the UI to be frozen
Thread t = new Thread() {
public void run()
{
// Input file
String inputPath = Environment.getExternalStorageDirectory() + File.separator + "test.jpg";
// Output file
String outputPath = Environment.getExternalStorageDirectory() + File.separator + "out.pdf";
// Run conversion
final boolean result = JpgToPdfActivity.this.convertToPdf(inputPath, outputPath);
// Notify the UI
runOnUiThread(new Runnable() {
public void run()
{
if (result) Toast.makeText(JpgToPdfActivity.this, "The JPG was successfully converted to PDF.", Toast.LENGTH_SHORT).show();
else Toast.makeText(JpgToPdfActivity.this, "An error occured while converting the JPG to PDF.", Toast.LENGTH_SHORT).show();
}
});
}
};
t.start();
}
});
}
public static void convertToPdf(String jpgFilePath, String outputPdfPath)
{
try
{
// Check if Jpg file exists or not
File inputFile = new File(jpgFilePath);
if (!inputFile.exists()) throw new Exception("File '" + jpgFilePath + "' doesn't exist.");
// Create output file if needed
File outputFile = new File(outputPdfPath);
if (!outputFile.exists()) outputFile.createNewFile();
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(outputFile));
document.open();
Image image = Image.getInstance(jpgFilePath);
document.add(image);
document.close();
return true;
}
catch (Exception e)
{
e.printStackTrace();
}
return false;
}
}
+0
感谢您的code.it工作!!!!!!! – poojagupta
+0
@poojagupta @poojagupta如果代码工作,那么请标记问题解决,并upvote投票人为他的努力 –
+0
@manitoba我面临的一个问题,它不是将整个图像转换为pdf,但只有一部分。可以帮我。 – poojagupta
相关问题
- 1. 使用exec将pdf转换为JPG
- 2. 将PDF转换为JPG格式?
- 3. PDF to JPG转换器
- 4. PDF到JPG本地转换
- 5. PHP Imagick CMYK PDF转换为JPG
- 6. Carrierwave PDF格式转换为JPG(MiniMagick)
- 7. 将文档转换为Jpg
- 8. 将.eps转换为.jpg
- 9. 将DCOM转换为JPG
- 10. 将TXT转换为JPG
- 11. c#将HTML转换为JPG
- 12. 将JPG转换为HTML
- 13. 如何使用GhostScript将PDF转换为JPG
- 14. 将PDF转换为600dpi和JPG格式的TIFF 96 dpi
- 15. ImageMagick/Imagick使用原生PHP API将PDF转换为JPG
- 16. 将PDF转换为使用ImageMagick/Ghostscript的JPG时文件太大
- 17. ImageMagick将PDF转换为低分辨率JPG文件
- 18. 如何将jpg转换为Android java中的pdf
- 19. 将PDF转换为JPG的替代解决方案
- 20. 使用ImageMagick将PDF转换为jpg而不使用Ghostscript
- 21. 将PDF转换为JPG时质量不佳
- 22. 将ZPL字符串转换为JPG图像和PDF
- 23. 在Drupal上自动将PDF转换为JPG
- 24. 使用PHP将PDF转换为JPG格式图像
- 25. 使用自定义字体将SVG转换为PDF/JPG/PNG
- 26. 如何获得PDF首页并将其转换为JPG
- 27. iText PDF;如何将jpeg2000转换为使用Java的jpg
- 28. Ghostscript将pdf转换为jpg,保存文件或临时文件?
- 29. 使用iText或其他格式将PDF转换为多个JPG
- 30. 在Java-GAE上将PDF页转换为JPG
可能重复:http://stackoverflow.com/questions/15744454/how-to-convert-jpg-to-pdf-in-android-java – Manitoba
我试图用这种方式,但未能实现它 – poojagupta
问题是什么? – Manitoba