AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

PDF Creator innerhalb Delphi nutzen

Ein Thema von Cogito · begonnen am 18. Jun 2010 · letzter Beitrag vom 17. Mai 2016
Antwort Antwort
squetk

Registriert seit: 29. Aug 2004
Ort: Cottbus
118 Beiträge
 
Delphi XE2 Professional
 
#1

AW: PDF Creator innerhalb Delphi nutzen

  Alt 1. Jul 2010, 18:39
Folgender im Netz gefundener Sourcecode als Beispiel für eine späte Bindung funktioniert:
Delphi-Quellcode:
unit uPDFCreator;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ActiveX, ComObj, Mask, JvExMask, JvToolEdit;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    ButtonExecute : TButton;
    procedure ButtonExecuteClick(Sender: TObject);
  private
    PDFCreator, MSWord: OleVariant;
    optUseAutosave, optUseAutoSaveDirectory, optAutosaveFormat,
    optPDFColorsColorModel: Integer;
    optAutosaveDirectory, optAutosaveFilename: String;
    // Security Options
    optPDFUseSecurity, optPDFHighEncryption, optPDFOwnerPass: Integer;
    optPDFDisallowCopy, optPDFDisallowModifyAnnotations,
    optPDFDisallowModifyContents, optPDFDisallowPrinting: Integer;
    optPDFAllowAssembly, optPDFAllowDegradedPrinting, optPDFAllowFillIn,
    optPDFAllowScreenReaders: Integer;
    optPDFOwnerPasswordString: String;
    procedure StorePDFOptions;
    procedure SetPDFOptions(const filename, ownerpass: String);
    procedure RestorePDFOptions;
  public

  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ButtonExecuteClick(Sender: TObject);
const
   output_filename: String = 'D:\test.pdf';
   owner_password: String = '';
var
   c, printJobCount: Integer;
   optPrintBackground: Boolean;
begin
   PDFCreator := CreateOLEObject('PDFCreator.clsPDFCreator');

   PDFCreator.cStart('/NoProcessingAtStartup');

   StorePDFOptions;
   SetPDFOptions(output_filename, owner_password);

   MSWord := CreateOleObject('Word.Application');
   try
      MSWord.DisplayAlerts := 0;

      MSword.Documents.Open(Edit1.Text);

      MSWord.ActiveDocument.TrackRevisions := false;
      optPrintBackground := MSWord.Options.PrintBackground;
      if not optPrintBackground then
         MSWord.Options.PrintBackground := True;
      MSWord.ActiveWindow.View.ShowRevisionsAndComments := False;
      MSWord.Options.WarnBeforeSavingPrintingSendingMarkup := false;

      printJobCount := PDFCreator.cCountOfPrintJobs;
      /// ------------------------------------------
      /// Have PDFCreator wait for pages
      PDFCreator.cPrinterStop := true;

      MSWord.ActiveDocument.PrintoutOld;

      /// we don't want to continue on right after telling word to print, because it can cause problems
      while MSWord.BackgroundPrintingStatus <> 0 do
      begin
        Sleep(10);
        Application.ProcessMessages;
      end;

      /// Tell PDFCreator we're done printing pages.
      PDFCreator.cPrinterStop := false;
      /// ------------------------------------------

      /// now wait for PDFCreator to finish
      c := PDFCreator.cCountOfPrintJobs;
      while c > printJobCount do
      begin
         Sleep(10);
         Application.ProcessMessages;
         c := PDFCreator.cCountOfPrintJobs;
      end;

      MSword.ActiveDocument.Close(SaveChanges := 0);
   finally
      VarClear(MSWord);
   end;

   RestorePDFOptions;

   PDFCreator.cClose;

   VarClear(PDFCreator);

end;

procedure TForm1.RestorePDFOptions;
begin
   PDFCreator.cOption('UseAutosave') := optUseAutosave;
   PDFCreator.cOption('UseAutosaveDirectory') := optUseAutosaveDirectory;
   PDFCreator.cOption('AutosaveDirectory') := optAutosaveDirectory;
   PDFCreator.cOption('AutosaveFilename') := optAutosaveFilename;
   PDFCreator.cOption('AutosaveFormat') := optAutosaveFormat;
   PDFCreator.cOption('PDFColorsColorModel') := optPDFColorsColorModel;

   // security options
   PDFCreator.cOption('PDFUseSecurity') := optPDFUseSecurity;
   PDFCreator.cOption('PDFHighEncryption') := optPDFHighEncryption;
   PDFCreator.cOption('PDFOwnerPass') := optPDFOwnerPass;
   PDFCreator.cOption('PDFOwnerPasswordString') := optPDFOwnerPasswordString;
   PDFCreator.cOption('PDFDisallowCopy') := optPDFDisallowCopy;
   PDFCreator.cOption('PDFDisallowModifyAnnotations') := optPDFDisallowModifyAnnotations;
   PDFCreator.cOption('PDFDisallowModifyContents') := optPDFDisallowModifyContents;
   PDFCreator.cOption('PDFDisallowPrinting') := optPDFDisallowPrinting;
   PDFCreator.cOption('PDFAllowAssembly') := optPDFAllowAssembly;
   PDFCreator.cOption('PDFAllowDegradedPrinting') := optPDFAllowDegradedPrinting;
   PDFCreator.cOption('PDFAllowFillIn') := optPDFAllowFillIn;
   PDFCreator.cOption('PDFAllowScreenReaders') := optPDFAllowScreenReaders;

   PDFCreator.cSaveOptions;

   Sleep(100);
end;

procedure TForm1.SetPDFOptions(const filename, ownerpass: String);
begin
   // set the options we want, auto-save PDF with specific filename
   PDFCreator.cOption('UseAutosave') := 1;
   PDFCreator.cOption('UseAutosaveDirectory') := 1;
   PDFCreator.cOption('AutosaveDirectory') := ExtractFilePath(filename);
   PDFCreator.cOption('AutosaveFilename') := ExtractFileName(filename);
   PDFCreator.cOption('AutosaveFormat') := 0; // PDF format
   PDFCreator.cOption('PDFColorsColorModel') := 0; // RGB format

   if Trim(ownerpass) <> 'then
   begin
      PDFCreator.cOption('PDFUseSecurity') := 1;
      PDFCreator.cOption('PDFHighEncryption') := 1;
      PDFCreator.cOption('PDFOwnerPass') := 1;
      PDFCreator.cOption('PDFOwnerPasswordString') := ownerpass;
      PDFCreator.cOption('PDFDisallowCopy') := 0;
      PDFCreator.cOption('PDFDisallowModifyAnnotations') := 1;
      PDFCreator.cOption('PDFDisallowModifyContents') := 1;
      PDFCreator.cOption('PDFDisallowPrinting') := 0;
      PDFCreator.cOption('PDFAllowAssembly') := 0;
      PDFCreator.cOption('PDFAllowDegradedPrinting') := 0;
      PDFCreator.cOption('PDFAllowFillIn') := 0;
      PDFCreator.cOption('PDFAllowScreenReaders') := 0;
   end;
   PDFCreator.cSaveOptions;
end;

procedure TForm1.StorePDFOptions;
begin
   // save the current options to put back when we're finished.
   optUseAutosave := PDFCreator.cOption['UseAutosave'];
   optUseAutosaveDirectory := PDFCreator.cOption['UseAutosaveDirectory'];
   optAutosaveDirectory := PDFCreator.cOption['AutosaveDirectory'];
   optAutosaveFilename := PDFCreator.cOption['AutosaveFilename'];
   optAutosaveFormat := PDFCreator.cOption['AutosaveFormat'];
   optPDFColorsColorModel := PDFCreator.cOption['PDFColorsColorModel'];

   // security options
   optPDFUseSecurity := PDFCreator.cOption['PDFUseSecurity'];
   optPDFHighEncryption := PDFCreator.cOption['PDFHighEncryption'];
   optPDFOwnerPass := PDFCreator.cOption['PDFOwnerPass'];
   optPDFOwnerPasswordString := PDFCreator.cOption['PDFOwnerPasswordString'];
   optPDFDisallowCopy := PDFCreator.cOption['PDFDisallowCopy'];
   optPDFDisallowModifyAnnotations := PDFCreator.cOption['PDFDisallowModifyAnnotations'];
   optPDFDisallowModifyContents := PDFCreator.cOption['PDFDisallowModifyContents'];
   optPDFDisallowPrinting := PDFCreator.cOption['PDFDisallowPrinting'];
   optPDFAllowAssembly := PDFCreator.cOption['PDFAllowAssembly'];
   optPDFAllowDegradedPrinting := PDFCreator.cOption['PDFAllowDegradedPrinting'];
   optPDFAllowFillIn := PDFCreator.cOption['PDFAllowFillIn'];
   optPDFAllowScreenReaders := PDFCreator.cOption['PDFAllowScreenReaders'];
end;

end.
Ich hätte aber gern die frühe Bindung eingesetzt (wie z.B. im PDFCreator COM Sample für C# gezeigt), leider funktioniert dieses nicht, wenn ich es in Delphi umsetze. Der Vorteil wäre, dass man dann auf Ereignisse wie OnError und OnReady reagieren könnte.

Eins noch: Der o.a. Quellcode funktionierte bei mir erst dann, als ich den PDFCreator-Druckertreiber auf Direktdruck gestellt habe. Kenn jemand eine Möglichkeit, dies vom Programmcode aus einzustellen?
  Mit Zitat antworten Zitat
inca

Registriert seit: 23. Jul 2011
4 Beiträge
 
#2

AW: PDF Creator innerhalb Delphi nutzen

  Alt 27. Jul 2011, 18:27
Hm, bei mir funktioniert es nicht. Ich kann noch nicht einmal programmseitig, also von Delphi aus, den Namen der pdf-Datei festlegen....
  Mit Zitat antworten Zitat
omata

Registriert seit: 26. Aug 2004
Ort: Nebel auf Amrum
3.154 Beiträge
 
Delphi 7 Enterprise
 
#3

AW: PDF Creator innerhalb Delphi nutzen

  Alt 28. Jul 2011, 00:30
Vielleicht hilft etwas von hier weiter.
  Mit Zitat antworten Zitat
inca

Registriert seit: 23. Jul 2011
4 Beiträge
 
#4

AW: PDF Creator innerhalb Delphi nutzen

  Alt 28. Jul 2011, 08:39
Danke erstmal für die Antwort. Ich weiß nicht, ob ich dich richtig verstanden habe, aber ich habe nur beim Arbeiten mit dem pdfCreator das Problem, dass der filename nicht vergeben werden kann. Der Codeschnipsel hat bei mir keine Auswirkungen. Der pdfcreator arbeitet an sich einwandfrei, ich kann pdfs drucken, habe auch einen ähnlichen Code wie in dem von dir beschriebenen link laufen, alles korrekt. Aber der pdfcreator hört nicht auf meine filename-Zuweisung. Ausgegeben werden immer pdf-Dateien, deren Format etwa so ausschaut. 20110728093845.pdf. Also Datum zuerst und dann ein Rest. Hat jemand vielleicht noch einen Rat?!?
  Mit Zitat antworten Zitat
Jumpy

Registriert seit: 9. Dez 2010
Ort: Mönchengladbach
1.740 Beiträge
 
Delphi 6 Enterprise
 
#5

AW: PDF Creator innerhalb Delphi nutzen

  Alt 28. Jul 2011, 09:09
Ich hab das mal in Excel nach der folgenden Anleitung gemacht:

http://www.excelguru.ca/node/21

Da stand auch was zu den Settings:

http://www.excelguru.ca/node/37

OK. Ist VBA aber die Idee dahinter sollte dieselbe sein.


Was den Filename angeht, kann man (wenn ich mich recht erinnere) irgendwo in den Programmeinstellungen einstellen, das der einen automatischen Namen vergeben soll und dann ist es egal, was man bei filename angibt, das wird ignoriert. Keine Ahnung, wie man das per Programmierung ausstellt, doch kann man das ja testweise auch manuell im PDFCreator selbst machen. Einmal geändert, müsste die Einstellung erhalten bleiben.
Ralph
  Mit Zitat antworten Zitat
Benutzerbild von rweinzierl
rweinzierl

Registriert seit: 22. Mär 2005
98 Beiträge
 
#6

AW: PDF Creator innerhalb Delphi nutzen

  Alt 28. Jul 2011, 09:16
Hallo

Vielleicht hilft dir das weiter , im pdfCreator auf Autospeichern stellen und dann von Delphi aus die Pfade in die registry schreiben ==> Dann druckt er in die gewünschte Datei

mfg

Reinhold


var
reg: TRegistry;
begin
result := false;

reg := TRegistry.create;
reg.RootKey := HKEY_CURRENT_USER;
if reg.OpenKey('\Software\PDFCreator\Program',false) then
begin
reg.LazyWrite := false;
reg.WriteString('AutoSaveDirectory',extractfilepat h(Pdf_Dateiname));
reg.WriteString('AutoSaveFilename',extractfilename (Pdf_Dateiname));
reg.WriteString('UseAutosave','1');
result := true;
reg.CloseKey;
sleep(10);
end;
reg.Free;
  Mit Zitat antworten Zitat
inca

Registriert seit: 23. Jul 2011
4 Beiträge
 
#7

AW: PDF Creator innerhalb Delphi nutzen

  Alt 24. Aug 2011, 17:52
Hallo rweinzierl,

danke für die Antwort. Ich habe es ausprobiert, aber der Code hat keine Auswirkung. Der Name wird weiterhin vom Creator festgelegt, der Pfad auch, egal was ich versuche.

Hast du den Codeschnipsel ausprobiert und bei dir zum Laufen gebracht? Fände ich jetzt seeeeehr interessant ...

LG
Inca
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 10:45 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz