I have a question. I have this program in C# which creates a PDF using iText7, using exclusively images. (here's an example)
public void fromJPEGFileToPDF(String[] pathFiles, string path)
{
PdfWriter writer = new PdfWriter(path);
PdfDocument pdfDoc = new PdfDocument(writer);
Document doc = new Document(pdfDoc);
for (int i = 0; i < pathFiles.Length; i++)
{try
{
ImageData imageData = ImageDataFactory.Create(FileToByte(pathFiles[i]));
ImagePDF imgTemp = new ImagePDF(imageData);
doc.Add(imgTemp);
}
catch (Exception e)
{
//DO SOMETHING
}
}
doc.Close();
}
After that, I have this other function that extracts back the images from the file PDF, but it doesn't work as I'd want, as I often get images that don't open. To be fair, I get usually the proper image(s) I put in the pdf + some void files of small dimension that don't look like image files at all. And lately I got some pdf with not extractable images at all. Can I ask for your help? Here's my code:
public void fromPDFtoJPEGFiles(string pathFolder, string filename )
{
PdfDocument pdfDoc = new PdfDocument(new PdfReader(pathFolder+ "\\" + filename + ".pdf"));
PdfObject obj;
List<int> streamLengths = new List<int>();
for (int i = 1; i <= pdfDoc.GetNumberOfPdfObjects(); i++)
{
obj = pdfDoc.GetPdfObject(i);
if (obj != null && obj.IsStream())
{
byte[] b;
try
{
b = ((PdfStream)obj).GetBytes();
}
catch (PdfException exc)
{
b = ((PdfStream)obj).GetBytes(false);
}
MemoryStream fos = new MemoryStream(b);
FileStream file = new FileStream(pathFolder+ filename + "(" + (i + 1) + ").jpg", FileMode.Create, System.IO.FileAccess.Write);
fos.WriteTo(file);
streamLengths.Add(b.Length);
fos.Close();
file.Close();
}
}
pdfDoc.Close();
}