c# - How to resize PDF Document to fit a Bitmap -
( using pdf sharp )
i have few bitmaps , each bitmap create new pdf page. problem pdf page not have enough height contain entire bitmap, i'm losing portion of bitmap.
what best way resize pdf page entire bitmap fit 1 pdf page?
public static pdfdocument getpdf(list<bitmap> pages, bool makefit = false) { using (var doc = new pdfdocument()) { (byte = 0; < pages.count(); i++) { pdfpage opage = new pdfpage(); doc.pages.add(opage); using (var xgr = xgraphics.frompdfpage(opage)) { using (var bm = pages[i]) { using (var img = ximage.fromgdiplusimage(bm)) { xgr.drawimage(img, 0, 0); } } } } return doc; } }
i did try set size @ location
xgr.drawimage(img, 0, 0);
but made bitmap larger , not pdf page.
i may need point out concept behind saving bitmaps pdf pages, , give small simple example of how done.
i've looked on of files @ pdf sharp , they're not easy grasp.
i'm hoping can tell me how approach in simplest form. need save bitmap pdf page , make sure bitmap has been saved pdf page viewable.
thank you!!
------------------------------------------edit------------------------------------
i've added updated version, snapshot on left of raw .png image being rendered in browser , 1 on right browser rendering pdf.
here updated code:
public static pdfdocument getpdf(list<bitmap> pages, bool makefit = false) { using (var doc = new pdfdocument()) { (byte = 0; < pages.count(); i++) { pdfpage opage = new pdfpage(); doc.pages.add(opage); if (makefit) { xsize size = new xsize(pages[i].width, pages[i].height); opage.height = size.height; opage.width = size.width; } using (var xgr = xgraphics.frompdfpage(opage)) { using (var bm = pages[i]) { using (var img = ximage.fromgdiplusimage(bm)) { xgr.drawimage(img, 0, 0); } } } } return doc; } }
----update image------------
------------------------------last edit works----------------------------
what i've done resize pdf page bitmap , drawed bitmap onto page , resize bitmap pdf page size -5px.
public static pdfdocument getpdf(list<bitmap> pages, bool makefit = false) { xsize size = new xsize(0, 0); using (var doc = new pdfdocument()) { (byte = 0; < pages.count(); i++) { pdfpage opage = new pdfpage(); doc.pages.add(opage); if (makefit) { size = new xsize(pages[i].width, pages[i].height); opage.height = size.height; opage.width = size.width; } using (var xgr = xgraphics.frompdfpage(opage)) { using (var bm = pages[i]) { using (var img = ximage.fromgdiplusimage(bm)) { if (makefit) { xgr.drawimage(img, 0, 0, size.width - 5, size.height - 5); } else { xgr.drawimage(img, 0, 0); } } } } } return doc; } }
--snap shots shows produce same view---------
perhaps you're looking for: http://www.pdfsharp.net/wiki/pagesizes-sample.ashx
in particular, note @ top: "you can set size of page size using width , height properties."
i.e.
opage.width = ... opage.height = ...
Comments
Post a Comment