Reputation: 1
I am trying to implement a C# function that automatically generates tables in Word based on attributes and properties belonging to classes in my project. The document generation occurs when a button is clicked on the form. Within the table, if I encounter paths that correspond to an image, I want to display the image instead of the path. Therefore, I have a control that, in the previously mentioned case, calls a method that creates a row with the image; otherwise, it creates a normal row. Everything works, but when I open the file, I get the error message shown in the figure.
Post: I've commented out all the code related to inserting images, and I've created a new method to insert an image from the desktop. Even when I comment out the test method (so there's no code related to image insertion left), the document opens. However, when I use the test method (InsertImageIntoWord), I get the same error when opening the Word document.
It is therefore evident that the issue is caused by the code contained within InsertImageIntoWord.
There are no errors or exceptions in the code, the images are accessible, and I'm using the latest version of DocumentFormat.OpenXml.
public void InsertImageIntoWord(MainDocumentPart mainPart, Body body, string imagePath)
{
ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Png);
using (FileStream stream = new FileStream(imagePath, FileMode.Open))
{
imagePart.FeedData(stream);
}
string imagePartId = mainPart.GetIdOfPart(imagePart);
var element =
new Drawing(
new DW.Inline(
new DW.Extent() { Cx = 500000L, Cy = 500000L }, // Dimensioni dell'immagine
new DW.EffectExtent()
{
LeftEdge = 0L,
TopEdge = 0L,
RightEdge = 0L,
BottomEdge = 0L
},
new DW.DocProperties()
{
Id = (UInt32Value)1U,
Name = "Immagine"
},
new DW.NonVisualGraphicFrameDrawingProperties(
new A.GraphicFrameLocks() { NoChangeAspect = true }),
new A.Graphic(
new A.GraphicData(
new A.Picture(
new A.NonVisualPictureProperties(
new A.NonVisualDrawingProperties()
{
Id = (UInt32Value)0U,
Name = "Picture"
},
new A.NonVisualPictureDrawingProperties()),
new A.BlipFill(
new A.Blip()
{
Embed = imagePartId,
CompressionState = A.BlipCompressionValues.Print
},
new A.Stretch(new A.FillRectangle())),
new A.ShapeProperties(
new A.Transform2D(
new A.Offset() { X = 0L, Y = 0L },
new A.Extents() { Cx = 990000L, Cy = 792000L }),
new A.PresetGeometry(new A.AdjustValueList())
{ Preset = A.ShapeTypeValues.Rectangle })))
)
{ })
);
Paragraph paragraph = new Paragraph(new Run(element));
body.Append(paragraph);
}
public void GenerateDocxDocument(TreeView treeViewFunction)
{
using (WordprocessingDocument wordDocument = WordprocessingDocument.Create($"Prova_documento.docx", WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
mainPart.Document = new Document();
Body body = new Body();
foreach (var hf in HydFunctionList)
{
...
if (hf is Cylinder cyl)
{
IterateClassProperties(cyl, mainPart, table);
}
else if (hf is MasterValve mv)
{
IterateClassProperties(mv, mainPart, table);
}
body.Append(table);
body.Append(new Paragraph(new Run(new Text(""))));
}
InsertImageIntoWord(mainPart, body, imagePath);
mainPart.Document.Append(body);
mainPart.Document.Save();
}
}
The error is: Word experienced an error trying to open the file. try these suggestions. *Check the file permission for the document or drive. *Make sure there is sufficient free memory and disk space. *Open the file with the recovery converter.
I don't understand if the problem comes from the code or, for example, from the way I access the images, or from something external to the code.
Upvotes: 0
Views: 61