How to add a paragraph with an image to a cell in a table in iTextSharp?

I create a parser that converts Word documents to PDF files with some specialized processing along the way. I decided to make this a recursive parser so that it matches the OpenXml structure.

I am having problems with images. Given the structure of OpenXml, an image will always be a drawing element within a paragraph. If the paragraph is directly in the document, this works fine, in essence, like this (recursion dismissed for this example):

using (var document = new Document(PageSize.A4)) { PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create)); document.Open(); var paragraph = new Paragraph(); var image = Image.GetInstance(@"C:\image1.jpg"); paragraph.Add(image); document.Add(paragraph); document.Close(); } 

This code correctly inserts the image into the document. The problem arises when the image is inside the table, which is common in the documents we work with. The OpenXml structure will end like this:

DocumentBody => Table => Cell => Paragraph => Drawing

So, in terms of iTextSharp, which correspond to:

Document => Table => Cell => Paragraph => Image

Adding a paragraph with an image directly to the cell creates an empty table with zero height. If I add a fragment to the paragraph, the image will appear, but will be drastically changed (smaller). I cannot figure out what the basis for this resizing is:

 using (var document = new Document(PageSize.A4)) { PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create)); document.Open(); var paragraph = new Paragraph(); var image = Image.GetInstance(@"C:\image1.jpg"); paragraph.Add(new Chunk(image, 0, 0)); var table = new PdfPTable(1); var cell = new PdfPCell { PaddingLeft = 5, PaddingTop = 5, PaddingBottom = 5, PaddingRight = 5 }; cell.HorizontalAlignment = Element.ALIGN_CENTER; cell.AddElement(paragraph); table.AddCell(cell); document.Add(table); document.Close(); } 

If someone can help me get this image into a cell without resizing, that would be perfect.

UPDATE *

I decided that the image itself does not change proportionally - if I extract the image from the resulting pdf and save it, it will save the size of the original image.

+6
source share
4 answers

Try adding Image to the cell directly, rather than adding it to Paragraph :

 var cell = new PdfPCell { PaddingLeft = 5, PaddingTop = 5, PaddingBottom = 5, PaddingRight = 5 }; cell.HorizontalAlignment = Element.ALIGN_CENTER; cell.AddElement(paragraph); cell.AddElement(image); 

Considering your example, you can also omit the line cell.AddElement(paragraph); because your paragraph seems to be blank.

Note that using addElement(image) scales the image to fit 100% of the width of the table column (usually you do not want the image to overlap with other columns). You can use the setWidthPercentage() method on the image to change this percentage.

+3
source

In the code where you add the image to the paragraph, add the fourth parameter to Chunk

 paragraph.Add(new Chunk(image, 0, 0, true)); 

The presenter is fixed, so the image changes in accordance with the size. Installation

 changeLeading = true //fourth parameter 

image is displayed normally

+3
source

May try the following:

  iTextSharp.text.Document doc = new iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35); PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("C://PDF//.pdf ", FileMode.Create)); doc.Open(); iTextSharp.text.Paragraph paragraph = new iTextSharp.text.Paragraph(//value); doc.Add(paragraph); doc.Close(); 
0
source

I think that the image in this case is added to the background of the table. To add an image when it is added to the table, you can use the code below.

 using (var document = new Document(PageSize.A4)) { PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create)); document.Open(); var paragraph = new Paragraph(); var image = Image.GetInstance(@"C:\image1.jpg"); var table = new PdfPTable(1); var cell = new PdfPCell { PaddingLeft = 5, PaddingTop = 5, PaddingBottom = 5, PaddingRight = 5 }; cell.HorizontalAlignment = Element.ALIGN_CENTER; cell.AddElement(paragraph); table.AddCell(cell); //image.ScaleToFit(JpgBg.Width, JpgBg.Height); image.ScaleAbsolute(table.Width, table.Height); image.Alignment = iTextSharp.text.Image.UNDERLYING; document.Add(image); document.Add(table); document.Close(); } 
0
source

Source: https://habr.com/ru/post/975670/


All Articles