Free Support Forum - aspose.com

Error adding blank pages to PDF


#1

Hi!

I’m using Aspose.PDF for .NET, version 19.5.0.0.
I want to convert TIFF to PDF so I’m adding pages and add the file to them. Since the PDFs I’m trying to use are compressed in Old-Style JPEG and the System library doesn’t support them, I’m using Magick.NET to create a new image but with the new JPEG compression, then add that one into the PDF page.

But I get this error:
image.png (93.1 KB)

My code is:

            public void TifToPdf3(string dest, string[] e) {  

            MagickReadSettings magickReadSettings = new MagickReadSettings();
            magickReadSettings.Compression = ImageMagick.CompressionMethod.JPEG;
            magickReadSettings.Format = MagickFormat.Tiff;

            for (int i = 0; i < e.Length; i++)
            {
                bool isError = false;
                try
                {                    
                    using (MagickImageCollection collection = new MagickImageCollection(e[i], magickReadSettings))
                    {
                        string pastatif = AppDomain.CurrentDomain.BaseDirectory + "temporario\\" + Path.GetFileNameWithoutExtension(e[i]);
                        if (!Directory.Exists(pastatif)) Directory.CreateDirectory(pastatif);

                        Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
                        // Add a page to pages collection of document

                        for (int y = 0; y < collection.Count; y++)
                        {
                            Aspose.Pdf.Page page = doc.Pages.Add();
                            int activePage = y + 1;
                            string file = pastatif + "\\" + Path.GetFileNameWithoutExtension(e[i]) + " -Pagina " + activePage + ".tif";
                            MagickImage image = new MagickImage(collection[y]);
                            image.Quality = 50;
                            image.Format = MagickFormat.Tiff;
                            image.Write(file, MagickFormat.Tif);

                            FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
                            byte[] tmpBytes = new byte[fs.Length];
                            fs.Read(tmpBytes, 0, int.Parse(fs.Length.ToString()));

                            MemoryStream mystream = new MemoryStream(tmpBytes);
                            Bitmap bm = new Bitmap(file);

                            page.PageInfo.Margin.Bottom = 0;
                            page.PageInfo.Margin.Top = 0;
                            page.PageInfo.Margin.Left = 0;
                            page.PageInfo.Margin.Right = 0;

                            page.CropBox = new Aspose.Pdf.Rectangle(0, 0, bm.Width, bm.Height);
                            Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();
                            page.Paragraphs.Add(image1);
                            image1.ImageStream = mystream;
                            mystream.Close();
                        }

                        doc.Save(dest + Path.GetFileNameWithoutExtension(e[i]) + ".PDF");
                    }
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "Erro", MessageBoxButtons.OK);
                    isError = true;
                }
                if (isError) continue;
            }
        }

#2

@SofiaRodrigues25

Thank you for contacting support.

You are currently facing evaluation limitations. However, you can test the API in its full capacity by applying a free 30-days temporary license. Once you complete evaluation process, you may purchase the license to keep using APIs features.

In case you face any issue, please feel free to let us know.


#3

Thank you for you reply.