locked
how to implement day/night mode for pdfrendering in Windows Store?

Answers

  • You can set the page background in the PdfPageRenderOptions you pass to RenderToStreamAsync (or the PDF_RENDER_PARAMS if you're using IPdfRendererNative and RenderPageToSurface, but there isn't a way to override the foreground at that level.

    Once you've rendered the page into an off-screen bitmap you can edit its pixels. For example, you can load it into a WriteableBitmap and then loop through the pixels and invert the colours:

                StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///assets/demo.pdf"));
                PdfDocument pdfDoc = await PdfDocument.LoadFromFileAsync(file);
                PdfPage pdfPage = pdfDoc.GetPage(0);
    
                using (IRandomAccessStream stream = new InMemoryRandomAccessStream())
                {
                    PdfPageRenderOptions options = new PdfPageRenderOptions();
    
                    await pdfPage.RenderToStreamAsync(stream, options);
    
                    WriteableBitmap wb = new WriteableBitmap((int)pdfPage.Size.Height, (int)pdfPage.Size.Width);
                    await wb.SetSourceAsync(stream);
    
                    using (Stream pixels = wb.PixelBuffer.AsStream())
                    {
                        pixels.Seek(0, SeekOrigin.Begin);
    
                        for (int i = 0; i < pixels.Length; i++)
                        {
                            byte subPixel = (byte)pixels.ReadByte();
                            // WB pixels are RGBA. Only change RGB, not A
                            if ((i + 1) % 4 != 0)
                            {
                                // write over the same pixel we just read
                                pixels.Seek(-1, SeekOrigin.Current);
                                // write the modified pixel (inverted colour in this case)
                                pixels.WriteByte((byte)(byte.MaxValue - subPixel));
                            }
                        }
                    }
                    // Display the page on an Image in our Xaml Visual Tree
                    img.Source = wb;


    • Marked as answer by venkata suresh Monday, September 29, 2014 6:30 AM
    Friday, September 26, 2014 8:00 PM
    Owner
  • Why can't you enumerate the pages? How do you get them into your page otherwise?

    That doesn't have anything to do with your night-mode transformation. You need to read the pages in to display them (by enumerating through the pages in the document) and can apply the night-mode transform the same way for the other pages as you do the first as you read them in.

    • Marked as answer by venkata suresh Friday, December 19, 2014 12:35 PM
    Friday, December 19, 2014 1:42 AM
    Owner

All replies

  • I never heard of this before.  Put an opaque layer over the PDF to lower the brightness.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, September 23, 2014 2:13 PM
    Moderator
  • Hi,

    i am rendering PDF from pdfdocument API. Day night mode means for the PDF page background to be black and the Foreground to be white and vice versa..suggest me Some idea on this

    Thanks


    m venkatasuresh

    Friday, September 26, 2014 1:45 PM
  • You can set the page background in the PdfPageRenderOptions you pass to RenderToStreamAsync (or the PDF_RENDER_PARAMS if you're using IPdfRendererNative and RenderPageToSurface, but there isn't a way to override the foreground at that level.

    Once you've rendered the page into an off-screen bitmap you can edit its pixels. For example, you can load it into a WriteableBitmap and then loop through the pixels and invert the colours:

                StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///assets/demo.pdf"));
                PdfDocument pdfDoc = await PdfDocument.LoadFromFileAsync(file);
                PdfPage pdfPage = pdfDoc.GetPage(0);
    
                using (IRandomAccessStream stream = new InMemoryRandomAccessStream())
                {
                    PdfPageRenderOptions options = new PdfPageRenderOptions();
    
                    await pdfPage.RenderToStreamAsync(stream, options);
    
                    WriteableBitmap wb = new WriteableBitmap((int)pdfPage.Size.Height, (int)pdfPage.Size.Width);
                    await wb.SetSourceAsync(stream);
    
                    using (Stream pixels = wb.PixelBuffer.AsStream())
                    {
                        pixels.Seek(0, SeekOrigin.Begin);
    
                        for (int i = 0; i < pixels.Length; i++)
                        {
                            byte subPixel = (byte)pixels.ReadByte();
                            // WB pixels are RGBA. Only change RGB, not A
                            if ((i + 1) % 4 != 0)
                            {
                                // write over the same pixel we just read
                                pixels.Seek(-1, SeekOrigin.Current);
                                // write the modified pixel (inverted colour in this case)
                                pixels.WriteByte((byte)(byte.MaxValue - subPixel));
                            }
                        }
                    }
                    // Display the page on an Image in our Xaml Visual Tree
                    img.Source = wb;


    • Marked as answer by venkata suresh Monday, September 29, 2014 6:30 AM
    Friday, September 26, 2014 8:00 PM
    Owner
  • @Rob Caplan: Thanks for the reply . Actually i am rendering pdf  and using vertical  Scroll viewer for Scrolling PDF. 

    briefly if i click button the entire PDF pages  need to change its Color.. I Want to know is it possible to do the Same for all pages of PDF  or for a particular page only? 

    m venkatasuresh

    Wednesday, October 1, 2014 5:21 AM
  • You can do the same for each page. For demonstration purposes I only modified the first page. In a real app you'd enumerate the pages and could modify each one before adding it to your ScrollViewer.

    --Rob

    Thursday, October 2, 2014 1:33 AM
    Owner
  • Hi Rob,

    Working Fine with the Single page of the PDF .for this issue any Solution to convert all the pages of the entire PDFDocument  Inside the Scroll Viewer Without the need of Enumerating or Modify Dynamically all the Time?

    Suggest me Any best possible Solution.


    m venkatasuresh

    Wednesday, December 3, 2014 7:03 AM
  • You will need to enumerate the pages to apply this conversion to each one.

    Wednesday, December 3, 2014 9:38 PM
    Owner
  • Hi Rob,

    This Solution  for one page is Working Fine.i am having PDF with 100 pages .when i turned into Night mode  all the pages of PDF need to convert into Night Mode Like  PDF reader ..Is that possible?If yes Please Suggest me Best Solution..

    Thanks


    m venkatasuresh

    Thursday, December 11, 2014 11:46 AM
  • Enumerate all of the pages and apply the same transform to each page when you display it.
    Thursday, December 11, 2014 4:39 PM
    Owner
  • Hi rob,

    my requirement is to scroll the PDF page in the Night mode.. I cant Enumerate like that.please Suggest the Suitable Solution.


    m venkatasuresh

    Tuesday, December 16, 2014 5:38 AM
  • Why can't you enumerate the pages? How do you get them into your page otherwise?

    That doesn't have anything to do with your night-mode transformation. You need to read the pages in to display them (by enumerating through the pages in the document) and can apply the night-mode transform the same way for the other pages as you do the first as you read them in.

    • Marked as answer by venkata suresh Friday, December 19, 2014 12:35 PM
    Friday, December 19, 2014 1:42 AM
    Owner
  • HI Rob,

    Thanks  I Did it :)


    m venkatasuresh

    Friday, December 19, 2014 12:35 PM