我正在使用OpenXML将图像插入到我的文档中.Microsoft提供的代码可以工作,但会使图像更小:
public static void InsertAPicture(string document, string fileName)
{
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(document, true))
{
MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;
ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);
using (FileStream stream = new FileStream(fileName, FileMode.Open))
{
imagePart.FeedData(stream);
}
AddImageToBody(wordprocessingDocument, mainPart.GetIdOfPart(imagePart));
}
}
private static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId)
{
// Define the reference of the image.
var element =
new Drawing(
new DW.Inline(
new DW.Extent() { Cx = 990000L, Cy = 792000L },
new DW.EffectExtent()
{
LeftEdge = 0L,
TopEdge = 0L,
RightEdge …Run Code Online (Sandbox Code Playgroud) 我正在提供一个可从我的网站下载的文件,该文件正在运行.但是,我注意到Chrome的这种行为.
我认为我有正确的MIME类型设置,但Chrome显示此消息并且还将请求标记为红色.
我设置的MIME类型是:
application/vnd.openxmlformats-officedocument.wordprocessingml.document
这是*.docx文件的预期行为吗?好像我可能做错了什么.

我需要以编程方式使用C#将几个预先存在的docx文件附加到一个长docx文件中 - 包括子弹和图像等特殊标记.页眉和页脚信息将被删除,因此这些信息不会导致任何问题.
我可以找到有关docx使用.NET Framework 3 操作单个文件的大量信息,但对于如何合并文件没有任何简单或明显的信息.还有一个第三方程序(Acronis.Words)可以做到这一点,但它非常昂贵.
已经建议通过Word进行自动化,但是我的代码将在IIS Web服务器上的ASP.NET上运行,因此对我而言,不能选择使用Word.很抱歉没有提到这一点.
我有以下代码将页面附件带给用户:
private void GetFile(string package, string filename)
{
var stream = new MemoryStream();
try
{
using (ZipFile zip = ZipFile.Read(package))
{
zip[filename].Extract(stream);
}
}
catch (System.Exception ex)
{
throw new Exception("Resources_FileNotFound", ex);
}
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/unknown";
if (filename.EndsWith(".docx"))
{
Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
}
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
Response.BinaryWrite(stream.GetBuffer());
stream.Dispose();
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
Run Code Online (Sandbox Code Playgroud)
问题是所有支持的文件都能正常工作(jpg,gif,png,pdf,doc等),但.docx文件在下载时已损坏,需要Office修复才能打开.
起初我不知道问题是解压缩包含.docx的zip文件,所以不是仅将输出文件放在响应中,而是先保存它,然后文件成功打开,所以我知道问题应该回应写作.
你知道会发生什么吗?
我正在使用knitr和pandoc通过markdown生成一些odt/docx报告,现在我想知道如何制作表格.主要是我有兴趣添加规则(至少在标题下方的顶部,底部和一个,但是能够在表格中添加任意的规则也很好).
通过pandoc(没有任何特殊参数)从pandoc文档运行以下示例只会产生一个"普通"表,没有任何规则/颜色/指南(在任何一个-t odt或中-t docx).
+---------------+---------------+--------------------+
| Fruit | Price | Advantages |
+===============+===============+====================+
| Bananas | $1.34 | - built-in wrapper |
| | | - bright color |
+---------------+---------------+--------------------+
| Oranges | $2.10 | - cures scurvy |
| | | - tasty |
+---------------+---------------+--------------------+
Run Code Online (Sandbox Code Playgroud)
我已经通过"样式"查看了在引用.docx/.odt中指定表格格式的可能性,但在"表格标题"和"表格内容"样式之外没有发现任何明显的东西,这两种样式似乎只涉及格式化表格中的文字.
由于对WYSIWYG风格的文档处理器不太熟悉,我对如何继续而感到迷茫.
有些格式实际上是伪装的zip文件,例如docx或odt.如果我将它们直接存储在版本控制中,它们将作为二进制文件处理.我理想的解决方案是
foo.docx/为每个foo.docx文件创建一个目录,将所有文件解压缩到其中foo.docx可以在更新后从存储的文件中重新创建我不希望docx文件本身受版本控制.(我知道一个相关的问题,建议使用自定义差异的不同方法.)
这可行吗?这可以用mercurial吗?
更新:
我知道钩子.我对细节很感兴趣.这是一个演示预期行为的会话.
> hg add foo.docx
> hg status
A foo.docx
> hg commit
> # Change foo.docx with external editor
> hg status
M foo.docx
> hg diff
+++ foo.docx/word/document.xml
- <w:t>An idea</w:t>
+ <w:t>A much better idea</w:t>
Run Code Online (Sandbox Code Playgroud) 我有大约10个word文档,我使用open xml和其他东西生成.现在我想创建另一个word文档,我想逐个加入到这个新创建的文档中.我希望使用open xml,任何提示都会很明显.以下是我的代码:
private void CreateSampleWordDocument()
{
//string sourceFile = Path.Combine("D:\\GeneralLetter.dot");
//string destinationFile = Path.Combine("D:\\New.doc");
string sourceFile = Path.Combine("D:\\GeneralWelcomeLetter.docx");
string destinationFile = Path.Combine("D:\\New.docx");
try
{
// Create a copy of the template file and open the copy
//File.Copy(sourceFile, destinationFile, true);
using (WordprocessingDocument document = WordprocessingDocument.Open(destinationFile, true))
{
// Change the document type to Document
document.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
//Get the Main Part of the document
MainDocumentPart mainPart = document.MainDocumentPart;
mainPart.Document.Save();
}
}
catch
{
}
}
Run Code Online (Sandbox Code Playgroud)
更新(使用AltChunks):
using (WordprocessingDocument myDoc = WordprocessingDocument.Open("D:\\Test.docx", true)) …Run Code Online (Sandbox Code Playgroud) 我必须将docx文件格式(以openXML格式)转换为JSON格式.我需要一些指导方针来做到这一点.提前致谢.
我在Rstudio中用Rmarkdown键入报告.当html用knitr 转换它时,还有一个markdown由knitr生成的文件.我转换此文件pandoc如下:
pandoc -f markdown -t docx input.md -o output.docx
Run Code Online (Sandbox Code Playgroud)
该output.docx文件很好,除了一个问题:数字的大小被改变,我需要手动调整Word中的数字.有没有什么可做的,也许是一个选项pandoc,以获得正确的数字大小?
是否有用于将Word文件转换为HTML而不会丢失格式的API?
谷歌文件API可以用于此吗?
我尝试了saaspose但返回的结果始终是服务器错误.
解决方案对我不起作用: