ststeiger / PdfSharpCore

Port of the PdfSharp library to .NET Core - largely removed GDI+ (only missing GetFontData - which can be replaced with freetype2)
Other
1.08k stars 237 forks source link

PdfSharpCore + MigraDocCore: NullReferenceException DocumentRenderer.RenderObject paragraph.AddBookmark #317

Open virajkanwade opened 1 year ago

virajkanwade commented 1 year ago

I am trying to add Table of Contents to the PDF. I referred: http://www.pdfsharp.net/wiki/HelloMigraDoc-sample.ashx http://www.pdfsharp.com/PDFsharp/index.php?option=com_content&task=view&id=55&Itemid=63

But when I try to paragraph.AddBookmark and then DocumentRenderer.RenderObject, it raises this exception.

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at MigraDocCore.Rendering.ParagraphRenderer.FormatBookmarkField(BookmarkField bookmarkField)
   at MigraDocCore.Rendering.ParagraphRenderer.FormatElement(DocumentObject docObj)
   at MigraDocCore.Rendering.ParagraphRenderer.Format(Area area, FormatInfo previousFormatInfo)
   at MigraDocCore.Rendering.DocumentRenderer.RenderObject(XGraphics graphics, XUnit xPosition, XUnit yPosition, XUnit width, DocumentObject documentObject)

Sample code:

var document = new PdfDocument();
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
Document doc = new Document();
Section section = doc.AddSection();
Paragraph paragraph = section.AddParagraph("Charts");
paragraph.AddBookmark("Charts");
DocumentRenderer docRenderer = new DocumentRenderer(doc);
docRenderer.PrepareDocument();
docRenderer.RenderObject(gfx, XUnit.FromCentimeter(5), XUnit.FromCentimeter(10), XUnit.FromCentimeter(12), paragraph);

What am I missing here?

I am using the packages from nuget.

virajkanwade commented 1 year ago
        FormatResult FormatBookmarkField(BookmarkField bookmarkField)
        {
            this.fieldInfos.AddBookmark(bookmarkField.Name);
            return FormatResult.Ignore;
        }

this.fieldInfos is null which is raising this exception.

        public void RenderObject(XGraphics graphics, XUnit xPosition, XUnit yPosition, XUnit width, DocumentObject documentObject)
        {
            if (graphics == null)
                throw new ArgumentNullException("graphics");

            if (documentObject == null)
                throw new ArgumentNullException("documentObject");

            if (!(documentObject is Shape) && !(documentObject is Table) &&
                !(documentObject is Paragraph))
                throw new ArgumentException(AppResources.ObjectNotRenderable, "documentObject");

            Renderer renderer = Renderer.Create(graphics, this, documentObject, null);
            renderer.Format(new Rectangle(xPosition, yPosition, width, double.MaxValue), null);

            RenderInfo renderInfo = renderer.RenderInfo;
            renderInfo.LayoutInfo.ContentArea.X = xPosition;
            renderInfo.LayoutInfo.ContentArea.Y = yPosition;

            renderer = Renderer.Create(graphics, this, renderer.RenderInfo, null);
            renderer.Render();
        }

RenderObject creates new renderer objects but passes null to fieldInfos.