簡體   English   中英

Word Open XML:如果插入了三個以上的圖像,則文件已損壞

[英]Word Open XML: file corrupted if more than 3 images are inserted

在映像創建過程中,OPEN XML遇到了一些麻煩。

我正在用一些圖像替換文檔中的一些文本。 如果我替換1到3張圖像,則保存的文件是完美的:顯示了圖像,一切看起來都很好。

如果我替換了3張以上的圖像,則文件結果已損壞,在Microsoft Word“恢復”之后,它也很完美。

我試圖移動圖像,更改順序,更改圖像等等,但是當我查看插入的3張圖像時,文檔似乎壞了。

這是我調用的方法:

  private static void ReplaceTextWithImage(string find, string filepath, Bitmap bitmap, int incremental)
    {
        using (WordprocessingDocument doc = WordprocessingDocument.Open(filepath, true))
        {
            MainDocumentPart mainPart = doc.MainDocumentPart;
            ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);

            using (var ms = new MemoryStream())
            {
                bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                ms.Position = 0;
                imagePart.FeedData(ms);
            }

            var relID = mainPart.GetIdOfPart(imagePart);

            var element =
                 new Drawing(
                     new DW.Inline(
                         new DW.Extent() { Cx = 990000L * (long)(7.13 / 1.08), Cy = 792000L * (long)(8.51 / 0.87) },
                         new DW.EffectExtent()
                         {
                             LeftEdge = 0L,
                             TopEdge = 0L,
                             RightEdge = 0L,
                             BottomEdge = 0L
                         },
                         new DW.DocProperties()
                         {
                             Id = (UInt32Value)1U,
                             Name = "img" + incremental
                         },
                         new DW.NonVisualGraphicFrameDrawingProperties(
                             new A.GraphicFrameLocks() { NoChangeAspect = true }),
                         new A.Graphic(
                             new A.GraphicData(
                                 new PIC.Picture(
                                     new PIC.NonVisualPictureProperties(
                                         new PIC.NonVisualDrawingProperties()
                                         {
                                             Id = (UInt32Value)0U,
                                             Name = "img" + incremental + ".jpg"
                                         },
                                         new PIC.NonVisualPictureDrawingProperties()),
                                     new PIC.BlipFill(
                                         new A.Blip(
                                             new A.BlipExtensionList(
                                                 new A.BlipExtension()
                                                 { 
                                                     Uri = Guid.NewGuid().ToString()
                                                 })
                                         )
                                         {
                                             Embed = relID,
                                             CompressionState = A.BlipCompressionValues.Print
                                         },
                                         new A.Stretch(new A.FillRectangle())),
                                     new PIC.ShapeProperties(
                                         new A.Transform2D(
                                             new A.Offset() { X = 0L, Y = 0L },
                                             new A.Extents() { Cx = 990000L * (long)(7.13 / 1.08), Cy = 792000L * (long)(8.51 / 0.87) }),
                                         new A.PresetGeometry( 
                                             new A.AdjustValueList()
                                         ) 
                                         { Preset = A.ShapeTypeValues.Rectangle }))
                             )
                             { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
                     )
                     {
                         DistanceFromTop = (UInt32Value)0U,
                         DistanceFromBottom = (UInt32Value)0U,
                         DistanceFromLeft = (UInt32Value)0U,
                         DistanceFromRight = (UInt32Value)0U,
                         EditId = "50D07946"
                     });

            var paragraphs = doc.MainDocumentPart.Document.Body.ChildElements.First(x => x.OuterXml.Contains(find));
            doc.MainDocumentPart.Document.Body.InsertAfter(new Paragraph(new Run(element)), paragraphs);
            doc.MainDocumentPart.Document.Body.RemoveChild(paragraphs);
            doc.Close();
        }
    }

如您所見,這是非常典型的方法,我已經嘗試更改一些ID等,但沒有成功!

嘗試使用OpenXml SDK 2.0生產率工具打開有錯誤的文檔,看看是否可以在document.xml文件中找到錯誤。

另外,如果文本是靜態的,則可以考慮添加內容控件,然后動態替換它們。

這可能是一個老話題,但是我想我今天早些時候會面對這個問題,所以我會回答這個問題。 您需要為圖像賦予XML文檔中包含的所有其他內容的唯一ID。

new DW.DocProperties()
                         {
                             Id = (UInt32Value)1U, // THIS NEEDS TO BE A UNIQUE VALUE
                             Name = "img" + incremental
                         },

就像...

new PIC.NonVisualDrawingProperties()
{
       Id = (UInt32Value)0U, // UNIQUE VALUE HERE AS WELL

此外,早於2010年的單詞不支持EditId。

我注意到該代碼來自MSDN,與往常一樣,它們的內容簡直是垃圾,幾乎沒有解釋。

我也建議您使用驗證工具檢查其他潛在問題...

將此粘貼在您的代碼中,然后對您的文檔運行它。

using DocumentFormat.OpenXml.Validation;
using System.Diagnostics;


public static bool IsDocumentValid(WordprocessingDocument mydoc)
        {
            OpenXmlValidator validator = new OpenXmlValidator();
            var errors = validator.Validate(mydoc);
            Debug.Write(Environment.NewLine);
            foreach (ValidationErrorInfo error in errors)
                Debug.Write(error.Description + Environment.NewLine);
            return (errors.Count() == 0);
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM