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/display/pdfnet/Working+with+Document+(Generator)
https://docs.aspose.com/pages/viewpage.action?pageId=7120443
What should I use instead?
Hi Thomas,
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”);
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.
Hi Thomas,
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,
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).
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,
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
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.