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

Notification

Icon
Error

2 Pages<12
New Topic Post Reply
Options
Go to last post Go to first unread
Paul Rayman  
#21 Posted : Wednesday, June 26, 2019 3:52:29 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)
Originally Posted by: rhnatiuk Go to Quoted Post
It seems that conversion from fake annotation to stream shrinks all contained objects. But this happens only to some files, like this one. Any ideas?


222.png

The problem is that the file contains an “unbalanced” zoom out matrix (line 21 in the screenshot)
And the text object, has the zoom in matrix (line 52)
The bounding box of the text object is obtained by multiplying these matrices.
When we add the path to the content of the page, this "unbalanced" matrix is ​​also applied to it (because it is not enclosed in the brackets of the stack operator - operator q..Q.
We must somehow compensate for this matrix, but I have not yet figured out how to do this, especially since a solution is needed in a general way.

So, if you add following
Code:
 pdfPathObject.Matrix = new FS_MATRIX(100 / 12f, 0, 0, 100 / 12f, 0, 0);

The path will shown in right place. But how to obtain these values....

Edited by user Wednesday, June 26, 2019 4:10:03 AM(UTC)  | Reason: Not specified

Paul Rayman  
#22 Posted : Wednesday, June 26, 2019 4:22:58 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)
Originally Posted by: rhnatiuk Go to Quoted Post
It seems that conversion from fake annotation to stream shrinks all contained objects. But this happens only to some files, like this one. Any ideas?


I have one more idea.
You can eliminate the effect of drawing operators (as well as matrix operators) on your objects if you add your sinsert your stream as a first element of the contents array.

instead of
Code:
contentsArray.AddIndirect(list, stream);

use
Code:
contentsArray.Insert(...);


like shown below
Code:
var stream = PdfTypeStream.Create();
var list = PdfIndirectList.FromPdfDocument(doc);
int num = list.Add(stream);
....
contentsArray.Insert(0, PdfTypeIndirect.Create(list, num));


thanks 1 user thanked Paul Rayman for this useful post.
rhnatiuk on 6/26/2019(UTC)
rhnatiuk  
#23 Posted : Wednesday, June 26, 2019 4:40:24 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
No, no

I mean following [...]


This one works, also on those "broken" files! :) I had to add AnnotationFlags.Locked | AnnotationFlags.LockedContents | AnnotationFlags.ReadOnly | AnnotationFlags.Print flags to all annotations (sad, that it is impossible to make annotations not selectable), but the result looks good. Probably we will go with this solution for now. Thank you!

rhnatiuk  
#24 Posted : Wednesday, June 26, 2019 5:25:57 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
Originally Posted by: rhnatiuk Go to Quoted Post
It seems that conversion from fake annotation to stream shrinks all contained objects. But this happens only to some files, like this one. Any ideas?


I have one more idea.
You can eliminate the effect of drawing operators (as well as matrix operators) on your objects if you add your sinsert your stream as a first element of the contents array.

instead of
Code:
contentsArray.AddIndirect(list, stream);

use
Code:
contentsArray.Insert(...);



And yes, this works on "broken" PDFs as well!!! It is a bit faster than annotations-based solution as well. Thank you! We will use this approach!
Guest  
#25 Posted : Wednesday, September 2, 2020 1:55:35 AM(UTC)
Quote
Guest

Rank: Guest

Groups: Guests
Joined: 1/5/2016(UTC)
Posts: 162

Was thanked: 5 time(s) in 5 post(s)
Originally Posted by: Paul Rayman Go to Quoted Post


The problem is that the file contains an “unbalanced” zoom out matrix (line 21 in the screenshot)
And the text object, has the zoom in matrix (line 52)
The bounding box of the text object is obtained by multiplying these matrices.
When we add the path to the content of the page, this "unbalanced" matrix is ​​also applied to it (because it is not enclosed in the brackets of the stack operator - operator q..Q.
We must somehow compensate for this matrix, but I have not yet figured out how to do this, especially since a solution is needed in a general way.

。。。。


Do you now have a general solution to this “unbalanced matrices” problem?
Guest  
#26 Posted : Wednesday, January 27, 2021 12:47:13 PM(UTC)
Quote
Guest

Rank: Guest

Groups: Guests
Joined: 1/5/2016(UTC)
Posts: 162

Was thanked: 5 time(s) in 5 post(s)
Hello,

I am trying to evaluate this SDK for use in my company. One key element is adding images to PDFs. However, I have run into the same issue where the GenerateContent method is very slow. Like almost 30 seconds to add a single image to a single page. It works fine, just unacceptably slow. It also nearly doubles the file size. Our pages are erection drawings for commercial buildings created from a CAD program so they are not simple and do have a lot of content. I have tried all the solutions posted here but get errors when trying to open in Adobe acrobat afterward. The PDF will render but complains about an error on the page and doesn't show the image I added. However, if I open the PDF in the WPF viewer part of this SDK or in another viewer like Foxit, it works fine. So using the stream fixes the speed issue but makes the PDF incompatible with Acrobat which is the standard typically and we are sending these PDFs to customers across the country.

Any ideas?

Thanks,
Paul Rayman  
#27 Posted : Wednesday, January 27, 2021 8:20:04 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)
Hello,

Please contact support@patagames.com with the sample console app there this issue isreproduced, the document and the image you are trying to insert.
rhnatiuk  
#28 Posted : Monday, February 1, 2021 5:44:45 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)
Hi, Paul Rayman ,

Originally Posted by: Paul Rayman Go to Quoted Post
The problem is that the file contains an “unbalanced” zoom out matrix (line 21 in the screenshot)
And the text object, has the zoom in matrix (line 52)
The bounding box of the text object is obtained by multiplying these matrices.
When we add the path to the content of the page, this "unbalanced" matrix is ​​also applied to it (because it is not enclosed in the brackets of the stack operator - operator q..Q.
We must somehow compensate for this matrix, but I have not yet figured out how to do this, especially since a solution is needed in a general way.

So, if you add following
Code:
 pdfPathObject.Matrix = new FS_MATRIX(100 / 12f, 0, 0, 100 / 12f, 0, 0);

The path will shown in right place. But how to obtain these values....


Any news on the subject? Now we cannot use Pdfium for writing files because of prohibitively slow GenerateContent on one side, and these "unbalanced" matrices on the other, preventing us from adding a new stream to the end of the list (and we couldn't go the way you proposed, and insert it into the beginning of the list: some PDF objects, including texts, had backgrounds, and therefore our additions went under those).

So, it would be really cool if you would come up with some solution for this problem.

Thank you!

Edited by user Monday, February 1, 2021 5:46:12 AM(UTC)  | Reason: Not specified

Paul Rayman  
#29 Posted : Monday, February 1, 2021 5:46:20 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)
Hello,

Please contact support@patagames.com with the sample console app there this issue is reproduced, the document and the image you are trying to insert.
rhnatiuk  
#30 Posted : Monday, February 1, 2021 6:10:03 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
Hello,

Please contact support@patagames.com with the sample console app there this issue is reproduced, the document and the image you are trying to insert.


There is not that much new to add to this thread. We were developing it together with you about two years ago, and the final solution was seemingly working, except that we hit the problem with non-transparent objects overlaying our annotations and texts if we were inserting the new stream into the beginning of the list. And inserting those new streams into the end of the list was helping with this overlaying issue, but was broken in other documents with "unbalanced" zoom matrices... So, technically, both solutions work for some documents, but they are not general enough to work for all documents.

You said two years ago that you will think about those "unbalanced" zoom matrices, thus I am asking if anything has happened on this topic.

Paul Rayman  
#31 Posted : Wednesday, February 3, 2021 4:31:56 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)
try to wrap all original content with q-Q statements. That is, to bring the content of the page to the form
[q]
[original content 1]
[original content 2]
...
[original content n]
[Q]
[your content]

Quote:
q - Save the current graphics state on the graphics state stack.
Q - Restore the graphics state by removing the most recently saved state from the stack and making it the current state.

Edited by user Wednesday, February 3, 2021 4:34:54 PM(UTC)  | Reason: Not specified

rhnatiuk  
#32 Posted : Thursday, February 4, 2021 2:01:40 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
try to wrap all original content with q-Q statements. That is, to bring the content of the page to the form
[q]
[original content 1]
[original content 2]
...
[original content n]
[Q]
[your content]


Well, this sounds cool! But how do I do it using Pdfium?

Also, now thinking about it, these q/Q seem to work like push/pop on the stack. Wouldn't it mean that the problem of unbalanced matrices will not be solved by this wrapping?

Is there some way to determine using Pdfium whether the page has unbalanced q/Q and add Q to the end of it to bring it back to balance?
Paul Rayman  
#33 Posted : Thursday, February 4, 2021 2:25:19 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)
Quote:

Well, this sounds cool! But how do I do it using Pdfium?

Earlier in this topic, the code for transforming the page content stream into an array of streams was given. Use this code.
https://forum.patagames....Content-is-SLOW#post1667

The first item of the array should be 'q' operator (just a text string in a stream). Next item/s - orginal content, after which the operator 'Q'. And finally, the last item of the array is your data.

Quote:

Is there some way to determine using Pdfium whether the page has unbalanced q/Q and add Q to the end of it to bring it back to balance?



You can try to parse the page content - it's just a string

Paul Rayman  
#34 Posted : Thursday, February 4, 2021 5:49:53 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)
Some posts about incremental saving have been moved here:
https://forum.patagames....Content-is-SLOW#post2141
rhnatiuk  
#35 Posted : Thursday, February 4, 2021 11:50:18 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)
Paul, one more question. This idea of somehow "balancing" unbalanced q/Qs is not letting me sleep well! :)

Would it be possible to add Q to the end of the page "just in case"? Would it cause problems on balanced pages? I guess it would not, as PDF parsers seem to be quite forgiving. But I have no idea how to do it to give it a try. Would you be able to show a snippet doing just that - adding Q to the end of the page content so that it would balance potentially unbalanced q?

Thank you!
Paul Rayman  
#36 Posted : Friday, February 5, 2021 12:29:58 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)
Originally Posted by: rhnatiuk Go to Quoted Post
Paul, one more question. This idea of somehow "balancing" unbalanced q/Qs is not letting me sleep well! :)

Would it be possible to add Q to the end of the page "just in case"? Would it cause problems on balanced pages? I guess it would not, as PDF parsers seem to be quite forgiving. But I have no idea how to do it to give it a try. Would you be able to show a snippet doing just that - adding Q to the end of the page content so that it would balance potentially unbalanced q?

Thank you!


More or less like this

Code:

using (var doc = PdfDocument.Load(@"e:\11\test.pdf"))
{
    var page = doc.Pages[0];

    var pageContentStream = page.Dictionary["Contents"].As<PdfTypeStream>();
    string pageContentAsString = pageContentStream.DecodedText;

    pageContentAsString = $"\nq {pageContentAsString} Q\n";

    var newStream = PdfTypeStream.Create();
    newStream.Init(PdfCommon.DefaultAnsiEncoding.GetBytes(pageContentAsString), PdfTypeDictionary.Create());
    var list = PdfIndirectList.FromPdfDocument(doc);
    list.Add(newStream);
                
    page.Dictionary.SetIndirectAt("Contents", list, newStream);

    doc.Save(@"e:\11\test_noincremental.pdf", SaveFlags.NoIncremental);
}


Please keep in mind that page.Dictionary["Contents"] can be an array of streams.
thanks 1 user thanked Paul Rayman for this useful post.
rhnatiuk on 2/5/2021(UTC)
Quick Reply Show Quick Reply
Users browsing this topic
Guest (2)
2 Pages<12
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.