简体   繁体   中英

Xamarin.Forms itext7 - There is no associate PdfWriter for making indirects

I am using itext7 to get the Stream of the file and fill in the form fields.

Here is my code:

using (MemoryStream outFile = new MemoryStream())
{
    var streamFile = await App.GraphClient.Me.Drive.Items[item.Id].Content.Request().GetAsync();

    using (PdfDocument pdf = new PdfDocument(new PdfReader(streamFile)))
    {

        PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, true);

        IDictionary<String, PdfFormField> fields = form.GetFormFields();

        PdfFormField toSet;
        fields.TryGetValue("Full_Names", out toSet);
        toSet.SetValue(Customer_Name.Text);

        form.FlattenFields();
        pdf.Close();

    }
}

But I get an error on this line: toSet.SetValue(Customer_Name.Text); when trying to set the value, the error is:

There is no associate PdfWriter for making indirects.

My question is what am I doing wrong and how do I fix this? I am getting the file from OneDrive using Microsoft Graph API. The IDictionary of fields is getting populated and the name of field I am trying to set the value for is accurate.

The problem with your code is that you have dedicated outFile stream for the output result (the document with flattened fields) but you are not telling iText anything about that desired output destination.

PdfDocument has several constructors and the one you are using is PdfDocument(PdfReader) and it is dedicated for reading the document, not changing it (and setting a value is considered changing the document of course). So you should use PdfDocument(PdfReader, PdfWriter) constructor to provide the source document and the target destination to iText.

You should create your PdfDocument as follows:

PdfDocument pdf = new PdfDocument(new PdfReader(streamFile), new PdfWriter(outFile))

At the end of the execution, your outFile stream will contain the bytes of the resultant document.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM