开发者

Anything wrong about this code? This is giving me corrupted content

private static void UpdateFontSettings(WordprocessingDocument content, WordprocessingDocument template)

    {
        if (content != null && content.MainDocumentPart != null && content.MainDocumentPart.FontTablePart != null &&
            template != null && template.MainDocumentPart != null && template.MainDocumentPart.FontTablePart != null)
        {
            IEnumerable<OpenXmlPart> partsToDelete = from idPartPair in content.MainDocumentPart.FontTablePart.Parts
                                                     select idPartPair.OpenXmlPart;

            content.MainDocumentPart.FontTablePart.DeleteParts(partsToDelete);

            StreamReader streamReader = null;
            StreamWriter streamWriter = null;

            try
            {
                streamReader = new StreamReader(template.MainDocumentPart.FontTablePart.GetStream());
                streamWriter = n开发者_运维问答ew StreamWriter(content.MainDocumentPart.FontTablePart.GetStream(FileMode.Create));
            }
            catch
            {
                if (streamReader != null)
                {
                    streamReader.Close();
                }

                throw;
            }

            using (streamWriter)
            {
                streamWriter.Write(streamReader.ReadToEnd());
            }

            if (streamReader != null)
            {
                streamReader.Close();
            }
        }
    }


I would suggest setting your encoding type when you create your StreamWriter. I believe it defaults to ANSI for most systems, though you might require UTF8 or something else.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜