Aspose.words如何获取word中的每一页

你好:
请问一下,我想对一个多页word中的每一页插入一张相同的图片,但是我没有找到相关的示例的代码,所以来问一下应该如何操作?

主要问题就是:因为我查看了相关的代码,使用documentbulider来插入图片,那么当word是多页的时候,我怎么对不同的页面进行相同的操作呢,有什么方法能让程序在处理文档的时候移动到下一页进行这个操作嘛

您好 @jidesheng,

问题是Word文档是一个流文档,不包含有关其布局的任何信息,进入行和页面。 所以,不幸的是,没有公共API,它允许您确定页面从Aspose.Words开始或结束的位置。

但是,您可以将图像插入页眉或页脚,此图像将显示在每个页面上。这是一个例子:

Document doc = new Document("multipage.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.insertImage("image.png", 50, 50);

doc.save("multipageWithImage.docx");

multipage.zip (51.0 KB)


您可以找到更多示例如何使用文档中的页眉和页脚:

谢谢,我收到了,但是我想请教一下,那这样操作的话,有没有办法让word的每一页插入不同的图片?因为我昨天在支持论坛搜了一下,有看到说用documentpagesplit的,如果没办法在一个word文档中完成不同图片的插入,那把他们先拆分成不同的文档,再对他们进行读取单独操作,最后合并成一个word文档呢?如果有的话,请问aspose有么有支持多个word文档合并成一个的API?万分感谢

@jidesheng,

在我给您答案之前,我想知道您是否尝试将不同的图像插入到新文档或现有文档中。 您能给我更多关于您想要达到的目标的细节吗?

如果您要在现有文档中插入不同的图像,能否附上该文档? 您也可以手动将几张图像插入到此类文档中并附上它以显示最终文档的外观吗?

然后,我将尝试为您提供示例代码。

以下是用于拆分和合并文档的示例和 API:

谢谢您的回答,我简单描述一下我的想法:

1、因为您之前和我说过aspose没有公共API来获取每一页为一个单独的document,所以我在论坛搜了一下,找到了documentpagesplit的代码添加到了我的项目中,他顺利的运行了。
2、但是分割出来的word会有多出一页的情况,看似是多了一个回车,我不知道怎么处理,最开始我在代码里使用删除分栏符之后再保存出来,我成功了,但是第二次再运行的时候好像就不起作用了,这在我提供的附件中也有说明
3、最后我使用代码合并word文档的时候,总是会多出来一个类似回车的东西,但是我不清楚如何删除,不过我尝试手动更改了原稿的页边距就没有这种情况了。
以下是附件,其中有:原稿,分割没问题的文档、分割有问题的文档(就是变成两页那种的)、最后合并的word文档,合并的word文档差不多就是我要的样子,最右下角二维码应该是从100开始一直到107结束,演示的都是100,当成递增的二维码图片就好
存在疑惑的文档.zip (382.4 KB)

您好 @jidesheng,

请看一下下面给出的代码。 它使用 LayoutEnumerator 类在每个页面上插入具有不同 QR 图像的形状。

Document doc = new Document("in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

// Get paragraphs in the main document body.
java.util.List<Paragraph> mainBodyParagraphs = new ArrayList<Paragraph>();
for (Paragraph para : (Iterable<Paragraph>) doc.getChildNodes(NodeType.PARAGRAPH, true))
{
    if (para.getParentNode().getNodeType() == NodeType.BODY)
        mainBodyParagraphs.add(para);
}

LayoutCollector layoutCollector = new LayoutCollector(doc);
int pageToInsert = 1;
for (Paragraph para : mainBodyParagraphs)
{
    int paraPage = layoutCollector.getEndPageIndex(para);
    if (paraPage == pageToInsert)
    {
       // Create a shape with QR image
        Shape qrShape = new Shape(doc, ShapeType.IMAGE);
        qrShape.isLayoutInCell(false);
        qrShape.getImageData().setImage("qrs\\" + pageToInsert + ".png");

        qrShape.setWidth(50);
        qrShape.setHeight(50);

        qrShape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
        qrShape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
        qrShape.setWrapType(WrapType.NONE);
        qrShape.setHorizontalAlignment(HorizontalAlignment.RIGHT);
        qrShape.setVerticalAlignment(VerticalAlignment.BOTTOM);

        para.appendChild(qrShape);
        pageToInsert++;
    }
}

doc.save("outWithImages.docx");

files.zip (125.1 KB)

非常感谢,不过我使用的是c#,我看了您展示的插入图片部分代码,问题是对于我来说其实这个图片有点太靠下了,请问是否有办法能让图片插入的位置刚好在word四周的L图案上
L图案如附件所示,
QQ截图20220322105910.png (1.9 KB)

您好 @jidesheng,

请使用 Shape.LeftShape.Top 属性在文档中定位形状。 这是一个例子:

Document doc = new Document(@"in.docx");

// Get paragraphs in the main document body.
List<Node> mainBodyParagraphs = doc.GetChildNodes(NodeType.Paragraph, true).
    Where(p => p.ParentNode.NodeType == NodeType.Body).ToList();

LayoutCollector layoutCollector = new LayoutCollector(doc);
int pageToInsert = 1;
foreach (Paragraph para in mainBodyParagraphs)
{
    int paraPage = layoutCollector.GetEndPageIndex(para);
    if (paraPage == pageToInsert)
    {
        // Create a shape with QR image
        Shape qrShape = new Shape(doc, ShapeType.Image);
        qrShape.IsLayoutInCell = false;
        qrShape.ImageData.SetImage("qrs\\" + pageToInsert + ".png");

        qrShape.Width = 50;
        qrShape.Height = 50;

        qrShape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
        qrShape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
        qrShape.WrapType = WrapType.None;

        // Postion the qrShape relative to the left side of the page.
        qrShape.Left = 520;
        // Postion the qrShape relative to the top side of the page.
        qrShape.Top = 750;

        pageToInsert++;
        para.AppendChild(qrShape);
    }
}

doc.Save(@"outWithImages.docx");

outWithImages.docx (70.1 KB)