Tue Dec 16, 2025 2:49 am
您好,
感謝您的詢問。
針對Word轉PDF時圖片過高導致顯示不全的問題,建議您在轉換前根據頁面高度,按比例縮放超出頁面的圖片。請參考以下代碼:
- Code: Select all
Document doc = new Document();
doc.LoadFromFile(@"test.docx");
// 遍历所有节
foreach (Section section in doc.Sections)
{
// 获取当前节的有效页面高度
float pageHeight = section.PageSetup.PageSize.Height;
float pageWidth = section.PageSetup.PageSize.Width;
float topMargin = section.PageSetup.Margins.Top;
float bottomMargin = section.PageSetup.Margins.Bottom;
float leftMargin = section.PageSetup.Margins.Left;
float rightMargin = section.PageSetup.Margins.Right;
float usablePageHeight = pageHeight - topMargin - bottomMargin;
float usablePageWidth = pageWidth - leftMargin - rightMargin;
// 遍历当前节的所有段落
foreach (Paragraph paragraph in section.Paragraphs)
{
// 遍历段落中的所有子对象
foreach (DocumentObject docObj in paragraph.ChildObjects)
{
if (docObj is DocPicture)
{
DocPicture picture = docObj as DocPicture;
// 判断图片高度是否超出页面可用高度
if (picture.Height > usablePageHeight)
{
float scale = usablePageHeight / picture.Height;
// 等比例缩放图片
picture.Width *= scale;
picture.Height *= scale;
}
// 宽度超出页面进行缩放
if (picture.Width > usablePageWidth)
{
float widthScale = usablePageWidth / picture.Width;
picture.Width *= widthScale;
picture.Height *= widthScale;
}
}
}
}
}
ToPdfParameterList pdfParams = new ToPdfParameterList();
pdfParams.PdfConformanceLevel = Spire.Doc.PdfConformanceLevel.None; // 避免压缩
pdfParams.PdfImageCompression = PdfImageCompression.Auto; // 避免压缩
doc.SaveToFile(@"Sample.pdf", Spire.Doc.FileFormat.PDF);
如果以上方法不能解決您的問題,請將原始輸入文件以及您期望的輸出效果截圖提供給我們,我們將爲您提供更合適的方案。期待您的反饋。
Last edited by
talia.liu on Tue Dec 16, 2025 6:15 am, edited 1 time in total.
Sincerely,
Talia
E-iceblue support team