Using PDFSharp (1.50) I’m trying to open an existing pdf, add some text, then save it again.
However at the line that draws the text I’m getting a System.ArgumentException "An item with the same key has already been added."
I do not understand what this error is trying to tell me or how to resolve it.
Commenting out DrawString
line allows the new pdf to be writen, but how do I add text to the PDF?
<code>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:TestOut.pdf";
PdfDocument document = PdfReader.Open(@"C:TestIn.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);
}
}
}
</code>
<code>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:TestOut.pdf";
PdfDocument document = PdfReader.Open(@"C:TestIn.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);
}
}
}
</code>
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:TestOut.pdf";
PdfDocument document = PdfReader.Open(@"C:TestIn.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);
}
}
}
1