I am attempting to append a text on a PDF page but it throws an exception : System.ArgumentException: An entry with the same key already exists.
I am using Aspose.Pdf version 19.9.0.0 and .Net version 4.6.1.
I append the text in a page using Aspose.Pdf.Text.Textbuilder class and pass the pdf page to the Textbuilder there and then it throws the below exception.
Below is the stacktrace for the exception :
System.ArgumentException: An entry with the same key already exists.
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.TreeSet1.AddIfNotPresent(T item) at System.Collections.Generic.SortedDictionary
2.Add(TKey key, TValue value)
at #=zNk3AsTPFQ2EkegyDfWuwq5jwQ78$eLvWIOUrxvK$3zVnC42qUw==.#=zmOnCVH8Iuy3E()
at #=zoCGnhrJd7g$41iRHszv63stjzwpbJYcQHzdCsjhS1kWzhDIm71rPFBCy0F7K.#=zX8Cg2dXU22zCPXg_KA==()
at #=zoCGnhrJd7g$41iRHszv63stjzwpbJYcQHzdCsjhS1kWzhDIm71rPFBCy0F7K.#=z_psdCtyZwSvq(BaseOperatorCollection #=zKsiV6Rc=, Resources #=zwGy_ws0=)
at #=zoCGnhrJd7g$41iRHszv63stjzwpbJYcQHzdCsjhS1kWzhDIm71rPFBCy0F7K.#=zXqy_QAs=()
at #=zoCGnhrJd7g$41iRHszv63stjzwpbJYcQHzdCsjhS1kWzhDIm71rPFBCy0F7K…ctor(Page #=zIl68e1s=, TextSearchOptions #=zTtjBa0fdt9vm, Resources #=zwGy_ws0=, BaseOperatorCollection #=zSnTsPMU=)
at #=zoCGnhrJd7g$41iRHszv63stjzwpbJYcQHzdCsjhS1kWzhDIm71rPFBCy0F7K…ctor(Page #=zIl68e1s=, BaseOperatorCollection #=zSnTsPMU=)
at Aspose.Pdf.Text.TextBuilder…ctor(Page page)
Following is the code I am using to append the text.
var date = DateTime.Now.ToShortDateString();
// Create text fragment
TextFragment textFragment = new TextFragment(date);
textFragment.Position = new Position((double)stamp.DateBottomLeftXcoordinate, (double)stamp.DateTopRightYcoordinate - textFragment.Rectangle.Height);
// Set text properties
textFragment.TextState.FontSize = PdfMergeDocument.FontSize;
Font font = FontRepository.FindFont(PdfMergeDocument.FontStyle);
textFragment.HorizontalAlignment = HorizontalAlignment.Left;
textFragment.VerticalAlignment = VerticalAlignment.Top;
font.IsEmbedded = true;
font.FontOptions.NotifyAboutFontEmbeddingError = true;
textFragment.TextState.Font = font;
// Create TextBuilder object --- **Throws Exception here**
TextBuilder textBuilder = new TextBuilder(page);
// Append the text fragment to the PDF page
textBuilder.AppendText(textFragment);
It does not work for some pdfs pages otherwise it works smoothly and append the text on the pdf.
I am also attaching the pdf file here to look into the issue more deeply.
SameKeyError-Issue.pdf (68.2 KB)