Page 1 of 1

Heading no longer exist

PostPosted: Tue Aug 31, 2021 8:27 pm
by besterht
Hi Jason,

I'm currently using org.docx4j:docx4j-JAXB-ReferenceImpl:8.3.1 to generate a document replacing variables.

I fetch the template (with placeholders) from a google drive using the export link for DOCX.

Code: Select all
public InputStream getDocumentByTitle(String title, String loggedInEmail) {
        try {
            credential = createGoogleApiService("drive", loggedInEmail);
            Drive service = new Drive.Builder(httpTransport, jsonFactory, credential)
                    .setApplicationName(applicationNames.get("drive"))
                    .build();
           
            Drive.Files.List files = service.files().list()
                    .setQ("mimeType != 'application/vnd.google-apps.folder' and trashed = false and 'xxx' in parents and name = '" + title + "'")
                    .setCorpora("allDrives")
                    .setSupportsTeamDrives(true)
                    .setIncludeTeamDriveItems(true)
                    .setFields(
                            "files(name, webViewLink, iconLink, exportLinks)");
           
            FileList documents = files.execute();
            if (documents != null && !documents.isEmpty() && !documents.getFiles().isEmpty()) {
                File file = documents.getFiles().get(0);
                ArrayMap exportLinks = (ArrayMap) file.get("exportLinks");
               
                if (exportLinks != null) {
                    String url = (String)exportLinks.get(DOCX_MIME);
                   
                    if (url != null) {
                        HttpRequest req = service.getRequestFactory().buildGetRequest(
                                new GenericUrl(url));
                        HttpResponse res = req.execute();
                       
                        if (res.getStatusCode() == 200) {
                            return res.getContent();
                        }
                    }
                }
                return null;
            }
        }catch(Exception ex){
            log.info("Error occurred while accessing Google API");
            return null;
        }
        return null;
    }


I then generate the doc and upload it back to google as a GDOC.
Code: Select all
private void upload(OpcPackage wordMLPackage, String title, String loggedInEmail) throws Exception {
   
        GoogleAppsUtils utils = new GoogleAppsUtils();
   
        String folderId = "xxx";
        com.google.api.services.drive.model.File fileMetadata = new com.google.api.services.drive.model.File();
        fileMetadata.setName(title);
        fileMetadata.setParents(Collections.singletonList(folderId));
        fileMetadata.setMimeType(GDOC_MIME);
       
        InputStream inputStream = new ByteArrayInputStream(getDocxAsByteArray(wordMLPackage));
   
        File file = new File("/tmp/output.txt");
        Files.copy(inputStream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
       
        utils.uploadToDrive(fileMetadata, new FileContent(null, file), loggedInEmail);
    }


Here is a portion of the template being used
https://docs.google.com/document/d/1JcV ... sp=sharing

And here is the generated doc
https://docs.google.com/document/d/1q1k ... sp=sharing

When you click on the hyperlink text it the template it finds the heading, but not in the generated document.

Thanking you in advance!!

Re: Heading no longer exist

PostPosted: Sun Sep 26, 2021 6:26 pm
by jason
In your template docx, you have

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
      <w:hyperlink w:anchor="_1fob9te">
        <w:r w:rsidDel="00000000" w:rsidR="00000000" w:rsidRPr="00000000">
          <w:rPr>
            <w:color w:val="1155cc"/>
            <w:u w:val="single"/>
            <w:rtl w:val="0"/>
          </w:rPr>
          <w:t xml:space="preserve">Action Items</w:t>
        </w:r>
      </w:hyperlink>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


pointing at:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
    <w:p w:rsidR="00000000" w:rsidDel="00000000" w:rsidP="00000000" w:rsidRDefault="00000000" w:rsidRPr="00000000" w14:paraId="00000014">
      <w:pPr>
        <w:pStyle w:val="Heading2"/>
        <w:pageBreakBefore w:val="0"/>
        <w:spacing w:after="0" w:before="0" w:line="276" w:lineRule="auto"/>
        <w:ind w:right="-45" w:firstLine="0"/>
        <w:rPr>
          <w:highlight w:val="magenta"/>
        </w:rPr>
      </w:pPr>
      <w:bookmarkStart w:colFirst="0" w:colLast="0" w:name="_1fob9te" w:id="2"/>
      <w:bookmarkEnd w:id="2"/>
      <w:r w:rsidDel="00000000" w:rsidR="00000000" w:rsidRPr="00000000">
        <w:rPr>
          <w:rtl w:val="0"/>
        </w:rPr>
        <w:t xml:space="preserve">Action Items for ${accountNameH2}</w:t>
      </w:r>
      <w:r w:rsidDel="00000000" w:rsidR="00000000" w:rsidRPr="00000000">
        <w:rPr>
          <w:rtl w:val="0"/>
        </w:rPr>
      </w:r>
    </w:p>
 
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


via the bookmark.

After your processing, that bookmark is missing, which is why you can't click the hyperlink to jump to it anymore.

I have just tested variable replacement using XmlUtils.unmarshallFromTemplate, and it preserves the bookmark elements (as expected).

So (unless your code is dropping them), it seems like Google Docs might be dropping it. You could confirm this by inspecting your docx immediately before you upload it.

A workaround might be to wrap the entire w:p in the bookmark, like this:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
      <w:bookmarkStart w:colFirst="0" w:colLast="0" w:name="_1fob9te" w:id="2"/>
    <w:p w:rsidR="00000000" w:rsidDel="00000000" w:rsidP="00000000" w:rsidRDefault="00000000" w:rsidRPr="00000000" w14:paraId="00000014">
      <w:pPr>
        <w:pStyle w:val="Heading2"/>
        <w:pageBreakBefore w:val="0"/>
        <w:spacing w:after="0" w:before="0" w:line="276" w:lineRule="auto"/>
        <w:ind w:right="-45" w:firstLine="0"/>
        <w:rPr>
          <w:highlight w:val="magenta"/>
        </w:rPr>
      </w:pPr>
      <w:r w:rsidDel="00000000" w:rsidR="00000000" w:rsidRPr="00000000">
        <w:rPr>
          <w:rtl w:val="0"/>
        </w:rPr>
        <w:t xml:space="preserve">Action Items for ${accountNameH2}</w:t>
      </w:r>
      <w:r w:rsidDel="00000000" w:rsidR="00000000" w:rsidRPr="00000000">
        <w:rPr>
          <w:rtl w:val="0"/>
        </w:rPr>
      </w:r>
    </w:p>
      <w:bookmarkEnd w:id="2"/>
 
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


since maybe Google is only dropping empty 'point' bookmarks.

Re: Heading no longer exist

PostPosted: Wed Oct 13, 2021 8:44 pm
by besterht
Thanks Jason - got this after some more searching
https://stackoverflow.com/questions/589 ... -bookmarks