Search in sources :

Example 1 with Docx4JException

use of org.docx4j.openpackaging.exceptions.Docx4JException in project Java-Tutorial by gpcodervn.

the class Docx4jUtils method setPageMargins.

/**
 * Set document page margins to 50 pixels
 */
private void setPageMargins() {
    try {
        Body body = wordMLPackage.getMainDocumentPart().getContents().getBody();
        PageDimensions page = new PageDimensions();
        PgMar pgMar = page.getPgMar();
        pgMar.setBottom(BigInteger.valueOf(pixelsToDxa(50)));
        pgMar.setTop(BigInteger.valueOf(pixelsToDxa(50)));
        pgMar.setLeft(BigInteger.valueOf(pixelsToDxa(50)));
        pgMar.setRight(BigInteger.valueOf(pixelsToDxa(50)));
        SectPr sectPr = factory.createSectPr();
        body.setSectPr(sectPr);
        sectPr.setPgMar(pgMar);
    } catch (Docx4JException e) {
        e.printStackTrace();
    }
}
Also used : PageDimensions(org.docx4j.model.structure.PageDimensions) PgMar(org.docx4j.wml.SectPr.PgMar) SectPr(org.docx4j.wml.SectPr) Body(org.docx4j.wml.Body) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException)

Example 2 with Docx4JException

use of org.docx4j.openpackaging.exceptions.Docx4JException in project com.revolsys.open by revolsys.

the class XlsxRecordReader method initDo.

@Override
protected void initDo() {
    super.initDo();
    try (InputStream in = this.resource.newBufferedInputStream()) {
        final SpreadsheetMLPackage spreadsheetPackage = (SpreadsheetMLPackage) OpcPackage.load(in);
        final DocPropsCustomPart customProperties = spreadsheetPackage.getDocPropsCustomPart();
        if (customProperties != null) {
            int srid = 0;
            try {
                srid = Integer.parseInt(customProperties.getProperty("srid").getLpwstr());
            } catch (final Throwable e) {
            }
            int axisCount = 2;
            try {
                axisCount = Integer.parseInt(customProperties.getProperty("axisCount").getLpwstr());
                if (axisCount > 4) {
                    axisCount = 2;
                }
            } catch (final Throwable e) {
            }
            double scaleXy = 0;
            try {
                scaleXy = Double.parseDouble(customProperties.getProperty("scaleXy").getLpwstr());
            } catch (final Throwable e) {
            }
            double scaleZ = 0;
            try {
                scaleZ = Double.parseDouble(customProperties.getProperty("scaleZ").getLpwstr());
            } catch (final Throwable e) {
            }
            final GeometryFactory geometryFactory = GeometryFactory.fixed(srid, axisCount, scaleXy, scaleXy, scaleZ);
            setGeometryFactory(geometryFactory);
        }
        WorksheetPart worksheetPart = null;
        for (final Part part : spreadsheetPackage.getParts().getParts().values()) {
            if (part instanceof WorksheetPart) {
                if (worksheetPart == null) {
                    worksheetPart = (WorksheetPart) part;
                }
            } else if (part instanceof SharedStrings) {
                final SharedStrings sharedStrings = (SharedStrings) part;
                final CTSst contents = sharedStrings.getContents();
                this.sharedStringList = contents.getSi();
            }
        }
        if (worksheetPart != null) {
            final Worksheet worksheet = worksheetPart.getContents();
            final SheetData sheetData = worksheet.getSheetData();
            this.rows = sheetData.getRow();
            final List<String> line = readNextRow();
            final String baseName = this.resource.getBaseName();
            newRecordDefinition(baseName, line);
        }
    } catch (final IOException | Docx4JException e) {
        Logs.error(this, "Unable to open " + this.resource, e);
    } catch (final NoSuchElementException e) {
    }
}
Also used : SpreadsheetMLPackage(org.docx4j.openpackaging.packages.SpreadsheetMLPackage) GeometryFactory(com.revolsys.geometry.model.GeometryFactory) InputStream(java.io.InputStream) WorksheetPart(org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart) Worksheet(org.xlsx4j.sml.Worksheet) IOException(java.io.IOException) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException) DocPropsCustomPart(org.docx4j.openpackaging.parts.DocPropsCustomPart) CTSst(org.xlsx4j.sml.CTSst) SheetData(org.xlsx4j.sml.SheetData) WorksheetPart(org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart) DocPropsCustomPart(org.docx4j.openpackaging.parts.DocPropsCustomPart) Part(org.docx4j.openpackaging.parts.Part) SharedStrings(org.docx4j.openpackaging.parts.SpreadsheetML.SharedStrings) NoSuchElementException(java.util.NoSuchElementException)

Example 3 with Docx4JException

use of org.docx4j.openpackaging.exceptions.Docx4JException in project docx4j-template by vindell.

the class Word_解压_Unzip_S3_Test method zipXml.

public void zipXml(String inputfilepath, String outFilePath) throws Exception {
    System.out.println(inputfilepath);
    // Load the docx
    File baseDir = new File(inputfilepath);
    UnzippedPartStore partLoader = new UnzippedPartStore(baseDir);
    final Load3 loader = new Load3(partLoader);
    OpcPackage opc = loader.get();
    // Save it zipped
    ZipPartStore zps = new ZipPartStore();
    zps.setSourcePartStore(opc.getSourcePartStore());
    Save saver = new Save(opc, zps);
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(new File(outFilePath));
        saver.save(fos);
    } catch (FileNotFoundException e) {
        throw new Docx4JException("Couldn't save " + outFilePath, e);
    } finally {
        IOUtils.closeQuietly(fos);
    }
}
Also used : UnzippedPartStore(org.docx4j.openpackaging.io3.stores.UnzippedPartStore) ZipPartStore(org.docx4j.openpackaging.io3.stores.ZipPartStore) FileOutputStream(java.io.FileOutputStream) OpcPackage(org.docx4j.openpackaging.packages.OpcPackage) FileNotFoundException(java.io.FileNotFoundException) Save(org.docx4j.openpackaging.io3.Save) File(java.io.File) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException) Load3(org.docx4j.openpackaging.io3.Load3)

Example 4 with Docx4JException

use of org.docx4j.openpackaging.exceptions.Docx4JException in project docx4j-template by vindell.

the class Docx4jUtils method docxToPdf.

/**
 * docx文档转换为PDF
 *
 * @param docx
 *            docx文档
 * @param pdfPath
 *            PDF文档存储路径
 * @throws Exception
 *             可能为Docx4JException, FileNotFoundException, IOException等
 */
public static void docxToPdf(String docxPath, String pdfPath) throws Exception {
    OutputStream output = null;
    try {
        output = new FileOutputStream(pdfPath);
        WordprocessingMLPackage wmlPackage = WordprocessingMLPackage.load(new File(docxPath));
        WMLPACKAGE_BUILDER.configChineseFonts(wmlPackage).configSimSunFont(wmlPackage);
        WMLPACKAGE_WRITER.writeToPDF(wmlPackage, output);
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        IOUtils.closeQuietly(output);
    }
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File) IOException(java.io.IOException) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException)

Example 5 with Docx4JException

use of org.docx4j.openpackaging.exceptions.Docx4JException in project flexmark-java by vsch.

the class DocxHelper method getNumPrFor.

// get a num pr for a list of given color (other than default)
// 
public BigInteger getNumPrFor(BigInteger baseNumID, Color color) {
    RPr rPr = getResolver().getEffectiveRPr(myOptions.PREFORMATTED_TEXT_STYLE);
    if (rPr != null && rPr.getColor() != null) {
        if (keepDiff(rPr.getColor(), color) == null) {
            return baseNumID;
        }
    }
    String colorID = String.format("%s:%s", baseNumID.toString(), color.getVal());
    final BigInteger numID = myNumPrColorMap.get(colorID);
    if (numID != null) {
        return numID;
    }
    // we create a copy of the baseNubPr and add the changed color property
    final NumberingDefinitionsPart ndp = myDocumentPart.getNumberingDefinitionsPart();
    try {
        final Numbering numbering = ndp.getContents();
        final List<Numbering.Num> num = numbering.getNum();
        final List<Numbering.AbstractNum> abstractNumList = numbering.getAbstractNum();
        for (Numbering.AbstractNum abstractNum : abstractNumList) {
            if (abstractNum.getAbstractNumId().compareTo(baseNumID) == 0) {
            // we have our list to copy
            // TODO: create a copy and set the color it the list's rpr.
            }
        }
        return null;
    } catch (Docx4JException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : NumberingDefinitionsPart(org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart) BigInteger(java.math.BigInteger) Docx4JException(org.docx4j.openpackaging.exceptions.Docx4JException)

Aggregations

Docx4JException (org.docx4j.openpackaging.exceptions.Docx4JException)19 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)10 File (java.io.File)7 IOException (java.io.IOException)4 SpecExample (com.vladsch.flexmark.spec.SpecExample)3 FileOutputStream (java.io.FileOutputStream)3 Save (org.docx4j.openpackaging.io3.Save)3 Node (com.vladsch.flexmark.ast.Node)2 DocxRenderer (com.vladsch.flexmark.docx.converter.internal.DocxRenderer)2 Parser (com.vladsch.flexmark.parser.Parser)2 FileNotFoundException (java.io.FileNotFoundException)2 BigInteger (java.math.BigInteger)2 Load3 (org.docx4j.openpackaging.io3.Load3)2 UnzippedPartStore (org.docx4j.openpackaging.io3.stores.UnzippedPartStore)2 ZipPartStore (org.docx4j.openpackaging.io3.stores.ZipPartStore)2 OpcPackage (org.docx4j.openpackaging.packages.OpcPackage)2 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)1 FootnoteBlock (com.vladsch.flexmark.ext.footnotes.FootnoteBlock)1 TrpDoc (eu.transkribus.core.model.beans.TrpDoc)1 ExportCache (eu.transkribus.core.model.builder.ExportCache)1