Error when creating a PDF

Last post 12-01-2010, 2:00 AM by shahzad.latif. 6 replies.
Sort Posts: Previous Next
  •  11-25-2010, 11:51 AM 270675

    Error when creating a PDF .NET

    Attachment: Present (inaccessible)
    I'm trying to create a new PDF from a starting one which has an editable form: I want to fill the form fields and to save it to a given folder.
    Here you can find the code I wrote: I wasn't able to make it work.

    Any help would be appreciated.
    Many thanks
    Regards
    Stefano



    string ContainerFolder = @"C:\temp";
    string sourceFile = "test.pdf";
    string targetFile = string.Format("{0}_{1}{2}_antrag.pdf", DateTime.Now.ToString("yyyyMMddhhmmss"), Lead.SecondName, Lead.FirstName);

    //create input and output file streams
    FileStream sourceStream = new FileStream(ContainerFolder + "sources\\" + sourceFile, FileMode.Open, FileAccess.ReadWrite);
    FileStream targetStream = new FileStream(ContainerFolder + targetFile, FileMode.Create, FileAccess.Write);

    byte[] data = new byte[sourceStream.Length];
    sourceStream.Read(data, 0, data.Length);

    //create input and output memory streams
    MemoryStream inputms = new MemoryStream(data, 0, data.Length);
    //MemoryStream outputms = new MemoryStream();
    inputms.Seek(0, SeekOrigin.Begin);

    using (var licenseStream = System.IO.File.Open(ContainerFolder + "sources\\" + "Aspose.Pdf.Kit.lic", System.IO.FileMode.Open))
    {
    Aspose.Pdf.Kit.License license = new Aspose.Pdf.Kit.License();
    license.SetLicense(licenseStream);
    }

    Aspose.Pdf.Kit.Form form = new Aspose.Pdf.Kit.Form(inputms, targetStream);

    Here I get the following exception:

    Read pdf error:The type initializer for 'Aspose.Pdf.Kit.PdfFileInfo' threw an exception.

    [IOException: Read pdf error:The type initializer for 'Aspose.Pdf.Kit.PdfFileInfo' threw an exception.]
       xeb116a323308e2f7.x7759a935a2782a02.readPdf() +316
       Aspose.Pdf.Kit.Form.xedff4d4fd296f454() +141

    I also tried the following code
    Aspose.Pdf.Kit.Form form = new Aspose.Pdf.Kit.Form(ContainerFolder + sourceFile, ContainerFolder + "sources\\" + targetFile);
    but I get a SecurityException (FileIOException).
    Googling, I found out that I need to set the FullTrust level in the web.config. I've done it, but nothing changed.

    I'd like to focus on the fact that I was able to read the license, but I wasn't able to create the Form object.
    We are using WS2008 with IIS7, .Net3.5

    Thanks

    Filed under: Aspose.pdf.Kit
     
  •  11-26-2010, 1:14 AM 270716 in reply to 270675

    Re: Error when creating a PDF

    Hi Stefano,

    Thank you very much for considering Aspose.

    First of all, please make sure that you're using the latest version of the component at your end.
    Secondly, please select the DLL for the .NET Framework 3.5 i.e. from net3.5 folder inside the bin folder.
    Thirdly, please make sure that you have appropriate permissions assigned to the folder you have placed the PDF file(s) in.

    If it doesn't resolve your issue then please share whether this issue occurs when running the application using Visual Studio or when the application is deployed on the IIS. Also, please share a sample application file you're using to reproduce the issue, so we could reproduce the same issue at our end as well.

    We're sorry for the inconvenience and looking forward to help you out.
    Regards,

    Shahzad Latif - [Follow me on Twitter!]
    Support Developer/Developer Evangelist
    Aspose Sialkot Team
    Aspose - Your File Format Experts

    Keep in touch! We're on Twitter and Facebook
     
  •  11-26-2010, 2:46 AM 270730 in reply to 270716

    Re: Error when creating a PDF

    I'm running my website under IIS7; the application pool is running with administrator identity.
    The code I provided you is the code for the sample application. I just have an aspx page that invokes that method during the Page_Load (or CreateChildControls).
    Each time I invoke the page, the target file is created by the FileStream targetStream: this means that the account has read/write rights on the folders.
    Attached, you can find also the test.pdf that you can use to create  the sample application.

    Many thanks for your support
    Regards
    Stefano


     
  •  11-27-2010, 1:04 AM 270792 in reply to 270730

    Re: Error when creating a PDF

    Hi Stefano,

    Thank you very much for sharing further details. We'll try to investigate and reproduce the issue at our end and update you accordingly.

    We're sorry for the inconvenience.
    Regards,

    Shahzad Latif - [Follow me on Twitter!]
    Support Developer/Developer Evangelist
    Aspose Sialkot Team
    Aspose - Your File Format Experts

    Keep in touch! We're on Twitter and Facebook
     
  •  11-30-2010, 5:40 AM 271162 in reply to 270792

    Re: Error when creating a PDF

    Hi Stefano,

    I have again tested this problem at my end on WS2008 with .NET Framework 3.5 and IIS7, but couldn't reproduce the issue. I used the Windows Authentication in web.config and IIS7. The code snippet was executed successfully with the attached sample file.

    Kindly share a sample project which you are using at your end, so we could test the issue using that sample. I would like to share with you that it is very important for our team to reproduce your particular issue in order to understand and then resolve it. Also, please share what other settings you configured on the IIS.

    We're sorry for the inconvenience and appreciate your cooperation.
    Regards,

    Shahzad Latif - [Follow me on Twitter!]
    Support Developer/Developer Evangelist
    Aspose Sialkot Team
    Aspose - Your File Format Experts

    Keep in touch! We're on Twitter and Facebook
     
  •  11-30-2010, 6:30 AM 271172 in reply to 271162

    Re: Error when creating a PDF

    I tried to use the same code on a Windows7 (IIS7 and .Net3.5), this time running with administrator account: it worked.
    On my pre-production environment I'm getting now the following exception:

    Security Exception

    Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

    Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

    Source Error:

    [No relevant source lines]

    Source File: c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\efc0fb6c\250713f8\App_Web_oisooasm.1.cs    Line: 0

    Stack Trace:

    [SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
       System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) +0
       System.Security.CodeAccessPermission.Demand() +54
       System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) +2103
       System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) +138
       System.IO.FileStream..ctor(String path, FileMode mode) +91
       Aspose.Pdf.Kit.Form..ctor(String srcFileName, String destFileName) +89
       B2C.UI.Web.Content.Lead.Utils.GenerateLetterPDF(LeadEntity Lead, String ContainerFolder) in C:\Workspaces\E-Commerce\Main\FE\Source\B2C.UI.Web\Content\Lead\Utils.cs:213
       B2C.UI.Web.Pages.Lead.LeadTester.CreateChildControls() in C:\Workspaces\E-Commerce\Main\FE\Source\B2C.UI.Web\Pages\Lead\LeadTester.aspx.cs:39
       System.Web.UI.Control.EnsureChildControls() +146
       System.Web.UI.Control.PreRenderRecursiveInternal() +61
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +11045655
       System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +11045194
       System.Web.UI.Page.ProcessRequest() +91
       System.Web.UI.Page.ProcessRequest(HttpContext context) +240
       ASP.pages_lead_leadtester_aspx.ProcessRequest(HttpContext context) in c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\efc0fb6c\250713f8\App_Web_oisooasm.1.cs:0
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +599
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +171
    

    The piece of code  that is throwing the exception is the following:
    Aspose.Pdf.Kit.Form form = new Aspose.Pdf.Kit.Form(ContainerFolder + "sources\\" + sourceFile, ContainerFolder + targetFile);

    Setting the trust level to full didn't work.
    The application pool user is not an administrator.
    I'm sorry to inform you that retrieving the IIS configuration is not so easy for me: I'm a developer, not a system administrator, so I don't have access to the IIS itself.

    Hope this can help you; otherwise, contact me and I'll try to provide you more infos.

    Many thanks
    Regards
    Stefano
     
  •  12-01-2010, 2:00 AM 271342 in reply to 271172

    Re: Error when creating a PDF

    Hi Stefano,

    I would like to share with you that Aspose.Pdf.Kit for .NET can only work in Full Trust environment and the trust level should be granted on the web server hosting application. You should contact your system administrator to allow full trust for your applications. If you still find any issue after granting permission then please do let us know.

    We're sorry for the inconvenience and looking forward to help you out.
    Regards,

    Shahzad Latif - [Follow me on Twitter!]
    Support Developer/Developer Evangelist
    Aspose Sialkot Team
    Aspose - Your File Format Experts

    Keep in touch! We're on Twitter and Facebook
     
View as RSS news feed in XML