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);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM