简体   繁体   English

Xfinium PDF:无法保存 pdf 文件。 接收错误根缺失条目

[英]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 / EndSaveEndSave尝试使用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.

相关问题 无法从Android中的链接保存pdf文件 - Unable to save pdf file from link in Android XFinium PDF库mobile:如何获取包装文本的高度 - XFinium PDF library mobile: How to get height of a wrapped text 我正在使用Pdf查看器库打开Pdf文件。 它显示了很长时间的pdf加载 - I am using Pdf viewer library to open Pdf file. It shows pdf loading for a long time 无法从 WebView 下载 pdf 文件。 始终下载.bin 文件。 我签入了具有 android 10 的一加手机 - Unable to download the pdf file from WebView. Always download .bin file. I checked in one plus mobile having android 10 使用ITextPDF保存PDF文件 - Save a PDF file using ITextPDF 如何在 SQLite 中保存 pdf 文件 - how to save pdf file in SQLite 无法在应用程序中打开.pdf文件 - Unable to open .pdf file in the application Android Kotlin PDFtron:如何将 pdf 从内部存储连接到 ZCE8AE9DA5B7CD6C3DF29295243。 为什么我的错误“无法附加文件”。 发生了什么? - Android Kotlin PDFtron: How to attach an pdf from internal Storage to an Email. Why my Error "Couldn't attach file." happends? 无法使用 FileProvider 和外部 PDF 编辑器保存 PDF 文件 - Can not save PDF file with FileProvider and external PDF Editor android studio下载pdf文件并将其保存在sharedpreferences中 - android studio download pdf file and save it in sharedpreferences
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM