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.