DocX 在文档中插入图片时,为什么不能按实际设置的大小插入,而Spire.Doc却可以

我的目标目标要求:将一个图片插入到页面中,页面边界为0,使用下面的代码去实现(button1UseDocX_Click函数),生成的文档不能达到目的。而使用Spire.Doc却能达到目的button1UseSpireDoc_Click函数。

之所以要用DocX是因为Spire.的库文件太大了,只是这一个小功能,就要30多M.

private void button1UseDocX_Click(object sender, EventArgs e)
{
// Create a document.
string filename =  "_加页码.docx";

using (DocX document = DocX.Create(filename + "_TEST.docx"))
{
document.InsertSection();
;
//document.InsertSection();
// Add a simple image from disk.
//document.PageWidth = 595;
//document.PageHeight = 842;
document.MarginBottom = 0;
document.MarginFooter = 0;
document.MarginHeader = 0;
document.MarginLeft = 0;
document.MarginRight = 0;
document.MarginTop = 0;

var image = document.AddImage("test1.jpg");
var picture = image.CreatePicture((int)document.PageHeight, (int)document.PageWidth);

var p = document.InsertParagraph();

p.AppendPicture(picture);
document.Save();

}

private void button1UseSpireDoc_Click(object sender, EventArgs e)
{
string filename =  "_输出.docx";
//wordDocx = DocX.Create(filename);
Document document = new Document();

//set the background type as picture.
document.Background.Type = Spire.Doc.Documents.BackgroundType.Picture;

Spire.Doc.Section s = document.AddSection();
s.PageSetup.PageSize = Spire.Doc.Documents.PageSize.A4;
s.PageSetup.Margins.All = 0;
Spire.Doc.Documents.Paragraph p = s.AddParagraph();


DocPicture Pic = p.AppendPicture(System.Drawing.Image.FromFile(imagefilename));
Pic.Width = s.PageSetup.PageSize.Width;
Pic.Height = s.PageSetup.PageSize.Height;
 

//Save and Launch
document.SaveToFile(filename, FileFormat.Docx);
System.Diagnostics.Process.Start(filename);
}

猜你喜欢

转载自www.cnblogs.com/mcjtcnblog/p/11695385.html