java.lang.illegalstateexception: 'current page not finished!'



在我使用itext7创建一个包含阿拉伯语文本的表的pdf失败后,我决定转向正常的方式,使用view.draw并将结果保存在pdf中。但这也没有奏效。这是代码:

Android.Graphics.Pdf.PdfDocument pdf_new = new Android.Graphics.Pdf.PdfDocument();
Android.Graphics.Pdf.PdfDocument.PageInfo pageInfo_new = new Android.Graphics.Pdf.PdfDocument.PageInfo.Builder(6000, 6000, 0).Create();
Android.Graphics.Pdf.PdfDocument.Page page_new = pdf_new.StartPage(pageInfo_new);
ListView listView = new ListView(this.Context);
accounts_info_homeadapter hmm = new accounts_info_homeadapter(this, pdflist);
listView.Adapter = hmm;
listView.SetBackgroundColor(Android.Graphics.Color.Aqua);
listView.Draw(page_new.Canvas);
// lst.Draw(page.Canvas);
//TextView txt = new TextView(this.Context);
//txt.Text = "hi";
//txt.Draw(page.Canvas);
var path = global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
file_Path = Path.Combine(path.ToString(), "Client.pdf");
new_stream = new FileStream(file_Path, FileMode.Create);
new_stream.Flush();
pdf_new.WriteTo(new_stream);
new_stream.Close();
pdf_new.Close();

我得到了一个例外:java.lang.illegalstateexception: 'current page not finished!',尽管我一周前在试用列表中尝试过,但它确实有效。现在我明白了。我想它可能有pdf的大小,所以我开始增加它,但没有成功。我试着更改变量的名称,我想也许是因为我以前试过在itext中使用它们,但仍然没有。我删除了我正在处理的片段,然后重新创建了它,但什么都没有。任何人都知道问题出在哪里。提前谢谢。

您漏掉了一行。编辑后您忘记关闭页面。

pdf_new.finishPage(page_new)

在关闭页面之前,必须通过调用finishPage(page(来完成页面,否则会引发异常。

关闭((文档参考:

如果startPage(android.graphics.pdf.PdfDocument.PageInfo(返回的页面未通过调用finishPage(andrio.graphics.cpdf.pdf Document.page(完成,请不要调用此方法。

在您的情况下,它应该是:

pdf_new.finishPage(page_new);
pdf_new.close();

来源:close((方法文档引用

在画布中插入所需文本后,请确保放置这行代码。这帮助我删除了错误

canvas.drawText("Financial Statement for 2021",40,50,myPaint);
myPdfDocument.finishPage(myPage1); //this one

最新更新