Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edited Form not able to save in React native #1555

Open
2 tasks done
vipulparmarmt opened this issue Nov 27, 2023 · 0 comments
Open
2 tasks done

Edited Form not able to save in React native #1555

vipulparmarmt opened this issue Nov 27, 2023 · 0 comments

Comments

@vipulparmarmt
Copy link

vipulparmarmt commented Nov 27, 2023

What were you trying to do?

I want to save the form input field into pdf so that whenever user open the pdf file then he only able to read that not able to edit.

How did you attempt to do it?

I have read this solution on some github issue but its not working on react native to save the edited field so can some one please Help to how can ww do ?.

What actually happened?

I have tried above solution to save the edited pdf and use readonly but it actually not working.

What did you expect to happen?

The Input field edited by user will save the answer into pdf and the other user acess pdf as read only not able to edit it.

How can we reproduce the issue?

` import {
PDFArray,
PDFNumber,
PDFBool,
PDFName,
} from 'pdf-lib';

const getAcroFields = (pdfDoc: any) => {
const acroForm = getAcroForm(pdfDoc);
if (!acroForm) return [];

const fieldRefs = acroForm.lookupMaybe(PDFName.of('Fields'), PDFArray);
if (!fieldRefs) return [];

const fields = new Array(fieldRefs.size());
for (let idx = 0, len = fieldRefs.size(); idx < len; idx++) {
  fields[idx] = fieldRefs.lookup(idx);
}
return fields;

};

const lockField = (acroField: any) => {
acroField.set(PDFName.of('Ff'), PDFNumber.of(1 << 0 /* Read Only */));
};

const saveFormField = async (url) => {

const pdfDoc = await PDFDocument.load(pdfSource);
const form = pdfDoc.getForm();
const fields = form.getFields();
fields.forEach((field) => {
  const type = field.constructor.name;
  const name = field.getName();
  console.log(`${type}: ${name}`);
});
const acroForm = getAcroForm(pdfDoc);
acroForm.set(PDFName.of('NeedAppearances'), PDFBool.True);

const acroFields = getAcroFields(pdfDoc);
acroFields.forEach((field) => lockField(field));

const pdfBytes = await pdfDoc.save();

};`

Version

1.17.1

What environment are you running pdf-lib in?

React Native

Checklist

  • My report includes a Short, Self Contained, Correct (Compilable) Example.
  • I have attached all PDFs, images, and other files needed to run my SSCCE.

Additional Notes

Please Help if anyone know how can we save the edited input fileds and save that as read only pdf. @Hopding
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant