I use Aspose.Word. When you try to resize the page, everything changes. BUT the images go beyond the boundaries of the text space. There are several images in the document and I have no idea how to fix it. `
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);
`
Try to find correct method of word. Expecting all images at doc will be right dimensions
I think this code i need:
foreach (Aspose.Words.Drawing.Shape shape in doc)
{
shape.Width ...
}
But i have error : Не удалось привести тип объекта "Aspose.Words.Section" к типу "Aspose.Words.Drawing.Shape".
CodePudding user response:
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;
}
CodePudding user response:
To get all shapes in the document, you can use Document.GetChildNodes method passing the appropriate NodeType
as a parameter. For example the following code returns all shapes in the document:
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
You can use LINQ to filter the collection, for example the following code returns shapes that has an image:
List<Shape> shapes = doc.GetChildNodes(NodeType.Shape, true)
.Cast<Shape>().Where(s => s.HasImage).ToList();
It looks like your requirement is to fit the image to image size. I think the example provided here might be useful for you. In the provided example an image is instead into the document and page is adjusted to the actual image size. Then the result document is converted to PDF.