2
I am using Magick.NET-Q16-Anycpu version 7.12.0 and iTextSharp libraries.
First I open the image with Magick and save this TIFF page in a folder. Then I use this image to add it to the PDF.
I’ve been making changes to the code and it stopped working. And I’ve tried to use a code that I knew was correct and yet it gives me this error:
System.Argumentexception: Extra samples are not supported.
at iTextSharp.text.pdf.codec.Tiffimage.Gettiffimagecolor(Tiffdirectory dir, Randomaccessfileorarray s) at iTextSharp.text.pdf.codec.Tiffimage.Gettiffimage(Randomaccessfileorarray s, Int32 page, Boolean direct) at iTextSharp.text.Image.Getinstance(Uri url) at iTextSharp.text.Image.Getinstance(String filename) At projectofct.Form1.Convpdferror3(String dest, String[] e) in C: Users user Documents Projeto Projetoa Form1.Cs:line 5253
Code
public void ConvTiffToPdf(string dest, string[] e)
{
for (int i = 0; i < e.Length; i++)
{
int pages = 0;
string output = dest + "\\" + Path.GetFileNameWithoutExtension(e[i]) + ".pdf";
iTextSharp.text.Document document = new iTextSharp.text.Document();
iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(output, FileMode.Create));
writer.CompressionLevel = PdfStream.BEST_COMPRESSION;
writer.PdfVersion = PdfWriter.VERSION_1_7;
string folder = AppDomain.CurrentDomain.BaseDirectory + "temporario\\" + Path.GetFileNameWithoutExtension(e[i]);
if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
bool @continue = true;
while (@continue == true)
{
MagickReadSettings settings = new MagickReadSettings
{
Compression = ImageMagick.CompressionMethod.LZW,
Format = MagickFormat.Tiff,
FrameIndex = pages
};
try
{
using (MagickImage image = new MagickImage(e[i], settings))
{
string file = folder + "\\" + Path.GetFileNameWithoutExtension(e[i]) + "- Página " + (pages + 1) + ".tif";
image.Quality = 75;
image.Format = MagickFormat.Tiff;
image.Write(file, MagickFormat.Tiff);
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(file); //o erro é aqui
iTextSharp.text.Rectangle size = new iTextSharp.text.Rectangle(img.ScaledWidth, img.ScaledHeight);
document.SetPageSize(size);
document.SetMargins(0, 0, 0, 0);
document.Open();
iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
img.SetAbsolutePosition(0, 0);
img.SetDpi(300, 300);
cb.AddImage(img);
document.NewPage();
}
}
catch (Exception ex)
{
if (ex is MagickException)
{
document.Close();
Invoke(new Action(() => { succed = 1; pbar.PerformStep(); caseSwitch = 6; }));
@continue = false;
}
else if(ex is System.ObjectDisposedException)
{
document.Close();
Invoke(new Action(() => { succed = 1; pbar.PerformStep(); caseSwitch = 6; }));
@continue = false;
}
else if(ex is System.ArgumentException)
{
MessageBox.Show(ex.ToString(), "Erro", MessageBoxButtons.OK);
}
else
MessageBox.Show(ex.ToString(), "Erro", MessageBoxButtons.OK);
}
pages++;
}
}
}
Can someone help me figure out what’s wrong with my code? Thanks in advance.
which version of itext you are using?
– Lucas Miranda
@Lucasmiranda I have PDF with versions 1.3 and iText version 5 was not reading these versions, so I went to look for version 4.1.6
– Sofia Rodrigues
The tiff support in iText will always have problems because the tiff format is essentially a bag for a collection of image formats. If you are using Magick for conversions the best chance is to save the image as PNG and not as tiff. This will always work.
– Paulo Soares
@Paulosoares well seen, but the Pdfsharp library does what I need without giving anyone error, anyway I will try to save as PNG.
– Sofia Rodrigues
@Gradually saving yourself as PNG worked without any problem. Thank you.
– Sofia Rodrigues
I would recommend you abandon tiff, why you need to work with this extension?
– Leandro Angelo
@Leandeoangelo I need to work with her because I am doing an internship in a company that digitizes documents so the TIFF format has an excellent quality and being that it is Tagged is perfect for organizing documents.
– Sofia Rodrigues