英文:
New PDF page creation in Android is producing more pages than expected
问题
在我的应用程序中,用户可以通过将它们添加为ArrayList的对象来创建PDF的新页面。然后,通过for循环管理新页面的创建,以便迭代ArrayList中的每个对象并将其打印到新页面。
但是,当我打开已创建的PDF时,似乎会得到一个“平方”数量的页面。
例如
我创建2页,得到4页
我创建3页,得到9页
等等
我认为这与我的for循环有关,但我看不出来。有人可以看一下我的代码吗?
public class MainActivity extends AppCompatActivity {
private EditText firstEditText;
ArrayList<PDFPageModel> pageModelArrayList = new ArrayList<>();
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstEditText = findViewById(R.id.firstEditText);
//请求权限(我不需要这部分)
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);
//每次创建时清除数组内存
pageModelArrayList.clear();
}
public void addObjectToList(View view){
//创建新对象
PDFPageModel pdfPageModel = new PDFPageModel();
//从编辑文本中添加内容到对象并设置类型
pdfPageModel.setPageData(firstEditText.getText().toString().trim());
pdfPageModel.setType("Text");
pageModelArrayList.add(pdfPageModel);
Toast.makeText(MainActivity.this, "Text added", Toast.LENGTH_LONG).show();
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void createMyPDF(View view){
//分为3部分完成
//1. 创建PDF文档,2. 写入文档,3. 保存文档到文件夹
//1. 创建PDF文档
PdfDocument pdfDocument = new PdfDocument();
//2.
//用于ArrayList新页面的for循环
for (int i=0; i < pageModelArrayList.size(); i++){
for(PDFPageModel filledData : pageModelArrayList){
filledData.getPageData();
filledData.getType();
if(filledData.getType().equals("Text")){
//创建页面信息描述
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(595, 842, i+1).create();
//开始一个页面
PdfDocument.Page page = pdfDocument.startPage(pageInfo);
//写入新页面
Paint paint = new Paint();
//用于写入页面的画布,从输入的文本获取对象数据
String myString = filledData.getPageData();
//设置文本在页面上的起始位置
int x = 10, y = 25;
//多行循环
for(String line:myString.split("\n")){
page.getCanvas().drawText(line, x, y, paint);
y+=paint.descent()-paint.ascent();
}
//完成页面
pdfDocument.finishPage(page);
}else {
Toast.makeText(MainActivity.this, "Object Type not text", Toast.LENGTH_LONG).show();
}
}
}
//3. 保存到文件夹
String myFilePath = String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
String fileName = "/myNewPDF-TEST.pdf";
//创建要保存在其中的文件夹
File myFile = new File(myFilePath + "/PDFTestStorage");
if(!myFile.exists()){
myFile.mkdirs();
}
//新文件
File file = new File(myFile, fileName);
//保存文档
try {
pdfDocument.writeTo(new FileOutputStream(file));
Toast.makeText(MainActivity.this, "File Created", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Error: File not created", Toast.LENGTH_LONG).show();
}
//关闭PDF文档
pdfDocument.close();
}
}
希望这能帮助您解决问题。如果还有其他问题,请随时提出。
英文:
In my app users can create new pages for a PDF by adding them as objects to an ArrayList. The new page creation is then managed by a for loop to iterate through each object in the ArrayList and print it to a new page.
But when I open the PDF that has been created I seem to be getting a 'squared' amount of pages.
e.g
I create 2 pages, I get 4
I create 3 pages, I get 9
etc
I think it has something to do with my for loop but I can't see it. Can someone have a look at my code?
public class MainActivity extends AppCompatActivity {
private EditText firstEditText;
ArrayList<PDFPageModel> pageModelArrayList = new ArrayList<>();
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstEditText = findViewById(R.id.firstEditText);
//request permissions (I won't need this part)
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);
//clear array memory each time you onCreate
pageModelArrayList.clear();
}
public void addObjectToList(View view){
//create new object
PDFPageModel pdfPageModel = new PDFPageModel();
//add stuff to object from edit text and set type
pdfPageModel.setPageData(firstEditText.getText().toString().trim());
pdfPageModel.setType("Text");
pageModelArrayList.add(pdfPageModel);
Toast.makeText(MainActivity.this, "Text added", Toast.LENGTH_LONG).show();
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void createMyPDF(View view){
//done in 3 parts
//1. creation of PDF document, 2. writing to the document, 3. save document to folder
//1. PDF Document Creation
PdfDocument pdfDocument = new PdfDocument();
//2.
//for loop for ArrayList new page
for (int i=0; i < pageModelArrayList.size(); i++){
for(PDFPageModel filledData : pageModelArrayList){
filledData.getPageData();
filledData.getType();
if(filledData.getType() == "Text"){
//create a page info description
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(595, 842, i+1).create();
//start a page
PdfDocument.Page page = pdfDocument.startPage(pageInfo);
//writing to new page
Paint paint = new Paint();
//canvas used to write to page, getting object data from inputted text
String myString = filledData.getPageData();
//setting where to start text on the page
int x = 10, y = 25;
//multi line loop
for(String line:myString.split("\n")){
page.getCanvas().drawText(line, x, y, paint);
y+=paint.descent()-paint.ascent();
}
//finish page
pdfDocument.finishPage(page);
}else {
Toast.makeText(MainActivity.this, "Object Type not text", Toast.LENGTH_LONG).show();
}
}
}
//3. saving to folder
String myFilePath = String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
String fileName = "/myNewPDF-TEST.pdf";
//create folder to be saved in
File myFile = new File(myFilePath + "/PDFTestStorage");
if(!myFile.exists()){
myFile.mkdirs();
}
//new file
File file = new File(myFile, fileName);
//save document
try {
pdfDocument.writeTo(new FileOutputStream(file));
Toast.makeText(MainActivity.this, "File Created", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Error: File not created", Toast.LENGTH_LONG).show();
}
//close pdf doc
pdfDocument.close();
}
答案1
得分: 1
Simply removing the outermost for loop, i.e replacing:
for (int i=0; i < pageModelArrayList.size(); i++){
for(PDFPageModel filledData : pageModelArrayList){
filledData.getPageData();
filledData.getType();
if(filledData.getType() == "Text"){
//create a page info description
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(595, 842, i+1).create();
//start a page
PdfDocument.Page page = pdfDocument.startPage(pageInfo);
//writing to new page
Paint paint = new Paint();
//canvas used to write to page, getting object data from inputted text
String myString = filledData.getPageData();
//setting where to start text on the page
int x = 10, y = 25;
//multi line loop
for(String line:myString.split("\n")){
page.getCanvas().drawText(line, x, y, paint);
y+=paint.descent()-paint.ascent();
}
//finish page
pdfDocument.finishPage(page);
}else {
Toast.makeText(MainActivity.this, "Object Type not text", Toast.LENGTH_LONG).show();
}
}
}
with:
int i=0;
for(PDFPageModel filledData : pageModelArrayList){
filledData.getPageData();
filledData.getType();
if(filledData.getType() == "Text"){
//create a page info description
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(595, 842, i+1).create();
//start a page
PdfDocument.Page page = pdfDocument.startPage(pageInfo);
//writing to new page
Paint paint = new Paint();
//canvas used to write to page, getting object data from inputted text
String myString = filledData.getPageData();
//setting where to start text on the page
int x = 10, y = 25;
//multi line loop
for(String line:myString.split("\n")){
page.getCanvas().drawText(line, x, y, paint);
y+=paint.descent()-paint.ascent();
}
//finish page
pdfDocument.finishPage(page);
}else {
Toast.makeText(MainActivity.this, "Object Type not text", Toast.LENGTH_LONG).show();
i++;
}
}
Should get you one step closer to solving the issue. The squared behavior you're describing almost certainly comes from nested loops.
英文:
Simply removing the outermost for loop, i.e replacing:
for (int i=0; i < pageModelArrayList.size(); i++){
for(PDFPageModel filledData : pageModelArrayList){
filledData.getPageData();
filledData.getType();
if(filledData.getType() == "Text"){
//create a page info description
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(595, 842, i+1).create();
//start a page
PdfDocument.Page page = pdfDocument.startPage(pageInfo);
//writing to new page
Paint paint = new Paint();
//canvas used to write to page, getting object data from inputted text
String myString = filledData.getPageData();
//setting where to start text on the page
int x = 10, y = 25;
//multi line loop
for(String line:myString.split("\n")){
page.getCanvas().drawText(line, x, y, paint);
y+=paint.descent()-paint.ascent();
}
//finish page
pdfDocument.finishPage(page);
}else {
Toast.makeText(MainActivity.this, "Object Type not text", Toast.LENGTH_LONG).show();
}
}
}
with:
int i=0;
for(PDFPageModel filledData : pageModelArrayList){
filledData.getPageData();
filledData.getType();
if(filledData.getType() == "Text"){
//create a page info description
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(595, 842, i+1).create();
//start a page
PdfDocument.Page page = pdfDocument.startPage(pageInfo);
//writing to new page
Paint paint = new Paint();
//canvas used to write to page, getting object data from inputted text
String myString = filledData.getPageData();
//setting where to start text on the page
int x = 10, y = 25;
//multi line loop
for(String line:myString.split("\n")){
page.getCanvas().drawText(line, x, y, paint);
y+=paint.descent()-paint.ascent();
}
//finish page
pdfDocument.finishPage(page);
}else {
Toast.makeText(MainActivity.this, "Object Type not text", Toast.LENGTH_LONG).show();
i++
}
Should get you one step closer to solving the issue. The squared behaviour you're describing almost certainly comes from nested loops.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论