Entwickler-Ecke

Sonstiges (Delphi) - OpenOffice als PDF speichern


Dhakiyah - Mi 25.08.10 09:48
Titel: OpenOffice als PDF speichern
Hab ein OpenOffice Document geöffnet und gefüllt und möchte es jetzt als PDF speichern, automatisiert über Delphi.
Hab aber absolut keine Ahnung wie...


mkinzler - Mi 25.08.10 09:54

Wie füllst du es? (Wie erfolgt die Automatisierung)


Dhakiyah - Mi 25.08.10 09:58

???

Ich mache einen create, öffne das Dokument, fülle die Textmarken... Ich weiß nicht was du meinst...


mkinzler - Mi 25.08.10 10:12

Und wie erfolgt der Zugriff?


Dhakiyah - Mi 25.08.10 10:16


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
function TOLE_OpenOffice.Connect:boolean;
var rueckgabe : boolean;
begin
    try
      Service := CreateOleObject('com.sun.star.ServiceManager');
      Desktop := Service.createInstance('com.sun.star.frame.Desktop');
      rueckgabe := true;
    except
      rueckgabe := false;
    end;
    result := rueckgabe;
end;



function TOLE_OpenOffice.Open:boolean;
var rueckgabe : boolean;
begin
  try
    Document := Desktop.LoadComponentFromURL(
              'private:factory/swriter',
              '_blank',
              0,
              VarArrayCreate([0, - 1], varVariant));
    rueckgabe := true;
    setze_Doc_Type('s');
  except
    rueckgabe := false;
  end;
  result := rueckgabe;
end;


function TOLE_OpenOffice.ErsetzeBookmark(name,inhalt:String):boolean;
var objtext, tc, bm, li: Variant;
    i : integer;
    rueckgabe : boolean;
begin
  rueckgabe := false;
  try
    objText := Document.getText;
    tc := objText.createTextcursor;
    li := Document.Bookmarks.getElementNames;
    for i := VarArrayLowBound(li,1to VarArrayHighBound(li,1do
    begin
      if (uppercase(li[i]) =  uppercase(name)) then
      begin
        bm := Document.Bookmarks.getbyName(li[i]);
        tc := Document.Text.createTextCursorByRange(bm.Anchor);
        tc.string := inhalt;
        rueckgabe := true;
      end;
    end;
  except
    rueckgabe := false;
  end;
  result := rueckgabe;
end;


Meinst du das???


mkinzler - Mi 25.08.10 10:28

Ja.
Sollte mit .StoreAsURL() oder .StoreToURL() gehen

http://wiki.services.openoffice.org/wiki/Saving_a_document


JoelH - Mi 25.08.10 10:33


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
function TOLE_OpenOffice.SaveFileasPDF(sFile: String): boolean;
var  wProperties, fOpenOffice: variant;
     rueckgabe: Boolean;
begin
  try
    wProperties := VarArrayCreate([03], varVariant);
    wProperties[0] := Service.Bridge_GetStruct('com.sun.star.beans.PropertyValue');
    wProperties[0].Name := 'FilterName';
    case Doc_Type of
      oodtcalc    : wProperties[0].Value := 'calc_pdf_Export';
      oodtwriter  : wProperties[0].Value := 'writer_pdf_Export';
      oodtdraw    : wProperties[0].Value := 'draw_pdf_Export';
      oodtmath    : wProperties[0].Value := 'math_pdf_Export';
      oodtunknown : wProperties[0].Value := 'writer_pdf_Export';
    end;


    wProperties[1] := create_PropertyValue('CompressionMode''1');
    wProperties[2] := create_PropertyValue('Pages''All');
    wProperties[3] := create_PropertyValue('Overwrite', TRUE);
    Document.StoreToURL(ToOOPfad(sFile), wProperties);
    rueckgabe := true;
  except
    rueckgabe := false;
  end;
  result := rueckgabe;
end;

Die überflüssigen Doc_Types einfach rauswerfen, für Writer also

Delphi-Quelltext
1:
wProperties[0].Value := 'writer_pdf_Export';                    

nur verwenden, ich hab da mal die Unit weiter aufgebohrt.


Dhakiyah - Mi 25.08.10 10:45

So gings:


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
function TOLE_OpenOffice.SaveFileasPDF(sFile: String): boolean;
var  wProperties, fOpenOffice: variant;
     rueckgabe: Boolean;
begin
  try
    wProperties := VarArrayCreate([03], varVariant);
    wProperties[0] := Service.Bridge_GetStruct('com.sun.star.beans.PropertyValue');
    wProperties[0].Name := 'FilterName';
//    case Doc_Type of
//      oodtcalc    : wProperties[0].Value := 'calc_pdf_Export';
//      oodtwriter  :
      wProperties[0].Value := 'writer_pdf_Export';
//      oodtdraw    : wProperties[0].Value := 'draw_pdf_Export';
//      oodtmath    : wProperties[0].Value := 'math_pdf_Export';
//      oodtunknown : wProperties[0].Value := 'writer_pdf_Export';
//    end;


//    wProperties[1] := create_PropertyValue('CompressionMode', '1');
//    wProperties[2] := create_PropertyValue('Pages', 'All');
//    wProperties[3] := create_PropertyValue('Overwrite', TRUE);
    Document.StoreToURL(ToOOPfad(sFile), wProperties);
    rueckgabe := true;
  except
    rueckgabe := false;
  end;
  result := rueckgabe;
end;


Oder braucht man das auskommentierte?


JoelH - Mi 25.08.10 12:14

oh ich vergaß

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
function TOLE_OpenOffice.create_PropertyValue(PropName: string; PropValue: variant): variant;
begin
    Result := Service.Bridge_GetStruct('com.sun.star.beans.PropertyValue');
    Result .Name := PropName;
    Result .Value := PropValue;
end;


Darum wird das


Delphi-Quelltext
1:
2:
3:
    wProperties[1] := create_PropertyValue('CompressionMode''1');
    wProperties[2] := create_PropertyValue('Pages''All');
    wProperties[3] := create_PropertyValue('Overwrite', TRUE);


nicht funktioniert haben.

Aber wie ich festgestellt habe werden diese Properties scheinbar eh nicht mehr unterstützt, der Code ist schon ein wenig älter.