Page 1 of 1

Position displaced of image in generated PDF's header

PostPosted: Fri Nov 11, 2016 10:51 pm
by wagnerxp
Hi guys,

I'm using docx4J to convert a docx in a PDF, but my problem is when I get the PDF document generated by the doc.
I have an image in header, but the PDF is generated with the displaced image to the right, and not like in the original document. So, how it is a formal document, I can't leave the conversion like this.
Anyone has some help to solve it?

Attached is the original files doc and the converted PDF. Also, I'm sending the code of my program.

Thank you in advance!

Code: Select all
try {
            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(arquvivoFormatoDOCX);

            String outputfilepathPDF = pathArquivoTemp + File.separator + nomeArquivoTemp + ".pdf";
            OutputStream os;
            os = new java.io.FileOutputStream(outputfilepathPDF);

            java.io.File f = new java.io.File(outputfilepathPDF);
            //salva em .pdf       
            //o salvamento em pdf funciona da seguinte forma. Se incluir a lib docx4j-export-fo-3.3.0.jar + dependencias, o sistema tenta converter local.
            //sem incluir a lib, o sistema puxa uma configuração existente no /api/docx4jxxx/resources/docx4j.properties e joga o PDF para um site especializado na conversão (Plutex). Por padrão ele joa para o site plutx, mas nesse caso o site printa uma informação dizendo que é teste

            if (!Docx4J.pdfViaFO()) {
                //salva via plutex

                //convert com o plutex (online - (coloca mensagem de evaluate)
                Docx4J.toPDF(wordMLPackage, os);

            } else {
                // Refresh the values of DOCPROPERTY fields
                FieldUpdater updater = new FieldUpdater(wordMLPackage);
                updater.update(true);

                // Font regex (optional)
                // Set regex if you want to restrict to some defined subset of fonts
                // Here we have to do this before calling createContent,
                // since that discovers fonts
                //   String regex = null;
                // Windows:
                // String
                //     regex = ".*(calibri|camb|cour|arial|symb|times|Times|zapf).*";
                //   regex = ".*(calibri|camb|cour|arial|times|comic|georgia|impact|LSANS|pala|tahoma|trebuc|verdana|symbol|webdings|wingding|Liberation Serif).*";
                // Mac
                // String
                // regex = ".*(Courier New|Arial|Times New Roman|Comic Sans|Georgia|Impact|Lucida Console|Lucida Sans Unicode|Palatino Linotype|Tahoma|Trebuchet|Verdana|Symbol|Webdings|Wingdings|MS Sans Serif|MS Serif).*";
                //PhysicalFonts.setRegex(regex);
                // FO exporter setup (required)
                // .. the FOSettings object
                FOSettings foSettings = Docx4J.createFOSettings();
                boolean saveFO = false;
                if (saveFO) {
                    foSettings.setFoDumpFile(new java.io.File(pathArquivoTemp + File.separator
                            + "FOsetting.fo"));
                }

                foSettings.setWmlPackage(wordMLPackage);

                // Document format:
                // The default implementation of the FORenderer that uses Apache Fop will output
                // a PDF document if nothing is passed via
                // foSettings.setApacheFopMime(FOSettings.APACHEFOP_MIME);
                // apacheFopMime can be any of the output formats defined in org.apache.fop.apps.MimeConstants eg org.apache.fop.apps.MimeConstants.MIME_FOP_IF or
                // FOSettings.INTERNAL_FO_MIME if you want the fo document as the result.
                foSettings.setApacheFopMime(FOSettings.MIME_PDF);
                // Specify whether PDF export uses XSLT or not to create the FO
                // (XSLT takes longer, but is more complete).
                // Don't care what type of exporter you use

                //i tried this
                Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
                //i tried this to
                //Docx4J.toPDF(wordMLPackage, os);

                // Prefer the exporter, that uses a xsl transformation
                // Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
                // Prefer the exporter, that doesn't use a xsl transformation (= uses a visitor)
                // .. faster, but not yet at feature parity
                // Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_NONXSL);
                // Clean up, so any ObfuscatedFontPart temp files can be deleted
                if (wordMLPackage.getMainDocumentPart().getFontTablePart() != null) {
                    wordMLPackage.getMainDocumentPart().getFontTablePart().deleteEmbeddedFontTempFiles();
                }
                // This would also do it, via finalize() methods
                updater = null;
                foSettings = null;
                wordMLPackage = null;

                os.flush();
                os.close();

            }
            //carrega o arquivo
            InputStream is = Utils.carregaInputStreamAPartirDeFile(f);
            byte[] arquivo = IOUtils.toByteArray(is);
            is.close();
            // quando chama a partir do envio de e-mail, não deve se deletar o arquivo antes de dar um .send(), já quando chama a partir da visualização da proposta em word, pode-se deletar o arquivo temporário
            if (excluirFile) {
                f.delete();
            }
            return arquivo;
        } catch (FileNotFoundException ex) {
            throw new HeraException("Não foi possível converter de DOCX para PDF. " + ex.getMessage());

        }

Re: Position displaced of image in generated PDF's header

PostPosted: Sat Nov 12, 2016 12:40 pm
by jason
This appears to be an issue in the XSL/FOP based conversion, the source code for which is at https://github.com/plutext/docx4j-export-FO/

Plutext's commercial PDF Converter at http://converter-eval.plutext.com handles your docx fine, if that's any consolation.

Re: Position displaced of image in generated PDF's header

PostPosted: Tue Nov 15, 2016 4:17 am
by wagnerxp
I have the same problem using a text box and a image in a header. The text box is not showing in the header. Maybe, there is a solution for this without to use Plutext's commercial PDF Converter?
See the attachments bellow. The code is the same.

Thanks...

Re: Position displaced of image in generated PDF's header

PostPosted: Tue Nov 15, 2016 7:15 am
by jason
Unfortunately, there are real limitations to what can be done with text boxes in XSL FO.

export-FO does jump through some hoops to process these though ... https://github.com/plutext/docx4j-expor ... &q=textbox

Try putting the text box in the body of the docx (with the same kind of float settings). If that converts OK, then it may just be a simple matter of ensuring the same logic is used in a header.