How to rotate a PDF page using iTextSharp without causing errors in ghostscript?

I am trying to use iTextSharp to rotate a page and read it with ghostscript to create pages as images. This is how I rotate the pages:

byte[] retVal = null; using (MemoryStream oOutput = new MemoryStream()) { PdfReader oPDFReader = new PdfReader(BaseFile); PdfStamper stamper = new PdfStamper(oPDFReader, oOutput ); for (var pageNum = 1; pageNum <= oPDFReader.NumberOfPages; pageNum++) { var oDocumentPage = DocumentPages.Where(page => page.PageNumber == pageNum && page.RotationDegree != 0).FirstOrDefault(); if (oDocumentPage != null ) { if (oDocumentPage.RotationDegree < 0) oDocumentPage.RotationDegree = oDocumentPage.RotationDegree + 360; //make sure this is a positive value. int desiredRot = oDocumentPage.RotationDegree; PdfDictionary oPageDict = oPDFReader.GetPageN(pageNum); PdfNumber rotation = oPageDict.GetAsNumber(PdfName.ROTATE); if (rotation != null) { desiredRot += rotation.IntValue; desiredRot %= 360; // must be 0, 90, 180, or 270 } oPageDict.Put(PdfName.ROTATE, new PdfNumber(desiredRot)); } } stamper.FormFlattening = true; stamper.Writer.CloseStream = false; stamper.Close(); retVal = oOutput.ToArray(); oOutput.Close(); } return retVal; 

But when I run this in ghostscript, I get the following error:

  **** Warning: An error occured while reading an XREF table. **** The file has been damaged. This may have been caused **** by a problem while converting or transfering the file. **** Ghostscript will attempt to recover the data. **** **** This file had errors that were repaired or ignored. **** The file was produced by: **** >>>> Adobe LifeCycle Designer ES 8.2; modified using iTextSharp 4.1.6 by 1T3XT <<<< **** Please notify the author of the software that produced this **** file that it does not conform to Adobe published **** PDF specification. 

I am currently referencing ghostscript in a WCF project, so this console.out message really crashes my program. Ideally, I want to generate a PDF file without the program selecting this error, but as an alternative, I am considering placing this WCF in the console so that it can pull out bug fixes into its hearty content.

Any idea?

+1
source share

Source: https://habr.com/ru/post/978408/


All Articles