Reputation: 11871
I am using itext7 in Xamarin.Forms application. I am trying to take a PDF file from a Stream, use PdfAcroForm in itext7 to write to it, but when I do I get this error: Can not access a closed Stream. I feel I have tried everything, here is my original code:
MemoryStream outFile = new MemoryStream();
var streamFile = await App.GraphClient.Me.Drive.Items[item.Id].Content.Request().GetAsync();
PdfDocument pdf = new PdfDocument(new PdfReader(streamFile), new PdfWriter(outFile));
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, true);
IDictionary<String, PdfFormField> fields = form.GetFormFields();
PdfFormField toSet;
fields.TryGetValue("Full_Names", out toSet);
toSet.SetValue("aaa");
form.FlattenFields();
pdf.Close();
await App.GraphClient.Me.Drive.Items[newFolder.Id].ItemWithPath(item.Name).Content.Request().PutAsync<DriveItem>(outFile);
Then I tried with using and set the position of the stream to 0 and flush my stream and that still did not work.
using (MemoryStream outFile = new MemoryStream())
{
var streamFile = await App.GraphClient.Me.Drive.Items[item.Id].Content.Request().GetAsync();
PdfDocument pdf = new PdfDocument(new PdfReader(streamFile), new PdfWriter(outFile));
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, true);
IDictionary<String, PdfFormField> fields = form.GetFormFields();
PdfFormField toSet;
fields.TryGetValue("Full_Names", out toSet);
toSet.SetValue("aaa");
form.FlattenFields();
pdf.Close();
outFile.Flush();
outFile.Position = 0;
await App.GraphClient.Me.Drive.Items[newFolder.Id].ItemWithPath(item.Name).Content.Request().PutAsync<DriveItem>(outFile);
}
When I remove this line...pdf.Close(); the pdf get written, but when I open it in OneDrive its corrupted...What am I doing wrong?
Upvotes: 0
Views: 1297
Reputation: 11871
This worked:
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), new PdfWriter(outFile)))
{
pdf.SetCloseWriter(false);
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 + " " + Customer_LName.Text);
fields.TryGetValue("Full_Names0", out toSet);
toSet.SetValue(Customer_Name.Text + " " + Customer_LName.Text);
fields.TryGetValue("Full_Names1", out toSet);
toSet.SetValue(Customer_Name.Text + " " + Customer_LName.Text);
fields.TryGetValue("Address0", out toSet);
toSet.SetValue(Address1.Text);
fields.TryGetValue("City0", out toSet);
toSet.SetValue(City.Text);
fields.TryGetValue("Province0", out toSet);
toSet.SetValue(Province.Text);
fields.TryGetValue("Phone0", out toSet);
toSet.SetValue(Phone2Main.Text);
fields.TryGetValue("Email0", out toSet);
toSet.SetValue(Email.Text);
form.FlattenFields();
}
outFile.Flush();
outFile.Position = 0;
await App.GraphClient.Me.Drive.Items[newFolder.Id].ItemWithPath(item.Name).Content.Request().PutAsync<DriveItem>(outFile);
}
Upvotes: 2