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

Notification

Icon
Error

Post a reply
From:
Message:

Maximum number of characters in each post is: 32767
Bold Italic Underline   Highlight Quote Choose Language for Syntax Highlighting Insert Image Insert an existing Attachment or upload a new File... Create Link   Unordered List Ordered List   Left Justify Center Justify Right Justify   Outdent Indent   More BBCode Tags
Font Color Font Size
Security Image:
Enter The Letters From The Security Image:
  Preview Post Cancel

Last 10 Posts (In reverse order)
Paul Rayman Posted: Friday, February 5, 2021 12:29:58 AM(UTC)
 
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.
rhnatiuk Posted: Thursday, February 4, 2021 11:50:18 PM(UTC)
 
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 Posted: Thursday, February 4, 2021 5:49:53 PM(UTC)
 
Some posts about incremental saving have been moved here:
https://forum.patagames....Content-is-SLOW#post2141
Paul Rayman Posted: Thursday, February 4, 2021 2:25:19 AM(UTC)
 
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

rhnatiuk Posted: Thursday, February 4, 2021 2:01:40 AM(UTC)
 
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 Posted: Wednesday, February 3, 2021 4:31:56 PM(UTC)
 
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.
rhnatiuk Posted: Monday, February 1, 2021 6:10:03 AM(UTC)
 
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 Posted: Monday, February 1, 2021 5:46:20 AM(UTC)
 
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 Posted: Monday, February 1, 2021 5:44:45 AM(UTC)
 
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!
Paul Rayman Posted: Wednesday, January 27, 2021 8:20:04 PM(UTC)
 
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.