Load and Save

<< Click to Display Table of Contents >>

Navigation:  Tasks >

Load and Save

WPViewPDF can load the PDF from file and from stream.

 

Load a file. If an error happens return false, otherwise true.

 

function LoadFromFile(const filename: string): Boolean;

 

Load file completely - close the file stream afterwards.

 

function LoadFromFileAsCopy(const filename: string): Boolean;

 

Append a file to the currently loaded.

 

function AppendFromFile(const filename: string): Boolean;

 

Load PDF from a stream. Optionally clear the already loaded data.

 

function LoadFromStream(Stream: TStream ; WithClear : Boolean = false): Boolean;

 

Attach a stream - the stream will be used while the PDF is accessed.

 

function AttachStream(Stream: TStream): Boolean;

 

 

WPViewPDF PLUS can also save the PDF data

 

Hint: If the SaveToFile or CopyToClipboard function does not work for you, please check the setting of property SecurityOptions!

 

The flag wpDisableSave must not be set - if it is set once, saving cannot be enabled again!

 

Using the compiler switch IGNORE_SECOPT_IN_DFM it is possible with Delphi to disable that the property SecurityOptions is loaded from the DFM data. This makes it possible to set the property later in code.

 

 

It is also possible to save in RTF, TXT, XML and HTML format! (TWPViewPDF.GetPageText Method)

 

This methods are located in the sub interface "Plus"

 

function SaveToFile(const filename: WideString): WordBool;

 

function SaveSelectionToStream(Stream: TStream; FileExt : AnsiString = ''): WordBool;

 

Hint: To save only certain pages as PDF without having to use a selection use arrange form-to. The numbers are 1 based to make it easier provide a user interface.

    SaveSelectionToStream(stream, 'from-to;PDF')

 

function SaveToStream(Stream: TStream; FileExt : AnsiString = ''): WordBool;

 

Note: The save function does not reset the stream, so in case you reuse a memory stream you need to call memory.Clear first.

 

 

Also see "Load PDF" and "Save PDF" commands.

 

function CheckOwnerPassword can be used to pass an owner password to lift save restrictions. TRUE is returned if the password was accepted.

 

function MaySave can be used to check if the PDF file may be saved.

 

Please note: If security settings of a PDF file forbid saving, the component will not save. You as developer can override this at Your own risk. Use Command(COMPDF_DisableSecurityOverride,1) to disable this check.

 

(Note: PDF which use 256 bits AES encryption may never be saved unless the owner password was specified)

 

If your Delphi APP cannot save a PDF file, please check if you use the flag wpDisableSave in the property SecurityOptions. Please note, that once wpDisableSave was used, it cannot be enabled again to prevent hacking the application. The Security options can also be set with commands.

 

When extracting text from a PDF file WPViewPDF will first sort the text element using their horizontal coordinate. This can be switched off using COMPDF_TextExtractDontSort.

 

It is possible to disable saving using command(COMPDF_DisableSave). It is not possible to enable it again.

 

If your application allows fast scrolling between files we I suggest to use PostMessage to uncouple the update of the viewer from the scrolling. So the users can scroll fast but the viewer does not have to load a new file each time.

 

When you save to a PDF file you can use COMPDF_SetSaveMode = 613 to remove certain PDF document elements the next time PDF is saved.

 

Parameter of COMPDF_SetSaveMode can be this bit values:

 1: Remove the Annots except for Hyperlinks. Ommits "AcroForm"

 2: Remove the Hyperlinks

 4: Remove the Bookmarks

 8: Remove the StructElements

 16: Remove Transition Effects

 32: remove Page AA Actions

 64: remove PDFA flag

 128: Delete Extra XML Data

 256: Delete extra commands (such as images and DrawObjects)

 512: Delete named destinations

 1024: DO not create PDF A Marker

 2048: DO always create PDF A Marker

 4096: Do not save modified page sizes

 8192: Never write Cropbox parameter

 

 8192*2:   Delete extra commands (such as images and DrawObjects)

 8192*4:   Delete named destinations

 8192*8:   DO not create PDF A Marker

 8192*16:  DO always create PDF A Marker

 8192*32:  Don't modify page Size

8192*64:  Flatten the PDF on save time. This requires that proper Appearance streams are present for the Fields.

 8192*128:  Delete Metadata

 8192*256:  write "NeedAppearances=true"

 8192*512:  write "NeedAppearances=false"

 8192*1024: Save all fonts found on a page, not just the ones which are used.

 

 

When saving a PDF with WPViewPDF PLUS it is possible to reduce or enlarge the page size.

 

This feature is controlled by 3 command ids:

 

COMPDF_SaveScaledPDFMode = 617   - Activate the scaling mode while saving

  0: wpNoScaling = OFF

  1: wpScaleToWH = Use desired Width/H as exact value

  2: wpShrinkToWH = Use desired Width/H as MAXIMUM value

  3: wpScaleToWHIgnoreAspcect = Use desired Width/H as exact value and override aspect ratio

  4: wpScalePerThousand  = Use scaling factor DesiredWidth/1000

 

COMPDF_SaveScaledPDFSetX = 618 - This is either the desired page width in pt (=1/72 inch), or the horizontal page scaling in 1/1000

COMPDF_SaveScaledPDFSetY = 619 - This is either the desired page height in pt, or the horizontal page scaling in 1/1000

 

Please note that only the page content and the basis rectangle of annotations will be scaled.

 

Example:

 pdf.command(COMPDF_SaveScaledPDFMode4); // wpScalePerThousand

 try

    // Skaliere von DinA4 auf DinA5

    pdf.command(COMPDF_SaveScaledPDFSetX, round(1/sqr(2)*1000));

    pdf.command(COMPDF_SaveScaledPDFSetY, round(1/sqr(2)*1000));

    pdf.Plus.SaveToFile(SaveDialog1.FileName);

 finally

    pdf.command(COMPDF_SaveScaledPDFMode, 0);

 end;

 

When using Delphi you can use the method ActivateScaledPDFWriting to enable the scaling.