很多扫描版的电子书,留有很大的页边距,大屏的设备看起来没有啥影响,
可是在kindle上看起来就麻烦了,放大操作简直就没法用,最好能把留白去掉。
- 将pdf文件转换为图片
这个看看 例子里的 JpegDevice就可以了 - 设置需要裁剪的区域
首先将图片显示在pictureBox上,然后用鼠标画个差不多的框就可以了,
可能有方法识别书籍的内容区,那个我不会。
潜在的问题是有的书籍扫描得不是很好,偏得比较多,可能会导致内容被删除, - 将图片组装为pdf书
有个特殊的地方是dpi问题,
计算像素的时候,Aspose.Pdf好像用的是72,但是每个人的情况不一样,需要做个转换。int pageCount = pdfDocument.Pages.Count; Resolution resolution = new Resolution(300); if (pageCount == 0) { return; } float dpiX = 72; float dpiY = 72; using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero)) { dpiX = graphics.DpiX; dpiY = graphics.DpiY; } int pageWidth = rect.Width; float secPageWidth = (float)(pageWidth * 72 / dpiX); float secPageHeight = (float)(pdfDocument.PageInfo.Height * 72 / dpiY); System.Drawing.Rectangle clipRect = new System.Drawing.Rectangle(rect.X, 0, rect.Width, (int)pdfDocument.PageInfo.Height); JpegDevice jpegDevice = new JpegDevice((int)pdfDocument.PageInfo.Width, (int)pdfDocument.PageInfo.Height, resolution, 100); for (int i = 1; i <= pageCount; i++) { Aspose.Pdf.Generator.Section imageSection = newPdfFileDocument.Sections.Add(); imageSection.PageInfo.PageWidth = secPageWidth; imageSection.PageInfo.PageHeight = secPageHeight; Aspose.Pdf.Generator.MarginInfo marginInfo = new Aspose.Pdf.Generator.MarginInfo(); marginInfo.Bottom = 0; marginInfo.Left = 0; marginInfo.Right = 0; marginInfo.Top = 0; imageSection.PageInfo.Margin = marginInfo; Aspose.Pdf.Generator.Image imagePdfContent = new Aspose.Pdf.Generator.Image(imageSection); imageSection.Paragraphs.Add(imagePdfContent); imagePdfContent.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Jpeg; MemoryStream msSrcImg = new MemoryStream(); jpegDevice.Process(pdfDocument.Pages[i], msSrcImg); System.Drawing.Image image = System.Drawing.Image.FromStream(msSrcImg); System.Drawing.Bitmap bmpImage = new System.Drawing.Bitmap(image); System.Drawing.Bitmap bmpCrop = bmpImage.Clone(clipRect, bmpImage.PixelFormat); MemoryStream msCropImg = new MemoryStream(); bmpCrop.Save(msCropImg, ImageFormat.Jpeg); imagePdfContent.ImageInfo.ImageStream = msCropImg; GC.Collect(); }
时间: 2024-10-01 05:14:37