ITextSharp - Some pages are not marked as expected.

I use iTextSharp 5.0.6 to read an existing PDF file, repeat each page, typing on each, and then writing a new PDF stamp. The problem I am facing is that this does not work 100% of the time. For some PDF files, each page is marked as expected, for other pages most of the stamps, and some not. There seems to be a potential problem where the GetOverContent () template does not return the topmost layer, but this is just an assumption. Has anyone had a similar problem?

using iTextSharp.text; using iTextSharp.text.pdf; const string WATERMARK_TEXT = "John Doe"; static void Main(string[] args) { string masterPdf = "master.pdf"; string pdfToCreate = "watermark.pdf"; byte[] bytes = StampPDF(masterPdf); using (FileStream stream = new FileStream(pdfToCreate, FileMode.Create)) { stream.Write(bytes, 0, bytes.Length); } } static byte[] StampPDF(string PdfPath) { using (MemoryStream memoryStream = new MemoryStream()) { PdfReader reader = new PdfReader(PdfPath); int pageCount = reader.NumberOfPages; PdfStamper stamper = new PdfStamper(reader, memoryStream); float fontSize = 9; float textAngle = 0f; BaseFont font = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.EMBEDDED); BaseColor backgroundColor = new BaseColor(0, 0, 0); BaseColor fontColor = new BaseColor(255, 255, 255); float padding = 2f; float fontWidth = font.GetWidthPoint(WATERMARK_TEXT, fontSize); iTextSharp.text.Rectangle pageSize; PdfContentByte pageContents; for (int i = 1; i <= pageCount; i++) { pageSize = reader.GetPageSize(i); pageContents = stamper.GetOverContent(i); //draw a rectangle pageContents.SetColorFill(backgroundColor); pageContents.MoveTo(pageSize.Width - (fontWidth + padding), 0f); pageContents.LineTo(pageSize.Width, 0f); pageContents.LineTo(pageSize.Width, 14f); pageContents.LineTo(pageSize.Width - (fontWidth + padding), 14f); pageContents.Fill(); //drop our watermark on top of the rectangle we just created pageContents.BeginText(); pageContents.SetColorFill(fontColor); pageContents.SetFontAndSize(font, fontSize); pageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, WATERMARK_TEXT, pageSize.Width - fontWidth, 4, textAngle); pageContents.EndText(); } stamper.Close(); reader.Close(); return memoryStream.ToArray(); } } 
+4
source share
2 answers

For those who may encounter the same problem, the key checks the CropBox. Since PDF CropBox may be smaller than its page size, you need to conditionally use one or the other. Thus, based on the sample code above, the for loop will be changed like this:

 for (int i = 1; i <= pageCount; i++) { mediaBox = reader.GetPageSize(i); cropBox = reader.GetCropBox(i); overContent = stamper.GetOverContent(i); if (cropBox != null && (cropBox.Width < mediaBox.Width || cropBox.Height < cropBox.Height)) mediaBox = cropBox; //draw a rectangle overContent.SetColorFill(backgroundColor); overContent.MoveTo(mediaBox.Right - (fontWidth + fontPadding), mediaBox.Bottom); overContent.LineTo(mediaBox.Right, mediaBox.Bottom); overContent.LineTo(mediaBox.Right, mediaBox.Bottom + rectangleHeight); overContent.LineTo(mediaBox.Right - (fontWidth + fontPadding), mediaBox.Bottom + rectangleHeight); overContent.ClosePathFillStroke(); //drop our watermark on top of the rectangle we just created overContent.BeginText(); overContent.SetColorFill(fontColor); overContent.SetFontAndSize(font, fontSize); overContent.ShowTextAligned(PdfContentByte.ALIGN_LEFT, WATERMARK_TEXT, mediaBox.Right - fontWidth, mediaBox.Bottom + (rectangleHeight - fontSize), textAngle); overContent.EndText(); } 
+2
source

You made two mistakes:

  • You assume that the pages are not rotated, but they can be: 90, 180, 270. Please note that I have never seen 180 pages, but they are legal. When drawing on a rotated page, you must consider this rotation when drawing. Fun with transformation matrices.

  • You assume that the bottom left corner of the page (no revolution) is 0.0. You base your measurements on the width and height of the page (close), but do not adjust any offset in this lower left corner.

There are three ways to create a landscape page:
11 "x8.5"
8.5 "x11" when rotated 90 degrees
8.5 "x11" when rotated 270 degrees

Technically, the 4th way is to build 11x8.5 @ 180, but anyone who writes such code should be punished. A lot of.

There are various questions about how to deal with page rotation. Following your code, I would say that you will quickly find the llx, lly thing.

0
source

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


All Articles