ASP.NET下载带有嵌入式BMP的Aspose PDF

时间:2016-08-17 19:45:46

标签: c# asp.net aspose.pdf

我需要使用Aspose将BMP渲染为PDF并下载。

下载的PDF已损坏,Adobe无法打开它。保存到文件PDF很好并打开。有人知道为什么下载的PDF被破坏了吗?

enter image description here

    protected void ASPxButton1_OnClick(object sender, EventArgs e)
    {

         WebsiteToImage websiteToImage = new WebsiteToImage(HttpContext.Current.Request.Url.AbsoluteUri, @"C:\Temp\Test.jpg");
        var generate = websiteToImage.Generate();
        // Create a MemoryStream object from image Byte array
        MemoryStream ms = new MemoryStream();
        websiteToImage.Bitmap.Save(ms, ImageFormat.Jpeg);

        // create a PDF object
        Pdf pdf = new Pdf();
        // create a section and add it to pdf document
        Aspose.Pdf.Generator.Section MainSection = pdf.Sections.Add();
        //Add the radio form field to the paragraphs collection of the section
        // create an image object
        Aspose.Pdf.Generator.Image sample_image = new Aspose.Pdf.Generator.Image();
        // specify the image file path information
        //sample_image.ImageInfo.File = @"d:/pdftest/untitled.bmp";
        sample_image.ImageInfo.ImageStream = ms;
        // specify the image file type
        sample_image.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Bmp;
        // specify the image width information equal to page width 
        sample_image.ImageInfo.FixWidth = MainSection.PageInfo.PageWidth - MainSection.PageInfo.Margin.Left - MainSection.PageInfo.Margin.Right;
        // specify the image Height information equal to page Height
        sample_image.ImageInfo.FixWidth = MainSection.PageInfo.PageHeight - MainSection.PageInfo.Margin.Top - MainSection.PageInfo.Margin.Bottom;

        // create bitmap image object to load image information
        Bitmap myimage = websiteToImage.Bitmap;
        // check if the width of the image file is greater than Page width or not
        if (myimage.Width > MainSection.PageInfo.PageWidth)
            // if the Image width is greater than page width, then set the page orientation to Landscape
            MainSection.IsLandscape = true;
        else
            // if the Image width is less than page width, then set the page orientation to Portrait
            MainSection.IsLandscape = false;

        // add image to paragraphs collection of section
        MainSection.Paragraphs.Add(sample_image);
        // save the resultant PDF
        pdf.Save(@"C:\Temp\Test.pdf");
        pdf.Save(ms);
        byte[] bytes = ms.GetBuffer();
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ContentType = "application/pdf";
        HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + Operator.Emplid + "Gudiance.pdf");
        HttpContext.Current.Response.BinaryWrite(bytes);
        HttpContext.Current.Response.End();
        ms.Close();

1 个答案:

答案 0 :(得分:0)

您似乎正在使用传统的Aspose.Pdf.generator方法,因此为了满足您的要求,我们建议您尝试使用Aspose.Pdf命名空间的新文档对象模型。请尝试使用以下代码段。

    // create a PDF object
Document pdf = new Document();
// create a section and add it to pdf document
Aspose.Pdf.Page MainSection = pdf.Pages.Add();
//Add the radio form field to the paragraphs collection of the section
// create an image object
Aspose.Pdf.Image sample_image = new Aspose.Pdf.Image();
// specify the image file path information
sample_image.File = @"C:\pdftest\OutOfMemoryDemo\OutOfMemoryDemo\Sample.bmp";
// specify the image width information equal to page width 
sample_image.FixWidth = MainSection.PageInfo.Width - MainSection.PageInfo.Margin.Left - MainSection.PageInfo.Margin.Right;
// specify the image Height information equal to page Height
sample_image.FixWidth = MainSection.PageInfo.Height - MainSection.PageInfo.Margin.Top - MainSection.PageInfo.Margin.Bottom;

// create bitmap image object to load image information
System.Drawing.Bitmap myimage = new System.Drawing.Bitmap(@"C:\pdftest\OutOfMemoryDemo\OutOfMemoryDemo\Sample.bmp");
// check if the width of the image file is greater than Page width or not
if (myimage.Width > MainSection.PageInfo.Width)
    // if the Image width is greater than page width, then set the page orientation to Landscape
    MainSection.PageInfo.IsLandscape = true;
else
    // if the Image width is less than page width, then set the page orientation to Portrait
    MainSection.PageInfo.IsLandscape = false;

// add image to paragraphs collection of section
MainSection.Paragraphs.Add(sample_image);
MemoryStream stream = new MemoryStream();

// save the resultant PDF
pdf.Save(stream);

StreamReader reader = new System.IO.StreamReader(Request.InputStream); 
String Data = reader.ReadToEnd();
this.Title = Data;
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.Buffer = true;
Response.Charset = "UTF-8";
Response.AddHeader("Accept-Header", stream.Length.ToString());
Response.AddHeader("Content-Length", stream.Length.ToString());
Response.AddHeader("Expires", "0″");
Response.AddHeader("Pragma", "cache");
Response.AddHeader("Cache-Control", "private");
Response.AddHeader("content-disposition", String.Format("inline;filename={0}", "article" + "docId"+ ".pdf"));
Response.ContentType = "application/pdf";
Response.AddHeader("Accept-Ranges", "bytes");
Response.BinaryWrite(stream.ToArray());
Response.Flush();
Response.End();

PS,我的名字是Nayyer,我是Aspose的开发人员传播者。