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 3:41:46 PM(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 : Thursday, September 1, 2016 1:18:53 AM(UTC)
Paul Rayman

Rank: Administration

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

Thanks: 1 times
Was thanked: 84 time(s) in 83 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 10:59:16 AM(UTC)  | Reason: Not specified

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

Rank: Administration

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

Thanks: 1 times
Was thanked: 84 time(s) in 83 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 Wednesday, July 12, 2017 1:09:43 AM(UTC)  | Reason: Not specified

Paul Rayman  
#4 Posted : Friday, July 20, 2018 2:15:24 AM(UTC)
Paul Rayman

Rank: Administration

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

Thanks: 1 times
Was thanked: 84 time(s) in 83 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 12:50:43 PM(UTC)  | Reason: Not specified

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

Rank: Administration

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

Thanks: 1 times
Was thanked: 84 time(s) in 83 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 1:33:20 PM(UTC)
paleise

Rank: Advanced Member

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

Thanks: 1 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
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.