首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Web应用程序生成XPS文档

从Web应用程序生成XPS文档
EN

Stack Overflow用户
提问于 2009-09-01 17:26:38
回答 1查看 4.5K关注 0票数 3

我试图从一个web应用程序中生成一个多页XPS文档,并尝试将其按一下按钮。

公共类Class1 {

代码语言:javascript
复制
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();
    }
}

}

这将很好地解决开发environment.But在另一台机器上部署时出现此错误的问题。(IIS6)。

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

System.IO.FileFormatException:文件包含损坏的数据。( MS.Internal.IO.Zip.ZipIOEndOfCentralDirectoryBlock.FindPosition(Stream archiveStream) )( MS.Internal.IO.Zip.ZipIOBlockManager.LoadEndOfCentralDirectoryBlock() at MS.Internal.IO.Zip.ZipIOBlockManager.LoadEndOfCentralDirectoryBlock() at MS.Internal.IO.Zip.ZipArchive..ctor(Stream archiveStream,FileMode模式,FileAccess access,布尔流,布尔流) at MS.Internal.IO.Zip.ZipArchive.OpenOnStream(Stream流,FileMode模式,FileAccess access,布尔流)在System.IO.Packaging.ZipPackage..ctor(Stream s,FileMode模式,FileAccess access,FileAccess access,( MS.Internal.Documents.Application.TransactionalPackage..ctor(Stream原版的System.IO.Packaging.Package.Open(流流,FileMode packageMode,FileAccess packageAccess,布尔流)在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(Document文档)在MS.Internal.AppModel.ApplicationProxyInternal.InitContainer() at MS.Internal.AppModel.ApplicationProxyInternal.Run(InitData initData)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-09-02 08:00:57

我想问题在于没有将字节完全写入响应。尝试下面的方法,希望它能起作用。

代码语言:javascript
复制
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();
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1363657

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档