簡體   English   中英

如何使用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 )。

但是,您在這里應該更加謹慎,並使用僅包含空格或注釋或類似無害的字節數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM