Back to Blog

    Developer Blog

    Manipulate content streams in PDF Files using Quick PDF Library

    Foxit Quick PDF Library

    Foxit Quick PDF Library supports advanced programmatic manipulation of content streams in PDF files.

    What are Content Streams?

    A page in a PDF document has one or more content stream parts that together contain all the PDF page description commands for the page.

    A lot of PDF files only have one content stream but some PDF files have multiple content streams.

    Content streams are layered bottom to top. So text or image objects located on the bottom content stream will be hidden completely or partially by other objects that belong to the content stream above the bottom content stream.

    New Content Streams

    The NewContentStreams function can be used to add new content streams into a PDF file.

    Change Order of Content Streams

    The NewContentStreams function can be used to add new content streams into a PDF file.

    Combine Content Streams

    Use the CombineContentStreams function to combine all content streams for the selected page into one content stream.

    /* Combine all content streams for each page in a PDF */
    
    // Load PDF to process
    DPL.LoadFromFile("example.pdf", "");
    
    // Count pages
    int xPageCount = DPL.PageCount();
    
    // Go through each page and combine content streams
    for (int i = 1; i <= xPageCount; i++)
    {
        DPL.SelectPage(i);
        DPL.CombineContentStreams();
    }
    
    // Save the updated file
    DPL.SaveToFile("example_updated.pdf");
    

    Remove Shared Content Streams

    Use the RemoveSharedContentStreams function to ensure that none of the pages in the selected document share any content streams.

    /* Remove shared content streams */ 
    
    // Load the PDF
    DPL.LoadFromFile("example.pdf", "");
    
    // Remove shared content streams
    DPL.RemoveSharedContentStreams();
    
    // Save the updated PDF
    DPL.SaveToFile("example_updated.pdf");
    

    Encapsulate Content Streams

    Use the EncapsulateContentStream function to surround the current content stream with “save graphics state” and “restore graphics state” operators.

    /* Encapsulate content streams in a PDF */ 
    
    // Load the PDF
    DPL.LoadFromFile("example.pdf", "");
    
    // Count pages
    xPageCount = DPL.PageCount();
    
    // Go through each page and encapsulate content streams
    for (int i = 1; i <= xPageCount; i++)
    {
        DPL.SelectPage(i);
        int xContentStreamCount = DPL.ContentStreamCount();
    
        for (int x = 1; x <= xContentStreamCount; x++)
        {
            DPL.SelectContentStream(x);
            DPL.EncapsulateContentStream();
        }
    }
    
    // Save the updated file
    DPL.SaveToFile("example_updated.pdf");
    

    Normalize Page Content Rotation

    Use the NormalizePage function to automatically move and/or rotate the contents of the page so that subsequent drawing operations are at the expected position on the page. All the page boundary boxes are adjusted to the physical size of the page and the page’s rotation attribute is reset to zero.

    /* Normalize the page content rotation */
    DPL.LoadFromFile("Content_Rotation_Problems.pdf", "");
    DPL.NormalizePage(0);
    DPL.DrawText(284, 511, "Testing text");
    DPL.SaveToFile("Rotation_Problem_Fixed.pdf");
    

    This article refers to a deprecated product. If you are looking for support for Foxit PDF SDK, please click here.