Я создаю файл PDF с помощью iTextSharp в ASP.NET C#.Ошибка при загрузке PDF-документа - iTextSharp PDF
Я пытаюсь создать заголовок + таблицу для нескольких элементов. После каждого элемента я хочу начать новую страницу. Я делаю это с doc.NewPage()
, но когда я произвожу мой файл PDF я получаю следующее сообщение об ошибке при открытии файла: Загрузка
Ошибки документа PDF
я получаю эту ошибку, когда я пытаюсь открыть PDF файл в последней версии Google Chrome. Когда я пытаюсь открыть файл в Adobe Reader, я получаю следующее сообщение об ошибке:
Ошибка при открытии этого документа. Файл поврежден и не может быть восстановлен.
Размер файла PDF также только 1kb ...
Вот код, как я генерировать файл:
//Create a byte array that will eventually hold our final PDF
//must be outside of the foreach loop (and everything else), because we store every single generated table in here for the final pdf!!
Byte[] bytes;
List<TableObject> myTables = getTables();
TableObject currentTable = new TableObject();
//Boilerplate iTextSharp setup here
//Create a stream that we can write to, in this case a MemoryStream
using (MemoryStream ms = new MemoryStream())
{
//Create an iTextSharp Document which is an abstraction of a PDF but **NOT** a PDF
using (Document doc = new Document(PageSize.A4, 10f, 10f, 10f, 0f))
{
//Create a writer that's bound to our PDF abstraction and our stream
PdfWriter writer = PdfWriter.GetInstance(doc, ms);
//Open the document for writing
doc.Open();
//writer.CloseStream = false;
//loop all tableobjects inside the document & the instance of PDFWriter itself!
foreach (TableObject to in myTables.ToList())
{
//Get the data from database corresponding to the current tableobject and fill all the stuff we need!
DataTable dt = getDTFromID(to._tableID);
Object[] genObjects = new Object[5];
genObjects = gen.generateTable(dt, currentTable._tableName, currentTable._tableID.ToString(), currentTable, true);
StringBuilder sb = (StringBuilder)genObjects[1];
String tableName = sb.ToString();
Table myGenTable = (Table)genObjects[0];
//String table contains a valid html string, which works in the generate function for a single page document
String table = genObjects[2].ToString();
using (StringReader srHtml = new StringReader(table))
{
//Parse the HTML
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
}
//this will probably render a whole new page at the end of the file!! need to be fixed later!!!
//doc.NewPage();
}
//After all of the PDF "stuff" above is done and closed but **before** we
//close the MemoryStream, grab all of the active bytes from the stream
bytes = ms.ToArray();
doc.Close();
}
}
//Now we just need to do something with those bytes.
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=ShiftReport_complete.pdf");
Response.BinaryWrite(bytes);
Как я могу решить эту проблему, так что я получаю избавиться от сообщения об ошибке при открытии PDF-файла?
ошибка вы получаете может быть связано с браузером части, какой браузер вы открываете попробовать в? возможно, это может помочь вам http://stackoverflow.com/questions/1013271/error-while-trying-to-open-pdf-in-asp-net – BNN
Я попробовал это с Google Chrome v 47.0.2526.106, но также и с Adobe Reader , Adobe Reader дает следующее сообщение об ошибке: «Ошибка при открытии этого документа. Файл поврежден и не может быть восстановлен. ' – Hack4Life
вы можете посмотреть эту ссылку и попробовать. http://thenubbyadmin.com/2012/04/13/solving-the-error-the-file-is-damaged-and-could-not-be-repaired-when-opening-a-pdf-in-internet- explorer/ – BNN