I’m having problems saving TIFF files as PDFs. I’ve noticed that smaller files seem to work, but the larger examples I’m working with fail with the following error:
Hi Dan,
Hi, thanks for the suggestion, but with this approach I get the following error on save:
Hi Dan,
notice the same problem. For the sake of correction, I have logged this problem
as PDFNEWNET-39989 in our issue tracking system. We will
further look into the details of this problem and will keep you updated on the
status of correction. Please be patient and spare us little time. We are sorry
for this inconvenience.
Brilliant, thank you very much!
Hi Aspose Team, is there any update on this error?
Thanks for contacting support.
I am afraid that earlier logged issue is not yet resolved due to other high priority issues in the queue. However, we have recorded your concerns and will definitely consider them during issue resolution. Furthermore, would you please share your sample TIFF image with us, which is generating issue at your end while converting it into PDF. We will test the scenario in our environment and address it accordingly.
Hi Aspose Team,
it is just a white image (684 px, 236 px) with a black text in the middle, like “test content”). I don’t think it matters. You can check with any white tiff image.
Is there any chance you will fix it this week?
Hi Aspose Team,
I will additionally explain why I have this issue. I am migrating code to the newest Aspose. Below the old and new code. If you think I am doing something wrong, maybe you can just correct my code.
In the old code I used namespace:
using Aspose.Pdf.Generator;
using Aspose.Pdf.Text;
and in the new code:
using Aspose.Pdf;
Regards
Old code:
public void ProcessTiff(byte[] sourceFile)
{
Pdf pdf = new Pdf();
var section = pdf.Sections.Add();
section.PageInfo.PageWidth = PageSize.A4Width;
section.PageInfo.PageHeight = PageSize.A4Height;
MarginInfo marginInfo = new MarginInfo();
marginInfo.Top = 0;
marginInfo.Bottom = 0;
marginInfo.Left = 0;
marginInfo.Right = 0;
section.PageInfo.Margin = marginInfo;
using (MemoryStream imageStream = new MemoryStream(sourceFile))
{
var image = new Image(section);
image.ImageInfo.ImageStream = imageStream;
image.ImageInfo.ImageFileType = ImageFileType.Tiff;
section.Paragraphs.Add(image);
image.ImageInfo.TiffFrame = -1;
using (var pdfDocument = new Aspose.Pdf.Document(pdf))
{
New code:
public void ProcessTiff(byte[] sourceFile)
{
Document pdf = new Document();
var section = pdf.Pages.Add();
section.PageInfo.Width = PageSize.A4.Width;
section.PageInfo.Height = PageSize.A4.Height;
MarginInfo marginInfo = new MarginInfo();
marginInfo.Top = 0;
marginInfo.Bottom = 0;
marginInfo.Left = 0;
marginInfo.Right = 0;
section.PageInfo.Margin = marginInfo;
using (MemoryStream imageStream = new MemoryStream(sourceFile))
{
var image = new Image();
image.Margin = marginInfo;
image.FixWidth = PageSize.A4.Width;
image.FixHeight = PageSize.A4.Height;
image.ImageStream = imageStream;
image.FileType = ImageFileType.Svg;
section.Paragraphs.Add(image);
//image.ImageInfo.TiffFrame = -1;
var ms = new MemoryStream();
pdf.Save(ms, SaveFormat.Pdf);
using (var pdfDocument = new Document(ms))
Thanks for your feedback.
Please try using following code snippet in your environment and let us know about the results you get after executing it. In case it still produces any issue, it would be helpful if you can please share the same input TIFF file with us. We will further test it in our environment and share our feedback accordingly.
Document pdf1 = new Document();
MemoryStream ms = new MemoryStream();
new FileStream(dataDir + @"input.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;
sec.Paragraphs.Add(imageht);
}
pdf1.Save(dataDir + "TifftoPDF.pdf");
Hi Aspose Team,
thank you for fast answer. This solves the NullReference exception. But now my code breaks in next line which is:
pdfDocument.Convert(CallBackMethod); without even going to the callback method.
The Exception is:
Invalid index: index should be in the range [1…n] where n equals to the operators count.
Please let me know whether we can fix this.
Regards
Current code:
public void ProcessTiff(byte[] sourceFile)
{
Document pdf = new Document();
var section = pdf.Pages.Add();
section.PageInfo.Width = PageSize.A4.Width;
section.PageInfo.Height = PageSize.A4.Height;
MarginInfo marginInfo = new MarginInfo();
marginInfo.Top = 0;
marginInfo.Bottom = 0;
marginInfo.Left = 0;
marginInfo.Right = 0;
section.PageInfo.Margin = marginInfo;
using (MemoryStream imageStream = new MemoryStream(sourceFile))
{
var image = new Bitmap(imageStream);
FrameDimension dimension = new FrameDimension(image.FrameDimensionsList[0]);
int frameCount = image.GetFrameCount(dimension);
for (int frameIdx = 0; frameIdx <= frameCount - 1; frameIdx++)
{
Page sec = pdf.Pages.Add();
image.SelectActiveFrame(dimension, frameIdx);
MemoryStream currentImage = new MemoryStream();
image.Save(currentImage, ImageFormat.Tiff);
if (image.Width > image.Height)
{
sec.PageInfo.IsLandscape = true;
}
else
{
sec.PageInfo.IsLandscape = false;
}
Aspose.Pdf.Image imageht = new Aspose.Pdf.Image();
imageht.ImageStream = currentImage;
sec.Paragraphs.Add(imageht);
}
var ms = new MemoryStream();
pdf.Save(ms, SaveFormat.Pdf);
using (var pdfDocument = new Document(ms))
{
pdfDocument.Convert(CallBackMethod);
Thanks for writing back.
Would you please share the definition of CallBackMethod
method which you are using at your side. We will test the scenario in our environment and address it accordingly.
Hi Aspose Team,
the method looks more or less like following code. I cut business logic. targetFile is a .html file.
private string CallBackMethod(System.Drawing.Image img)
{
string tick = DateTime.Now.Ticks.ToString();
string hocr;
using (StreamReader streamReader = new StreamReader(targetFile))
{
hocr = streamReader.ReadToEnd();
}
return hocr;
}
Thanks for sharing requested details.
We have tested the scenario in our environment using one of our sample TIFF images and were unable to replicate the issue which you have mentioned. Would you please share a sample console application along with input/output files so that we can again test the scenario in our environment and address it accordingly.
Hi Aspose Team,
maybe this will help you. This is the stack trace I get:
at Aspose.Pdf.OperatorCollection.#=zq1cTQVs=(Int32 #=zsGb6rag=)
at #=z6zd4MBENZVcZkTeMiSOQCCv_p6hxq13KByg7N4tV7s1cM7AxRDkHf7Y=.#=zK7XUQHA=(CallBackGetHocr #=z1OgK1w$yYSKF, Document #=zcbz2dKo=)
after this there is stack trace of my code.
Exception: Invalid index: index should be in the range [1…n] where n equals to the operators count.
Asposepdf.dll: 19.1.0.0
Exact callback method name is CallBackGetHocr.
Please let me know if this is sufficient for you.
Best regards
Thanks for sharing the stack trace.
By looking at the stack trace it seems that issue is related to some operators collection in PDF and there could be more than single reasons of that. I regret that this information is not sufficient to investigate the issue you are facing and that is why we requested you to share a sample console application able to reproduce the issue. This would help us replicating the scenario in our environment and address it accordingly.
The issues you have found earlier (filed as PDFNET-39989) have been fixed in Aspose.PDF for .NET 22.3.