首页>Program>source

在我的应用程序中,用户可以通过将它们作为对象添加到ArrayList来为PDF创建新页面.然后,由for循环管理新页面的创建,以遍历ArrayList中的每个对象并将其print到新页面。

但是,当我打开已创建的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);
    //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();
}
最新回答
  • 7天前
    1 #

    只需删除最外层的for循环,即替换:

    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();
            }
        }
    }
    

    具有:

       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++
                }
    

    应该使您更进一步解决问题.您所描述的平方行为几乎肯定来自嵌套循环。

  • frequency:Python:获取列表中最低,最频繁的值
  • 如何为我的Flutter应用制作这样的背景?