AddPicture creates invalid file format on .NET Core 2.0 with v 1.0.2
I am trying to use the 1.0.2 version for .NET Core for this .AddPicture() method. I constantly get a "cannot be opened because there are problems with the contents" error any time I use the AddPicture method. If I only use text then I get the document I need. The filename and path are correct and the file size increases the proper size on the DOCX when I view the download listing. But the file never works. I have searched StackOverflow for answers but only see the same question.
`
var ms = new NpoiMemoryStream();
ms.AllowClose = false;
NPOI.XWPF.UserModel.XWPFDocument doc = new NPOI.XWPF.UserModel.XWPFDocument();
var p0 = doc.CreateParagraph();
p0.Alignment = NPOI.XWPF.UserModel.ParagraphAlignment.LEFT;
NPOI.XWPF.UserModel.XWPFRun r0 = p0.CreateRun();
r0.FontFamily = "Arial";
r0.FontSize = 12;
r0.IsBold = false;
r0.SetText("This is my paragraph");
string filename = <my valid name>;
string path = _env.WebRootPath + filename;
using (Stream s = System.IO.File.OpenRead(path))
{
doc.CreateParagraph().CreateRun().AddPicture(s, (int)PictureType.PNG, "logo-large.png", 400, 300);
}
doc.Write(ms);
ms.Flush();
ms.Seek(0, SeekOrigin.Begin);
ms.AllowClose = true;
return File(ms, "application/vnd.ms-word", id + "-ProposalExample.docx");`
I have the same problem.Have you solved your problem?
No I have not yet done that. I see some workaround in the Java version of POI but nothing for the .net core version. I saw a note on "use updated DLLs" for the windows version but I am on Mac OSX.
Sorry just saw this note. Been fixing other things on that same project.
Has anyone been able to find a workaround for this in .NET Core?
I switched to use the OpenXML library straight and it worked well.
Dale Bingham
On Apr 17, 2018, at 4:49 AM, Bus Wizard [email protected] wrote:
Has anyone been able to find a workaround for this in .NET Core?
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.
Thanks Dale, I'll give it a try.
Finally I have a workaround for .netCore. The problem is in generated document.xml. Following code: var p0 = doc.Paragraphs[0]; var r0 = p0.CreateRun(); r0.AddPicture(image, 6, "logo.png", Units.ToEMU(width), Units.ToEMU(height)); generates xml node: <wp:docPr name="Drawing 0" descr="logo.png"/> Attribute id is missing. When you change this node to: <wp:docPr name="Drawing 0" id="0" descr="logo.png"/> everything should work. This happens even if you already have images in your doc and only for first record. I couldn’t find the reason and way to fix it (probably something in export part), but if you change this id to other value it will work: var p0 = doc.Paragraphs[0]; var r0 = p0.CreateRun(); r0.AddPicture(image, 6, "logo.png", Units.ToEMU(width), Units.ToEMU(height)); var docPr = ((NPOI.OpenXmlFormats.Dml.WordProcessing.CT_Drawing)r0.GetCTR().Items[0]) .inline[0].docPr; docPr.id = 1000; This is required only for first image in file. If you have more images following code should work too: var p0 = doc.Paragraphs[0]; var r0 = p0.CreateRun(); r0.AddPicture(image, 6, "logo.png", Units.ToEMU(width), Units.ToEMU(height)); var docPr = ((NPOI.OpenXmlFormats.Dml.WordProcessing.CT_Drawing)r0.GetCTR().Items[0]) .inline[0].docPr; docPr.id = 1000; var p1 = doc.Paragraphs[1]; var r1 = p1.CreateRun(); r1.AddPicture(image2, 6, "cat.png", Units.ToEMU(width2), Units.ToEMU(height2));
Finally I have a workaround for .netCore. The problem is in generated document.xml. Following code: var p0 = doc.Paragraphs[0]; var r0 = p0.CreateRun(); r0.AddPicture(image, 6, "logo.png", Units.ToEMU(width), Units.ToEMU(height)); generates xml node: <wp:docPr name="Drawing 0" descr="logo.png"/> Attribute id is missing. When you change this node to: <wp:docPr name="Drawing 0" id="0" descr="logo.png"/> everything should work. This happens even if you already have images in your doc and only for first record. I couldn’t find the reason and way to fix it (probably something in export part), but if you change this id to other value it will work: var p0 = doc.Paragraphs[0]; var r0 = p0.CreateRun(); r0.AddPicture(image, 6, "logo.png", Units.ToEMU(width), Units.ToEMU(height)); var docPr = ((NPOI.OpenXmlFormats.Dml.WordProcessing.CT_Drawing)r0.GetCTR().Items[0]) .inline[0].docPr; docPr.id = 1000; This is required only for first image in file. If you have more images following code should work too: var p0 = doc.Paragraphs[0]; var r0 = p0.CreateRun(); r0.AddPicture(image, 6, "logo.png", Units.ToEMU(width), Units.ToEMU(height)); var docPr = ((NPOI.OpenXmlFormats.Dml.WordProcessing.CT_Drawing)r0.GetCTR().Items[0]) .inline[0].docPr; docPr.id = 1000; var p1 = doc.Paragraphs[1]; var r1 = p1.CreateRun(); r1.AddPicture(image2, 6, "cat.png", Units.ToEMU(width2), Units.ToEMU(height2));
works, thanku