Atalasoft
Welcome to Atalasoft Community Sign in | Join | Help
in

Out of memory when converting tiff to pdf

Last post 28 Aug 2008, 8:45 AM by loufranco. 1 replies.
Sort Posts: Previous Next
  •  21 Aug 2008, 3:47 PM 15326

    Out of memory when converting tiff to pdf

    My application pulls individual Tiff files out of a SQL Server DB and builds either a multipage Tiff or PDF file depending on the user's selection. The user views the files using Acrobat Reader or the MS Document Imaging Tool. Some of these files can end up being a 1000+ pages. The problem that I'm having is that the Tiff file will build and display while its PDF equivalent dies with a Failed to Allocate Pixel Memory error. In my current case, the file I'm trying to build has 668 pages total. The individual Tiff files making up this multipage file are appx. 200 kb in filesize, 6000x4000 pixels, 1bppIndexed, resolutions of 400/400/undefined. By the time I've added page 405 to the pdf collection the error message occurs.  Am I doing something wrong here when converting the Tiff files to PDF?

    public System.IO.MemoryStream GetMultipagePdf(){
    PdfImageCollection col = new PdfImageCollection();
    PdfEncoder pdfOut = new PdfEncoder();
    MemoryStream msOut = new MemoryStream();
    AtalaImage image = null;

    int
    sheetcount = this.GetSheets().Count;
    if (sheetcount > 0)
    {
    foreach (Sheet _Sheet in this.GetSheets())
    {
    Sheet sheet = Sheet.FromGuid(_Sheet.Guid);
    if (_Sheet.FileExtension.ToLower() == "tif")
    {
    image =
    AtalaImage.FromByteArray(sheet.FileContent);
    col.Add(new PdfImage(image , PdfCompressionType.CcittGroup4));
    }

    pdfOut.Save(msOut, col, null);
    msOut.Seek(0,
    SeekOrigin.Begin);
    image.Dispose();

    foreach (PdfImage img in col)
    {
    img.Image.Dispose();
    }

    if (msOut.Length > 0)
    return msOut;
    else
    return null;
    }
    else
    {
    throw new Exception("There are no sheets associated to DrawingID " + this._Drawing.DrawingID);
    }
    }

     

    Filed under: , ,
  •  28 Aug 2008, 8:45 AM 15376 in reply to 15326

    Re: Out of memory when converting tiff to pdf

    AtalaImage.FromByteArray() makes a copy of the bytes.  You still need to dispose your copy or get it out of memory.

    Also, the pdf collection needs to be made before it is saved -- and in this case that means that all of the AtalaImage's will need to be in memory. 

    And, since you are saving to a memorystream, the entire PDF will be in memory -- it's much better to build up the PDF into a FileStream when it's this big.

    I would suggest making a TIFF (which allows appending and doesn't keep the image in memory after it's written).

    Then you can use the code in this KB

        http://www.atalasoft.com/kb/article.aspx?id=10125

    To efficiently convert the TIFF from a PDF using minimal memory

    If you need more help with this, please open a support case http://www.atalasoft.com/support

     

View as RSS news feed in XML