Thomas Hoevel,
Thanks for your response. I tried to write file to the user with response.write it still doesn't work for me.
Here's my code:
Page load event:
Code:
protected void Page_Load(object sender, EventArgs e)
{
Document document = new Document();
document = CreateDocument(document);
document.UseCmykColor = true;
const bool unicode = false;
const PdfFontEmbedding embedding = PdfFontEmbedding.Always;
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(unicode, embedding);
pdfRenderer.Document = document;
const string filename = "test.pdf";
pdfRenderer.RenderDocument();
pdfRenderer.Save(filename);
// Send PDF to browser
MemoryStream stream = new MemoryStream();
pdfRenderer.Save(stream, false);
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", stream.Length.ToString());
Response.AddHeader("content-disposition", "attachment; filename=test.pdf");
Response.BinaryWrite(stream.ToArray());
Response.Flush();
stream.Close();
Response.End();
}
Create document:
Code:
public Document CreateDocument(Document document)
{
// Create a new MigraDoc document
document = new Document();
document.Info.Title = "test";
document.Info.Subject = "test";
document.Info.Author = "test";
DefineStyles(document);
CreatePage(document);
return document;
}
Define styles:
Code:
void DefineStyles(Document document)
{
// Get the predefined style Normal.
MigraDoc.DocumentObjectModel.Style style = document.Styles["Normal"];
// Because all styles are derived from Normal, the next line changes the
// font of the whole document. Or, more exactly, it changes the font of
// all styles and paragraphs that do not redefine the font.
style.Font.Name = "Arial";
style = document.Styles[StyleNames.Header];
style.ParagraphFormat.AddTabStop("8in", TabAlignment.Right);
style = document.Styles[StyleNames.Footer];
style.ParagraphFormat.AddTabStop("8in", TabAlignment.Center);
// Create a new style called Table based on style Normal
style = document.Styles.AddStyle("Table", "Normal");
style.Font.Name = "Arial";
// style.Font.Name = "Times New Roman";
style.Font.Size = 10;
// Create a new style called Reference based on style Normal
style = document.Styles.AddStyle("Reference", "Normal");
style.ParagraphFormat.SpaceBefore = "5mm";
style.ParagraphFormat.SpaceAfter = "5mm";
style.ParagraphFormat.TabStops.AddTabStop("16cm", TabAlignment.Right);
}
And finally createpage (Commented where it fails)
Code:
void CreatePage(Document document)
{
Section section = document.AddSection();
//HERE WHERE IT FAILS WITH:
//'((System.IO.Stream)(stream)).ReadTimeout' threw an exception of type 'System.InvalidOperationException'
//'((System.IO.Stream)(stream)).WriteTimeout' threw an exception of type 'System.InvalidOperationException'
byte[] _byte = File.ReadAllBytes("c:\\image1.jpg");
MemoryStream stream = new MemoryStream(_byte);
Paragraph paragraph = section.Footers.Primary.AddParagraph();
paragraph = section.AddParagraph();
paragraph.AddImage(stream);
Paragraph header = section.Headers.Primary.AddParagraph();
header.AddFormattedText("Test",new Font("Arial", 15));
header.AddLineBreak();
header.AddFormattedText("Test",new Font("Arial", 8));
header.Format.Alignment = ParagraphAlignment.Center;
paragraph.AddText("Page ");
paragraph.AddPageField();
paragraph.AddLineBreak();
paragraph.AddText(DateTime.Now.ToShortDateString());
paragraph.Format.Alignment = ParagraphAlignment.Right;
this.addressFrame = section.AddTextFrame();
this.addressFrame.Height = "3.0cm";
this.addressFrame.Width = "7.0cm";
this.addressFrame.Left = ShapePosition.Left;
this.addressFrame.RelativeHorizontal = RelativeHorizontal.Margin;
this.addressFrame.Top = "5.0cm";
this.addressFrame.RelativeVertical = RelativeVertical.Page;
// Create the item table
this.table = section.AddTable();
this.table.Style = "Table";
this.table.Borders.Color = TableBorder;
this.table.Borders.Width = 0.25;
this.table.Borders.Left.Width = 0.5;
this.table.Borders.Right.Width = 0.5;
this.table.Rows.LeftIndent = 0;
// Before you can add a row, you must define the columns
Column column = this.table.AddColumn("4cm");
column.Format.Alignment = ParagraphAlignment.Center;
column = this.table.AddColumn("4cm");
column.Format.Alignment = ParagraphAlignment.Left;
column.Format.Font.Bold = true;
column = this.table.AddColumn("4cm");
column.Format.Alignment = ParagraphAlignment.Left;
column = this.table.AddColumn("4cm");
column.Format.Alignment = ParagraphAlignment.Left;
// Create the header of the table
Row row = table.AddRow();
row.HeadingFormat = true;
row.Format.Alignment = ParagraphAlignment.Center;
row.Format.Font.Bold = true;
row.Shading.Color = TableBlue;
row.Format.Font.Color = White;
row.Cells[0].AddParagraph("col1");
row.Cells[0].Format.Alignment = ParagraphAlignment.Left;
row.Cells[1].AddParagraph("col2");
row.Cells[1].Format.Alignment = ParagraphAlignment.Left;
row.Cells[2].AddParagraph("col3");
row.Cells[2].Format.Alignment = ParagraphAlignment.Left;
row.Cells[3].AddParagraph("col4");
row.Cells[3].Format.Alignment = ParagraphAlignment.Left;
this.table.SetEdge(0, 0,4, 1, Edge.Box, MigraDoc.DocumentObjectModel.BorderStyle.Single, 0.75, Color.Empty);
}
Let me know if there's something in my code that can be changed to make it work. the same image works if added directly as a file though.