使用iTextSharp的写数据到PDF的伟大工程,但使用Acrobat Reader询问:“你要保存更改”关闭文件时 [英] Using iTextSharp to write data to PDF works great, but Acrobat Reader asks 'Do you want to save changes' when closing file

查看:178
本文介绍了使用iTextSharp的写数据到PDF的伟大工程,但使用Acrobat Reader询问:“你要保存更改”关闭文件时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用iTextSharp的5.3.2.0将信息添加到一个包含W-2表格现有的PDF文件。一切都可以正常使用,当写入浏览器的响应流的PDF文件看起来不错;但是,当用户完成在看PDF,他问:你要在关闭前将更改保存到W2.pdf'?每次他认为从网页文档。

I'm using iTextSharp 5.3.2.0 to add information to an existing PDF file that contains a W-2 form. Everything is working perfectly and the PDF file looks great when written into the browser's response stream; however, when the user is done looking at the PDF, he is asked "Do you want to save changes to 'W2.pdf' before closing?" every time he views the document from the web page.

在努力缩小这个问题的时候,我实际上已经剥夺了我所有的修改,但问题仍然存在。这里是我的代码的简单的版本,我的数据写入调用注释掉:

In trying to narrow the problem down, I've actually stripped out all of my modifications but the problem continues. Here's the simple version of my code, with my data-writing call commented out:

PdfReader pdfReader = new PdfReader(dataSource.ReportTemplate);

using(MemoryStream outputStream = new MemoryStream())
using (PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream))
{
   //dataSource.DrawDataFields(pdfStamper);
   pdfStamper.FormFlattening = true;
   return outputStream;
}

在这种情况下,空PDF被写入到浏览器和外观不错,但我仍然可以问:你想保存当我关闭Acrobat窗口。

In this case, the "empty" PDF is written to the browser and looks good, but I still get asked, "Do you want to save" when I close the Acrobat window.

在这一点上,我想,有什么不对的源PDF文件。然而,当我发回的PDF文件的原始字节到浏览器,我不是问你想保存使用下面的代码时的问题。

At this point I was thinking that there was something wrong with the source PDF file. However, when I send back the PDF file's raw bytes to the browser, I am NOT asked the "Do you want to save" question when using the code below.

byte[] bytes = File.ReadAllBytes(dataSource.ReportTemplate);

using (MemoryStream outputStream = new MemoryStream())
{
    outputStream.Write(bytes, 0, bytes.Length);
    return outputStream;
}



我的结论是,iTextSharp的是做一些坏在PDF格式打开它,并写入字节流,但我是新来的iTextSharp的,并可以很容易地失去了一些东西的过程。

My conclusion is that iTextSharp is doing something "bad" to the PDF in the process of opening it and writing the bytes to the stream, but I'm new to iTextSharp and could easily be missing something.

FWIW,这是Acobat读者10.1.4,我们正在谈论

FWIW, this is Acobat Reader 10.1.4 that we're talking about.

编辑:原用作模板的PDF大约是80K的尺寸。如果我看到一个已经通过我的浏览器流下来的临时文件,以书面iTextSharp的PDF文件大约为150K。然而,当我回答是的保存更改的问题通过Acrobat Reader软件要求,生成的文件大约是80K一次。 。iTextSharp的肯定是做一些意想不到的这个文件

The original PDF used as a template is approximately 80K in size. If I look at the temporary file that's been streamed down through my browser, the PDF file written by iTextSharp is approximately 150K. However, when I answer "Yes" to the "Save Changes" question asked by Acrobat Reader, the resulting file is approximately 80K again. iTextSharp is definitely doing something unexpected to this file.

推荐答案

非工作:

public byte[] MergeDataByDrawing(int copies)
{
    PdfReader pdfReader = new PdfReader(reportTemplate);

    using (MemoryStream outputStream = new MemoryStream())
    using (PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream))
    {
        pdfStamper.FormFlattening = true;
        return outputStream.GetBuffer();
    }
} 



工作

Working:

public byte[] MergeDataByDrawing(int copies)
{
    PdfReader pdfReader = new PdfReader(reportTemplate);

    using (MemoryStream outputStream = new MemoryStream())
    using (PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream))
    {
        pdfStamper.FormFlattening = true;
        return outputStream.ToArray();
    }
}



好像所述的GetBuffer方法是有问题的。我不明白为什么,但我要的结果!

Seems the GetBuffer method is a problem. I don't understand why, but I'll take the result!

道具MKL给我一个想法并的弗雷德里克在正确的时间正确的榜样。

这篇关于使用iTextSharp的写数据到PDF的伟大工程,但使用Acrobat Reader询问:“你要保存更改”关闭文件时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆