[英]Xfinium PDF: Unable to save pdf file. Receiving error root missing entry
I have a problem saving the Xfinium PDF.我在保存 Xfinium PDF 时遇到问题。 I loaded a document and simply draw the line across the page and save.我加载了一个文档,然后简单地在页面上画线并保存。 The file generated but when I used that file and loaded back to PdfFixedDocument I have the error生成的文件但是当我使用该文件并加载回 PdfFixedDocument 时出现错误
Root entry is missing in file trailer文件尾中缺少根条目
My code is very simple:我的代码很简单:
var pdf = new PdfFixedDocument (document.Location);
var page = pdf.Pages [pageIndex];
var graphics = page.Graphics;
var directory = FileUtilities.GetExternalPrivateDirectory (PdfCore.CACHE_DIRECTORY);
//var png = FileUtilities.GetFile (directory + "/test.pdf");
//var rawStream = File.OpenWrite (png.AbsolutePath);
var stream = new FileStream (directory + "/test.pdf", FileMode.Create);
pdf.BeginSave (stream);
graphics.DrawLine(new Xfinium.Pdf.Graphics.PdfPen (),
new Xfinium.Pdf.Graphics.PdfPoint (0,0),
new Xfinium.Pdf.Graphics.PdfPoint (page.Width, page.Height));
page.SaveGraphics ();
pdf.EndSave ();
Instead of manually creating the stream and using BeginSave
/ EndSave
, just try using Save
: 与其尝试手动创建流并使用BeginSave
/ EndSave
, EndSave
尝试使用Save
:
var pdf = new PdfFixedDocument();
var page = pdf.Pages.Add();
var graphics = page.Graphics;
graphics.DrawLine(new Xfinium.Pdf.Graphics.PdfPen(),
new Xfinium.Pdf.Graphics.PdfPoint(0, 0),
new Xfinium.Pdf.Graphics.PdfPoint(page.Width, page.Height));
var directory = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads);
pdf.Save(Path.Combine(directory.Path, "test.pdf"));
Java.IO.File pdfFILE = new Java.IO.File(Path.Combine(directory.Path, "test.pdf"));
Intent intent = new Intent(Intent.ActionView);
intent.SetDataAndType(Uri.FromFile(pdfFILE), "application/pdf");
StartActivity(intent);
I believe that the PDF document is not properly written to file because the stream is not closed. 我认为PDF文档未正确写入文件,因为未关闭流。 Try adding these lines at the end of your code (after pdf.EndSave()): 尝试在代码末尾(在pdf.EndSave()之后)添加以下行:
stream.Flush();
stream.Close();
The BeginSave/SaveGraphics/EndSave mode should be used when creating pages with heavy content, such as vector maps, and you want to reduce memory consumption by calling SaveGraphics after drawing a certain number of graphic objects (SaveGraphics can be called multiple times for a page). 当创建内容丰富的页面(如矢量地图)时,应使用BeginSave / SaveGraphics / EndSave模式,并且您希望通过在绘制一定数量的图形对象后调用SaveGraphics来减少内存消耗(SaveGraphics可以为页面多次调用)。
For your scenario you can simplify the code as follows (like SushiHangover suggested): 对于您的方案,您可以按以下方式简化代码(建议使用SushiHangover):
var pdf = new PdfFixedDocument (document.Location);
var page = pdf.Pages [pageIndex];
var graphics = page.Graphics;
graphics.DrawLine(new Xfinium.Pdf.Graphics.PdfPen (),
new Xfinium.Pdf.Graphics.PdfPoint (0,0),
new Xfinium.Pdf.Graphics.PdfPoint (page.Width, page.Height));
var directory = FileUtilities.GetExternalPrivateDirectory(PdfCore.CACHE_DIRECTORY);
pdf.Save(directory + "/test.pdf");
Disclaimer: I work for the company that develops XFINIUM.PDF library. 免责声明:我为开发XFINIUM.PDF库的公司工作。
.Save (document path) is massively slow when it comes to eg: 100 pages and lots of data so the end file has about 145MB or more, then the saving operation takes 2 minutes pls - this is ways to slow so I will try the BeginSave and EndSave approach also .Save(文档路径)非常慢,例如:100页和大量数据,所以最终文件大约有145MB或更多,然后保存操作需要2分钟 - 这是减慢速度的方法,所以我会尝试BeginSave 和 EndSave 方法也
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.