Aspose.Pdf.Generator namespace obsolete?

Hi,
I get a warning in my code, saying that Aspose.Pdf.Generator namespace is obsolete.

Im setting the “margin” using section.PageInfo.Margin

But there are several examples on your website, which uses this namespace? Like:

https://docs.aspose.com/pdf/net/working-with-documents/
Aspose Documentation

What should I use instead?


Hi Thomas,


Thanks for your inquiry. I am afraid Aspose.Pdf.Generator is old generator and obsolete now. Recently in new releases we started showing warnings. We are making changes and improvements in new generator Aspose.Pdf (DOM approach), it is more improved and efficient. In new DOM approach, Page object is similar as Section in generator. Please see following code snippet to set the margin as a sample.

C#

Document doc = new Document();

Page page = doc.Pages.Add();

MarginInfo marginInfo = new MarginInfo(50, 72, 40, 65);

doc.PageInfo.Margin = marginInfo;

Table table = new Table();

Row row = table.Rows.Add();

Cell cell = row.Cells.Add();

cell.Paragraphs.Add(new TextFragment(“First cell”));

doc.Pages[1].Paragraphs.Add(table);

doc.Save(dataDir + “test.pdf”);

If you need further assistance, please feel free to contact us.

Best Regards,

When will you be updating your documentation with the new approach? We’re using the Aspose.Pdf.Generator namespace to Bind XML to an instance of the Aspose.Pdf.Generator.Pdf class. Your documentation is using the obsolete classes.


Here’s a code sample of what we’re doing:

var pdf = new Pdf();
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString())))
{
pdf.BindXML(ms, null);
}

// later in the code
var document = new Document();
document.Pages.Add(new Document(pdf).pages);

Hi Thomas,


Thanks for contacting support.

Almost all of the features present in legacy Aspose.Pdf.Generator are available in new DOM approach of Aspose.Pdf namespace. Now concerning to the conversion of XML to PDF format, you need to first transform the XML to new DOM approach. For further details, please visit Convert XML file to PDF

I’m using Aspose.Pdf.Generator to convert images to PDFs. What are the alternatives to Aspose.Pdf.Generator.Image and Aspose.Pdf.Generator.Section?

Hi Cody,


Thanks for your inquiry.

Aspose.Pdf.Generator.Section in the old legacy approach, are now Aspose.Pdf.Page in the new DOM (Document Object Model) approach. However for more information please visit following articles, where you can check all features provided by Aspose.Pdf for .NET DOM model

- PDF DOM Structure
- Manipulate Images
- Working With Aspose.Pdf for .NET

In case of any further assistance, please feel free to contact us.


Best Regards,

I tried Aspose.Pdf for .Net version 17.5 today. In this version, the Aspose.Net.Generator namespace does not seem to work at all anymore. In version 17.4, it still works (but is marked as obsolete as you say).


For me it was unclear how to add text line-by-line from a large text file in the new model. It contains a BaseParagraph class which is abstract, but no derived class TextParagraph or similar. There is Aspose.Pdf.Text.TextParagraph, but that does not inherit from BaseParagraph.

This code sample is in urgent need of rewrite for the new model:
https://docs.aspose.com/display/pdfnet/How+to+Convert+a+text+file+to+PDF

Hello Gert,

Thanks for your inquiry.

In new DOM approach you can use TextFragment class to add text inside pages of PDF document. Please check following code snippet to convert TXT file into PDF document by reading content line by line.

System.IO.TextReader tr = new StreamReader(dataDir + “testtext.txt”, Encoding.Default, true);
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
Page page = doc.Pages.Add();
String strLine;
while ((strLine = tr.ReadLine()) != null)
{
    Aspose.Pdf.Text.TextFragment text = new Aspose.Pdf.Text.TextFragment(tr.ReadToEnd());
    page.Paragraphs.Add(text);
}
doc.Save(dataDir + "TexttoPDF_out.pdf");
tr.Close();

To convert complete TXT file into PDF at once, please check “Convert Text File into PDF” article in our API documentation. In case of any further assistance, please feel free to contact us.

Best Regards,

Hi Asad,

Your sample worked for me after fixing a small bug, thanks for providing it so quickly.
My bugfix, highlighted change below:

System.IO.TextReader tr = new StreamReader(dataDir + “testtext.txt”, Encoding.Default, true);
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
Page page = doc.Pages.Add();
String strLine;
while ((strLine = tr.ReadLine()) != null)
{
    Aspose.Pdf.Text.TextFragment text = new Aspose.Pdf.Text.TextFragment(strLine);
    page.Paragraphs.Add(text);
}
doc.Save(dataDir + “TexttoPDF_out.pdf”);
tr.Close();

The original sample would not convert the first line and read the remainder of the text file all at once, although the intention of the sample appeared to be to read line-by-line.

Hi Gert,


Thanks for highlighting the bug and sorry for mistake.

It is good to know that suggested approach worked for you. Please keep using our API and in case of any further assistance, please feel free to contact us.


Best Regards,

Hello,

We are evaluating to update to the latest version and we need our current code to work correctly.

We make a multitiff file conversion to pdf, but with the new version generates error in Aspose.pdf.generator

the code we are using is this:

            Stream oReturn = new MemoryStream();

            //Instantiate a Pdf object by calling its empty constructor
            Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();

            //Create a section in the Pdf object
            pdf1.PageSetup.Margin.Left = 0;
            pdf1.PageSetup.Margin.Right = 0;
            pdf1.PageSetup.Margin.Top = 0;
            pdf1.PageSetup.Margin.Bottom = 0;

            Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();

            //Create an image object in the section 
            Aspose.Pdf.Generator.Image imageht = new Aspose.Pdf.Generator.Image(sec1);
            //Set the type of image using ImageFileType enumeration
            imageht.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Tiff;
            // Specify the image source as MemoryStream
            oStream.Position = 0;
            imageht.ImageInfo.ImageStream = oStream;

            //Add image object into the Paragraphs collection of the section
            sec1.Paragraphs.Add(imageht);
            imageht.ImageInfo.TiffFrame = -1;
            imageht.ImageInfo.IsAllFramesInNewPage = true;

            //Save the Pdf
            pdf1.Save(oReturn);

How can we migrate this code with the new versions of your library

Very Thanks

@grojas

Thanks for your inquiry.

In order to convert multi-page TIFF image into PDF, please use following code snippet along with latest version of the API, which is Aspose.Pdf for .NET 17.8.

Document pdf1 = new Document();
MemoryStream ms = new MemoryStream();
new FileStream(dataDir + @"TestTif4.tif", FileMode.Open).CopyTo(ms);
Bitmap myimage = new Bitmap(ms);
FrameDimension dimension = new FrameDimension(myimage.FrameDimensionsList[0]);
int frameCount = myimage.GetFrameCount(dimension);
for (int frameIdx = 0; frameIdx <= frameCount - 1; frameIdx++)
{
 Page sec = pdf1.Pages.Add();
 myimage.SelectActiveFrame(dimension, frameIdx);
 MemoryStream currentImage = new MemoryStream();
 myimage.Save(currentImage, ImageFormat.Tiff);

 if (myimage.Width > myimage.Height)
 {
  sec.PageInfo.IsLandscape = true;
 }
 else
 {
   sec.PageInfo.IsLandscape = false;
 }
 Aspose.Pdf.Image imageht = new Aspose.Pdf.Image();
 imageht.ImageStream = currentImage;
 imageht.IsBlackWhite = true;
 sec.Paragraphs.Add(imageht);
}
pdf1.Save(dataDir + "SampleTiff2PDF.pdf"); 

In case if you face any issue, please share your sample source TIFF with us, so that we can test the scenario in our environment and address it accordingly.