Reputation: 363
So In my app I upload images to my company server
These Images are then converted to PDF with the built PDFdocument method,
Now my Issue is I am trying to make the PDF genertation dynamic by adding IF statements. So My images are loaded into a arraylist and I call a PDF page creation if there is an item in a certain position in the ArrayList with
if(list.contains(list.get(0))){
code here
}
BUT I am getting Cannot resolve symbols on the code for the next page.
So what I want to Happen IF the list contains a item at position X then it makes a page so the endgame is that for every image the user selects a new page will be created
PDF Creation Code
PdfDocument document=new PdfDocument();
if(list.contains(list.get(0))) {
PdfDocument.PageInfo pageInfo=new PdfDocument.PageInfo.Builder(300, 6000, 1).create();
PdfDocument.Page page=document.startPage(pageInfo);
Canvas canvas=page.getCanvas();
Bitmap image=BitmapFactory.decodeFile(list.get(0));
canvas.setDensity(DENSITY_XHIGH);
image.setDensity(DENSITY_XHIGH);
canvas.drawBitmap(image, 1, 1, null);
document.finishPage(page);
}
** here I am Getting cannot resolve symbols on page, pageInfo, canvas**
if(list.contains(list.get(1))) {
pageInfo = new PdfDocument.PageInfo.Builder(, 6000, 2).create();
page = document.startPage(pageInfo);
canvas = page.getCanvas();
Bitmap image2=BitmapFactory.decodeFile(list.get(1));
canvas.setDensity(DENSITY_XHIGH);
image2.setDensity(DENSITY_XHIGH);
canvas.drawBitmap(image2, 1, 1, null);
document.finishPage(page);
}
String directory_path=Environment.getExternalStorageDirectory().getPath() + "/mypdf/";
File file=new File(directory_path);
if (!file.exists()) {
file.mkdirs();
}
String timeStamp = (new SimpleDateFormat("yyyyMMdd_HHmmss")).format(new Date());
String targetPdf=directory_path + timeStamp +".pdf";
File filePath=new File(targetPdf);
try {
document.writeTo(new FileOutputStream(filePath));
Toast.makeText(this, "Done", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Log.e("main", "error " + e.toString());
Toast.makeText(this, "Something wrong: " + e.toString(), Toast.LENGTH_LONG).show();
}
// close the document
document.close();
I think that My problem is I have syntax error somewhere with m IF statements, but I am not sure Any help Will be appreciated
Upvotes: 0
Views: 797
Reputation: 44854
To re-iterate
In this code
if(list.contains(list.get(0))) {
PdfDocument.PageInfo pageInfo=new PdfDocument.PageInfo.Builder(300, 6000, 1).create();
PdfDocument.Page page=document.startPage(pageInfo);
Canvas canvas=page.getCanvas();
....
}
you are declaring the variables pageInfo
page
and canvas
- they are limited in scope to this block, so they are not visible to
if(list.contains(list.get(1))) {
pageInfo = new PdfDocument.PageInfo.Builder(, 6000, 2).create();
page = document.startPage(pageInfo);
canvas = page.getCanvas();
....
}
You can declare them before the first if
block as
PdfDocument.PageInfo pageInfo = null;
PdfDocument.Page page = null;
Canvas canvas = null;
if(list.contains(list.get(0))) {
pageInfo = .... // set value
}
and
if(list.contains(list.get(1))) {
pageInfo = ....
}
Upvotes: 1
Reputation: 9437
It has nothing to do with your if statements, just with the scope of your code blocks.
if ( condition ) {
String t = "yes";
}
System.out.println("myString " + t);
this simple code will cause the exact same problem as your current code. t
is a local variable in your if block. Once that block is finished, the variable doesn't exist anymore.
To fix this:
String t = "";
if ( condition ) {
t = "yes";
}
System.out.println("myString " + t);
This way, the t
variable is accessible after your if block is finished, because, since it wasn't declared within the if block, it isn't limited to it's scope.
Upvotes: 2