Skip to content

DrawString fails with NewLine character in it #488

@TonyValenti

Description

@TonyValenti

If you have this code:



using PdfSharpCore.Drawing;
using PdfSharpCore.Drawing.Layout;
using PdfSharpCore.Pdf.IO;
using System.Reflection.Metadata;

var Source = $@"C:\Users\Admin\Downloads\sample-1.pdf";

var Accuracy = PdfSharpCore.Pdf.IO.enums.PdfReadAccuracy.Moderate;

var PDF = PdfReader.Open(Source, PdfDocumentOpenMode.Modify, Accuracy);

var This = PDF.Pages[0];

var Message = "Line1\r\nLine2";

var Font = new XFont("Arial", 12);
using var G = XGraphics.FromPdfPage(This);
var TF = new XTextFormatter(G);
var Rect = new XRect(0, 0, This.Width, This.Height);
TF.DrawString(Message, Font, XBrushes.Black, Rect);

You will always get:

Object reference not set to an instance of an object

It appears that the issue is because the newline has a NULL for the content and the .Trim() throws the error in this line:

_gfx.DrawString(block.Text.Trim(), font, brush, num2, num + array3.First().Location.Y, XStringFormats.TopLeft);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions