empira / PDFsharp-1.5

A .NET library for processing PDF
MIT License
1.27k stars 588 forks source link

Square brackets "[]" in custom property name corrupts pdf file on save #43

Closed ehcanadianCA closed 6 years ago

ehcanadianCA commented 6 years ago

If a PDF has a custom property whose name contains square brackets the file is corrupted when it is saved by PDFsharp. On the next open, PDFsharp gives an UnexpectedToken error and is unable to open the file. One is able to open the file in Acrobat Reader, but not able to view the file's properties.

I have included an example solution. CustomPropertyIssue.zip

ehcanadianCA commented 6 years ago

The PDF isn't corrupt until PDFsharp saves it. The exception occurs on line 39 of the example I submitted. I just tested with beta5 and the issue exists.

In this case, the PDF has a custom property of 'Nominal Diameter [in]'.

image

ehcanadianCA commented 6 years ago

Attached is an updated solution. It opens the PDF that has the custom property in it and saves it to a new file. That file causes an exception on load. I then delete the custom property and save the file again. That new file opens fine. CustomPropertyIssueBeta5.zip

ThomasHoevel commented 6 years ago

Quick fix: in file PdfWriter.cs locate the method "public void Write(PdfName value)" and insert two new lines for square brackets: case ')': case '[': case ']': case '#':

This change will be included with the next release.

ehcanadianCA commented 6 years ago

Excellent, thank you!