简体   繁体   中英

What to use in place of PdfWriter.AddToBody in iText7?

The AddToBody methods in PdfWriter and derived classes appear to be gone in 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. I'm trying to add a key/value pair to a dictionary where the value is an indirect reference to a PdfStream . My itext 5 code looked something like this:

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?

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 .

In iText 7 you can do it like this for a given PdfDocument pdfDocument , a given target PdfDictionary pdfDictionary , and a given 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 :

pdfStream.Flush();

You can also write it more compactly:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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