如何使用 itextsharp 将特定字节数添加到 pdf 文件
How to add specific number of bytes to pdf file using itextsharp
我正在使用 iTextsharp 创建一个 pdf 文件。 pdf 文件的最小大小应为 1024 字节。有什么方法可以将特定的块数据添加到文件中。我尝试添加 1024 个空白字符。但它不起作用。
您可以添加具有您选择的流内容大小的 PDF 流对象:
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, output);
...
byte[] bytes = new byte[1024];
for (int i = 0; i < bytes.Length; i++)
bytes[i] = (byte)(i & 0xff);
PdfStream pdfStream = new PdfStream(bytes);
writer.AddToBody(pdfStream, false);
这会将包含给定字节的真正 PDF 流对象添加到 PDF。未应用压缩。
添加的字节数不仅仅是数组长度,PDF 流本身及其在交叉引用中的条目有一些开销。
或者您可以直接将字节直接添加到底层流中:
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, output);
...
byte[] bytes = new byte[1024];
for (int i = 0; i < bytes.Length; i++)
bytes[i] = (byte)(0x0a);
writer.Os.Write(bytes, 0, bytes.Length);
增加的字节数(几乎)正好是数组长度;仅仅是交叉引用的偏移量现在可能需要一个额外的数字(例如,不添加四位数字 9000
,添加五位数字 10024
)。
不过,您在这里应该更加谨慎,并使用仅包含空格或注释或类似无害的内容的字节数组。
我正在使用 iTextsharp 创建一个 pdf 文件。 pdf 文件的最小大小应为 1024 字节。有什么方法可以将特定的块数据添加到文件中。我尝试添加 1024 个空白字符。但它不起作用。
您可以添加具有您选择的流内容大小的 PDF 流对象:
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, output);
...
byte[] bytes = new byte[1024];
for (int i = 0; i < bytes.Length; i++)
bytes[i] = (byte)(i & 0xff);
PdfStream pdfStream = new PdfStream(bytes);
writer.AddToBody(pdfStream, false);
这会将包含给定字节的真正 PDF 流对象添加到 PDF。未应用压缩。
添加的字节数不仅仅是数组长度,PDF 流本身及其在交叉引用中的条目有一些开销。
或者您可以直接将字节直接添加到底层流中:
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, output);
...
byte[] bytes = new byte[1024];
for (int i = 0; i < bytes.Length; i++)
bytes[i] = (byte)(0x0a);
writer.Os.Write(bytes, 0, bytes.Length);
增加的字节数(几乎)正好是数组长度;仅仅是交叉引用的偏移量现在可能需要一个额外的数字(例如,不添加四位数字 9000
,添加五位数字 10024
)。
不过,您在这里应该更加谨慎,并使用仅包含空格或注释或类似无害的内容的字节数组。