GdPicture.NET.14.API
GdPicture14 Namespace / GdPicturePDF Class / RemoveUnusedResources Method / RemoveUnusedResources() Method
Example





In This Topic
RemoveUnusedResources() Method
In This Topic
Removes all unused resources from the currently loaded PDF document in order to minimize its file size mostly after splitting or removing pages.

This method is particularly useful when you remove pages or split large PDF documents to smaller ones. Sometimes it happens that shared document resources are transferred to single pages when splitting, or they have been left unused after removing their source pages. You can simply use this method to eliminate such redundant resources and to reduce the file size if necessary.

Syntax
'Declaration
 
Public Overloads Function RemoveUnusedResources() As GdPictureStatus
public GdPictureStatus RemoveUnusedResources()
public function RemoveUnusedResources(): GdPictureStatus; 
public function RemoveUnusedResources() : GdPictureStatus;
public: GdPictureStatus RemoveUnusedResources(); 
public:
GdPictureStatus RemoveUnusedResources(); 

Return Value

A member of the GdPictureStatus enumeration. If the method has been successfully followed, then the return value is GdPictureStatus.OK.

We strongly recommend always checking this status first.

Remarks
This method is only allowed for use with non-encrypted documents.

You can find an example of usage for this method here.

Example
How to simply remove unused resources from the PDF document.
Dim caption As String = "Example: RemoveUnusedResources"
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF()
    If gdpicturePDF.LoadFromFile("test.pdf", False) = GdPictureStatus.OK Then
        Dim pageCount As Integer = gdpicturePDF.GetPageCount()
        If (gdpicturePDF.GetStat() = GdPictureStatus.OK) AndAlso
           (gdpicturePDF.GetPageCount() > 2) Then
            'Removing some pages.
            If (gdpicturePDF.DeletePage(pageCount) = GdPictureStatus.OK) AndAlso
               (gdpicturePDF.DeletePage(1) = GdPictureStatus.OK) Then
                If gdpicturePDF.RemoveUnusedResources() = GdPictureStatus.OK Then
                    If gdpicturePDF.SaveToFile("test_optimized.pdf", True) = GdPictureStatus.OK Then
                        MessageBox.Show("The example has been followed successfully.", caption)
                    Else
                        MessageBox.Show("The file can't be saved. Status: " + gdpicturePDF.GetStat().ToString(), caption)
                    End If
                Else
                    MessageBox.Show("The error occurs when reducing resources. Status: " + gdpicturePDF.GetStat().ToString(), caption)
                End If
            Else
                MessageBox.Show("No pages have been removed, the example has not been followed. Status: " + gdpicturePDF.GetStat().ToString(), caption)
            End If
        Else
            MessageBox.Show("No pages have been removed, the example has not been followed. Status: " + gdpicturePDF.GetStat().ToString(), caption)
        End If
    Else
        MessageBox.Show("The file can't be opened. Status: " + gdpicturePDF.GetStat().ToString(), caption)
    End If
End Using
string caption = "Example: RemoveUnusedResources";
using (GdPicturePDF gdpicturePDF = new GdPicturePDF())
{
    if (gdpicturePDF.LoadFromFile("test.pdf", false) == GdPictureStatus.OK)
    {
        int pageCount = gdpicturePDF.GetPageCount();
        if ((gdpicturePDF.GetStat() == GdPictureStatus.OK) &&
            (gdpicturePDF.GetPageCount() > 2))
        {
            //Removing some pages.
            if ((gdpicturePDF.DeletePage(pageCount) == GdPictureStatus.OK) &&
                (gdpicturePDF.DeletePage(1) == GdPictureStatus.OK))
            {
                if (gdpicturePDF.RemoveUnusedResources() == GdPictureStatus.OK)
                {
                    if (gdpicturePDF.SaveToFile("test_optimized.pdf", true) == GdPictureStatus.OK)
                        MessageBox.Show("The example has been followed successfully.", caption);
                    else
                        MessageBox.Show("The file can't be saved. Status: " + gdpicturePDF.GetStat().ToString(), caption);
                }
                else
                    MessageBox.Show("The error occurs when reducing resources. Status: " + gdpicturePDF.GetStat().ToString(), caption);
            }
            else
                MessageBox.Show("No pages have been removed, the example has not been followed. Status: " + gdpicturePDF.GetStat().ToString(), caption);
        }
        else
            MessageBox.Show("No pages have been removed, the example has not been followed. Status: " + gdpicturePDF.GetStat().ToString(), caption);
    }
    else
        MessageBox.Show("The file can't be opened. Status: " + gdpicturePDF.GetStat().ToString(), caption);
}
See Also