简体   繁体   English

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

[英]Resize Page Height and Width with image inside c#

I use Aspose.Word.我使用 Aspose.Word。 When you try to resize the page, everything changes.当您尝试调整页面大小时,一切都会改变。 BUT the images go beyond the boundaries of the text space.但是图像 go 超出了文本空间的边界。 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".但我有错误:Не удалось привести тип объекта "Aspose.Words.Section" к типу "Aspose.Words.Drawing.Shape"。

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

To get all shapes in the document, you can use Document.GetChildNodes method passing the appropriate NodeType as a parameter.要获取文档中的所有形状,您可以使用Document.GetChildNodes方法传递适当的NodeType作为参数。 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:您可以使用 LINQ 过滤集合,例如以下代码返回具有图像的形状:

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.然后将结果文档转换为 PDF。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM