这是我在 NSAttributedString 类中发现的奇怪行为。根据文档,此类的对象可以产生各种类型的数据。我想将文本转换为 docx 现代格式。再次根据文档,我应该使用 NSOfficeOpenXMLTextDocumentType 文档属性。所以我这样做:
// produce an NSAttributedString with all of the indents have set
let stringToConvert = currentDocument.computePrintableAttributedString(forProduct: product)`
// now create a document attributes dictionary
let documentAttributes = [NSDocumentTypeDocumentAttribute : NSOfficeOpenXMLTextDocumentType]
// finally convert the string into the docx data
do {
let convertedData = try stringToConvert.dataFromRange(fullLength, documentAttributes: documentAttributes)
} catch {
print(error)
}
一切正常。我将数据写入文件。并在 MS Word 中打开它。一切看起来都很好...除了文档丢失了属性字符串段落样式的所有尾部缩进。
我做错了什么?如何在导出的docx文档中保留尾部缩进?
我尝试了 NRTFTextDocumentType 属性,并且得到了一个格式非常良好的 rtf 文件,其中所有尾部缩进都已就位。
我对 NSOfficeOpenXMLTextDocumentType 缺少什么?
最佳答案
您似乎发现了一个错误/功能。我可以使用在 OS X 10.11.4 下运行的 TextEdit 1.11 重现该行为,并且 TextEdit 在内部使用 NSAttributedString
。保存为 rtf 时,尾部缩进和首行缩进均保留;当另存为 Word 97、2003 或 2007 时,如果在 TextEdit 中重新打开文件,尾部缩进将会丢失;如果在第 5 页中打开文件,第一行缩进也会丢失。 LibreOffice 在读取所有三个时会丢失尾部缩进,在读取 2003 或 2007 时会丢失首行缩进,在读取 2003 时会丢失对齐。在 PC Word 2013 上,读取所有三个时会丢失尾部缩进,而在读取 2007 时会丢失首行缩进。
建议您通过 bug report.apple.com 向 Apple 报告该问题
关于swift - NSAttributedString dataFromRange 生成没有任何尾部缩进的 docx 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37380632/