简体   繁体   English

在iText7中代替PdfWriter.AddToBody使用什么?

[英]What to use in place of PdfWriter.AddToBody in iText7?

The AddToBody methods in PdfWriter and derived classes appear to be gone in iText7. PdfWriter中的AddToBody方法和派生类在iText7中似乎已消失。 I would have expected that functionality to have been migrated to the PdfDocument class, but it was not clear to me if that class or the underlying writer had anything similar. 我本来希望该功能已迁移到PdfDocument类,但是对于该类或底层编写器是否具有类似功能,我尚不清楚。 I'm trying to add a key/value pair to a dictionary where the value is an indirect reference to a PdfStream . 我正在尝试将键/值对添加到字典中,其中该值是对PdfStream的间接引用。 My itext 5 code looked something like this: 我的itext 5代码看起来像这样:

dict.Put(new PdfName("IndexerReportNames"), writer.AddToBody(new PdfStream(GetReportNames(reports))).IndirectReference);

The question is, how do I port this, and similar code to iText7? 问题是,如何移植此代码以及与iText7类似的代码?

To add a key/value pair to a dictionary where the value is an indirect reference to a PdfStream , you do not need to actually already write the object to the result stream, you merely need to make it indirect in a given PdfDocument . 要将键/值对添加到其中值是对PdfStream的间接引用的PdfStream ,您实际上不需要已经将对象写入结果流,只需在给定的PdfDocument使其间接。

In iText 7 you can do it like this for a given PdfDocument pdfDocument , a given target PdfDictionary pdfDictionary , and a given PdfStream pdfStream : 在iText 7中,您可以针对给定的PdfDocument pdfDocument ,给定的目标PdfDictionary pdfDictionary和给定的PdfStream pdfStream

pdfStream.MakeIndirect(pdfDocument);
pdfDictionary.Put(new PdfName("IndexerReportNames"), pdfStream);

If you also want it to be written to the result stream early, you can immediately call Flush : 如果您还希望将其尽早写入结果流,则可以立即调用Flush

pdfStream.Flush();

You can also write it more compactly: 您还可以更紧凑地编写它:

pdfDictionary.Put(new PdfName("IndexerReportNames"), new PdfStream(...).MakeIndirect(pdfDocument));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM