【问题标题】:Losing page orientation by adding WordprocessingMLPackage to main package通过将 WordprocessingMLPackage 添加到主包来丢失页面方向
【发布时间】:2018-10-05 07:51:09
【问题描述】:

我正在使用 docx4j 和 docx stamper 创建报告

(https://github.com/thombergs/docx-stamper)

我有一个主文档,其中包含我的目录和徽标以及为其设置的页码。然后我开始使用 docx stamper 填充其他 docx 布局,然后将它们添加到主要部分的末尾。问题是,如果我将一个 docx 布局中的页面方向设置为横向,然后将其添加到主要部分,它就会失去方向。结果包含两个部分和正确的页码。保留所有样式,但具有横向方向的第一和第三部分显示为纵向,与主文档中一样。

documentPartBase = wordMLPackageBase.getMainDocumentPart();
WordprocessingMLPackage wordMLPackageOne = fillFirstTemplate.fillTempLe();
    for (Object obj : wordMLPackageOne.getMainDocumentPart().getContent()) {
        documentPartBase.addObject(obj);
    }
    documentPartBase.addObject(pageBreak);
  WordprocessingMLPackage wordMLPackageTwo = fillSecTemplate.fillTempLe();
    for (Object obj : wordMLPackageTwo.getMainDocumentPart().getContent()) {
        documentPartBase.addObject(obj);
    }
    documentPartBase.addObject(pageBreak);
 WordprocessingMLPackage wordMLPackageThree = fillThirdTemplate.fillTempLe();
    for (Object obj : wordMLPackageThree.getMainDocumentPart().getContent()) {
        documentPartBase.addObject(obj);
    }
    documentPartBase.addObject(pageBreak);
    wordMLPackageBase.save(new File(Paths.get(".").getFileSystem().getPath("D:").toString(), resultFileName))

我尝试在将部分添加到主要部分之前添加一个横向包,但没有帮助:

WordprocessingMLPackage aPackage;
    try {
        aPackage = createPackage(PageSizePaper.A4, true);
    } catch (InvalidFormatException e) {
        throw new RuntimeException("Unhandled exception occurred.", e);
    }
    for (Object obj :aPackage.getMainDocumentPart().getContent()){
        documentPartBase.addObject(obj);
    }
WordprocessingMLPackage wordMLPackageThree = fillThirdTemplate.fillTempLe();
    for (Object obj : wordMLPackageThree.getMainDocumentPart().getContent()) {
        documentPartBase.addObject(obj);
    }

并且还尝试添加:

try {
        sectPr = (SectPr) XmlUtils.unmarshalString("<w:sectPr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">"
            + "<w:pgSz w:w=\"16839\" w:h=\"11907\" w:orient=\"landscape\"/>"
            + "</w:sectPr>");
    } catch (JAXBException e) {
        throw new RuntimeException("Unhandled exception occurred.", e);
    }
    wordMLPackageThree.getMainDocumentPart().getContent().add(sectPr);

根据

添加如下方法

Change the page orientation in middle of doc

就像下面的代码使整个文档显示为横向!

    makeLandscape(wordMLPackageThree.getMainDocumentPart(), STPageOrientation.LANDSCAPE);
            for (Object obj : wordMLPackageThree.getMainDocumentPart().getContent()) 
{
        //Properties properties = Docx4jProperties.getProperties();
        //properties.setProperty("docx4j.PageOrientationLandscape", "true");
        documentPartBase.addObject(obj);
}
        private void makeLandscape(MainDocumentPart mdp, STPageOrientation stPageOrientation) 
    {
          ObjectFactory objectFactory = new ObjectFactory();
          SectPr sectionLandscape = objectFactory.createSectPr();
          SectPr.PgSz landscape = new SectPr.PgSz();
          landscape.setOrient(stPageOrientation);
          landscape.setH(BigInteger.valueOf(11906));
          landscape.setW(BigInteger.valueOf(16383));
          sectionLandscape.setPgSz(landscape);
          org.docx4j.wml.P p = objectFactory.createP();
          PPr createPPr = objectFactory.createPPr();
          createPPr.setSectPr(sectionLandscape);
          p.setPPr(createPPr);
          mdp.addObject(p);
    }

我按照第一条评论中的说法进行了尝试,但整个文档是横向的,并且在末尾添加了一个空的纵向页面:

makeLandscape(documentPartBase,STPageOrientation.LANDSCAPE);
    WordprocessingMLPackage wordMLPackageThree= fillFunktionenLe.fillFunktionenLe();
    for (Object obj : wordMLPackageThree.getMainDocumentPart().getContent()) {
            documentPartBase.addObject(obj);
        }
    makeLandscape(documentPartBase,STPageOrientation.PORTRAIT);

这也没有帮助。

如何在将内容附加在一起后使用 docx4j 保持页面方向?

【问题讨论】:

  • 在你想要的横向内容之后添加一个 sectPr 元素是你需要做的。这意味着除非第 1 页是横向的,否则您还需要在横向内容之前添加一个 sectPr(即指定您想要纵向内容)。
  • 我尝试在第三部分前后改变方向,但它只是让我的页脚消失了!
  • 好吧,您需要了解页脚在 sectPr 中是如何工作的。如果太难了,Plutext 的商业 Docx4j Enterprise 中的 MergeDocx 组件可能会有所帮助:plutext.com/m/index.php/products

标签: docx4j


【解决方案1】:

我在 TOC 的第一个 layout.docx 中添加了分节符。然后在要显示为横向的布局中,我在内容之前以纵向放置了一个分节符,并将另一页设为横向,然后在之后添加了另一个分节符作为纵向。最后我按照here 的解释合并了填充的布局,稍微修改了一下以获得布局列表:

//the mainDocIn is an empty docx. All other layout are merged there afther filling
    void mergeDocx(OutputStream out, InputStream mainDocIn, InputStream... chaptersIn) {
    try {
        WordprocessingMLPackage mainDoc = WordprocessingMLPackage.load(mainDocIn);

        for (int i = 0; i < chaptersIn.length; i++) {
            insertDocx(mainDoc.getMainDocumentPart(), 
            IOUtils.toByteArray(chaptersIn[i]), i);
        }
        mainDoc.save(out);
    } catch (IOException | Docx4JException e) {
        logger.error("Error in merging", e);
    }
}

private static void insertDocx(MainDocumentPart main, byte[] bytes, int chunkIndex) {
    try {
        AlternativeFormatInputPart afiPart = new 
        AlternativeFormatInputPart(new PartName("/part" + (chunkIndex) + ".docx"));
        afiPart.setContentType(new ContentType(CONTENT_TYPE));
        afiPart.setBinaryData(bytes);
        Relationship altChunkRel = main.addTargetPart(afiPart);

        CTAltChunk chunk = Context.getWmlObjectFactory().createCTAltChunk();
        chunk.setId(altChunkRel.getId());

        main.addObject(chunk);
    } catch (InvalidFormatException e) {
        logger.error("Data format wrong!", e);
    }
}

【讨论】:

    猜你喜欢
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多