We are currently trying to convert a word document to a PDF but for security purposes we need to flatten each page (convert to an image) and then add that image to the PDF document.
We have had this working for several months for lots of customers but one customer is having issues where they get 0 bytes for the PDF and later an error when we try to use the file ‘file is currently being used by another process’.
I have tried several things to attempt to get this issue resolved but now at a loose end and do not know how to proceed.
Below is the function that generates the PDF from a word document.
Public Shared Sub SavePdf(ByRef awDoc As Aspose.Words.Document, ByVal fileName As String,
Optional ByVal flattenDocumentBool As Boolean = True)
’ If this document type is supposed to be flattened, and the global setting to do so is turned on
If flattenDocumentBool AndAlso FlattenDocument Then
Dim pdf As Generator.Pdf = New Generator.Pdf()
Dim options As New ImageSaveOptions(Aspose.Words.SaveFormat.Png)
options.PageCount = 1
options.UseHighQualityRendering = True
options.Resolution = CInt(DSSLConfigurationDB.GetGlobalSetting(“PDFImageResolution”,
“Sets the resolution of the image used when flattening documents to be used in a pdf”,
“300”))
options.UseAntiAliasing = True
’ Loop over each page in the document, converting it to an image
’ (and adding each image to it’s own PDF page)
For i As Integer = 0 To awDoc.PageCount - 1
Using ms As New MemoryStream()
options.PageIndex = i
Dim pageInfo As Rendering.PageInfo = awDoc.GetPageInfo(i)
’ Render the page into the bitmap at the desired scale
#If DEBUG Then
awDoc.Save(String.Format(fileName.Replace(".pdf", “_{0}.png”), i), options)
#End If
awDoc.Save(ms, options)
’ Add the image to it’s own page in the PDF (taking into account the original page size + orientation)
Dim section As Generator.Section = pdf.Sections.Add()
section.PageInfo.Margin.Top = 0
section.PageInfo.Margin.Bottom = 0
section.PageInfo.Margin.Left = 0
section.PageInfo.Margin.Right = 0
section.IsLandscape = pageInfo.Landscape
Dim image As New Generator.Image(section)
image.ImageInfo.ImageFileType = ImageFileType
image.ImageInfo.ImageStream = ms
section.Paragraphs.Add(image)
End Using
Next
’ Encrypt & save the PDF
pdf.Security = New Security()
pdf.Security.MasterPassword = Guid.NewGuid.ToString
pdf.Security.Is128BitsEncrypted = True
pdf.Security.IsDefaultAllAllowed = False
pdf.Security.IsPrintingAllowed = CBool(DSSLConfigurationDB.GetGlobalSetting(“PDF_PrintingAllowed”, “Whether eModule PDF are allowed to be printed”, “false”))
pdf.Security.IsDegradedPrintingAllowed = CBool(DSSLConfigurationDB.GetGlobalSetting(“PDF_DegradedPrintingAllowed”, “Whether eModule PDF are allowed to be printed but are of lower quality”, “false”))
pdf.Security.IsCopyingAllowed = CBool(DSSLConfigurationDB.GetGlobalSetting(“PDF_CopyingAllowed”, “Whether eModule PDF allows elements to be copied”, “false”))
pdf.Security.IsDocumentAssemblyingAllowed = CBool(DSSLConfigurationDB.GetGlobalSetting(“PDF_DocumentAssemblyAllowed”, “Whether eModule PDF allows inserting, rotating, or deleting pages and creating navigation elements such as bookmarks or thumbnail images.”, “false”))
'pdf.Security.IsFormFillingAllowed = CBool(DSSLConfigurationDB.GetGlobalSetting(“PDF_FormFillingAllowed”, “Whether eModule PDF allows filling in forms and signing the document is allowed”, “false”))
'pdf.Security.IsContentsModifyingAllowed = CBool(DSSLConfigurationDB.GetGlobalSetting(“PDF_ContentsModifyingAllowed”, “Whether eModule PDF allows its content to be changed”, “false”))
pdf.Save(fileName)
Else
’ Save word document as PDF into memory
Using ms As New MemoryStream()
awDoc.Save(ms, Aspose.Words.SaveFormat.Pdf)
’ Construct new PDF document object from memory stream
Dim document As New Aspose.Pdf.Document(ms)
’ Encrypt & save it
document.Encrypt(String.Empty, Guid.NewGuid.ToString, DocumentPrivilege.ForbidAll, CryptoAlgorithm.RC4x128,
False)
document.Save(fileName)
End Using
End If
End Sub