DeflateStream进行pdf创建吗

DeflateStream进行pdf创建吗

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

问题描述

我正在尝试通过C#代码创建pdf文件的功能.我一直在研究PDF规范,并且能够通过使用UTF8编码获取数据字符串并将其编码为字节数组来创建有效的PDF文件.

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对象的文本版本( \ r \ n 在每一行的末尾,在这里不可见):

Here is the text version of the pdf object that is in question (\r\n 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文件供参考(明文, \ r \ n 也位于每行的末尾,在这里不可见):

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 \r\n 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

推荐答案

DeflateStream实际上正在实现 RFC 1951 (DEFLATE),其中将PDF压缩使用与 RFC 1950 兼容的压缩方法.在相关的 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创建吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 12:24