简体   繁体   中英

PDFsharp example “ExportImages” removes watermark

Hello I'm using the PDFsharp example to convert PDFs to an image format. But my PDFs got a date stamp (watermark) and this gets lost in the image file. Is there a way to also get the stamp on the image?

The code ( http://www.pdfsharp.net/wiki/ExportImages-sample.ashx )

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;
        }
    }

The sample shows how to extract JPEG files that are embedded in PDF files. It does not render PDF pages to image files.

If there is a text watermark overlaying the image then you will get the image only.

PDFsharp cannot render PDF files, so there is no way to get a PDF page including the watermark as a graphics files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM