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

Notification

Icon
Error

Options
Go to last post Go to first unread
Xavier Verwaerde  
#1 Posted : Wednesday, August 31, 2016 11:41:46 AM(UTC)
Xavier Verwaerde

Rank: Newbie

Groups: Registered
Joined: 8/31/2016(UTC)
Posts: 1
France
Location: Villeneuve d'Ascq

Is there a way to open a Pdf document read-only in the WPF Viewer?

More precisely, how to forbid fields edition in the document (flatening is not an option)

Thank you,
Paul Rayman  
#2 Posted : Wednesday, August 31, 2016 9:18:53 PM(UTC)
Paul Rayman

Rank: Administration

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

Thanks: 7 times
Was thanked: 128 time(s) in 125 post(s)
There is no the documented way, but I can suggest the following trick

Code:


private PdfForms _forms = null;
private bool _readOnly = false;

public MainWindow()
{
    InitializeComponent();
    PdfCommon.Initialize();
    pdfViewer1.DocumentLoaded += (s, e) => { _forms = pdfViewer1.Document.FormFill; };
}

bool ReadOnly
{
    get
    {
        return _readOnly;
    }
    set
    {
        if (_forms == null || _readOnly == value)
            return; //do nothing

        //Trick - Get access to internal propery - FormFill. 
        //To enable readonly mode we need to set that property to null.
        //And restore its original value (stored in _forms variable) to disable one.
        BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
        var propInfo = typeof(PdfDocument).GetProperty("FormFill", bindFlags);
        propInfo.SetValue(pdfViewer1.Document, value ? null : _forms, null);
        _readOnly = value;

        if(_readOnly)
            pdfViewer1.RenderFlags |= Patagames.Pdf.Enums.RenderFlags.FPDF_ANNOT;
        else
            pdfViewer1.RenderFlags ^= Patagames.Pdf.Enums.RenderFlags.FPDF_ANNOT;
    }
}

Edited by user Thursday, September 1, 2016 6:59:16 AM(UTC)  | Reason: Not specified

Paul Rayman  
#3 Posted : Tuesday, July 11, 2017 9:48:03 AM(UTC)
Paul Rayman

Rank: Administration

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

Thanks: 7 times
Was thanked: 128 time(s) in 125 post(s)
The proper way to achieve the same is the following.

You should disable the calling of DrawFillForms and OnPreviewKeyDown methods by creating a derived class

Code:

public class MyPdfViewer : PdfViewer
{
	private bool _readOnly = false;

	public bool ReadOnly
	{
		get
		{
			return _readOnly;
		}
		set
		{
			if(_readOnly== value)
				return; //do nothing

			_readOnly = value;
			if (_readOnly)
				RenderFlags |= Patagames.Pdf.Enums.RenderFlags.FPDF_ANNOT;
			else
				RenderFlags ^= Patagames.Pdf.Enums.RenderFlags.FPDF_ANNOT;
            ClearRenderBuffer();

        }
    }

	protected override void DrawFillForms(PdfBitmap bmp, PdfPage page, Rectangle actualRect)
	{
		if(!_readOnly)
			base.DrawFillForms(bmp, page, actualRect);
	}

    protected override void OnPreviewKeyDown(PreviewKeyDownEventArgs e)
    {
        if (!_readOnly)
            base.OnPreviewKeyDown(e);
    }
}

Edited by user Tuesday, July 11, 2017 9:09:43 PM(UTC)  | Reason: Not specified

Paul Rayman  
#4 Posted : Thursday, July 19, 2018 10:15:24 PM(UTC)
Paul Rayman

Rank: Administration

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

Thanks: 7 times
Was thanked: 128 time(s) in 125 post(s)
Originally Posted by: Xavier Verwaerde Go to Quoted Post
Is there a way to open a Pdf document read-only in the WPF Viewer?

More precisely, how to forbid fields edition in the document (flatening is not an option)

Thank you,


It seems that you can just set the ReadOnly flag for all fields by analogy as here:

https://forum.patagames.com/posts/m1380-hide-form-button#post1380

Edited by user Tuesday, November 6, 2018 7:50:43 AM(UTC)  | Reason: Not specified

Paul Rayman  
#5 Posted : Wednesday, November 7, 2018 2:08:04 AM(UTC)
Paul Rayman

Rank: Administration

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

Thanks: 7 times
Was thanked: 128 time(s) in 125 post(s)
And one more way that I thought of all of a sudden :)

You can load document into viewer like following

Code:
pdfViewer1.Document = PdfDocument.Load(@"d:\21\otpt4.pdf");


In this case viewer try to use FillForms from document, but document loaded in this way does not have FillForms.
So this document will be shown in read only mode.
paleise  
#6 Posted : Friday, February 15, 2019 8:33:20 AM(UTC)
paleise

Rank: Advanced Member

Groups: Registered
Joined: 10/13/2017(UTC)
Posts: 51
Germany
Location: Freiburg

Thanks: 3 times
Hi Paul,
thanks for your answer.
Both solution not work proper for me.
But I found a solution in the documentation.


foreach (PdfField pdfField in pdfForm.Fields)
{
pdfField.Dictionary["Ff"] = PdfTypeNumber.Create(edit ? 0 : 1);
}

Perhaps someone else is interested in.

Regards Peter
patrick.muellner  
#7 Posted : Tuesday, November 29, 2022 4:20:59 AM(UTC)
patrick.muellner

Rank: Newbie

Groups: Registered
Joined: 11/28/2022(UTC)
Posts: 1
Austria
Location: Lower Austria

Hi,

I have one question to writeable PDF document.
How set ReadOnly right?

I only want to show the document and the whole content but I don't want that user can write in the pdf.

How can I set this - with this code from below it doesn't works?

Regards
Patrick
Paul Rayman  
#8 Posted : Wednesday, November 30, 2022 3:46:15 AM(UTC)
Paul Rayman

Rank: Administration

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

Thanks: 7 times
Was thanked: 128 time(s) in 125 post(s)
Originally Posted by: patrick.muellner Go to Quoted Post
How set ReadOnly right?


Hi,

Post #5
https://forum.patagames....ument-Read-Only#post1514

Also pdfViewer.RenderFlags |= RenderFlags.FPDF_ANNOT
must be set for the annotation to be visible.
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.