Free Support Forum - aspose.com

There was an error opening this document

Hello i’m currently using aspose.pdf 3.6.1.0 to create a survey.

when opening the document with adobe 6.0 i get this error message:

there was an error opening this document. the file is damaged and could not be repaired.

The file is viewable in both foxit and adobe reader 8.xx

I have attached the document.

Below is my code which basically adds images to the pdf document.

void ibPDF_Click(object sender, ImageClickEventArgs e)
{
survey.ViewMode = SurveyViewMode.PDF;

Pdf pdfPresentation = new Pdf();
section = pdfPresentation.Sections.Add();

// sets margins
section.PageInfo.Margin.Left = 15;
section.PageInfo.Margin.Top = 20;
section.PageInfo.Margin.Bottom = 0;


foreach (Schema sch in survey.getSchemas())
{
if (sch.Active)
{
addTable();

foreach (Question q in sch.Questions)
{
if (q.IsActive)
{
// kjører teller på nominal group for å kunne skille med even og odd
if (q.ProductID == 1 || q.ProductID == 6)
{
NominalGroupNumber++;
if (NominalGroupNumber % 2 == 1)
q.Odd = true;
}

addQuestion(q);
}
}
}
}
// adds wanted whitespace to the last page
if (tableHeight > 0)
addEndSpace(tableHeight, survey.addins.HasAddin(2));

// adds pager on last page
if (survey.addins.HasAddin(2))
{
renderPagerAddin();
}

// Sends document to output stream
string filename = “test”;

// skriver ut headere
Context.Response.ContentType = “application/pdf”;
Context.Response.AppendHeader(“Content-Disposition”, “attachment; filename=” + filename + “.pdf”);
Context.Response.Flush();

// deklarer en outputstream
System.IO.Stream st = Context.Response.OutputStream;

// lagrer presentasjonen i minne
pdfPresentation.Save(st);

Context.Response.End();

}

public void addQuestion(Question q)
{


// legger på checkbox f.eks anonym
renderPersonaliaAddin(q);

Row Row1 = tblInner.Rows.Add();
Cell c1 = Row1.Cells.Add();

StringBuilder SB = new StringBuilder();
StringWriter SW = new StringWriter(SB);
HtmlTextWriter htmlTW = new HtmlTextWriter(SW);

q.RenderControl(htmlTW);

string html = “” + survey.css.getAsString() + “” + SW.ToString() + “”;

System.Drawing.Image img = (System.Drawing.Image)HtmlToImage.GetImage(html, 760, 400, 200);

System.IO.MemoryStream stream = new System.IO.MemoryStream();
img.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);

Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(section);

// gjør om stream til et bilde
image1.ImageInfo.ImageFileType = ImageFileType.Bmp;
image1.ImageInfo.ImageStream = stream;


// skalerer bilde
image1.ImageInfo.FixWidth = 542;

// setter marginger
if (tblInner.Rows.Count == 1)
{
image1.Margin.Top = 10;
}
image1.Margin.Left = 10;
image1.Margin.Right = 10;

// legger til bilde i siden
c1.Paragraphs.Add(image1);

// setter tableheight
tableHeight += ((float)542) / img.Width * img.Height;

// sjekker høyde på tabell
if (tableHeight > 740)
{
tblInner.Rows.Remove(Row1);
tableHeight -= ((float)542) / img.Width * img.Height;
addEndSpace(tableHeight,q.addins.HasAddin(2));
addTable();
addQuestion(q);
}
// sjekker høyde
//int tableheight = (int)tblInner.GetHeight(pdfPresentation);
}

public void addEndSpace(float height, bool pager)
{
float availableSpace = 775;
float pagerRow = 30;

Row Row1 = tblInner.Rows.Add();
Cell c1 = Row1.Cells.Add();

if (pager)
{
Row row = tblInner.Rows.Add();
Cell cell = row.Cells.Add();
PagerCells.Add(cell);

availableSpace -= pagerRow;
}

Text text = new Text("");
text.Margin.Top = availableSpace - (int)height;
c1.Paragraphs.Add(text);
}

private void renderPagerAddin()
{
int i = 0;
foreach (Cell c in PagerCells)
{
i++;

StringBuilder SB = new StringBuilder();
StringWriter SW = new StringWriter(SB);
HtmlTextWriter writer = new HtmlTextWriter(SW);


writer.AddStyleAttribute(“border”, “solid 1px #fffffe”);
writer.AddStyleAttribute(HtmlTextWriterStyle.Width, “685px”);
writer.RenderBeginTag(HtmlTextWriterTag.Table);
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.AddAttribute(HtmlTextWriterAttribute.Align, “center”);
writer.RenderBeginTag(HtmlTextWriterTag.Td);

Label l = new Label();
l.Text = maze.Text.GetText(“txtPage”) + " " + i.ToString() + “/” + PagerCells.Count.ToString();
l.CssClass = “classPageNr”;
l.RenderControl(writer);

writer.RenderEndTag(); // td
writer.RenderEndTag(); // tr
writer.RenderEndTag(); // table

string html = “” + survey.css.getAsString() + “” + SW.ToString() + “”;

System.Drawing.Image img = (System.Drawing.Image)HtmlToImage.GetImage(html, 760, 400, 200);

System.IO.MemoryStream stream = new System.IO.MemoryStream();
img.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);

Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(section);


// gjør om stream til et bilde
image1.ImageInfo.ImageFileType = ImageFileType.Bmp;
image1.ImageInfo.ImageStream = stream;


// skalerer bilde
image1.ImageInfo.FixWidth = 542;

// setter marginger
image1.Margin.Left = 10;
image1.Margin.Right = 10;
image1.Margin.Top= 7;
image1.Margin.Bottom = 7;

// legger til bilde i siden
c.Paragraphs.Add(image1);

// setter tableheight
float test = ((float)542) / img.Width * img.Height;

}
}

private void renderPersonaliaAddin(Question q)
{
// kjører sjekk på personalia for å kunne skrive ut addins på riktig sted
if (q.ProductID == 9)
Personalia = true;

// skriver ut addins
if (Personalia && q.ProductID != 9 && !AddinPrinted)
{
StringBuilder SB = new StringBuilder();
StringWriter SW = new StringWriter(SB);
HtmlTextWriter writer = new HtmlTextWriter(SW);

AddinPrinted = true;
Personalia = false;

List strings = new List();

if (q.addins.HasAddin(4))
{
strings.Add(maze.Text.GetText(“txtAnonymous”));
}

if (q.addins.HasAddin(20))
{
strings.Add(maze.Text.GetText(“txtAnonymousContest”));
}

if (q.addins.HasAddin(7))
{
strings.Add(maze.Text.GetText(“txtFutureParticipation”));
}

// renders html
foreach (string text in strings)
{
writer.AddStyleAttribute(“border”, “solid 1px #fffffe”);
writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, “0”);
writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, “3”);
writer.AddStyleAttribute(HtmlTextWriterStyle.Width, “685px”);
writer.AddAttribute(HtmlTextWriterAttribute.Class, “classQuestion”);
writer.RenderBeginTag(HtmlTextWriterTag.Table);
writer.RenderBeginTag(HtmlTextWriterTag.Tr);

// venstre kolonne
writer.AddStyleAttribute(HtmlTextWriterStyle.Width, “83px”);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.RenderEndTag(); // td
writer.AddAttribute(HtmlTextWriterAttribute.Align, “left”);
writer.RenderBeginTag(HtmlTextWriterTag.Td);

writer.AddAttribute(HtmlTextWriterAttribute.Align, “left”);
writer.AddAttribute(HtmlTextWriterAttribute.Class, “classQuestion”);
writer.RenderBeginTag(HtmlTextWriterTag.Table);
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.AddAttribute(HtmlTextWriterAttribute.Valign, “top”);
writer.AddAttribute(HtmlTextWriterAttribute.Align, “left”);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
CheckBox cb = new CheckBox();

cb.CssClass = “classQuestion”;
cb.RenderControl(writer);
writer.RenderEndTag(); // td
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.RenderBeginTag(HtmlTextWriterTag.Span);
writer.Write(text);
writer.RenderEndTag(); // span
writer.RenderEndTag(); // td
writer.RenderEndTag(); // tr
writer.RenderEndTag(); // table

writer.RenderEndTag(); // td;
writer.RenderEndTag(); // tr;
writer.RenderEndTag(); // table;
}

string html = “” + survey.css.getAsString() + “” + SW.ToString() + “”;


// set entry for image
Row Row1 = tblInner.Rows.Add();
Cell c1 = Row1.Cells.Add();

// converts html to image
System.Drawing.Image img = (System.Drawing.Image)HtmlToImage.GetImage(html, 760, 400, 200);
System.IO.MemoryStream stream = new System.IO.MemoryStream();
img.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(section);


// converts stream to picture
image1.ImageInfo.ImageFileType = ImageFileType.Bmp;
image1.ImageInfo.ImageStream = stream;


// set width
image1.ImageInfo.FixWidth = 542;

// set margins
image1.Margin.Left = 10;
image1.Margin.Right = 10;

// ladds picture
c1.Paragraphs.Add(image1);

// adjust table height
tableHeight += ((float)542) / img.Width * img.Height;
}
}

public void addTable()
{
// sets table height
tableHeight = 0;

// set border color
System.Drawing.Color border1 = survey.css.classTableFrame.BorderColor;

// outer table - outer border
Aspose.Pdf.Table tblOuter = new Aspose.Pdf.Table();
tblOuter.IsFirstParagraph = true;
Row tblOuterRow = tblOuter.Rows.Add();
Cell tblOuterCell = tblOuterRow.Cells.Add();
tblOuter.ColumnWidths = “566”;
section.Paragraphs.Add(tblOuter);
tblOuter.Border = new BorderInfo((int)BorderSide.All, 1F, new Color(border1.R, border1.G, border1.B));

// inner table - inner border
tblInner = new Aspose.Pdf.Table();
MarginInfo margins = new MarginInfo();
margins.Top = 2;
margins.Right = 2;
margins.Bottom = 2;
margins.Left = 2;

tblInner.Margin = margins;
tblInner.Border = new BorderInfo((int)BorderSide.All, 1F, new Color(border1.R, border1.G, border1.B));
tblInner.ColumnWidths = “562”;
tblOuterCell.Paragraphs.Add(tblInner);
}


This message was posted using Page2Forum (attachment) from Advance Features - Aspose.Pdf for .NET and Java

Hi,

Thank you for considering Aspose.

I have reproduced this error using Adobe Reader 6. Can you please provide the complete peoject (including the images) and let us test it?

I’m not able to that as the images are generated from html retrieved from a database, what i can do is to make a simplified project and try to reproduce the error.

I will try and get this done as fast as possible.

thank you for your prompt response

Hi, <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

The file is damaged and by repaired by a tool called Advanced PDF Repair. You can try this tool before the bug is fixed.

Alan