我需要一个有效的android代码来将pdf文件打印到bmp中。我所拥有的代码能够打印,但文字非常模糊和缩小,以至于很难阅读。用于打印的设备很小,纸张宽度为384。 印刷文本应清晰可读。
public class PdfToImage {
private int ViewSize = 384;
private String pdfErrorCode = null;
private String pdfImageErrorCode = null;
private String pdfImageSaveErrorCode = null;
private String returnError = null;
public String pdfToImage(File pdfFilePath) {
PDFImage.sShowImages = true;
PDFPaint.s_doAntiAlias = true;
HardReference.sKeepCaches = true;
try {
RandomAccessFile pdfAccessFile = new RandomAccessFile(pdfFilePath,
"r");
byte[] pdfData = new byte[(int) pdfAccessFile.length()];
pdfAccessFile.readFully(pdfData);
returnError = pdfLoadImages(pdfData);
pdfErrorCode = "SUCCESS";
} catch (Exception ignored) {
pdfErrorCode = "PDF FILE NOT FOUND";
}
if (returnError.equals("PDF TO BMP CONVERSION SUCCESS")) {
return pdfErrorCode;
} else {
return "FAILED";
}
}
@SuppressLint("NewApi")
private String pdfLoadImages(final byte[] data) {
try {
ByteBuffer byteBuffer = ByteBuffer.NEW(data);
PDFFile pdfFile = new PDFFile(byteBuffer);
PDFPage pdfPage = pdfFile.getPage(1, true);
final float scaleImage = ViewSize / pdfPage.getWidth() * 0.95f;
Bitmap bitmapPdfPage = pdfPage.getImage(
(int) (pdfPage.getWidth() * scaleImage),
(int) (pdfPage.getHeight() * scaleImage), null, true, true);
SaveImage(bitmapPdfPage, 1);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmapPdfPage.compress(Bitmap.CompressFormat.PNG, 100, stream);
stream.reset();
for (int i = 2; i <= pdfFile.getNumPages(); i++) {
pdfPage = pdfFile.getPage(i, true);
bitmapPdfPage = pdfPage.getImage(
(int) (pdfPage.getWidth() * scaleImage),
(int) (pdfPage.getHeight() * scaleImage), null, true,
true);
bitmapPdfPage.compress(Bitmap.CompressFormat.PNG, 100, stream);
SaveImage(bitmapPdfPage, i);
pdfImageErrorCode = "PDF TO BMP CONVERSION SUCCESS";
}
stream.close();
} catch (Exception e) {
Log.d("error", e.toString());
pdfImageErrorCode = "PDF TO BMP CONVERSION FAILED";
}
System.gc();
return pdfImageErrorCode;
}
private String SaveImage(Bitmap pdfBitmap, int pageNumber) {
String sdcardPath = Environment.getExternalStorageDirectory()
.toString();
File pdfDir = new File(sdcardPath + "/pdftobmp");
pdfDir.mkdirs();
String pdfToImageFileName = "pdf-" + pageNumber + ".png";
File imageFile = new File(pdfDir, pdfToImageFileName);
if (imageFile.exists())
imageFile.delete();
try {
FileOutputStream outputStream = new FileOutputStream(imageFile);
pdfBitmap.compress(Bitmap.CompressFormat.PNG, 90, outputStream);
outputStream.flush();
outputStream.close();
pdfImageSaveErrorCode = "IMAGE SAVED";
} catch (Exception e) {
e.printStackTrace();
pdfImageSaveErrorCode = "IMAGE NOT SAVED";
}
return pdfImageSaveErrorCode;
}
}
答案 0 :(得分:0)
在这里,您将图像压缩到原始质量的1/10:
bitmapPdfPage.compress(Bitmap.CompressFormat.PNG, 10, stream);
为什么不尝试删除该行,或者将压缩调整为更高的值,比如90?
bitmapPdfPage.compress(Bitmap.CompressFormat.PNG, 90, stream);
修改强>
如果情况并非如此,那么我可以给你一些调试建议。
我想知道你的加载或保存代码是否导致了小而模糊的大小,所以如果我是你,我会在读取的行上放置一个调试断点/ p>
SaveImage(bitmapPdfPage, i);
然后,在变量列表中,您将能够找到bitmapPdfPage
(在Android Studio中),并查看位图本身。如果位图在这里很小且模糊,那么可能会更新问题,知道它是不正确的加载代码。如果它很大并且您可以阅读文本,那么反之亦然,您知道它是位图保存代码的错误。