【问题标题】:Generating XPS document from a Web Application从 Web 应用程序生成 XPS 文档
【发布时间】:2012-05-11 19:44:24
【问题描述】:

我正在尝试从 Web 应用程序生成多页 XPS 文档,并尝试在单击按钮时流式传输该文档。

公开课 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();
    }
}

}

这在开发环境中运行良好。但在不同的机器上部署时出现此错误。(IIS6)。

启动 URI:C:\Documents and Settings\050583b.syn\Desktop\document.xps 应用程序标识:

System.IO.FileFormatException:文件包含损坏的数据。 在 MS.Internal.IO.Zip.ZipIOEndOfCentralDirectoryBlock.FindPosition(流存档流) 在 MS.Internal.IO.Zip.ZipIOEndOfCentralDirectoryBlock.SeekableLoad(ZipIOBlockManager blockManager) 在 MS.Internal.IO.Zip.ZipIOBlockManager.LoadEndOfCentralDirectoryBlock() 在 MS.Internal.IO.Zip.ZipArchive..ctor(流 archiveStream,FileMode 模式,FileAccess 访问,布尔流,布尔 ownStream) 在 MS.Internal.IO.Zip.ZipArchive.OpenOnStream(流流,FileMode 模式,FileAccess 访问,布尔流) 在 System.IO.Packaging.ZipPackage..ctor(Stream s,FileMode 模式,FileAccess 访问,布尔流) 在 System.IO.Packaging.Package.Open(流流,FileMode packageMode,FileAccess packageAccess,布尔流) 在 System.IO.Packaging.Package.Open(流流) 在 MS.Internal.Documents.Application.TransactionalPackage..ctor(流原始) 在 MS.Internal.Documents.Application.PackageController.MS.Internal.Documents.Application.IDocumentController.Open(文档文档) 在 MS.Internal.Documents.Application.DocumentManager.DispatchOpen(IDocumentController 控制器,文档文档) 在 MS.Internal.Documents.Application.DocumentManager.c__DisplayClass6.b__5(IDocumentController 控制器,文档主题) 在 MS.Internal.Documents.Application.ChainOfResponsiblity2.Dispatch(Action action, S subject) at MS.Internal.Documents.Application.DocumentManager.<>c__DisplayClass6.<OrderByLeastDependent>b__4(Document member) at MS.Internal.Documents.Application.ChainOfDependencies1.OrderByLeastDependent(T 成员,操作动作) 在 MS.Internal.Documents.Application.DocumentManager.OrderByLeastDependent(DispatchDelegate 操作,文档文档) 在 MS.Internal.Documents.Application.DocumentManager.Open(文档文档) 在 MS.Internal.AppModel.ApplicationProxyInternal.InitContainer() 在 MS.Internal.AppModel.ApplicationProxyInternal.Run(InitData initData)


【问题讨论】:

    标签: asp.net xps


    【解决方案1】:

    我想问题是字节没有完全写入响应。 尝试以下方法,希望它会起作用。

    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();
    

    【讨论】:

    • 谢谢。此更改解决了问题。
    猜你喜欢
    • 1970-01-01
    • 2016-11-26
    • 2011-06-29
    • 2012-04-01
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    • 2014-01-24
    • 1970-01-01
    相关资源
    最近更新 更多