It is possible to cut an image of ItextSharp.Text.Image?

别说谁变了你拦得住时间么 提交于 2019-12-24 07:26:01

问题


I'm creating a pdf file from an large image, but my image is too large, and it does not fit in the only page. then i need split this image to create more pages. any idea?

 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");

                }
            }
}

来源:https://stackoverflow.com/questions/37734347/it-is-possible-to-cut-an-image-of-itextsharp-text-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!