This is a migrated thread and some comments may be shown as answers.

ExportToPdf - How to cancel the Response Output

4 Answers 97 Views
Editor
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 07 Oct 2010, 08:42 PM
I am currently looking to use the export to pdf of the RadEditor however rather than allowing the user to save this directly, I am capturing the PDF byte array in the OnExport method to allow me to add custom headers and footers. I need to be able to cancel the writing of the PDF to the response by the radeditor itself but cannot find a way to prevent this. Can anyone point me in the right direction? If there is not a specific parameter to cancel the writing to the response object, is there any part of the page lifecyle I can hook into to clear the response?

4 Answers, 1 is accepted

Sort by
0
Cori
Top achievements
Rank 2
answered on 07 Oct 2010, 09:24 PM
Hello David,

I don't believe the ExportContent event is raised when exporting to PDF, only RTF. I've tried it myself and the event was never raised for PDF exporting.
0
David
Top achievements
Rank 1
answered on 08 Oct 2010, 07:51 AM
The ExportContent Event definitely works (I am using Q2 2009 Telerik Controls). I have already built the code to catch the event and then using iTextSharp I am adding a custom Header and Footer. What I want to be able to do though at the end of this method is to write my amended PDF to the Response so the user can download it. Unfortunately when I do that, RadEditor seems to be overwriting the Response Output Stream later in the page life cycle. As of yet I have not found anywhere in the lifecycle that I can hook into to do the same.

Once I have resolved the issue, I will share the code to add custom headers and footers.
0
Dobromir
Telerik team
answered on 13 Oct 2010, 10:03 AM
Hi,

RadEditor's ExportContent event is fired for ExportToPdf and ExportToRtf methods. At present, it is not possible to cancel or interrupt the default execution of ExportToRtf() and ExportToPdf() methods of RadEditor.

However, we always try to improve RadControls and we will consider the implementation of such functionality. Unfortunately, I am unable to provide you a firm estimate. I will bookmark this ticket and write back to you as soon as we get more details on the subject.

Best wishes,
Dobromir
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
David
Top achievements
Rank 1
answered on 13 Oct 2010, 10:15 AM
I have now worked out how to undertake this action. The Response just needs to be closed after writing out my amended document to the response. Code is below for adding custom header and footer which kicks off from btnSubmit_Click:

private

 

float FooterHeight = 0;

 

protected void btnSubmit_Click(object sender, EventArgs e)

 

{

 

//Calculate the height of the footer if applicable and set the margins apprpriately

iTextSharp.text.

Image CMlogo = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath("~/images/HeaderFooter/MyFooter.png"));

 

 

CMlogo.ScalePercent(32);

FooterHeight = CMlogo.ScaledHeight;

 

 

 

PdfDesigner.ExportToPdf();

}

 

 

 

private PdfPTable GetHeaderFooterTable(string url, float documentWidth)

{

 

//I use a PdfPtable with 1 column to position my header where I want it

 

PdfPTable headerTbl = new PdfPTable(1);

 

//set the width of the table to be the same as the document

 

headerTbl.TotalWidth = documentWidth;

//I use an image logo in the header so I need to get an instance of the image to be able to insert it. I believe this is something you couldn't do with older versions of iTextSharp

 

iTextSharp.text.

Image logo = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath(url));  

 

//I used a large version of the logo to maintain the quality when the size was reduced. I guess you could reduce the size manually and use a smaller version, but I used iTextSharp to reduce the scale. As you can see, I reduced it down to 32% of original size.

 

logo.ScalePercent(32);

//create instance of a table cell to contain the logo

 

PdfPCell cell = new PdfPCell(logo);

 

//align the logo to the right of the cell

 

cell.HorizontalAlignment =

Element.ALIGN_LEFT;

 

//add a bit of padding to bring it away from the right edge

 

cell.PaddingRight = 20;

//remove the border

 

cell.Border = 0;

//Add the cell to the table

 

headerTbl.AddCell(cell);

//I use a PdfPtable with 1 column to position my header where I want it

 

return headerTbl; 

}

 

 

protected void PdfDesigner_ExportContent(object sender, Telerik.Web.UI.EditorExportingArgs e)

{

 

float width = 595;

 

float height = 841;

 

//Get the generated PDF as byte array

 

Byte[] info = System.Text.Encoding.Default.GetBytes(e.ExportOutput);

stream =

new MemoryStream();  

 

 

iTextSharp.text.

Document document = new iTextSharp.text.Document(new Rectangle(width, height), 0, 0, 0, 0);

 

 

 

PdfReader reader = new PdfReader(info);

 

PdfWriter writer = PdfWriter.GetInstance(document, stream);

document.Open();

 

PdfContentByte cb = writer.DirectContent;  

 

PdfImportedPage page;  

 

int rotation;

 

int n = reader.NumberOfPages;  

 

PdfPTable headerTbl = new PdfPTable(1);
PdfPTable footerTbl = new PdfPTable(1);

 

 

headerTbl = GetHeaderFooterTable(

"~/images/HeaderFooter/MyHeader.png", document.PageSize.Width);

 

 

footerTbl = GetHeaderFooterTable(

"~/images/HeaderFooter/MyFooter.png", document.PageSize.Width);
int
i = 0;

 

 

 

 

while (i < n)

{

i++;

 

document.SetPageSize(new Rectangle(width, height));  

 

 

document.NewPage();

if (rdoHeaderFooter.SelectedValue != "0")

{

headerTbl.WriteSelectedRows(0, -1, 0, (document.PageSize.Height), writer.DirectContent);

footerTbl.WriteSelectedRows(0, -1, 0, FooterHeight, writer.DirectContent);

}

page = writer.GetImportedPage(reader, i);

rotation = reader.GetPageRotation(i);

 

 

if (page.Height < page.Width)

{

cb.AddTemplate(page, 0, -1f, 1f, 0, 0, height);

}

 

 

else

 

{

if (rotation == 90 || rotation == 270)

{

cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);

}

 

 

else

 

{

cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);

}

}

}

 

writer.CloseStream =

false;

 

 

 

document.Close();

 

Response.Clear();

Response.ContentType =

"application/pdf";

 

 

Response.AddHeader(

"Content-Disposition", "inline;attachment; filename=" + "File.pdf");

 

 

 

HttpContext.Current.Response.BinaryWrite(stream.ToArray());

Response.Flush();

Response.Close();

 

}

 


 

 

 

 

Tags
Editor
Asked by
David
Top achievements
Rank 1
Answers by
Cori
Top achievements
Rank 2
David
Top achievements
Rank 1
Dobromir
Telerik team
Share this question
or