[英]Conversion of PDF files to BMP and print
我需要一个有效的 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;
}
}
在这里,您将图像压缩到原始质量的 1/10:
bitmapPdfPage.compress(Bitmap.CompressFormat.PNG, 10, stream);
为什么不尝试删除该行,或将压缩调整为更高的值,例如 90?
bitmapPdfPage.compress(Bitmap.CompressFormat.PNG, 90, stream);
编辑
如果不是这种情况,那么我可以给你一些调试建议。
我想知道是你的加载还是保存代码导致了小而模糊的大小,所以如果我是你,我会在读取的行上放置一个调试断点
SaveImage(bitmapPdfPage, i);
然后,在变量列表中,您将能够找到bitmapPdfPage
(在 Android Studio 中),并查看位图本身。 如果这里的位图小而模糊,那么可能会更新问题,知道它是不正确的加载代码。 如果它很大并且您可以阅读文本,那么反之亦然,并且您知道这是位图保存代码有问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.