Reputation: 1072
I'm trying to create a PDF file from a view that I create programatically. It seems that the pdf is created correctly only if the view is added to my main layout, but I don't want that. Here's my test code:
RelativeLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imgView = (ImageView) findViewById(R.id.imageView);
mainView = (RelativeLayout) findViewById(R.id.main_layout);
layout = new RelativeLayout(this);
RelativeLayout.LayoutParams imgParams = new RelativeLayout.LayoutParams(800, 800);
imgParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
imgParams.addRule(RelativeLayout.ALIGN_PARENT_START);
ImageView newImg = new ImageView(this);
newImg.setLayoutParams(imgParams);
newImg.setImageDrawable(imgView.getDrawable());
layout.addView(newImg);
}
private void create() {
layout.layout(0, 0, 1000, 1000);
layout.post(new Runnable() {
@Override
public void run() {
createPdf();
}
});
}
private void createPdf() {
OutputStream os = null;
File file = null;
PdfDocument pdfDoc = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(1000, 1000, 1).create();
PdfDocument.Page page = pdfDoc.startPage(pageInfo);
layout.draw(page.getCanvas());
pdfDoc.finishPage(page);
try {
file = new File(Environment.getExternalStorageDirectory(), "page.pdf");
os = new BufferedOutputStream(new FileOutputStream(file));
pdfDoc.writeTo(os);
pdfDoc.close();
os.close();
} catch (IOException e) {
}
}
This creates an empty pdf. If I add these two lines of code in create()
, it works:
mainView.removeAllViews();
mainView.addView(layout);
but I don't want to remove the content on the main view. How can I make this work?
I have found a possible solution. For whatever reason it wants to attach to a view that is statically defined in xml. So define an empty viewgroup in xml which has an id. You get a reference in onCreate()
then you can basically use the code from above, but instead you use addView()
on this view. Oh and set the viewgroup to invisible since you don't want it to be displayed. I've only tested on one device however.
Upvotes: 2
Views: 1015
Reputation: 353
layout.measure(1000, 1000); layout.layout(0, 0, 1000, 1000);
I was having similar problem and this fixed it for me.
Android: inflating a layout and writting it to PDF produces a blank PDF
Upvotes: 1