how to resize a pdf file from legal format to letter format using c#` -
i have pdf files exported legal format , want convert them letter format (basically shrink them), each file may have 1 3 pages, below code tried have these problems:
the page size reduced good, can't use margin properties put page @ correct borders of container (the page kind of shrinked drawn somewhere @ bottom of resulted pdf file)
i couldn't increment number of pages code draws both pages, 1 on top of other.
here's code
pdfimportedpage page; pdfreader reader = new pdfreader(@"c:\pdf\legalformat.pdf"); document doc = new document(pagesize.a4, 0, 0, 0, 0); pdfwriter writer = pdfwriter.getinstance(doc, new filestream(@"c:\pdf\letterformat.pdf", filemode.create)); doc.open(); pdfcontentbyte cb = writer.directcontent; (int = 1 ; < reader.numberofpages + 1; i++){ page = writer.getimportedpage(reader, i); // number of page float scale = 0.67f; cb.addtemplate(page, scale, 0, 0, scale, 0, 0); } doc.close();
problem solved:
in main proc run test.
string original = args[0]; string inpdf = original; string outpdf = directory.getdirectoryroot(original) + "temp.pdf"; pdfreader pdfr = new pdfreader(inpdf); document doc = new document(pagesize.letter); document.compress = true; pdfwriter writer = pdfwriter.getinstance(doc, new filestream(outpdf, filemode.create)); doc.open(); pdfcontentbyte cb = writer.directcontent; pdfimportedpage page; (int = 1; < pdfr.numberofpages + 1; i++) { page = writer.getimportedpage(pdfr, i); cb.addtemplate(page, pagesize.letter.width / pdfr.getpagesize(i).width, 0, 0, pagesize.letter.height / pdfr.getpagesize(i).height, 0, 0); doc.newpage(); } doc.close(); //just renaming, conversion / resize process ends @ doc.close() above file.delete(original); file.copy(outpdf, original);
Comments
Post a Comment