是否可以使用 .NET DeflateStream 来创建 pdf? [英] Is it possible to use the .NET DeflateStream for pdf creation?

查看:25
本文介绍了是否可以使用 .NET DeflateStream 来创建 pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 C# 代码创建 pdf 文件的能力.我一直在查看 PDF 规范,并且已经能够创建一个有效的 PDF 文件,方法是获取数据字符串并使用 UTF8 编码将它们编码为字节数组.

I'm playing around with the ability to create pdf files through C# code. I have been looking at the PDF specifications and have been able to create a working PDF file, done by taking strings of data and encoding them into byte arrays using the UTF8 Encoding.

我遇到的问题是当我尝试使用 DeflateStream 在 pdf 流对象上.它似乎不起作用:

The problem I run into is when I try to use the DeflateStream on the pdf stream objects. It just doesn't seem to work:

这是有问题的 pdf 对象的文本版本( 在每行的末尾,只是在此处不可见):

Here is the text version of the pdf object that is in question ( is at the end of each line, just not visible here):

5 0 obj
<</Length 45>>
stream
BT 70 50 TD /F1 12 Tf (Hello, world!) Tj ET
endstream
endobj

当我尝试使用 DeflateStream 类来压缩行 BT 70 50 TD/F1 12 Tf (Hello, world!) Tj ET 时,pdf 似乎不行.我注意到很多其他库,例如 iTextSharp 使用他们自己的 Deflate 压缩实现.

When I attempt to use the DeflateStream class to compress the line BT 70 50 TD /F1 12 Tf (Hello, world!) Tj ET, the pdf seems to not work. I noticed that a lot of other libraries such as iTextSharp use their own implementation of the Deflate compression.

是否有任何原因导致 Microsoft 的 DeflateStream 类实现不起作用?是我使用不当还是实施不当之类的?

Is there any reason why Microsoft's implementation of the DeflateStream class isn't working? Am I using it incorrectly or is it implemented incorrectly or what?

我知道 PDF 文件是二进制文件(不是文本),但如果我不加密任何内容,则可以将其全部视为文本.这是整个 PDF 文件以供参考(以纯文本形式,也 位于每行的末尾,只是在此处不可见):

I know that PDF files are binary (not text), but if I'm not encrypting anything then it is possible to view it all as text. Here is the entire PDF file for reference (in plaintext, also is at the end of each line, just not visible here):

%PDF-1.7
1 0 obj
<</Type /Catalog /Pages 2 0 R>>
endobj
2 0 obj
<</Type /Pages /MediaBox [ 0 0 200 200 ] /Count 1 /Kids [ 3 0 R ]>>
endobj
3 0 obj
<</Type /Page /Parent 2 0 R /Resources <</Font <</F1 4 0 R>>>> /Contents 5 0 R>>
endobj
4 0 obj
<</Type /Font /Subtype /Type1 /BaseFont /Times-Roman>>
endobj
5 0 obj
<</Length 45>>
stream
BT 70 50 TD /F1 12 Tf (Hello, world!) Tj ET
endstream
endobj
xref
0 6
0000000000 65535 f
0000000017 00000 n
0000000067 00000 n
0000000153 00000 n
0000000252 00000 n
0000000325 00000 n
trailer
<</Size 6/Root 1 0 R>>
startxref
422
%%EOF

推荐答案

是否有任何原因导致 Microsoft 的 DeflateStream 类实现不起作用?是我使用不当还是实施不当之类的?

Is there any reason why Microsoft's implementation of the DeflateStream class isn't working? Am I using it incorrectly or is it implemented incorrectly or what?

DeflateStream 实际上是在实施 RFC 1951 (DEFLATE),其中使用与 RFC 1950 兼容的压缩方法压缩 PDF.这是详细的解决方法,在这个相关的 Microsoft Connect 错误报告.

DeflateStream is actually implementing RFC 1951 (DEFLATE), where PDF is compressed using a compression method compatible with RFC 1950. This is detailed, with a workaround, in this related Microsoft Connect bug report.

一个简单的解决方法是使用第三方压缩库,例如 DotNetZip,它将支持正确的格式.话虽如此,Connect 报告表明,在大多数情况下,跳过前两个字节可能会使其起作用.

A simple workaround would be to use a third party compression library, such as DotNetZip, which will support the proper format. That being said, the Connect report suggests that skipping the first two bytes may cause this to work in most cases.

这篇关于是否可以使用 .NET DeflateStream 来创建 pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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