2009-09-01 6 views
3

J'ai essayé de générer un document XPS multipage à partir d'une application Web et d'essayer de le diffuser en un clic.Génération de document XPS à partir d'une application Web

public class Class1 {

protected void btnGenerateLetter_OnClick(object sender, EventArgs e) 
{ 
    try 
    { 
     string sid = Request.Form["id"]; 
     byte[] bytes = FlowDocumentToXPS(GenerateLetter(), 640, 800); 
     Response.Clear(); 
     Response.ContentType = "application/vnd.ms-xpsdocument"; 
     Response.AddHeader("Content-Disposition", "attachment; filename=document.xps"); 
     Response.OutputStream.Write(bytes, 0, bytes.Length); 
     Response.Flush(); 
     Response.Close(); 
    } 
    catch (Exception ex) 
    { 
    } 

} 

private FlowDocument GenerateLetter() 
{ 
    FlowDocument flowDocument = new FlowDocument(); 

    string Header = "Test Header Message"; 
    string Body = "Content goes here"; 
    string Footer = "Footer Text"; 

    for (int i = 0; i < 3; i++) 
    { 
     Paragraph header = new Paragraph(); 
     header.Margin = new System.Windows.Thickness(250, 100, 250, 10); 
     header.BreakPageBefore = true; 

     header.Inlines.Add(new Run(Header)); 
     header.Inlines.Add(new LineBreak()); 
     header.Inlines.Add(new LineBreak()); 
     header.Inlines.Add(new LineBreak()); 

     Paragraph body = new Paragraph(); 
     body.Inlines.Add(new Run(Body)); 
     body.Inlines.Add(new LineBreak()); 
     body.Inlines.Add(new LineBreak()); 

     Paragraph footer = new Paragraph(); 
     footer.Inlines.Add(new Run(Footer)); 

     flowDocument.Blocks.Add(header); 
     flowDocument.Blocks.Add(body); 
     flowDocument.Blocks.Add(footer); 
    } 
    return flowDocument; 
} 

public static byte[] FlowDocumentToXPS(FlowDocument flowDocument, int width, int height) 
{ 
    MemoryStream stream = new MemoryStream(); 
    // create a package 
    using (Package package = Package.Open(stream, FileMode.CreateNew)) 
    { 
     // create an empty XPS document 
     using (XpsDocument xpsDoc = new XpsDocument(package, CompressionOption.NotCompressed)) 
     { 
      // create a serialization manager 
      XpsSerializationManager rsm = new XpsSerializationManager(new XpsPackagingPolicy(xpsDoc), false); 
      // retrieve document paginator 
      DocumentPaginator paginator = ((IDocumentPaginatorSource)flowDocument).DocumentPaginator; 
      // set page size 
      paginator.PageSize = new System.Windows.Size(width, height); 
      // save as XPS 
      rsm.SaveAsXaml(paginator); 
      rsm.Commit(); 
     } 
     return stream.ToArray(); 
    } 
} 

}

Cette wroks bien sur le développement environment.But obtenir cette erreur lorsqu'il est déployé sur une autre machine. (IIS6).

URI démarrage: C: \ Documents and Settings \ 050583b.syn \ Desktop \ document.xps d'identité d'application:

System.IO.FileFormatException: fichier contient des données corrompues. à MS.Internal.IO.Zip.ZipIOEndOfCentralDirectoryBlock.FindPosition (courant archiveStream) à MS.Internal.IO.Zip.ZipIOEndOfCentralDirectoryBlock.SeekableLoad (ZipIOBlockManager GestionnaireBlocs) à MS.Internal.IO.Zip.ZipIOBlockManager.LoadEndOfCentralDirectoryBlock() à MS.Internal.IO.Zip.ZipArchive..ctor (Flux archiveStream, mode FileMode, accès FileAccess, streaming booléen, Boolean ownStream) sur MS.Internal.IO.Zip.ZipArchive.OpenOnStream (Flux de flux, mode FileMode, FileAccess accès, streaming booléen) à System.IO.Packaging.ZipPackage..ctor (Streams, mode FileMode, accès FileAccess, streaming booléen) à System.IO.Packaging.Package.Open (Stream stream, FileMode packageMode, FileAccess packageAccess , Boolean streaming) à System.IO.Pa ckaging.Package.Open (flux de flux) à MS.Internal.Documents.Application.TransactionalPackage..ctor (Stream original) à MS.Internal.Documents.Application.PackageController.MS.Internal.Documents.Application.IDocumentController.Open (Document de document) à MS.Internal.Documents.Application.DocumentManager.DispatchOpen (contrôleur IDocumentController, document Document) à MS.Internal.Documents.Application.DocumentManager. <> c__DisplayClass6.b__5 (contrôleur IDocumentController, sous réserve de document) à MS.Internal.Documents.Application.ChainOfResponsiblity 2.Dispatch(Action action, S subject) at MS.Internal.Documents.Application.DocumentManager.<>c__DisplayClass6.<OrderByLeastDependent>b__4(Document member) at MS.Internal.Documents.Application.ChainOfDependencies 1.OrderByLeastDependent (T membres, une action d'action) à MS.Internal.Documents.Application.DocumentManager.OrderByLeastDependent (action DispatchDelegate , document de document) à MS.Internal.Documents.Application.DocumentManager.Open (document de document) à MS.Internal.AppModel.ApplicationProxyInternal.InitContainer() à MS.Internal.AppModel.ApplicationProxyInternal.Run (InitData InitData)


Répondre

3

Je suppose que le problème est le avec les octets pas complètement w ritten dans la réponse. Essayez ce qui suit et j'espère que cela devrait fonctionner.

HttpContext context = HttpContext.Current; 
context.Response.Clear(); 
context.Response.ContentType = "application/vnd.ms-xpsdocument"; 
context.Response.AppendHeader("Content-Disposition", "attachment; filename=document.xps"); 
context.Response.End(); 
+0

Merci.Cette modification a résolu le problème. – Kumar

Questions connexes