- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何使用 PDFSharp 从 PDF 文档中提取经过 FlateDecoded(例如 PNG)的图像?
我在 PDFSharp 示例中发现了该评论:
// TODO: You can put the code here that converts vom PDF internal image format to a
// Windows bitmap
// and use GDI+ to save it in PNG format.
// [...]
// Take a look at the file
// PdfSharp.Pdf.Advanced/PdfImage.cs to see how we create the PDF image formats.
有人能解决这个问题吗?
感谢您的回复。
编辑:因为我无法在 8 小时内回答我自己的问题,所以我这样做了:
感谢您的快速回复。
我在“ExportAsPngImage”方法中添加了一些代码,但没有得到想要的结果。它只是提取了一些更多的图像 (png),它们没有正确的颜色并且被扭曲了。
这是我的实际代码:
PdfSharp.Pdf.Filters.FlateDecode flate = new PdfSharp.Pdf.Filters.FlateDecode();
byte[] decodedBytes = flate.Decode(bytes);
System.Drawing.Imaging.PixelFormat pixelFormat;
switch (bitsPerComponent)
{
case 1:
pixelFormat = PixelFormat.Format1bppIndexed;
break;
case 8:
pixelFormat = PixelFormat.Format8bppIndexed;
break;
case 24:
pixelFormat = PixelFormat.Format24bppRgb;
break;
default:
throw new Exception("Unknown pixel format " + bitsPerComponent);
}
Bitmap bmp = new Bitmap(width, height, pixelFormat);
var bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, pixelFormat);
int length = (int)Math.Ceiling(width * bitsPerComponent / 8.0);
for (int i = 0; i < height; i++)
{
int offset = i * length;
int scanOffset = i * bmpData.Stride;
Marshal.Copy(decodedBytes, offset, new IntPtr(bmpData.Scan0.ToInt32() + scanOffset), length);
}
bmp.UnlockBits(bmpData);
using (FileStream fs = new FileStream(@"C:\Export\PdfSharp\" + String.Format("Image{0}.png", count), FileMode.Create, FileAccess.Write))
{
bmp.Save(fs, System.Drawing.Imaging.ImageFormat.Png);
}
这是正确的方法吗?还是我应该选择另一种方式?非常感谢!
最佳答案
我知道这个答案可能要晚几年,但也许会对其他人有所帮助。
在我的例子中出现了这种困惑,因为 image.Elements.GetInteger(PdfImage.Keys.BitsPerComponent)
似乎没有返回正确的值。作为Vive la déraison根据您的问题指出,您将获得使用 Marshal.Copy
的 BGR 格式。因此,在执行 Marshal.Copy
后反转字节并旋转位图将完成这项工作。
生成的代码如下所示:
private static void ExportAsPngImage(PdfDictionary image, ref int count)
{
int width = image.Elements.GetInteger(PdfImage.Keys.Width);
int height = image.Elements.GetInteger(PdfImage.Keys.Height);
var canUnfilter = image.Stream.TryUnfilter();
byte[] decodedBytes;
if (canUnfilter)
{
decodedBytes = image.Stream.Value;
}
else
{
PdfSharp.Pdf.Filters.FlateDecode flate = new PdfSharp.Pdf.Filters.FlateDecode();
decodedBytes = flate.Decode(image.Stream.Value);
}
int bitsPerComponent = 0;
while (decodedBytes.Length - ((width * height) * bitsPerComponent / 8) != 0)
{
bitsPerComponent++;
}
System.Drawing.Imaging.PixelFormat pixelFormat;
switch (bitsPerComponent)
{
case 1:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format1bppIndexed;
break;
case 8:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format8bppIndexed;
break;
case 16:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format16bppArgb1555;
break;
case 24:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
break;
case 32:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
break;
case 64:
pixelFormat = System.Drawing.Imaging.PixelFormat.Format64bppArgb;
break;
default:
throw new Exception("Unknown pixel format " + bitsPerComponent);
}
decodedBytes = decodedBytes.Reverse().ToArray();
Bitmap bmp = new Bitmap(width, height, pixelFormat);
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
int length = (int)Math.Ceiling(width * (bitsPerComponent / 8.0));
for (int i = 0; i < height; i++)
{
int offset = i * length;
int scanOffset = i * bmpData.Stride;
Marshal.Copy(decodedBytes, offset, new IntPtr(bmpData.Scan0.ToInt32() + scanOffset), length);
}
bmp.UnlockBits(bmpData);
bmp.RotateFlip(RotateFlipType.Rotate180FlipNone);
bmp.Save(String.Format("exported_Images\\Image{0}.png", count++), System.Drawing.Imaging.ImageFormat.Png);
}
代码可能需要一些优化,但在我的例子中它确实正确地导出了 FlateDecoded Images。
关于c# - 如何使用 PDFSharp 从 PDF 中提取 FlateDecoded 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10024908/
我得到了具有其他语言字体的文件。他们无法下载字体,因此希望我将其嵌入pdf。 现在,我只有PDF阅读器,因此无法编辑或创建pdf文件。因此,我决定使用PDFSharp库在C#.NET中快速完成此操作,
使用 PDFsharp .NET 库,我需要将 PDF 文档中的页面大小设置为与图像相同。 示例,图像编号。 1 尺寸为 152 像素 x 1775 像素。 图片编号2 个 100 像素 x 1582
在 Migradoc 和 PDFSharp 示例页面中,有一个将 Migradoc 文档绘制为 PDFSharp 文档:http://www.pdfsharp.net/wiki/MixMigraDoc
我对设置XGraphics的剪切区域有点困惑,pdfSharp只提供了一个方法IntersectClip,这意味着如果我想改变剪切区域,该区域会越来越小(与之前的区域相交),有没有人知道无论如何自由设
在 Migradoc 和 PDFSharp 示例页面中,有一个将 Migradoc 文档绘制为 PDFSharp 文档:http://www.pdfsharp.net/wiki/MixMigraDoc
发现 PDFSharp 在使用比例变换以不同比例绘制时存在问题。 在此示例中,我们以不同比例绘制两个矩形,从 FixedDocumentSequence 生成 XPS,最后使用 PDFsharps X
我正在尝试获取新行,但如果我使用 \n 它不起作用。 任何通过向字符串添加一些东西来换行的方法,例如 \r\n (这也不起作用) gfx.DrawString("Project No \n" + te
是否可以从例如缩放页面? A2 到 A1 与 PDFsharp?我可以通过大小、宽度和高度来设置页面的大小。但是如何缩放页面的内容呢? 最佳答案 根据 Vive 的评论和那里提供的链接,这里有一个使用
如何在渲染之前获取页数?我需要在我的文档中将数字写成单词。 最佳答案 对于页码作为数字: 您不必获取编号,您可以在文档中添加占位符(称为字段),并且 MigraDoc 将在呈现文档时自动插入页码。 见
我正在使用 MigraDoc 创建 PDF。根据我的要求,我必须根据页面\部分的内容添加不同的标题。我可以通过添加新部分并为每个部分定义新标题来实现它。到这里为止,一切正常。 在页脚中,我使用“Add
希望有人可能遇到与此相同的问题并找到解决方案。 我正在尝试将表中的某些行设置为分组,以便如果一行落入下一页,其他行也会进入下一页。 我正在使用 PdfSharp 和 Migradoc 将行 KeepW
无论我是否使用 XTextFormatter,我都会遇到关于 LayoutRectangle 的高度必须为 0 或类似的错误。 new PdfSharp.Drawing.Layout.XTextFor
我正在使用 PDFsharp绘制形状相当复杂的线条。为了简化这个过程,让它更容易理解,我想把原点从页面的左上角改为左下角。目前,更改原点是 not yet implemented .有没有办法可以使用
我得到了以下代码,用于将两个文本框内容简单地添加到 pdf 文件中: using System; using System.Windows.Forms; using PdfSharp.Pdf; usi
我之前使用 PDFsharp 进行了一些文件合并,现在我正在尝试更改几个文件(插入或删除一些页面)并且我遇到了问题,即图书馆看不到页面。它说 PageCount == 0 并且我无法在对象中找到页面(
我正在尝试将我的位图保存在 .pdf 文件中。它不起作用。我没有收到错误,但它不会打开对话框来保存 .pdf 文件。 这是我的代码: PdfDocument doc = new PdfDocument
我在 C# 的 PDFSharp 中显示中文字符时遇到问题。在创建 PDF 字符串的过程中没问题,但在创建 pdf 文件后它不显示它。我找到了一个解决方案,它是 XFont font_small2 =
我目前正在研究 PDF 生成器。我需要先绘制背景图像,然后绘制一个透明度为 85% 的深色图层。我可以很好地绘制它,但是当我想在那之后绘制两个图像时,那些图像也获得了透明度,这不是我想要的。 XBru
是否可以为 PDFsharp 库找到好的文档? 我在 Google 上进行了搜索,但没有找到任何引用文档。 我不知道 XGraphics.RotateAtTransform 和 XGraphics.T
PDFSharp 支持在绘制长文本部分时自动换行: textFormatter.DrawString(text, font, XBrushes.Black, new XRect(x, y, textA
我是一名优秀的程序员,十分优秀!