Rank: Newbie
Groups: Registered
Joined: 8/31/2016(UTC) Posts: 1 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,
|
|
|
|
Rank: Administration
Groups: Administrators
Joined: 1/5/2016(UTC) Posts: 1,104
Thanks: 7 times Was thanked: 129 time(s) in 126 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
|
|
|
|
Rank: Administration
Groups: Administrators
Joined: 1/5/2016(UTC) Posts: 1,104
Thanks: 7 times Was thanked: 129 time(s) in 126 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
|
|
|
|
Rank: Administration
Groups: Administrators
Joined: 1/5/2016(UTC) Posts: 1,104
Thanks: 7 times Was thanked: 129 time(s) in 126 post(s)
|
Originally Posted by: Xavier Verwaerde 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#post1380Edited by user Tuesday, November 6, 2018 7:50:43 AM(UTC)
| Reason: Not specified
|
|
|
|
Rank: Administration
Groups: Administrators
Joined: 1/5/2016(UTC) Posts: 1,104
Thanks: 7 times Was thanked: 129 time(s) in 126 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.
|
|
|
|
Rank: Advanced Member
Groups: Registered
Joined: 10/13/2017(UTC) Posts: 51 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
|
|
|
|
Rank: Newbie
Groups: Registered
Joined: 11/28/2022(UTC) Posts: 1 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
|
|
|
|
Rank: Administration
Groups: Administrators
Joined: 1/5/2016(UTC) Posts: 1,104
Thanks: 7 times Was thanked: 129 time(s) in 126 post(s)
|
|
|
|
|
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.
Important Information:
The Patagames Software Support Forum uses cookies. By continuing to browse this site, you are agreeing to our use of cookies.
More Details
Close