可以剪切ItextSharp.Text.Image的图像吗?

时间:2016-06-09 19:11:07

标签: c# asp.net-mvc pdf itext

我正在从大图片创建一个pdf文件,但我的图片太大,并且它不适合唯一的页面。然后我需要拆分此图像以创建更多页面。 任何想法?

 public FileResult ResultadoParaPdf(string file)
        {

            string fileStringReplace = file.Replace("data:image/png;base64,", "");
            var image = Convert.FromBase64String(fileStringReplace);


            const int HorizontalMargin = 40;
            const int VerticalMargin = 40;

            using (var outputMemoryStream = new MemoryStream())
            {
                using (var pdfDocument = new Document(PageSize.A4, HorizontalMargin, HorizontalMargin, VerticalMargin, VerticalMargin))
                {
                    iTextSharp.text.pdf.PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDocument, outputMemoryStream);
                    pdfWriter.CloseStream = false;

                    pdfDocument.Open();

                    PdfPTable table = new PdfPTable(1);
                    table.WidthPercentage = 100;
                    Image img = Image.GetInstance(image);
                    img.WidthPercentage = 100;
                    PdfPCell c = new PdfPCell(img, true);
                    c.Border = PdfPCell.NO_BORDER;
                    c.Padding = 5;
                    c.Image.ScaleToFit(750f, 750f);
                    table.AddCell(c);
                    pdfDocument.Add(table);

                    pdfDocument.Close();

                    byte[] created = outputMemoryStream.ToArray();

                    outputMemoryStream.Write(created, 0, created.Length);
                    outputMemoryStream.Position = 0;

                    return  File(created, "application/pdf", "teste.pdf");

                }
            }
}

0 个答案:

没有答案