logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

New Topic Post Reply
Options
Go to last post Go to first unread
rhnatiuk  
#1 Posted : Thursday, February 4, 2021 5:21:33 AM(UTC)
Quote
rhnatiuk

Rank: Advanced Member

Groups: Registered
Joined: 4/30/2019(UTC)
Posts: 48
Man
Finland
Location: Raisio

Thanks: 11 times
Was thanked: 3 time(s) in 2 post(s)
Moved from this thread
https://forum.patagames....ateContent-is-SLOW/page2


Thank you for your effort!

It seems I might be able to overcome the limitations of the two above-mentioned methods (objects to stream, and annotations to page) by combining them. I might be able to do it because of the nature of our needs: we have to annotate existing texts, so these will be normal annotations, and then we have to add new texts, but only outside of the current page's content, so I can insert their stream to position 0 and avoid the problem of unbalanced matrices.

But, there is one last remaining problem. I need to add Unicode texts, and because of that, I need to embed a Unicode font into the document. I am doing it like this:

Code:
var pdfFont = PdfFont.CreateEmbeddedFont(this.pdfDocument, fontBytes, FontCharSet.UNICODE_CHARSET, false);
var pdfTextObject = PdfTextObject.Create(text, x, y, pdfFont, size);


Now, if I do

Code:
pdfDocument.Save(filename, SaveFlags.Incremental)


the saving is fast, the embedded font is gone. So, I need to do

Code:
pdfDocument.Save(filename, SaveFlags.NoIncremental)


which is saving the font, but is very slow.

Is there some trick to get embedded fonts working with the Incremental flag?

Edited by moderator Thursday, February 4, 2021 5:50:59 PM(UTC)  | Reason: Moved from other thread

Paul Rayman  
#2 Posted : Thursday, February 4, 2021 5:55:45 AM(UTC)
Quote
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,138

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
Most likely it is possible. For this you need to use this method.
https://pdfium.patagames...PDFOBJ_SetIsModified.htm

The question is which object to pass there as a parameter. Most likely it should be page’s resource dictionary.
With an incremental save, only the changed objects are saved. If the object for some reason is not marked as changed, then this can be done by the above function.
rhnatiuk  
#3 Posted : Thursday, February 4, 2021 6:24:07 AM(UTC)
Quote
rhnatiuk

Rank: Advanced Member

Groups: Registered
Joined: 4/30/2019(UTC)
Posts: 48
Man
Finland
Location: Raisio

Thanks: 11 times
Was thanked: 3 time(s) in 2 post(s)
Originally Posted by: Paul Rayman Go to Quoted Post
Most likely it is possible. For this you need to use this method.
https://pdfium.patagames...PDFOBJ_SetIsModified.htm

The question is which object to pass there as a parameter. Most likely it should be page’s resource dictionary.
With an incremental save, only the changed objects are saved. If the object for some reason is not marked as changed, then this can be done by the above function.


Well, using Pdfium.FPDFOBJ_SetIsModified(pdfPage.Dictionary.Handle, true) didn't help. But isn't embedded font stored somewhere on the document level? I have no idea, to be honest, how to make it work. I appreciate your help a lot!!!
Paul Rayman  
#4 Posted : Thursday, February 4, 2021 5:40:42 PM(UTC)
Quote
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,138

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
Well, I think it is an architectural oversight that newly created fonts are not marked as modified. I think this will be fixed in future releases. I've already passed this issue over to the core development team.
Currently, you can use the following workaround.

Code:

using (var doc = PdfDocument.Load(@"e:\11\test.pdf"))
{
    var font = PdfFont.CreateEmbeddedFont(doc, fontData, FontCharSet.UNICODE_CHARSET, false);
    SetAsModifiedRecursively(font.Dictionary);

    doc.Pages[0].PageObjects.Add(PdfTextObject.Create("Test string", 50, 50, font, 15));
    doc.Pages[0].GenerateContent();
    doc.Save(@"e:\11\test_incremental.pdf", SaveFlags.Incremental);
}


Code:

private static void SetAsModifiedRecursively(PdfTypeDictionary dictionary)
{
    Pdfium.FPDFOBJ_SetIsModified(dictionary.Handle, true);
    foreach(var item in dictionary.Values)
    {
        if (item.Is<PdfTypeDictionary>())
            SetAsModifiedRecursively(item.As<PdfTypeDictionary>());
        else if(item.Is<PdfTypeArray>())
            SetAsModifiedRecursively(item.As<PdfTypeArray>());
        else
            Pdfium.FPDFOBJ_SetIsModified(Pdfium.FPDFOBJ_GetDirect(item.Handle), true);
    }
}
private static void SetAsModifiedRecursively(PdfTypeArray array)
{
    Pdfium.FPDFOBJ_SetIsModified(array.Handle, true);
    foreach (var item in array)
    {
        if (item.Is<PdfTypeDictionary>())
            SetAsModifiedRecursively(item.As<PdfTypeDictionary>());
        else if (item.Is<PdfTypeArray>())
            SetAsModifiedRecursively(item.As<PdfTypeArray>());
        else
            Pdfium.FPDFOBJ_SetIsModified(Pdfium.FPDFOBJ_GetDirect(item.Handle), true);
    }
}
thanks 1 user thanked Paul Rayman for this useful post.
rhnatiuk on 2/4/2021(UTC)
rhnatiuk  
#5 Posted : Thursday, February 4, 2021 11:37:21 PM(UTC)
Quote
rhnatiuk

Rank: Advanced Member

Groups: Registered
Joined: 4/30/2019(UTC)
Posts: 48
Man
Finland
Location: Raisio

Thanks: 11 times
Was thanked: 3 time(s) in 2 post(s)
Originally Posted by: Paul Rayman Go to Quoted Post
Well, I think it is an architectural oversight that newly created fonts are not marked as modified. I think this will be fixed in future releases. I've already passed this issue over to the core development team.
Currently, you can use the following workaround.


OMG, Paul, you are GENIUS!!! :D It works!
Paul Rayman  
#6 Posted : Thursday, February 4, 2021 11:49:41 PM(UTC)
Quote
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,138

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
Thanks a lot.

By the way, the fix has already been published.
https://forum.patagames....on-Feb-05--2021#post2149
Quick Reply Show Quick Reply
Users browsing this topic
Guest (2)
New Topic Post Reply
Forum Jump  
You can post new topics in this forum.
You can reply to topics in this forum.
You can delete your posts in this forum.
You can edit your posts in this forum.
You cannot create polls in this forum.
You can vote in polls in this forum.