We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Convert Multi page Tiff to Pdf

Hello,

I searched on your site for an answer but do you have an example code for c#
to convert multi page tiff to pdf with the latest version of Aspose.Total?

thank you in advance

@bigouke

Thanks for contacting support.

Please use following code snippet to convert Multi-Page TIFF image into PDF using Aspose.PDF for .NET:

Document pdf1 = new Document();
MemoryStream ms = new MemoryStream();
new FileStream(dataDir + @"MultipageTiff.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 you want to set orientation, you can place check
  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");

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

Hello,

We upgraded to the latest version of Aspose and some code stopped working for us.

I tried to implement the code according to how you described it in the previous post, but for me it does not work. Please help me figure this out.

  1. I get a Multi-Page tiff in Base64string.
  2. Next, I need to convert the tiff to a pdf document.
  3. Next, I need to convert each page to PNG and output to a file.

It seems that the pdf document is being generated, but after converting the pages to PNG, I get a blank page. After converting to PNG, I check the encoded image here: https://base64.guru/converter/decode/file

///Generate Output xml

For Each profileInfo As ProfileInfo In _ProfileInfoList
If profileInfo.FileExtension IsNot Nothing Then
Using pdfDoc As Aspose.Pdf.Document = GetPDFDocument(profileInfo.ImageData)
Dim docPages As PageCollection = pdfDoc.Pages

				For Each pdgPage As Page In docPages
					Dim pictElem As XElement = rElem.Element(w + "pict")
					Dim imageId As String = Guid.NewGuid().ToString()

					pictElem.Element(w + "binData").Value = Convert.ToBase64String(GetImageBuffer(pdgPage))
					pictElem.Element(w + "binData").Attribute(w + "name").Value = $"wordml://{imageId}.png"
					pictElem.Element(v + "shape").Element(v + "imagedata").Attribute("src").Value = $"wordml://{imageId}.png"

					Dim height = icElem.Attribute("Height")?.Value
					Dim width = icElem.Attribute("Width")?.Value

					If (Not String.IsNullOrEmpty(height) AndAlso Not String.IsNullOrEmpty(width)) Then
						pictElem.Element(v + "shape").Attribute("style").Value = $"width:{width};height:{height};visibility:visible;mso-wrap-style:square"
					Else
						pictElem.Element(v + "shape").Attribute("style").Value = $"width:{pdgPage.MediaBox.Width.ToString()};height:{pdgPage.MediaBox.Height.ToString()};visibility:visible;mso-wrap-style:square"
					End If

					outputDocPart.Add(rElem)
				Next
			End Using
		End If
	Next

///Generate PDF Document
Private Function GetPDFDocument(image As String) As Aspose.Pdf.Document
Dim buffer = Convert.FromBase64String(image)
Dim mstream As MemoryStream = New MemoryStream(buffer)

	Dim doc = New Aspose.Pdf.Document()

	Dim myimage As Bitmap = New Bitmap(mstream)

	Dim dimension = New FrameDimension(myimage.FrameDimensionsList(0))
	Dim frameCount As Integer = myimage.GetFrameCount(dimension)

	For frameIdx As Integer = 0 To frameCount - 1
		Dim sec As Page = doc.Pages.Add()
		myimage.SelectActiveFrame(dimension, frameIdx)

		If myimage.Width > myimage.Height Then
			myimage.RotateFlip(RotateFlipType.Rotate90FlipNone)
		End If

		Dim currentImage As MemoryStream = New MemoryStream()
		myimage.Save(currentImage, ImageFormat.Tiff)

		Dim imageht = New Aspose.Pdf.Image()
		imageht.ImageStream = currentImage
		sec.Paragraphs.Add(imageht)
	Next

	doc.FitWindow = True

	Return doc
End Function

///Convert page to PNG
Private Function GetImageBuffer(pdgPage As Page) As Byte()
Dim imagedev As New PngDevice()
Dim buffer As Byte()
Using imageReader As New MemoryStream()
imagedev.Process(pdgPage, imageReader)
buffer = imageReader.ToArray()
End Using

	Return buffer
End Function

Earlier we used this piece of code and it worked for us.


Dim tiffpdf As New Generator.Pdf()
Dim tiffpage As Generator.Section = tiffpdf.Sections.Add()
Dim tiffimage As New Generator.Image(tiffpage)
tiffimage.ImageInfo.ImageStream = ms
tiffimage.ImageInfo.ImageFileType = Generator.ImageFileType.Tiff
tiffpage.Paragraphs.Add(tiffimage)
tiffimage.ImageInfo.TiffFrame = -1

			If tiffimage.ImageWidth > tiffpage.PageInfo.PageWidth Then
				tiffimage.RotatingAngle = 90
			End If

			tiffpage.PageInfo.PageGutter.Size = 0
			tiffpage.PageInfo.PageBorderMargin.Top = 0
			tiffpage.PageInfo.PageBorderMargin.Left = 0
			tiffpage.PageInfo.PageBorderMargin.Right = 0
			tiffpage.PageInfo.PageBorderMargin.Bottom = 0
			tiffpage.PageInfo.PageBorderMargin.Inner = 0
			tiffpage.PageInfo.PageBorderMargin.Outer = 0
			tiffpage.PageInfo.Margin.Inner = 0
			tiffpage.PageInfo.Margin.Outer = 0
			tiffpage.PageInfo.Margin.Top = 0
			tiffpage.PageInfo.Margin.Left = 0
			tiffpage.PageInfo.Margin.Right = 0
			tiffpage.PageInfo.Margin.Bottom = 0

			doc = New Aspose.Pdf.Document(tiffpdf)

I would be grateful for any help.

Thanks.

@Aliaksandr_S1

Would you kindly use DOM approach to convert PDF Pages into PNG images and in case you still face any issue, please share the sample PDF document which you are generating at your side from TIFF. We will test the scenario in our environment and address it accordingly.

Hello,

It seems to me that I did it, didn’t I?

Private Function GetImageBuffer(pdgPage As Page) As Byte()
Dim imagedev As New PngDevice()
Dim buffer As Byte()
Using imageReader As New MemoryStream()
imagedev.Process(pdgPage, imageReader)
buffer = imageReader.ToArray()
End Using

	Return buffer
End Function

@Aliaksandr_S1

Sorry for the confusion.

The code snippet was overlooked from your previous post. However, please share the sample PDF document so that we can test the scenario in our environment and proceed further to assist you.

This is a base64string TIFF.
Base64string_TIFF.zip (17.6 KB)

You can also use any TIFF file.

Thanks.

@Aliaksandr_S1

We used following complete code snippet in order to test the scenario in our environment using Aspose.PDF for .NET 20.3 and were unable to notice the issue of blank resultant images:

Document pdf1 = new Document();
MemoryStream ms = new MemoryStream(Convert.FromBase64String(File.ReadAllText(dataDir + "Base64string_TIFF.txt")));
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);
  Aspose.Pdf.Image imageht = new Aspose.Pdf.Image();
  imageht.ImageStream = currentImage;
  sec.Paragraphs.Add(imageht);
}
pdf1.Save(dataDir + "TifftoPDF.pdf");
pdf1 = new Document(dataDir + "TifftoPDF.pdf");

for (int pageCount = 1; pageCount <= pdf1.Pages.Count; pageCount++)
{
 Resolution resolution = new Resolution(300);
 PngDevice pngDevice = new PngDevice(resolution);
 MemoryStream mem = new MemoryStream();
 pngDevice.Process(pdf1.Pages[pageCount], mem);
 var arr = mem.ToArray();
 var bas = Convert.ToBase64String(arr);
}

For your kind reference, an output image is also attached. img.jpg (79.2 KB)

Would you kindly share a sample console application which is able to reproduce the error. We will again test the scenario in our environment and address it accordingly.

TiffCreation.zip (347.1 KB)

I attached a console application. In the code, I added a working and non-working case and wrote comments. In my case, what I need does not work, or am I doing it wrong.

@Aliaksandr_S1

We have checked the sample project that you have shared. Please note that you need to save the PDF document after converting TIFF image so that changes can be made to the file. This way it would be converted to PNG correctly. You can save the document into memory stream as well and re-initialize it from same memory stream in case you do not want to save at physical location.

Now it works. Thanks a lot for your help.