In This Topic
Programming / PDF / Compressing and packing existing PDF documents

Compressing and packing existing PDF documents

In This Topic

Compressing PDF documents using GdPicture.NET is done in two steps:

  1. You need to enable compression.
  2. You need to save the PDF document with enabled packing option.
Copy Code
'We assume that GdPicture has been correctly installed and unlocked.
Dim oGdPicturePDF As New GdPicturePDF()
Dim status As GdPictureStatus
'Loading your PDF document into the GdPicturePDF object.
status = oGdPicturePDF.LoadFromFile("input.pdf", False)
If status = GdPictureStatus.OK Then
    'Enabling compression.
    oGdPicturePDF.EnableCompression(True)
    'Saving the compressed PDF document without packing.
    status = oGdPicturePDF.SaveToFile("compressed.pdf", False)
    If status <> GdPictureStatus.OK Then
        MessageBox.Show("Error occurred when saving the compressed PDF document without packing: " + oGdPicturePDF.GetStat().ToString(), "Compress and Pack Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
    'Saving the compressed PDF document with packing.
    status = oGdPicturePDF.SaveToFile("compressed_pack.pdf", True)
    If status <> GdPictureStatus.OK Then
        MessageBox.Show("Error occurred when saving the compressed PDF document with packing: " + oGdPicturePDF.GetStat().ToString(), "Compress and Pack Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
    'Disabling compression.
    oGdPicturePDF.EnableCompression(False)
    'Saving the uncompressed PDF document without packing (the largest size).
    status = oGdPicturePDF.SaveToFile("uncompressed.pdf", False)
    If status <> GdPictureStatus.OK Then
        MessageBox.Show("Error occurred when saving the uncompressed PDF document without packing: " + oGdPicturePDF.GetStat().ToString(), "Compress and Pack Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
    'Saving the uncompressed PDF document with packing.
    status = oGdPicturePDF.SaveToFile("uncompressed_pack.pdf", True)
    If status <> GdPictureStatus.OK Then
        MessageBox.Show("Error occurred when saving the uncompressed PDF document with packing: " + oGdPicturePDF.GetStat().ToString(), "Compress and Pack Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If
    'Closing the document.
    oGdPicturePDF.CloseDocument()
Else
    MessageBox.Show("The document can't be loaded. Status: " + oGdPicturePDF.GetStat().ToString(), "Compress and Pack Example", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
oGdPicturePDF.Dispose()
Copy Code
//We assume that GdPicture has been correctly installed and unlocked.
GdPicturePDF oGdPicturePDF = new GdPicturePDF();
GdPictureStatus status = default(GdPictureStatus);
//Loading your PDF document into the GdPicturePDF object.
status = oGdPicturePDF.LoadFromFile("input.pdf", false);
if (status == GdPictureStatus.OK)
{
    //Enabling compression.
    oGdPicturePDF.EnableCompression(true);
    //Saving the compressed PDF document without packing.
    status = oGdPicturePDF.SaveToFile("compressed.pdf", false);
    if (status != GdPictureStatus.OK)
    {
        MessageBox.Show("Error occurred when saving the compressed PDF document without packing: " + oGdPicturePDF.GetStat().ToString(), "Compress and Pack Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    //Saving the compressed PDF document with packing.
    status = oGdPicturePDF.SaveToFile("compressed_pack.pdf", true);
    if (status != GdPictureStatus.OK)
    {
        MessageBox.Show("Error occurred when saving the compressed PDF document with packing: " + oGdPicturePDF.GetStat().ToString(), "Compress and Pack Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    //Disabling compression.
    oGdPicturePDF.EnableCompression(false);
    //Saving the uncompressed PDF document without packing (the largest size).
    status = oGdPicturePDF.SaveToFile("uncompressed.pdf", false);
    if (status != GdPictureStatus.OK)
    {
        MessageBox.Show("Error occurred when saving the uncompressed PDF document without packing: " + oGdPicturePDF.GetStat().ToString(), "Compress and Pack Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    //Saving the uncompressed PDF document with packing.
    status = oGdPicturePDF.SaveToFile("uncompressed_pack.pdf", true);
    if (status != GdPictureStatus.OK)
    {
        MessageBox.Show("Error occurred when saving the uncompressed PDF document with packing: " + oGdPicturePDF.GetStat().ToString(), "Compress and Pack Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    //Closing the document.
    oGdPicturePDF.CloseDocument();
}
else
{
    MessageBox.Show("The document can't be loaded. Status: " + oGdPicturePDF.GetStat().ToString(), "Compress and Pack Example", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
oGdPicturePDF.Dispose();