C# PDFsharp示例“;“输出图像”;删除水印

C# PDFsharp示例“;“输出图像”;删除水印,c#,pdfsharp,C#,Pdfsharp,您好,我正在使用PDFsharp示例将PDF转换为图像格式。但是我的PDF文件有一个日期戳(水印),这会在图像文件中丢失。有没有办法也能在图像上印上邮票 代码() private void GetImageFromPdf(字符串文件名路径) { PdfDocument document=PdfReader.Open(fileNamePath); int imageCount=0; //迭代页面 foreach(document.Pages中的PdfPage页) { //获取资源字典 PdfDic

您好,我正在使用PDFsharp示例将PDF转换为图像格式。但是我的PDF文件有一个日期戳(水印),这会在图像文件中丢失。有没有办法也能在图像上印上邮票

代码()

private void GetImageFromPdf(字符串文件名路径)
{
PdfDocument document=PdfReader.Open(fileNamePath);
int imageCount=0;
//迭代页面
foreach(document.Pages中的PdfPage页)
{
//获取资源字典
PdfDictionary resources=page.Elements.GetDictionary(“/resources”);
if(资源!=null)
{
//获取外部对象字典
PdfDictionary xObjects=resources.Elements.GetDictionary(“/XObject”);
if(xObject!=null)
{
ICollection items=xObjects.Elements.Values;
//迭代对外部对象的引用
foreach(PdfItem项目中的项目)
{
PdfReference reference=作为PdfReference的项目;
如果(引用!=null)
{
PdfDictionary xObject=引用。值为PdfDictionary;
//外部物体是图像吗?
if(xObject!=null&&xObject.Elements.GetString(“/Subtype”)==“/Image”)
{
ExportImage(xObject,ref imageCount);
}
}
}
}
}
}
System.Windows.Forms。
MessageBox.Show(imageCount+“导出图像”,“导出图像”);
}
静态无效导出映像(PdfDictionary映像,ref int计数)
{
字符串过滤器=image.Elements.GetName(“/filter”);
开关(过滤器)
{
大小写“/dct解码”:
exportJPEG图像(图像,参考计数);
打破
//案例“/格式代码”:
//ExportAsPngImage(图像,参考计数);
//中断;
}
}

此示例演示如何提取嵌入在PDF文件中的JPEG文件。它不会将PDF页面渲染为图像文件

如果图像上覆盖有文本水印,则只能获得图像

PDFsharp无法呈现PDF文件,因此无法获取包含水印的PDF页面作为图形文件

private void GetImageFromPdf(string fileNamePath)
    {
        PdfDocument document = PdfReader.Open(fileNamePath);

        int imageCount = 0;
        // Iterate pages
        foreach (PdfPage page in document.Pages)
        {
            // Get resources dictionary
            PdfDictionary resources = page.Elements.GetDictionary("/Resources");
            if (resources != null)
            {
                // Get external objects dictionary
                PdfDictionary xObjects = resources.Elements.GetDictionary("/XObject");
                if (xObjects != null)
                {
                    ICollection<PdfItem> items = xObjects.Elements.Values;
                    // Iterate references to external objects
                    foreach (PdfItem item in items)
                    {
                        PdfReference reference = item as PdfReference;
                        if (reference != null)
                        {
                            PdfDictionary xObject = reference.Value as PdfDictionary;
                            // Is external object an image?
                            if (xObject != null && xObject.Elements.GetString("/Subtype") == "/Image")
                            {
                                ExportImage(xObject, ref imageCount);
                            }
                        }
                    }
                }
            }
        }
        System.Windows.Forms.
        MessageBox.Show(imageCount + " images exported.", "Export Images");

    }

    static void ExportImage(PdfDictionary image, ref int count)
    {
        string filter = image.Elements.GetName("/Filter");
        switch (filter)
        {
            case "/DCTDecode":
                ExportJpegImage(image, ref count);
                break;

            //case "/FlateDecode":
            //    ExportAsPngImage(image, ref count);
            //    break;
        }
    }