Reference Guide
GdPicture14 Namespace / GdPicturePDF Class / RemoveUnusedResources Method
Example





In This Topic
    RemoveUnusedResources Method (GdPicturePDF)
    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 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
    Please note that 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 oGdPicturePDF As GdPicturePDF = New GdPicturePDF()
        If oGdPicturePDF.LoadFromFile("test.pdf", False) = GdPictureStatus.OK Then
            Dim pageCount As Integer = oGdPicturePDF.GetPageCount()
            If (oGdPicturePDF.GetStat() = GdPictureStatus.OK) AndAlso
               (oGdPicturePDF.GetPageCount() > 2) Then
                'Removing some pages.
                If (oGdPicturePDF.DeletePage(pageCount) = GdPictureStatus.OK) AndAlso
                   (oGdPicturePDF.DeletePage(1) = GdPictureStatus.OK) Then
                    If oGdPicturePDF.RemoveUnusedResources() = GdPictureStatus.OK Then
                        If oGdPicturePDF.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: " + oGdPicturePDF.GetStat().ToString(), caption)
                        End If
                    Else
                        MessageBox.Show("The error occurs when reducing resources. Status: " + oGdPicturePDF.GetStat().ToString(), caption)
                    End If
                Else
                    MessageBox.Show("No pages have been removed, the example has not been followed. Status: " + oGdPicturePDF.GetStat().ToString(), caption)
                End If
            Else
                MessageBox.Show("No pages have been removed, the example has not been followed. Status: " + oGdPicturePDF.GetStat().ToString(), caption)
            End If
        Else
            MessageBox.Show("The file can't be opened. Status: " + oGdPicturePDF.GetStat().ToString(), caption)
        End If
    End Using
    string caption = "Example: RemoveUnusedResources";
    using (GdPicturePDF oGdPicturePDF = new GdPicturePDF())
    {
        if (oGdPicturePDF.LoadFromFile("test.pdf", false) == GdPictureStatus.OK)
        {
            int pageCount = oGdPicturePDF.GetPageCount();
            if ((oGdPicturePDF.GetStat() == GdPictureStatus.OK) &&
                (oGdPicturePDF.GetPageCount() > 2))
            {
                //Removing some pages.
                if ((oGdPicturePDF.DeletePage(pageCount) == GdPictureStatus.OK) &&
                    (oGdPicturePDF.DeletePage(1) == GdPictureStatus.OK))
                {
                    if (oGdPicturePDF.RemoveUnusedResources() == GdPictureStatus.OK)
                    {
                        if (oGdPicturePDF.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: " + oGdPicturePDF.GetStat().ToString(), caption);
                    }
                    else
                        MessageBox.Show("The error occurs when reducing resources. Status: " + oGdPicturePDF.GetStat().ToString(), caption);
                }
                else
                    MessageBox.Show("No pages have been removed, the example has not been followed. Status: " + oGdPicturePDF.GetStat().ToString(), caption);
            }
            else
                MessageBox.Show("No pages have been removed, the example has not been followed. Status: " + oGdPicturePDF.GetStat().ToString(), caption);
        }
        else
            MessageBox.Show("The file can't be opened. Status: " + oGdPicturePDF.GetStat().ToString(), caption);
    }
    See Also