我正在使用PDFSharp(1.50或6.1.0 Preview 2)try 打开现有的pdf,添加一些文本,然后再次保存.

然而,在绘制文本的那一行,我得到了System. ArgumentResponse "An item with the same key has already been added."

我不明白这个错误想告诉我什么或如何解决它.

注释DrawString行即可编写新的pdf,但如何将文本添加到PDF中?

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);
        }
    }
}

STACKTRACE:

 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)

UPDATE:

我发现,如果我开始使用的PDF完全空或仅包含形状,上述代码仍然有效.如果PDF仅包含一段文本,则会触发此异常.那么这可能是PDFSharp中的一个错误吗?

引发该问题的PDF可以从here下载.

推荐答案

这是6.1.0 Preview 2版本之前的PDFSharp中的一个错误.

该错误已通过PDFsharp version 6.1.0 Preview 3修复,今天发布.

The check for duplicate resource names in PDFsharp was too strict and prevented those files from opening.
PDFsharp was published about 20 years ago and this issue had not been reported before

Csharp相关问答推荐

GC未修复有关.Net Entity框架版本8(C#控制台应用程序.Net8.02)的内存泄漏

Mstest + Coverlet在收件箱中没有达到100%的覆盖率,但我在Codecov中得到了100%的覆盖率

Should. ThrowAsynock未捕获来自httpClient. GetAsynock()请求的异常

Rx.Net -当关闭序列被触发时如何聚合消息并发出中间输出?

更新数据库中的对象失败,原因是:Microsoft. EntityFrame Core. GbUpdateConcurrencyResponse'

使用客户端密钥为Fabric Rest API生成令牌

如何使用while循环实现异常处理

使用C#中的SDK在Microsoft Graph API上使用SubscribedSkus的问题

限制特定REST API不被访问,但部署代码

如何在Parall.ForEachAsync中使用CancerationTokenSource

Unix上的.NET(核心):.NET意外地未看到通过P/Invoke系统调用对环境变量进行的进程内修改

C# CompareTo()和Compare()可以返回除-1和1以外的整数吗?

如何将字符串变量传递给JObject C#-无法加载文件或程序集';System.Text.Json

HttpRequestMessage.SetPolicyExecutionContext不会将上下文传递给策略

如何实现有条件的自定义Json转换器隐藏属性

我的命名管道在第一次连接后工作正常,但后来我得到了System.ObjectDisposedException:无法访问关闭的管道

C#USB条形码 scanner 在第二次扫描时未写入行尾字符

Xamarin.Forms中具有类似AspectFill的图像zoom 的水平滚动视图

将两个for循环更改为一条LINQ语句

项目参考和方法签名问题