Error! Reference source not found and TOC updates

Hi Team,

When I am converting doc to pdf, I am getting “Error! Reference source not found.” in pdf file and if I use pdfSaveOptions.setUpdateFields(false), table of contents is showing incorrect page numbers in pdf file.

Please suggest.

    private static void docToPdf() throws IOException {
//        InputStream stream = FileUtils.openInputStream(new File("/home/govindsharma/Downloads/WH02EU Service Wholesale Agreement-EU_Non EU EFTA (COT01019).docx"));
        InputStream stream = FileUtils.openInputStream(new File("/home/govindsharma/Downloads/Distributörsavtal SE Sverige-Alcadon Group AB v_1_2 2023-05-19.docx"));
        try {
            LoadOptions lo = new LoadOptions();
            lo.setPreserveIncludePictureField(true);
//            lo.setUpdateDirtyFields(true);
            Document document = new Document(stream, lo);
//            document.updateFields();
//            File file = new File("/home/govindsharma/Downloads/WH02EU Service Wholesale Agreement-EU_Non EU EFTA (COT01019).pdf");
            File file = new File("/home/govindsharma/Downloads/Distributörsavtal SE Sverige-Alcadon Group AB v_1_2 2023-05-19.pdf");
            FileOutputStream faos = new FileOutputStream(file);
//            lockFileNameFieldInDocument(document);
            com.aspose.words.PdfSaveOptions pdfSaveOptions = new com.aspose.words.PdfSaveOptions();
            pdfSaveOptions.setUpdateFields(true);
   //        pdfSaveOptions.setUpdateFields(false);
            pdfSaveOptions.setSaveFormat(SaveFormat.PDF);
            pdfSaveOptions.setMemoryOptimization(true);
            document.save(faos, pdfSaveOptions);

        } catch (Exception ex) {

        }
    }

@govindsgs Could you please attach your input and output documents here for testing? We will check the issue and provide you more information.

Distributörsavtal SE Sverige-Alcadon Group AB v_1_2 2023-05-19 (copy).pdf (25.9 KB)

Distributörsavtal SE Sverige-Alcadon Group AB v_1_2 2023-05-19 (copy).docx (32.1 KB)

I have added for “Error! Reference source not found.”, but for TOC it needs to upload whole documet which i can’t as it’s client document.

My requirement is while coverting doc to pdf, I should not get “Error! Reference source not found.” and my TOC will be updated as per page numbers in pdf document.
Currently if I am using pdfSaveOptions.updatefields(true) then TOC is coming as expected but reference error comes, if I am using pdfSaveOptions.updatefields(false) then reference error is not coming but TOC is not correct as per page numbers.

Please check.

@govindsgs In your code you can either disable updating REF fields or unlink them (replace with text) before converting document to PDF. For example see the following code:

Document doc = new Document("C:\\Temp\\in.docx");
for (Field f : doc.getRange().getFields())
{
    if (f.getType() == FieldType.FIELD_REF)
    {
        f.unlink();
        // Alternatively you can disable REF field updating:
        //f.isLocked(true);
    }
}
doc.save("C:\\temp\\out.pdf");

Thanks @alexey.noskov it worked.

1 Like