Step by step converting a PDF page to PNG using WinRT
Python 3.7.8 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 10:03:53) [MSC v.1916 64 bit (AMD64)] on win32 | |
Type "help", "copyright", "credits" or "license" for more information. | |
>>> import os, time | |
>>> PDF_FILENAME = input("Path to PDF: ") | |
>>> OUT_FILE = os.path.abspath(input("Path to output PNG: ")) | |
>>> | |
>>> import winrt.windows.data.pdf as PDF | |
>>> from winrt.windows.storage import StorageFile | |
>>> op = StorageFile.get_file_from_path_async(PDF_FILENAME) | |
>>> time.sleep(0.5) # should really await, but this is easier | |
>>> in_file = op.get_results() | |
>>> in_file | |
<_winrt_Windows_Storage.StorageFile object at 0x00000222A4795D30> | |
>>> op = PDF.PdfDocument.load_from_file_async(in_file) | |
>>> time.sleep(0.5) | |
>>> pdf = op.get_results() | |
<_winrt_Windows_Data_Pdf.PdfDocument object at 0x00000222A4795D90> | |
>>> pdf.page_count | |
10 | |
>>> page = pdf.get_page(1) | |
>>> page | |
<_winrt_Windows_Data_Pdf.PdfPage object at 0x00000222A4795D70> | |
>>> page.size.width | |
816.0 | |
>>> page.size.height | |
1056.0 | |
>>> | |
>>> from winrt.windows.storage import FileAccessMode, StorageOpenOptions | |
>>> from winrt.windows.storage.streams import FileRandomAccessStream, FileOpenDisposition | |
>>> op = FileRandomAccessStream.open_async(OUT_FILE, FileAccessMode.READ_WRITE, StorageOpenOptions.NONE, FileOpenDisposition.OPEN_ALWAYS) | |
>>> time.sleep(0.5) | |
>>> stream = op.get_results() | |
>>> | |
>>> op = page.render_to_stream_async(stream) | |
>>> time.sleep(0.5) | |
>>> op.get_results() | |
>>> stream.close() | |
>>> page.close() | |
>>> os.startfile(OUT_FILE) |
This comment has been minimized.
This comment has been minimized.
Thanks a lot! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
This page helped me a lot: https://github.com/microsoft/Windows-universal-samples/blob/master/Samples/PdfDocument/cs/Scenario1_Render.xaml.cs