Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/UglyToad.PdfPig/Writer/PdfPageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ public PdfPageBuilder CopyFrom(Page srcPage)
// We need to relocate the resources, and we have to make sure that none of the resources collide with
// the already written operation's resources

var resources = pageDictionary.GetOrCreateDict(NameToken.Resources);
var resources = pageDictionary.GetOrCreateDict(NameToken.Resources, srcPage.pdfScanner);

foreach (var set in srcResourceDictionary.Data)
{
Expand All @@ -858,7 +858,7 @@ public PdfPageBuilder CopyFrom(Page srcPage)
// Since we don't directly add font's to the pages resources, we have to go look at the document's font
if (srcResourceDictionary.TryGet(NameToken.Font, srcPage.pdfScanner, out DictionaryToken? fontsDictionary))
{
var pageFontsDictionary = resources.GetOrCreateDict(NameToken.Font);
var pageFontsDictionary = resources.GetOrCreateDict(NameToken.Font, srcPage.pdfScanner);

foreach (var fontSet in fontsDictionary.Data)
{
Expand Down Expand Up @@ -903,7 +903,7 @@ public PdfPageBuilder CopyFrom(Page srcPage)
// Since we don't directly add xobjects's to the pages resources, we have to go look at the document's xobjects
if (srcResourceDictionary.TryGet(NameToken.Xobject, srcPage.pdfScanner, out DictionaryToken? xobjectsDictionary))
{
var pageXobjectsDictionary = resources.GetOrCreateDict(NameToken.Xobject);
var pageXobjectsDictionary = resources.GetOrCreateDict(NameToken.Xobject, srcPage.pdfScanner);

foreach (var xobjectSet in xobjectsDictionary.Data)
{
Expand Down Expand Up @@ -945,7 +945,7 @@ public PdfPageBuilder CopyFrom(Page srcPage)
// Since we don't directly add xobjects's to the pages resources, we have to go look at the document's xobjects
if (srcResourceDictionary.TryGet(NameToken.ExtGState, srcPage.pdfScanner, out DictionaryToken? gsDictionary))
{
var pageGstateDictionary = resources.GetOrCreateDict(NameToken.ExtGState);
var pageGstateDictionary = resources.GetOrCreateDict(NameToken.ExtGState, srcPage.pdfScanner);

foreach (var gstate in gsDictionary.Data)
{
Expand Down
45 changes: 22 additions & 23 deletions src/UglyToad.PdfPig/Writer/WriterUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,36 @@

internal static class WriterUtil
{
public static Dictionary<string, IToken> GetOrCreateDict(this Dictionary<NameToken, IToken> dict, NameToken key)
public static Dictionary<string, IToken> GetOrCreateDict<T>(
this Dictionary<T, IToken> dict,
T key,
IPdfTokenScanner? sourceScanner = null) where T : notnull
{
if (dict.TryGetValue(key, out var item))
{
if (!(item is DictionaryToken dt))
var chainCount = 0;
var itemChain = item;
while (itemChain is IndirectReferenceToken ir && chainCount < 100)
{
throw new ApplicationException("Expected dictionary token, got " + item.GetType());
}
if (sourceScanner == null)
{
break;
}

if (dt.Data is Dictionary<string, IToken> mutable)
{
return mutable;
}
itemChain = sourceScanner.Get(ir.Data);

mutable = dt.Data.
ToDictionary(x => x.Key, x => x.Value);
dict[key] = DictionaryToken.With(mutable);
return mutable;
}
chainCount++;

var created = new Dictionary<string, IToken>();
dict[key] = DictionaryToken.With(created);
return created;
}
if (itemChain is ObjectToken ot)
{
itemChain = ot.Data;
}
}

public static Dictionary<string, IToken> GetOrCreateDict(this Dictionary<string, IToken> dict, string key)
{
if (dict.TryGetValue(key, out var item))
{
if (!(item is DictionaryToken dt))
if (itemChain is not DictionaryToken dt)
{
throw new ApplicationException("Expected dictionary token, got " + item.GetType());
throw new InvalidOperationException(
$"While trying to copy token called {key} which should have been a dictionary token we found a token of type {item.GetType()}");
}

if (dt.Data is Dictionary<string, IToken> mutable)
Expand All @@ -61,6 +59,7 @@ public static Dictionary<string, IToken> GetOrCreateDict(this Dictionary<string,
dict[key] = DictionaryToken.With(created);
return created;
}

/// <summary>
/// The purpose of this method is to resolve indirect reference. That mean copy the reference's content to the new document's stream
/// and replace the indirect reference with the correct/new one
Expand Down
Loading