无论如何从android中的PDF格式的字节加载PDF?我使用Android PDF编写器(http://coderesearchlabs.com/androidpdfwriter/)来创建PDF数据。
我不想保存文件,然后使用Acrobat Reader / Some PDF Reader打开它。我既不能将其上传到谷歌文档等,然后在网络视图中打开它。
我直接想要向用户显示创建的PDF数据。
我尝试了许多库,如MuPdf,VUDroid等,但它们都需要保存文件,然后使用这些库打开它。
编辑:
我可以生成PDF并将其保存在内部存储中,但问题是我不想将其保存在内存中。任何帮助将受到高度赞赏。
public class PDFWriterDemo extends Activity {
TextView mText;
private String generateHelloWorldPDF() {
PDFWriter mPDFWriter = new PDFWriter(PaperSize.FOLIO_WIDTH, PaperSize.FOLIO_HEIGHT);
// note that to make this images snippet work
// you have to uncompress the assets.zip file
// included into your project assets folder
AssetManager mngr = getAssets();
try {
Bitmap xoiPNG = BitmapFactory.decodeStream(mngr.open("border.png"));
Bitmap xoiJPG = BitmapFactory.decodeStream(mngr.open("star.jpg"));
Bitmap xoiBMP1 = BitmapFactory.decodeStream(mngr.open("bit1.bmp"));
Bitmap xoiBMP8 = BitmapFactory.decodeStream(mngr.open("bit8.bmp"));
Bitmap xoiBMP24 = BitmapFactory.decodeStream(mngr.open("bit24.bmp"));
mPDFWriter.addImage(400, 600, xoiPNG, Transformation.DEGREES_315_ROTATION);
mPDFWriter.addImage(300, 500, xoiJPG);
mPDFWriter.addImage(200, 400, 135, 75, xoiBMP24);
mPDFWriter.addImage(150, 300, 130, 70, xoiBMP8);
mPDFWriter.addImageKeepRatio(100, 200, 50, 25, xoiBMP8);
mPDFWriter.addImageKeepRatio(50, 100, 30, 25, xoiBMP1, Transformation.DEGREES_270_ROTATION);
mPDFWriter.addImageKeepRatio(25, 50, 30, 25, xoiBMP1);
} catch (IOException e) {
e.printStackTrace();
}
mPDFWriter.setFont(StandardFonts.SUBTYPE, StandardFonts.TIMES_ROMAN);
mPDFWriter.addRawContent("1 0 0 rg\n");
mPDFWriter.addTextAsHex(70, 50, 12, "68656c6c6f20776f726c6420286173206865782921");
mPDFWriter.setFont(StandardFonts.SUBTYPE, StandardFonts.COURIER, StandardFonts.WIN_ANSI_ENCODING);
mPDFWriter.addRawContent("0 0 0 rg\n");
mPDFWriter.addText(30, 90, 10, "© CRL", Transformation.DEGREES_270_ROTATION);
mPDFWriter.newPage();
mPDFWriter.addRawContent("[] 0 d\n");
mPDFWriter.addRawContent("1 w\n");
mPDFWriter.addRawContent("0 0 1 RG\n");
mPDFWriter.addRawContent("0 1 0 rg\n");
mPDFWriter.addRectangle(40, 50, 280, 50);
mPDFWriter.addText(85, 75, 18, "Code Research Laboratories");
mPDFWriter.newPage();
mPDFWriter.setFont(StandardFonts.SUBTYPE, StandardFonts.COURIER_BOLD);
mPDFWriter.addText(150, 150, 14, "http://coderesearchlabs.com");
mPDFWriter.addLine(150, 140, 270, 140);
int pageCount = mPDFWriter.getPageCount();
for (int i = 0; i < pageCount; i++) {
mPDFWriter.setCurrentPage(i);
mPDFWriter.addText(10, 10, 8, Integer.toString(i + 1) + " / " + Integer.toString(pageCount));
}
String s = mPDFWriter.asString();
return s;
}
private void outputToScreen(int viewID, String pdfContent) {
mText = (TextView) this.findViewById(viewID);
mText.setText(pdfContent);
}
private void outputToFile(String fileName, String pdfContent, String encoding) {
Log.i("NAUMAN", ""+getFilesDir().getAbsolutePath());
File newFile = new File(getFilesDir().getAbsolutePath() + "/" + fileName);
try {
newFile.createNewFile();
try {
FileOutputStream pdfFile = new FileOutputStream(newFile);
pdfFile.write(pdfContent.getBytes());
pdfFile.close();
} catch(FileNotFoundException e) {
e.printStackTrace();
}
} catch(IOException e) {
//
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String pdfcontent = generateHelloWorldPDF();
outputToScreen(R.id.text, pdfcontent);
outputToFile("helloworld.pdf",pdfcontent,"ISO-8859-1");
}
}