通过将WordprocessingMLPackage添加到主包中而失去页面方向

时间:2018-10-05 07:51:09

标签: docx4j

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

  

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

我有一个主文档,其中包含我的目录和徽标以及为其设置的页码。然后,我开始使用docx压模填充其他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);
    }

我按照第一条评论的尝试进行了尝试,但是整个文档都是“ Landscape”(横向),并且在末尾添加了一个空白的纵向页面:

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

也没有帮助。

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

1 个答案:

答案 0 :(得分:0)

我在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);
    }
}