When trying to add text to a PDF (That already contains text) I'm getting an Exception Exception System.ArgumentException
This issue is present in both 1.50 and 6.1.0
This issue only occurs if the input PDF already contains some text, if the input PDF only contains shapes, then the code below works and the text is added as expected.
Expected Behavior
I expected the additional text to be added to the the new PDF
Actual Behaviour
I get an exception System.ArgumentException "An item with the same key has already been added"
System.ArgumentException
HResult=0x80070057
Message=An item with the same key has already been added.
Source=mscorlib
StackTrace:
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at PdfSharp.Pdf.Advanced.PdfResourceMap.CollectResourceNames(Dictionary`2 usedResourceNames)
at PdfSharp.Pdf.Advanced.PdfResources.ExistsResourceNames(String name)
at PdfSharp.Pdf.Advanced.PdfResources.get_NextExtGStateName()
at PdfSharp.Pdf.Advanced.PdfResources.AddExtGState(PdfExtGState extGState)
at PdfSharp.Drawing.Pdf.PdfGraphicsState.RealizeFillColor(XColor color, Boolean overPrint, PdfColorMode colorMode)
at PdfSharp.Drawing.Pdf.PdfGraphicsState.RealizeBrush(XBrush brush, PdfColorMode colorMode, Int32 renderingMode, Double fontEmSize)
at PdfSharp.Drawing.Pdf.PdfGraphicsState.RealizeFont(XFont font, XBrush brush, Int32 renderingMode)
at PdfSharp.Drawing.Pdf.XGraphicsPdfRenderer.Realize(XFont font, XBrush brush, Int32 renderingMode)
at PdfSharp.Drawing.Pdf.XGraphicsPdfRenderer.DrawString(String s, XFont font, XBrush brush, XRect rect, XStringFormat format)
at PdfSharp.Drawing.XGraphics.DrawString(String text, XFont font, XBrush brush, XRect layoutRectangle, XStringFormat format)
Steps to Reproduce the Behavior
Using the following code, with a PDF that contains text, triggers the issue
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PdfSharp;
using PdfSharp.Pdf.IO;
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using PdfSharp.Fonts;
namespace MakeAPDF
{
internal class Program
{
static void Main(string[] args)
{
const string outfilename = @"C:\Test\Out.pdf";
PdfDocument document = PdfReader.Open(@"C:\Test\In.pdf",PdfDocumentOpenMode.Modify);
// Get an XGraphics object for drawing
PdfPage page = document.Pages[0];
XGraphics gfx = XGraphics.FromPdfPage(page);
// Create a font
XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic);
// Draw the text
// This line triggers System.ArgumentException
// Message=An item with the same key has already been added.
gfx.DrawString("Hello, World!", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
// Save the document...
document.Save(outfilename);
}
}
}
Issue
When trying to add text to a PDF (That already contains text) I'm getting an Exception Exception System.ArgumentException This issue is present in both 1.50 and 6.1.0 This issue only occurs if the input PDF already contains some text, if the input PDF only contains shapes, then the code below works and the text is added as expected.
Expected Behavior
I expected the additional text to be added to the the new PDF
Actual Behaviour
I get an exception System.ArgumentException "An item with the same key has already been added"
Steps to Reproduce the Behavior
Using the following code, with a PDF that contains text, triggers the issue