Help with the problem. For educational purposes, I am writing a small C # program for working with PDF, using ItextSharp 7 library. In one of the classes I try to copy the file for rewriting using System.IO.File.Copy() , but in return I get an empty file. What could be wrong? Distinct answers in Google, I did not find
Here is the class code:
string FilePath { get; set; } int Rotation { get; set; } // В этом методе проблема void RotateFile() { string dest = /* тут путь для сохранения */; if (FilePath != dest) // если файл назначения не совпадает с исходным, здесь все работает { if (File.Exists(FilePath)) { RotateThis(FilePath, dest); } else { MessageBox.Show("Файл не существует или был перемещен!"); } } else if (FilePath == dest) // А вот здесь проблема { try { FileInfo copier = new FileInfo(FilePath); if (!copier.Exists) { MessageBox.Show(this, "Файл не существует", "Ошибка!",MessageBoxButtons.OK, MessageBoxIcon.Error); } string temp = Path.Combine(copier.DirectoryName, DateTime.Now.ToString("ddMMHHmmss") + "temp.pdf"); if (Directory.Exists(copier.DirectoryName)) { try { // Вместо копирования, создает пустой файл File.Copy(copier.FullName, temp, true); } catch (IOException exception) { MessageBox.Show("Ошибка ввода-вывода при копировании во временный файл:" + exception.Message); } catch (Exception exc) { MessageBox.Show("Ошибка при копировании во временный файл" + exc.Message); } try { RotateThis(temp, FilePath); } catch (IOException exception) { MessageBox.Show("Ошибка ввода-вывода при повороте документа:" + exception.Message); } catch (Exception exc) { MessageBox.Show("Ошибка при повороте документа:" + exc.Message); } finally { File.Delete(temp); } } else { MessageBox.Show(this, "Файл не существует или был перемещен!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } } void RotateThis(string src, string dest) { try { using (PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest))) { int numberOfPages = pdfDoc.GetNumberOfPages(); try { for (int p = 1; p <= numberOfPages; p++) { var page = pdfDoc.GetPage(p); var rotate = page.GetPdfObject().GetAsNumber(PdfName.Rotate); if (rotate == null) { page.SetRotation(Rotation); } else { page.SetRotation((rotate.IntValue() + Rotation) % 360); } } } catch (Exception exc) { MessageBox.Show("Ошибка: " + exc.Message); } finally { pdfDoc.Close(); MessageBox.Show(this, "Завершено !","Готово",MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } } } catch (Exception e) { MessageBox.Show("Ошибка создания файла pdf: " + e.Message); } }
File.Copy(copier.FullName, temp, true);, specify whatcopier.FullNameandtempequalcopier.FullNameand whether there are such files on the disk at this moment. - VladD