c# - Why does switching my logo give me a corrupt PDF file? -
c# - Why does switching my logo give me a corrupt PDF file? -
i creating pdf file in asp.net c# windows console application, using itextsharp.
i have 1 piece of code. if site 'lmh', pdf can open adobe reader. if not, error: there error processing page. there problem reading document (114).
here code:
string applicationpath = system.io.directory.getcurrentdirectory(); pdfptable table = new pdfptable(4) { totalwidth = 800.0f, lockedwidth = true }; float[] widths = new[] { 80.0f, 80.0f, 500.0f, 140.0f }; table.setwidths(widths); table.horizontalalignment = 0; table.defaultcell.border = 0; if (activeprofile.site == "lmh") { image hmsimage = image.getinstance(applicationpath + "\\" + "hms logo.png"); hmsimage.scaletofit(80.0f, 40.0f); pdfpcell hmslogo = new pdfpcell(hmsimage); hmslogo.border = 0; hmslogo.fixedheight = 60; table.addcell(hmslogo); } else { image blankimage = image.getinstance(applicationpath + "\\" + "emptylogo.png"); blankimage.scaletofit(80.0f, 40.0f); pdfpcell emptycell = new pdfpcell(blankimage); emptycell.border = 0; emptycell.fixedheight = 60; table.addcell(emptycell); } and main trunk:
system.io.filestream file = new system.io.filestream(("c:/") + keypropertyid + ".pdf", system.io.filemode.openorcreate); document document = new document(pagesize.a4.rotate () , 20, 20, 6, 4); pdfwriter author = pdfwriter.getinstance(document, file ); document.addtitle(_title); document.open(); addheader(document); addgeneralinfo(document, keypropertyid); addappliances(document, _lgsrobj); addfaults(document, _lgsrobj); addalarms(document, _lgsrobj); addfinalcheck(document, _lgsrobj); addsignatures(document, _lgsrobj); addfooter(document, writer); document.close(); author .close (); file.close(); all has changed logo. both logo files have same dimensions. can perchance wrong? btw. file opens fine using foxit, not acceptable solution.
this pdf can't open adobe reader: https://dl.dropboxusercontent.com/u/20086858/1003443.pdf
this emptylogo.png file: https://dl.dropboxusercontent.com/u/20086858/emptylogo.png
this logo works: https://dl.dropboxusercontent.com/u/20086858/hms%20logo.png
this 'good' version of pdf logo works: https://dl.dropboxusercontent.com/u/20086858/1003443-good.pdf
it suspicious both 'good' , not-so-good version have identical size in spite of different image sizes. comparing them 1 sees both files differ in first 192993 bytes there on little. furthermore broken pdf contains eof markers in part denoting file ends @ indices 140338 , 192993 next bytes not @ clean incremental update.
cutting file @ first denoted file end, 140338, 1 gets file op wanted have.
thus:
the code overwrites existing files new data; if former file longer, remainder of longer file remains trash @ end and, therefore, renders new file broken.
the op opens stream this:
new system.io.filestream(("c:/") + keypropertyid + ".pdf", system.io.filemode.openorcreate); filemode.openorcreate causes observed behavior.
the filemode values documented here on msdn, especially:
thus, use
new system.io.filestream(("c:/") + keypropertyid + ".pdf", system.io.filemode.create); instead.
c# asp.net pdf adobe itextsharp
Comments
Post a Comment