|
|
Originally Posted by: rhnatiuk  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.
|
|
|
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!
|
|
|
|
|
|
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#post1667The 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
|
|
|
Originally Posted by: Paul Rayman  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?
|
|
|
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.
|
|
|
Originally Posted by: Paul Rayman  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.
|
|
|
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.
|
|
|
Hi,
Paul Rayman
, Originally Posted by: Paul Rayman  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!
|
|
|
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.
|