我正在开发一个程序,该程序从源文件中读取一些图像(.jpg)和文本并将它们组合成一个 PDF。我知道处理可能不是最好的语言,但它是我唯一知道如何使用的语言。无论如何,我遇到了处理调用设置两次的问题。我已经看到当 size() 是设置中的第一行时,这个问题得到了解决,但是我不能发生这种情况,因为我必须读入并存储我的所有数据,找到最宽图像的宽度,然后确保它足够高以容纳包含多个图像的页面,并在我决定窗口的宽度和高度之前添加文本。我正在寻找有关如何构建代码的建议,以便我可以获取所有信息而不必调用 setup 两次,因为那 s 导致我的 PDF 包含所有数据的两个副本。如果它对任何人有帮助,我已经包含了设置。谢谢!
void setup(){
font = loadFont("TimesNewRomanPSMT-20.vlw");
File clientsFolder = new File("C:/Users/[my name]/Documents/Processing/ExerciseProgram/Clients");
clients = clientsFolder.listFiles();
for(File x : clients){
println(x.getName());
}
//test files to see if they end in .txt, and have a matching .pdf extension that is newer
String nextClient = needPdf();
File nextClientData = new File("C:/Users/[my name]/Documents/Processing/ExerciseProgram/Clients/" + nextClient);
//println(nextClientData.getName());
//open the file for reading
//setup can't throw the exception, and it needs it, so this should take care of it
try{
Scanner scan = new Scanner(nextClientData);
while(scan.hasNextLine() ){
exercises.add(scan.nextLine());
}
//println(exercises.toString());
printedData = new Exercise[exercises.size()];
println(exercises.size());
for(int i = 0; i < exercises.size(); i++){
printedData[i] = new Exercise((String)exercises.get(i));
}
//count the width and height
int w = 0, h = 0;
for(Exercise e: printedData){
if(e.getWidest() > w){
w = e.getWidest();
}
if(e.getTallest() > h){
h = e.getHeight();
}
}
//and finally we can create the freaking window
// this cuts the .txt off
size(w, h, PDF, "C:/Users/[my name]/Desktop/" + nextClient.substring(0, nextClient.length() - 4) + ".pdf");
}catch (FileNotFoundException e){
println("Unknown error in PApplet.setup(). Exiting.");
println(e.getMessage() );
exit();
}
}