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

Notification

Icon
Error

Options
Go to last post Go to first unread
Paul Rayman  
#1 Posted : Sunday, May 5, 2019 5:18:30 AM(UTC)
Paul Rayman

Rank: Administration

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

Thanks: 6 times
Was thanked: 124 time(s) in 121 post(s)
I tried your ‘setting internal property FormFill to null then restoring’ method as described at: http://forum.patagames.com/posts/m509-Open-document-Read-Only#post509

Your examples using ‘RenderFlags.FPDF_ANNOT’ are the exact opposite of what I expected. I thought this flag should be set if annotations are to be rendered (and I always want them to be rendered, just not interactive), in which case why is the flag set when readOnly is true (ie. disable form fields) and cleared when readOnly is false (ie. enable form fields)? I would have thought it should do the opposite.

I would be very grateful if you could clarify the use and consequences of specifying ‘null’ or ‘new PdfForms()’ in a call to ‘PdfDocument.Load’, and the meaning of the ‘RenderFlags.FPDF_ANNOT’ flag.

Sorry to be a nuisance with these questions, but, as I say, at the moment I’m thoroughly confused!
Paul Rayman  
#2 Posted : Sunday, May 5, 2019 5:26:59 AM(UTC)
Paul Rayman

Rank: Administration

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

Thanks: 6 times
Was thanked: 124 time(s) in 121 post(s)
I will try to explain my vision of the situation.

Please look at the screenshot of the PDF document I created for this explanation.
This document contains
  • "usual text" that is in the content stream of the page,
  • free text annotation,
  • and text field with pre-filled text

1.png (54kb) downloaded 0 time(s).

If I render this document with the following code, I will get an image with "usual text" only.
Code:
using (var doc = PdfDocument.Load(@"d:\22\test.pdf")) 
{
    var page = doc.Pages[0];
    int w = (int)page.Width;
    int h = (int)page.Height;
    using (var bmp = new PdfBitmap(w, h, true))
    {
        page.Render(bmp, 0, 0, w, h, PageRotate.Normal, RenderFlags.FPDF_LCD_TEXT);
        bmp.Image.Save(@"d:\22\simple_render.png", ImageFormat.Png);
    }
}

output:

2.png (28kb) downloaded 0 time(s).

Please note, I have loaded a document without PdfForms, but this does not matter for Render() method.

Pdfium is also have an other render method - RenderForms(). This method designed for render annotations with user interaction (fields are also annotations of which type is widget).
These annotations are rendered considering user interaction - text carriage for example, or field highlights and text selection.
So if I render the document with following code, I will get another image.
code:
Code:
using (var doc = PdfDocument.Load(@"d:\22\test.pdf", new PdfForms())) 
{
    var page = doc.Pages[0];
    int w = (int)page.Width;
    int h = (int)page.Height;
    using (var bmp = new PdfBitmap(w, h, true))
    {
        bmp.FillRect(0, 0, w, h, Color.White);
        page.RenderForms(bmp, 0, 0, w, h, PageRotate.Normal, RenderFlags.FPDF_LCD_TEXT);
        bmp.Image.Save(@"d:\22\annots_render.png", ImageFormat.Png);
    }
}

and output:
3.png (30kb) downloaded 0 time(s).

As you can see now the image does not have "usual text", but have annotations (including text field).
Also the text field is highlighted by default color. Keep in mind this fact for future explanation please (when we will discuss about FPDF_ANNOT flag).

Please note this time I loaded the document with PdfForms.
RenderForms() does not work if PdfForms is not specified (interactive forms subsystem has not been initialized during loading of the document).

Thus if we want display annotations with user interaction support we must use RenderForm method. And we must pass PdfForms object to loading procedure to initialize interactive forms subsystem.
And vise versus, if we do not need user interaction we must not use RenderForm. And we must not initialize interactive forms subsystem (must not pass PdfForms on loading), otherwise the engine will send user interaction events.


Ok. What about PdfViewer?
PdfViewer uses both Render() and RenderForms(). Viewer generate first image, thеn generate second image, next combine these two images to getting the resulting image that finally displayed to the user.
And second image is rendered many times to provide interactivity (such as carriage blinking in the text field that have input focus)

As you can see the codes above does not use FPDF_ANNOT flag in both cases (and PdfViewer does not use it too). You probably already guessed what this flag is may be used for.

Please look at this code. Now I using FPDF_ANNOT flag for rendering
Code:
using (var doc = PdfDocument.Load(@"d:\22\test.pdf")) 
{
    var page = doc.Pages[0];
    int w = (int)page.Width;
    int h = (int)page.Height;
    using (var bmp = new PdfBitmap(w, h, true))
    {
        bmp.FillRect(0, 0, w, h, Color.White);
        page.Render(bmp, 0, 0, w, h, PageRotate.Normal, RenderFlags.FPDF_LCD_TEXT | RenderFlags.FPDF_ANNOT);
        bmp.Image.Save(@"d:\22\simple_render2.png", ImageFormat.Png);
    }
}

Output:
4.png (30kb) downloaded 0 time(s).

The code produce the image that contains all elements. But you can see that the text field is not highlighted like on previous image. This is because all annotations are now displayed without they interactivities.


Because of the foregoing, I believe that the most correct way to achieve read-only mode is to load a document without initializing PdfForms and use the FPDF_ANNOT flag on displaying.
So I can suggest you load document with PdfForms, check if it should be read only and than reload it without PdfForms.

This is why we still haven't implement ReadOnly property in PDFViewer. Because documents can be loaded outside of viewer. This behavior is currently controlled by the developers who use the viewer, but not by viewer itself. If only the Document property is used, not its own LoadDocument method.

I hope the explanations were clear. I apologize if somewhere it is not.
Please ask any questions, I will try to answer them.

Edited by user Sunday, May 5, 2019 5:56:55 AM(UTC)  | Reason: Not specified

Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.