Search code examples
c#abcpdf9

ABCpdf only rendering first page


When I try to save each page as GIF using ABCpdf, only the first page is saved.

For example: I have a PDF that has 3 pages. I use ABCpdf to render each page to a stream, which is saved to disk. When I open the files in my destination folder, all 3 files show the first page content.

Here's my code:

using (Doc theDoc = new Doc())
{
    XReadOptions options = new XReadOptions { ReadModule = ReadModuleType.Pdf };
    theDoc.Read(inputbytearray, options);

    using (MemoryStream ms = new MemoryStream())
    {
        theDoc.Rendering.DotsPerInch = 150;
        int n = theDoc.PageCount;

        for (int i = 1; i <= n; i++)
        {
            Guid FileName = Guid.NewGuid();

            theDoc.Rect.String = theDoc.CropBox.String;
            theDoc.Rendering.SaveAppend = (i != 1);
            theDoc.Rendering.SaveCompression = XRendering.Compression.G4;
            theDoc.PageNumber = i;



                theDoc.Rendering.Save(string.Format("{0}.gif", FileName), ms);

                using (var streamupload = new MemoryStream(ms.GetBuffer(), writable: false))
                {
                    _BlobStorageService.UploadfromStream(FileName.ToString(), streamupload, STR_Gif, STR_Imagegif);
                }



        }
        // theDoc.Clear();
    }
}

Solution

  • The Rendering.SaveAppend property is only applicable when saving TIFF images. For GIFs you would need to save a separate image for each PDF page.

    private void button1_Click(object sender, System.EventArgs e)
                {
                    string theDir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + @"\files\";
    
                    // Create test PDF
    
                    using (Doc doc = new Doc())
                    {
                        for (int i = 1; i <= 3; i++)
                        {
                            doc.Page = doc.AddPage();
                            doc.AddHtml("<font size=24>PAGE " + i.ToString());
                        }
                        doc.Save(Path.Combine(theDir, "test.pdf"));
                    }
    
                    // Save PDF pages to GIF streams
    
                    using (Doc doc = new Doc())
                    {
                        doc.Read(Path.Combine(theDir, "test.pdf"));
                        for (int i = 1; i <= doc.PageCount; i++)
                        {
                            doc.PageNumber = i;
                            doc.Rect.String = doc.CropBox.String;
                            using (MemoryStream ms = new MemoryStream())
                            {
                                doc.Rendering.Save("dummy.gif", ms);
                                using (FileStream fs = File.Create(Path.Combine(theDir, "p" + i.ToString() + ".gif")))
                                {
                                    ms.Seek(0, SeekOrigin.Begin);
                                    ms.CopyTo(fs);
                                }
                            }
                        }
                    }
                }