Free Support Forum - aspose.com

Preserve Extended Rights When Saving PDF To Response Stream .NET

I’m able to get the pdf to save and preserve the extended rights, when following the example code here,
Preserve Extended Rights when Working with Forms

'Open document            
Dim documentName As String = "GC-020.pdf"
Dim documentPath As String = String.Format("{0}\{1}", Server.MapPath("~/Data"), documentName)

Using fs As FileStream = New FileStream(documentPath, FileMode.Open, FileAccess.ReadWrite)
    'Open document
    Dim pdf As Aspose.Pdf.Document = New Aspose.Pdf.Document(fs)

    'Populate document
    Dim formFields As List(Of Aspose.Pdf.Forms.Field) = pdf.Form.Fields.Where(Function(x) x IsNot Nothing AndAlso Not x.ReadOnly).ToList()
    Dim currentFormField As Aspose.Pdf.Forms.Field = formFields.Where(Function(x) x.PartialName = "FillText63").FirstOrDefault()
    If currentFormField IsNot Nothing Then
        Dim txt As TextBoxField = DirectCast(currentFormField, TextBoxField)
        txt.Value = "TEST"
    End If

    'Save document
    Dim opts As New Aspose.Pdf.PdfSaveOptions
    opts.CloseResponse = True
    pdf.Save(Response, "output.pdf", ContentDisposition.Inline, opts)
End Using

But what I need to do is be able to save the pdf to the response output stream while preserving the extended rights.

I can send the pdf in private message just tell me what you need to help me find a solution.

Thanks!

@jmpppwrkr1

Thank you for contacting support.

As you are able to preserve extended rights by using the incremental approach which is explained in respective article; you may save the incremented PDF file from a FileStream to a MemoryStream and then pass bytes from that MemoryStream to Response object as mentioned in Sending PDF to Browser for Download.

We hope this will be helpful. In case you notice any problem then please share a narrowed down sample application with us so that we may assist you accordingly.

Also, I’m using the original pdf as a template and I don’t want to save any data to the template.
I will try the code out you mentioned to do this.
My goal is to open the pdf, fill the form fields and then send to the browser to download, while preserving the extended rights.

Using the code you mentioned doesn’t allow me to accomplish my goal mentioned previously.
From what I have found you have to save the pdf to the same stream in order to preserve the extended rights.

@jmpppwrkr1

Would you please share a narrowed down code snippet along with source PDF file so that we may investigate it in our environment and then share our findings with you.

Farhan,

After sleeping on it and revisiting my codes I came up with the following solution to accomplish my goal,

Dim documentName As String = "GC-020.pdf"
Dim documentPath As String = String.Format("{0}\{1}", Server.MapPath("~/Data"), documentName)

If File.Exists(documentPath) Then

    Try

        'Use document in memory, populate with data
        'Following this approach will preserve extended rights 
        Using ms As New MemoryStream()

            'Open document with file stream
            Using fs As New FileStream(documentPath, FileMode.Open, FileAccess.Read)

                'Copy file from file stream to memory stream
                fs.CopyTo(ms)
                fs.Flush()
                fs.Close()

            End Using

            'Create Apose.Pdf document from the memory stream
            Dim pdf As Aspose.Pdf.Document = New Aspose.Pdf.Document(ms)

            'Populate the document with data
            PopulateFormFieldsOfDocument(pdf, documentName)

            'Save document in memory back to the memory stream
            pdf.Save(ms)

            'Write memory stream to client
            Response.Clear()
            Response.ClearHeaders()
            Response.ClearContent()
            Response.Charset = "UTF-8"
            Response.AddHeader("content-length", ms.Length.ToString())
            Response.AddHeader("content-disposition", String.Format("attachment;filename={0}", documentName))
            Response.ContentType = "application/pdf"
            Response.BinaryWrite(ms.ToArray())
            Response.Flush()
            Response.End()

        End Using

    Catch ex As Exception
        'Display ex.Message
    End Try

Else

    'Display message "This file does not exist"

End If

Thanks for your help!

@jmpppwrkr1

Thank you for your kind feedback.

We are glad to know that you have been able to achieve your requirements with the help of suggested approach. Please keep using our API and in event of any further query, feel free to ask.