使用c#中的图像调整页面高度和宽度



我使用Aspose.Word。当你试图调整页面大小时,一切都会改变。但是这些图像超出了文本空间的边界。文档中有几个图像,我不知道如何修复。`

var input = @"d:1.docx";
var output = @"d:2.docx";
Document doc = new Document(input);
DocumentBuilder builder = new DocumentBuilder(doc);
if (project.Variables["flagsize"].Value=="69")
{
builder.PageSetup.PageWidth = ConvertUtil.MillimeterToPoint(152.4);
builder.PageSetup.PageHeight = ConvertUtil.MillimeterToPoint(228.6);
Node[] runs = doc.GetChildNodes(NodeType.Run, true).ToArray();
for (int j = 0; j < runs.Length; j++)
{   Run run = (Run)runs[j];
run.Font.Size = 18;
}
}
foreach (Section section in doc)
{
section.PageSetup.PaperSize = Aspose.Words.PaperSize.Custom;
section.PageSetup.LeftMargin= ConvertUtil.MillimeterToPoint(22);
section.PageSetup.RightMargin= ConvertUtil.MillimeterToPoint(22);
}
doc.Save(output);

`

试着找出正确的写法。期望文档中的所有图像都是正确尺寸的

我想我需要这个代码:

foreach (Aspose.Words.Drawing.Shape shape in doc)
{
shape.Width ...
}

但我有一个错误:";Aspose.Words.Section;";Aspose。Words。Drawing。Shape;。

要获取文档中的所有形状,可以使用document.GetChildNodes方法传递相应的NodeType作为参数。例如,以下代码返回文档中的所有形状:

NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);

您可以使用LINQ来过滤集合,例如,以下代码返回具有图像的形状:

List<Shape> shapes = doc.GetChildNodes(NodeType.Shape, true)
.Cast<Shape>().Where(s => s.HasImage).ToList();

看起来您的要求是使图像适合图像大小。我认为这里提供的例子可能对你有用。在所提供的示例中,图像被替换为文档,并且页面被调整为实际图像大小。然后将结果文档转换为PDF。

NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
PageSetup page_Setup = doc.FirstSection.PageSetup;
foreach (Shape shape in shapes)
{
shape.HorizontalAlignment = HorizontalAlignment.Center;
shape.Width = page_Setup.PageWidth - page_Setup.LeftMargin - page_Setup.RightMargin;
}

最新更新