DOCXREADWRITE issues

Questions and answers on how to use DOCXReadWrite
Post Reply
DmitryBerin
Posts: 1
Joined: Wed Feb 16, 2022 10:20 am

DOCXREADWRITE issues

Post by DmitryBerin »

Hello!

I've got some questions for the latest update of trial

1) How to set text font? It worked previously

my code:

CHPX := TAXWCHPX.Create(DOCX.Editor.MasterCHP);
Para := DOCX.Editor.Paras.AppendPara;
CHPX.FontName := 'Times New Roman';
Para.AppendPlainText(CHPX,'Lorem ');

your xml:
<w:rFonts w:eastAsia="Arial" w:cs="Times New Roman" />

ms word xml
<w:rFonts w:ascii="Arial Black" w:hAnsi="Arial Black"/>

2) TOC : AddTOCItem and AddTOCReference are missing
Maybe it is any new way to make table of contents?

3) How to update Image numbers?

my code:

P.AppendPlainText(RS_Figure + ' ');
P.AppendComplexField('1', 'SEQ Fig. \* ARABIC');

Thank you
DiBase
Posts: 13
Joined: Wed Feb 16, 2022 9:41 pm

Re: DOCXREADWRITE issues

Post by DiBase »

for 2) TOC

The sample here seems not to be correct: https://www.axolot.com/BB3/viewtopic.php?f=18&t=2496

I modified the code and got it (almost) to run properly:

Code: Select all

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

uses DOCXReadWriteVcl, AXWLogParas, AXWDOCProps, System.StrUtils;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  DOCX : TDOCXReadWriteVcl;
  P    : TAXWLogPara;
  TOC  : TList; // <TAXWLogPara>;
	Para : TAXWLogPara;
	TS   : TAXWTabStop;
begin
  DOCX := TDOCXReadWriteVcl.Create(Nil);
  try
    TOC := TList.Create; // <TAXWLogPara>.Create;
    try
      DOCX.Editor.Paras.AppendPara.BeginTOC('Table of Content','\o "1-9"');

      Para := DOCX.Editor.Paras.AppendPara;
      Para.AddTabStops;
      TS := Para.TabStops.Add( 400.0 );
      TS.Leader := atslDot;
      TS.Alignment := atsaRight;

      TOC.Add( Para );

      Para := DOCX.Editor.Paras.AppendPara;
      TOC.Add( Para );

      Para := DOCX.Editor.Paras.AppendPara;
      TOC.Add( Para );

      TAXWLogPara(TOC[0]).Runs.AddTOCItem('Heading 1');
      TAXWLogPara(TOC[1]).Runs.AddTOCItem('Heading 2');
      TAXWLogPara(TOC[2]).Runs.AddTOCItem('Heading 3');

      TAXWLogPara(TOC[2]).EndTOC;

      Para := DOCX.Editor.Paras.AddTOCReference( TOC[0] , 'TOC 1' , '' );
      Para.AppendFormattedText( '\s14\BHeading 1\b\s10' );
      Para := DOCX.Editor.Paras.AppendPara( DupeString( 'This is a sample text. ' , 40 ) );
      Para.AppendBreak( acrbtPageBreak );

      Para := DOCX.Editor.Paras.AddTOCReference( TOC[1] , 'TOC 2' , '' );
      Para.AppendFormattedText( '\s14\BHeading 2\b\s10' );
      Para := DOCX.Editor.Paras.AppendPara( DupeString( 'This is a sample text. ' , 40 ) );
      Para.AppendBreak( acrbtPageBreak );

      Para := DOCX.Editor.Paras.AddTOCReference( TOC[2] , 'TOC 3' , '' );
      Para.AppendFormattedText( '\s14\BHeading 3\b\s10' );
      Para := DOCX.Editor.Paras.AppendPara( DupeString( 'This is a sample text. ' , 40 ) );

    finally
      TOC.Free;
    end;

    DOCX.Editor.UpdateFields;  // tried to update page numbers in TOC with this function - but has no effect

    DOCX.SaveToFile( ExtractFilePath( Application.ExeName ) + '\sample.docx' );
    DOCX.SaveToFile( ExtractFilePath( Application.ExeName ) + '\sample.rtf' );

    DOCX.SaveToFile( ExtractFilePath( Application.ExeName ) + '\sample.pdf' );

  finally
    DOCX.Free;
  end;
end;

end.
I have three issues with it:

1. In Word I get no real "table of content block". No header 'Table of Content'. Only lines with page numbers.
2. The page numbers are all "0". Even if I insert UpdateFields() function. In Word I can update the page numbers manually - but not in pdf format.
3. Sometimes with slightly modified code I get an error message "list index above maximum (0)" when save in pdf format.

@larsa or any other: Any ideas how to modify my code to fix these three issues?

- Dirk
DiBase
Posts: 13
Joined: Wed Feb 16, 2022 9:41 pm

Re: DOCXREADWRITE issues

Post by DiBase »

Additional information:

Issue 3 ("error message when saving as pdf") comes up, when at least one of the Para.AppendFormattedText lines after the AddTOCReference is commented out ...

Code: Select all

      Para := DOCX.Editor.Paras.AddTOCReference( TOC[2] , 'TOC 3' , '' );
      // Para.AppendFormattedText( '\s14\BHeading 3\b\s10' );
      Para := DOCX.Editor.Paras.AppendPara( DupeString( 'This is a sample text. ' , 40 ) );
So this could be a work-around (to insert Para.AppendFormattedText), but issues 1 & 2 are still open.

- Dirk
DiBase
Posts: 13
Joined: Wed Feb 16, 2022 9:41 pm

Re: DOCXREADWRITE issues

Post by DiBase »

DmitryBerin wrote: Wed Feb 16, 2022 10:33 am 1) How to set text font? It worked previously

my code:

CHPX := TAXWCHPX.Create(DOCX.Editor.MasterCHP);
Para := DOCX.Editor.Paras.AppendPara;
CHPX.FontName := 'Times New Roman';
Para.AppendPlainText(CHPX,'Lorem ');
When using output format .docx you have also to set hAnsi and Ascii ...

Code: Select all

CHPX := TAXWCHPX.Create(DOCX.Editor.MasterCHP);
Para := DOCX.Editor.Paras.AppendPara;
CHPX.FontName := 'Times New Roman';
CHPX.hAnsi := 'Times New Roman';
CHPX.Ascii := 'Times New Roman';
Para.AppendPlainText(CHPX,'Lorem ');
For other output formats (.pdf, .rtf, ...) it's not necessary.

- Dirk
Post Reply