我意识到Android没有内置的显示PDF文件的方法.
如何在Android上使用Java渲染PDF文件?
我正在开发一个应用程序,我需要将图像转换为PDF.我尝试了一些东西,但问题是,PDF中的图像尺寸非常小.我需要解决方案来解决这个问题 此外,我正在寻找将多个图像转换为单个PDF文档.我将发布我尝试过的代码.
public void convertPDF(byte[] path)
{
String FILE = "mnt/sdcard/FirstPdf.pdf";
Document document=new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
try {
image=Image.getInstance(path);
document.add(new Paragraph("My Heading"));
document.add(image);
document.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (DocumentException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
当我将Bitmap转换为Byte数组时,我正在压缩图像,我猜,这就是原因.在不压缩图像的情况下,我无法将Bitmap转换为字节数组.
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG,100,stream);
byte[] byteArray=stream.toByteArray();
convertPDF(byteArray);
Run Code Online (Sandbox Code Playgroud)
这有什么解决方案吗?
更新
在这里,我已经实现了@Burak Cakir在答案中提出的答案.但现在我在PDF中获得更大的图像.为了更好地理解,请查看下面的图像.
我正在尝试在我的应用程序中创建一个pdf文件,将其保存在打开它的外部存储器上.保存文件对我来说不是问题,也不是打开文件,我的问题是创建文件并写入文件.所以在网上进行一些研究后,我发现了以下方法:
File file = new File(directoryName, fileName);
// Creating output stream to write in the newly created file
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// Creating a new document
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try {
PdfWriter.getInstance(document, fOut);
// Open the document for writing
document.open();
// Write in the document
document.add(new Paragraph("Hello world"));
document.close();
} catch(DocumentException de) {
System.err.println(de.getMessage());
}
Run Code Online (Sandbox Code Playgroud)
运行我的应用程序并执行上面的代码后,我收到以下错误:
java.lang.NoClassDefFoundError: Failed resolution of: …
Run Code Online (Sandbox Code Playgroud) 我正在创建一个PDF文件并将其保存在本地存储中.当试图打开它时,它在除了Android N之外的所有设备中都是完美的.我可以使用FileProvider在Android N中打开PDF文件,但它显示为空白.
这是我的URI
content://com.products.provider/external_storage_root/Android/data/com.products/in_17052017_170502_1_1001.pdf
Run Code Online (Sandbox Code Playgroud)
这是我的代码
Uri path;
File pdfFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"
+ "Android" + "/" + "data" + "/" + "com.products" + "/" + file);
if (Build.VERSION.SDK_INT >= 24) {
path = FileProvider.getUriForFile(getActivity(), "com.products.provider", pdfFile);
} else {
path = Uri.fromFile(pdfFile);
}
// Setting the intent for pdf reader
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
try {
startActivity(pdfIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(getActivity(), "Can't read pdf file", Toast.LENGTH_SHORT).show();
}
Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我将 pdf 下载到下载文件夹,并尝试使用以下代码打开。
String FileName="CardsDecks";
File imagePath = new File(this.getFilesDir(), "Download");
File newFile = new File(imagePath, FileName+ ".pdf");
Uri contentUri = FileProvider.getUriForFile(this, "com.cards.mycards.fileprovider", newFile);
Intent intent =new Intent(Intent.ACTION_VIEW)
.setDataAndType(contentUri, "application/pdf")
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent intent1 = Intent.createChooser(intent, "Open File");
startActivity(intent1);
Run Code Online (Sandbox Code Playgroud)
清单文件中的提供程序定义
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.cards.mycards.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
Run Code Online (Sandbox Code Playgroud)
provider_paths.xml
<paths>
<files-path name="CardsDecks" path="." />
</paths>
Run Code Online (Sandbox Code Playgroud)
该应用程序仅显示用于打开文件的驱动器 pdf 查看器和 word 选项,而不显示手机中的 pdf 查看器。
当我单击驱动器 pdf 查看器时,它会立即打开和关闭。我已经检查了下载文件夹中的文件,文件存在于其中,其中包含内容。
请帮助以上。
我打算使用内置的android pdf库.在我的应用程序中,我需要显示一个可能包含多个页面的pdf报告.用户应该能够打印这些页面.使用PrintDocumentAdapter进行打印看起来非常简单,但对我来说不清楚的是创建pdf的最佳方法是什么.我知道您可以使用View/canvas生成PdfDocument,或者在绘制线条,文本,绘画等时采用更"低级"的方法.
我看到三种可能性:
为每个页面创建一个视图.用户可以根据需要在视图之间导航并进行打印.但是我不清楚如何为每个页面/视图生成pdf.我的意思是,如果我正在查看页面/视图1,是的,我可以轻松地创建一个pdf,但其他页面呢?是的,我可以在内存中有这个,但我发现如果它们没有在屏幕上显示,它们会创建空的pdf.我不想让用户单独打印每个页面.
创建pdf文档(低级方法),集成pdf阅读器,并从那里显示/打印pdf.
为用户可以浏览的每个页面创建一个视图.调用print选项时,再次生成pdf文档(低级方法)
显然,选项1是首选方法,但我不清楚如何做到这一点.当然,我可能会遗漏一些东西,所以任何帮助将不胜感激!
在我的应用程序中,我在按钮单击事件上创建了一个 PDF 文档。并且我使用了打印框架来打印该文档。所有这些都工作正常。我需要的是,我想将该 pdf 文档保存到手机存储中的文件夹中.如何做到这一点,我不知道如何将此 pdf 保存到文件中。有人可以帮我吗?提前谢谢。
我的生成pdf的代码
public class CustomPrintActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_custom_print);
}
public void printDocument(View view)
{
PrintManager printManager = (PrintManager) this
.getSystemService(Context.PRINT_SERVICE);
String jobName = this.getString(R.string.app_name) +
" Document";
printManager.print(jobName, new MyPrintDocumentAdapter(this),
null);
}
public class MyPrintDocumentAdapter extends PrintDocumentAdapter {
Context context;
private int pageHeight;
private int pageWidth;
public PdfDocument myPdfDocument;
public int totalpages = 1;
public MyPrintDocumentAdapter(Context context) {
this.context = context;
}
@Override
public void onLayout(PrintAttributes …
Run Code Online (Sandbox Code Playgroud) android android-file android-print-framework android-pdf-api