Java Costream已关闭,无法读取。也许它的附件已经关闭了?
本质上,我正在尝试用Java创建一个小工具,从某种用户输入中提取文本,想象一个普通的文本框,并用它创建一个PDF文件 到目前为止,我用我对PDFBox的基本知识很快就掌握了一些东西 在我的应用程序中,我正在用GUI元素在另一个类中实例化这个类(如下所示),如果我在一个文本框中输入文本,然后运行这个Java Costream已关闭,无法读取。也许它的附件已经关闭了?,java,pdf-generation,pdfbox,Java,Pdf Generation,Pdfbox,本质上,我正在尝试用Java创建一个小工具,从某种用户输入中提取文本,想象一个普通的文本框,并用它创建一个PDF文件 到目前为止,我用我对PDFBox的基本知识很快就掌握了一些东西 在我的应用程序中,我正在用GUI元素在另一个类中实例化这个类(如下所示),如果我在一个文本框中输入文本,然后运行这个PDFLetter脚本一次-它就像一个符咒一样工作,但第二次运行它时,它会崩溃,并给我这个恼人的错误: Costream已关闭,无法读取。也许它是封闭的 您的文档是否已关闭 我真的看不出有什么方法可以在
PDFLetter
脚本一次-它就像一个符咒一样工作,但第二次运行它时,它会崩溃,并给我这个恼人的错误:
Costream已关闭,无法读取。也许它是封闭的
您的文档是否已关闭
我真的看不出有什么方法可以在我的代码中触发这个错误。我认为这与我的基本“跳转到下一页”解决方案有关,但它在当前状态下工作,所以我不知道该相信什么
如果您需要知道,我实例化类的方式如下:
PDFLetter.PDFLetterGenerate(textInput.getValue().toString());
此外,我认为引发这个问题的一定是垃圾收集的某种问题,但我不再认为是这种情况
public class PDFLetter {
private static final int PAGE_MARGIN = 80;
static float TABLE_HEIGHT;
static Boolean newPage = false;
public static String text = // null;
"Ding Dong ding dong Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et "
+ "Imperdiet dui accumsan sit amet. Risus in hendrerit gravida rutrum quisque non tellus orci ac.";
static List<String> textList = new ArrayList<String>();
PDDocument document = new PDDocument();
static PDPage main_page = new PDPage();
static PDPage new_page = new PDPage();
static File file = new File("C:/PDFTests/temp.pdf");
public void PDFLetterGenerate (String args) throws Exception {
text = args;
text = text.replace("\n", "").replace("\r", "");
if(file.exists()) file.delete();
file.createNewFile();
//Creating PDF document object
PDDocument document = new PDDocument();
document.addPage(main_page);
mainBody(document, main_page);
document.addPage(new_page);
if(!newPage) document.removePage(new_page);
document.save(file);
document.close();
}
public static void mainBody(PDDocument doc, PDPage page) throws Exception {
final float width = page.getMediaBox().getWidth()-(2*PAGE_MARGIN);
int fontSize = 11;
float leading = 1.5f * fontSize;
final float max = 256;
PDFont pdfFont = PDType1Font.HELVETICA;
@SuppressWarnings("deprecation")
PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true);
int lastSpace = -1;
while (text.length() > 0){
int spaceIndex = text.indexOf(' ', lastSpace + 1);
if (spaceIndex < 0) spaceIndex = text.length();
String subString = text.substring(0, spaceIndex);
float size = fontSize * pdfFont.getStringWidth(subString) / 1000;
if (size > width){
if (lastSpace < 0) lastSpace = spaceIndex;
subString = text.substring(0, lastSpace);
textList.add(subString);
text = text.substring(lastSpace).trim();
lastSpace = -1;
}
else if (spaceIndex == text.length()){
textList.add(text);
text = "";
}
else{
lastSpace = spaceIndex;
}
}
contentStream.beginText();
contentStream.setFont(pdfFont, fontSize);
contentStream.newLineAtOffset(PAGE_MARGIN, TABLE_HEIGHT);
@SuppressWarnings("deprecation")
PDPageContentStream newStream = new PDPageContentStream(doc, new_page, true, true);
int nextPage_i = 0;
for (int i=0; i<textList.size(); i++)//String line: textList){
System.out.println("HEIGHT: "+ TABLE_HEIGHT);
nextPage_i = i;
String line = textList.get(i);
float charSpacing = 0;
if (line.length() > 1){
float size = fontSize * pdfFont.getStringWidth(line) / 1000;
float free = width - size;
if (free > 0){
charSpacing = free / (line.length() - 1);
}
TABLE_HEIGHT = TABLE_HEIGHT - 10;
}
contentStream.setCharacterSpacing(charSpacing);
contentStream.showText(line);
contentStream.newLineAtOffset(0, -leading);
if(TABLE_HEIGHT <= 280){
contentStream.endText();
contentStream.close();
newPage = true;
break;
}
}
if(!newPage){
contentStream.endText();
contentStream.close();
}
else if (newPage){
float NEW_HEIGHT = 600;
newStream.beginText();
newStream.setFont(pdfFont, fontSize);
newStream.newLineAtOffset(PAGE_MARGIN, NEW_HEIGHT);
for (int j=nextPage_i; j<textList.size(); j++)//String line: textList){
System.out.println("HEIGHT: "+ NEW_HEIGHT);
nextPage_i = j;
String line = textList.get(j);
float charSpacing = 0;
if (line.length() > 1){
float size = fontSize * pdfFont.getStringWidth(line) / 1000;
float free = width - size;
if (free > 0)
{
charSpacing = free / (line.length() - 1);
}
NEW_HEIGHT = NEW_HEIGHT - 10;
}
newStream.setCharacterSpacing(charSpacing);
newStream.showText(line);
newStream.newLineAtOffset(0, -leading);
}
newStream.endText();
newStream.close();
}
lastSpace = -1;
}
公共类PDFLetter{
专用静态最终整版页边空白=80;
静态浮台高度;
静态布尔newPage=false;
公共静态字符串text=//空;
“我的知识是不存在的,我的知识是不存在的,我的知识是不存在的。”
+“在亨德雷特的孕妇rutrum quisque non tellus orci ac.”中,死亡和死亡是相互关联的;
静态列表textList=newarraylist();
PDDocument文档=新PDDocument();
静态PDPage主页=新PDPage();
静态PDPage new_page=new PDPage();
静态文件=新文件(“C:/PDFTests/temp.pdf”);
public void PDFLetterGenerate(字符串参数)引发异常{
text=args;
text=text.replace(“\n”和“”).replace(“\r”和“”);
如果(file.exists())file.delete();
createNewFile();
//创建PDF文档对象
PDDocument文档=新PDDocument();
文件添加页(主页);
主体(文件,主页);
文件。添加页面(新页面);
如果(!newPage)文档.removePage(新页面);
文件保存(文件);
document.close();
}
公共静态void主体(PDDocument文档,PDPage页面)引发异常{
最终浮动宽度=page.getMediaBox().getWidth()-(2*页边距);
int fontSize=11;
浮动前导=1.5f*fontSize;
最终浮动最大值=256;
PDFont pdfFont=PDType1Font.HELVETICA;
@抑制警告(“弃用”)
PDPageContentStream contentStream=新的PDPageContentStream(doc,page,true,true);
int lastSpace=-1;
while(text.length()>0){
int spaceIndex=text.indexOf(“”,lastSpace+1);
如果(spaceIndex<0)spaceIndex=text.length();
String subString=text.subString(0,spaceIndex);
float size=fontSize*pdfFont.getStringWidth(子字符串)/1000;
如果(尺寸>宽度){
如果(lastSpace<0)lastSpace=spaceIndex;
subString=text.subString(0,lastSpace);
textList.add(子字符串);
text=text.substring(lastSpace.trim();
lastSpace=-1;
}
else if(spaceIndex==text.length()){
textList.add(文本);
text=“”;
}
否则{
lastSpace=spaceIndex;
}
}
contentStream.beginText();
setFont(pdfFont,fontSize);
contentStream.newlineatofset(页边距、表格高度);
@抑制警告(“弃用”)
PDPageContentStream newStream=新的PDPageContentStream(doc,new_page,true,true);
int nextPage_i=0;
对于(inti=0;i1){
float size=fontSize*pdfFont.getStringWidth(行)/1000;
自由浮动=宽度-尺寸;
如果(自由>0){
字符间距=自由/(线长度()-1);
}
台面高度=台面高度-10;
}
contentStream.setCharacterSpacing(charSpacing);
contentStream.showText(行);
contentStream.newlineatofset(0,-前导);
如果(表格高度0)
{
字符间距=自由/(线长度()-1);
}
新高度=新高度-10;
}
newStream.setCharacterSpacing(charSpacing);
newStream.showText(行);
newStream.newlineatofset(0,-前导);
}
newStream.endText();
newStream.close();
}
lastSpace=-1;
}
将PDPage
实例化拉入PDFLetterGenerate
:
public void PDFLetterGenerate (String args) throws Exception {
PDPage main_page = new PDPage();
PDPage new_page = new PDPage();
text = args;
text = text.replace("\n", "").replace("\r", "");
if(file.exists()) file.delete();
file.createNewFile();
//Creating PDF document object
PDDocument document = new PDDocument();
document.addPage(main_page);
mainBody(document, main_page);
document.addPage(new_page);
if(!newPage) document.removePage(new_page);
document.save(file);
document.close();
}
在您的代码中,当本地PDDocument文档
在添加页面后关闭时,页面实例化一次,并且在第一次运行PDFLetterGenerate
后关闭底层流
此外,还要将new_page
设置为mainBody
的参数,而不是使用静态变量来保存它
您的代码中还有许多其他问题,但是上面的更改应该会让您开始。请保佑您的心,这是一个愚蠢的问题,但有时我想每个人都会像这样被难倒。