Free Support Forum - aspose.com

Aspose 17.9 Sections / Pages/ Margins: Conversion of old code

Hi,

I need to refactor the following code to Aspose.PDF versie 17.9. I’m having difficulties with the sections and concept pages where here there is no such thing as a page. Just sections. Any advice how to proceed with some code samples how to convert?

public class ImagingConverter
{
    /// <summary>
    /// Create a PDF file containing all image files
    /// </summary>
    /// <param name="fileNames">image files, suppored .jpg, .png, .gif, .bmp, .tif</param>
    /// <param name="pdfFileName">PDF file name</param>
    public static void CreatePDF(List<String> fileNames, String pdfFileName)
    {
        using (DisposeHelper dispHelper = new DisposeHelper())
        {
            var pdfCreator = new Pdf();
            foreach (String fileName in fileNames)
            {
                FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); // do not dispose before saving the PDF
                dispHelper.Add(fs);

                using (System.Drawing.Image image = System.Drawing.Image.FromStream(fs))
                {
                    //Create a section in the Pdf object
                    Aspose.Pdf.Generator.Section section = pdfCreator.Sections.Add();

                    //Assign the marginInfo instance to Margin property of sec1.PageInfo
                    var marginInfo = new MarginInfo { Top = 0, Bottom = 0, Left = 0, Right = 0 };
                    section.PageInfo.Margin = marginInfo;
                    section.PageInfo.PageBorderMargin = marginInfo;

                    // page size (in points, 1 inch = 72 points) equals image size
                    section.PageInfo.PageWidth = (image.Width / image.HorizontalResolution) * 72;
                    section.PageInfo.PageHeight = (image.Height / image.VerticalResolution) * 72;


                    //Add image object into the Paragraphs collection of the section
                    var pdfImage = new Aspose.Pdf.Generator.Image(section);
                    switch (Path.GetExtension(fileName).ToLower())
                    {
                        case ".jpeg":
                        case ".jpg":
                            pdfImage.ImageInfo.ImageFileType = ImageFileType.Jpeg;
                            break;
                        case ".png":
                            pdfImage.ImageInfo.ImageFileType = ImageFileType.Png;
                            break;
                        case ".gif":
                            pdfImage.ImageInfo.ImageFileType = ImageFileType.Gif;
                            break;
                        case ".bmp":
                            pdfImage.ImageInfo.ImageFileType = ImageFileType.Bmp;
                            break;
                        case ".tif":
                        case ".tiff":
                            {
                                pdfImage.ImageInfo.ImageFileType = ImageFileType.Tiff;
                                pdfImage.ImageInfo.TiffFrame = -1;
                            }
                            break;
                        default:
                            throw new Exception("File " + fileName + "unsupported image type.");
                    }

                    if (section.PageInfo.PageWidth > Aspose.Pdf.Generator.PageSize.A4Width
                        || section.PageInfo.PageHeight > Aspose.Pdf.Generator.PageSize.A4Height)
                    {
                        // rescale image
                        pdfImage.ImageScale = Math.Min(Aspose.Pdf.Generator.PageSize.A4Width / section.PageInfo.PageWidth
                            , Aspose.Pdf.Generator.PageSize.A4Height / section.PageInfo.PageHeight);
                        section.PageInfo.PageWidth *= pdfImage.ImageScale;
                        section.PageInfo.PageHeight *= pdfImage.ImageScale;
                    }

                    fs.Seek(0, SeekOrigin.Begin);
                    pdfImage.ImageInfo.ImageStream = fs;
                    section.Paragraphs.Add(pdfImage);
                }
            }
            pdfCreator.Save(pdfFileName);
        }
    }

    public static void CreatePDF(List<Stream> fileStreams, String fileExtension, String pdfFileName)
    {
        using (FileStream fileStream = new FileStream(pdfFileName, FileMode.OpenOrCreate))
        {
            CreatePDF(fileStreams, fileExtension, fileStream);
        }
    }

    public static void CreatePDF(List<Stream> fileStreams, String fileExtension, Stream pdfStream)
    {
        var pdfCreator = new Pdf();

        // get file type of all streams
        ImageFileType imageFileType;
        switch (fileExtension.ToLower())
        {
            case ".jpeg":
            case ".jpg":
                imageFileType = ImageFileType.Jpeg;
                break;
            case ".png":
                imageFileType = ImageFileType.Png;
                break;
            case ".gif":
                imageFileType = ImageFileType.Gif;
                break;
            case ".bmp":
                imageFileType = ImageFileType.Bmp;
                break;
            case ".tif":
            case ".tiff":
                imageFileType = ImageFileType.Tiff;
                break;
            default:
                throw new Exception("File extension '" + fileExtension + "' unsupported image type.");
        }

        // add all
        foreach (Stream fileStream in fileStreams)
        {
            using (System.Drawing.Image image = System.Drawing.Image.FromStream(fileStream))
            {
                //Create a section in the Pdf object
                Aspose.Pdf.Generator.Section section = pdfCreator.Sections.Add();

                //Assign the marginInfo instance to Margin property of sec1.PageInfo
                var marginInfo = new MarginInfo { Top = 0, Bottom = 0, Left = 0, Right = 0 };
                section.PageInfo.Margin = marginInfo;
                section.PageInfo.PageBorderMargin = marginInfo;

                // page size (in points, 1 inch = 72 points) equals image size
                section.PageInfo.PageWidth = (image.Width / image.HorizontalResolution) * 72;
                section.PageInfo.PageHeight = (image.Height / image.VerticalResolution) * 72;


                //Add image object into the Paragraphs collection of the section
                var pdfImage = new Aspose.Pdf.Generator.Image(section);

                pdfImage.ImageInfo.ImageFileType = imageFileType;
                pdfImage.ImageInfo.TiffFrame = -1;

                if (section.PageInfo.PageWidth > Aspose.Pdf.Generator.PageSize.A4Width
                    || section.PageInfo.PageHeight > Aspose.Pdf.Generator.PageSize.A4Height)
                {
                    // rescale image
                    pdfImage.ImageScale = Math.Min(Aspose.Pdf.Generator.PageSize.A4Width / section.PageInfo.PageWidth
                        , Aspose.Pdf.Generator.PageSize.A4Height / section.PageInfo.PageHeight);
                    section.PageInfo.PageWidth *= pdfImage.ImageScale;
                    section.PageInfo.PageHeight *= pdfImage.ImageScale;
                }

                fileStream.Seek(0, SeekOrigin.Begin);
                pdfImage.ImageInfo.ImageStream = fileStream;
                section.Paragraphs.Add(pdfImage);
            }
        }

        pdfCreator.Save(pdfStream);
    }
    
}

}

@accounts.recomatics

Thank you for contacting support.

We would like to request you to please upgrade to Aspose.PDF for .NET 18.6 and make following changes in your code snippet.

    //Pdf class from Generator namespace is replaced by Document
    //Document pdfCreator = new Document();

    //Section class is replaced by Page class
    //Aspose.Pdf.Page section = pdfCreator.Pages.Add();
                    
    //PageWidth can be set as section.PageInfo.Width
    //PageHeight can be set as section.PageInfo.Height

Generator namespace is obsolete so you can add an image to a paragraph by using the code snippet below:

    Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
    Page page = doc.Pages.Add();
    Aspose.Pdf.Image image = new Aspose.Pdf.Image();
    image.File = dataDir + "Test.jpg";
    page.Paragraphs.Add(image);
    doc.Save(dataDir + "Test_18.6.pdf");

We hope this will be helpful. Please feel free to contact us if you need any further assistance.

Hi again,

I still got some issues to find a replacement for the following code:

                    var marginInfo = new MarginInfo { Top = 0, Bottom = 0, Left = 0, Right = 0 };
                    page.PageInfo.Margin = marginInfo;
                    **page.PageInfo.PageBorderMargin = marginInfo;**

Also the following code is not supported anymore:

   switch (Path.GetExtension(fileName).ToLower())
                        {
                            case ".jpeg":
                            case ".jpg":
                                pdfImage.FileType = ImageFileType.**Jpeg**;
                                break;
                            case ".png":
                                pdfImage.FileType = ImageFileType.**Png**;
                                break;
                            case ".gif":
                                pdfImage.FileType = ImageFileType.**Gif**;
                                break;
                            case ".bmp":
                                pdfImage.FileType = ImageFileType.**Bmp**;
                                break;
                            case ".tif":
                            case ".tiff":
                                {
                                    pdfImage.FileType = ImageFileType.**Tiff**;
                                    **pdfImage.ImageInfo.TiffFrame = -1;**
                                }
                                break;
                            default:
                                throw new Exception("File " + fileName + "unsupported image type.");
                        }

I can’t find anything that resembles: PageBorderMargin, Tiff, Bmp, Png, Jpeg and pdfImage.ImageInfo.TiffFrame = -1;

Could you elaborate how we can change this?

@accounts.recomatics

You can set margins of a page with PageInfo.Margin property and assign an object of MarginInfo Class to this property, as in the first two lines of your margin related code.

Moreover, you can convert PDF pages to images as well as Images to PDF. Please visit Working with Images for your kind reference. In case you still face any problem, please explain your requirements so that we may guide you with respect to latest available version of the API.