Click here to Skip to main content
15,394,479 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I create a custom Pdf document in my application where among a lot of other things I print other images and pdfs inside it, recently I got a pdf document that contains two pages of an ID document, this pdf looks perfectly fine when it's viewed in Acrobat or another tool but when is printed in that other pdf via iTextSharp it's printed like an A4 page with the page in the right corner and a lot of white space, also worth to be noted I can print small sized Pdfs, also if I resize that pdf to a custom size, say 3in and 2inch it's displayed fine.
Below you'll find the code and the documents created and used:

Click: Link of pdf documents

C#
public static void AttachPdfDocument(DruckKontext<DruckDatenOhneErgebnis> druckKontext, string pdfBaseString, string docName,
           Dictionary<string, List<KeyValuePair<string, string>>> fieldNamesAndValues)
        {
            try
            {
                //druckKontext.AddHigHighlightedTitle("Angehängtes Dokument: " + docName);
                //druckKontext.PosY -= 90;
                PdfReader reader = new PdfReader(Convert.FromBase64String(pdfBaseString));
                PdfReader.unethicalreading = true;

                byte[] newDoc = null;

                using (MemoryStream streamTemplate = new MemoryStream())
                {
                    using (PdfStamper pdfStamper = new PdfStamper(reader, streamTemplate))
                    {
                        pdfStamper.FormFlattening = true;
                        pdfStamper.Close();
                    }
                    newDoc = streamTemplate.ToArray();
                }

                reader = new PdfReader(newDoc);
                PdfWriter writer = druckKontext.Writer;
                PdfContentByte canvas = writer.DirectContent;
                for (int p = 1; p <= reader.NumberOfPages; p++)
                {
                    AddDocumentUploadHeaders(druckKontext, "I. Dokumentenuploads", docName + " (" + p + " / " + reader.NumberOfPages + ")", fieldNamesAndValues);

                    float pageWidth = druckKontext.DocumentStyle.RechterRand - 90;
                    float pageHeight = druckKontext.PosY - 100;
                    PdfImportedPage page = canvas.PdfWriter.GetImportedPage(reader, p);
                    Rectangle pagesize = reader.GetPageSizeWithRotation(p);
                    float oWidth = pagesize.Width;
                    float oHeight = pagesize.Height;
                    float scale = getScale(oWidth, oHeight);
                    float scaledWidth = oWidth * scale;
                    float scaledHeight = oHeight * scale;
                    int rotation = pagesize.Rotation;

                    var rect = new iTextSharp.text.Rectangle(550, 680, 30, 100);
                    rect.BackgroundColor = BaseColor.LIGHT_GRAY;
                    canvas.Rectangle(rect);

                    var x = 30 + (pageWidth / 2) - (scaledWidth / 2);
                    var y = 100 + (pageHeight / 2) - (scaledHeight / 2);

                    AffineTransform transform = new AffineTransform(scale, 0, 0, scale, 0, 0);
                    switch (rotation)
                    {
                        case 0:
                            transform = new AffineTransform(scale, 0, 0, scale, x, y);
                            canvas.AddTemplate(page, transform);
                            break;

                        case 90:
                            AffineTransform rotate90 = new AffineTransform(0, -1f, 1f, 0, x, scaledHeight + y);
                            rotate90.Concatenate(transform);
                            canvas.AddTemplate(page, rotate90);
                            break;

                        case 180:
                            AffineTransform rotate180 = new AffineTransform(-1f, 0, 0, -1f, scaledWidth + x, scaledHeight + y);
                            rotate180.Concatenate(transform);
                            canvas.AddTemplate(page, rotate180);
                            break;

                        case 270:
                            AffineTransform rotate270 = new AffineTransform(0, 1f, -1f, 0, scaledWidth + x, y);
                            rotate270.Concatenate(transform);
                            canvas.AddTemplate(page, rotate270);
                            break;

                        default:
                            canvas.AddTemplate(page, scale, 0, 0, scale, x, y);
                            break;
                    }

                    if (p < reader.NumberOfPages)
                    {
                        druckKontext.NewPageBeratungsdokumentation();
                        //druckKontext.PosY -= 40;
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }

        private static float getScale(float width, float height)
        {
            float scaleX = (float)((PageSize.A4.Width / width) * 0.7);
            float scaleY = (float)((PageSize.A4.Height / height) * 0.7);
            return Math.Min(scaleX, scaleY);
        }


What I have tried:

At first I thought that the problem lays withing the code not printing small sized Pdf pages properly but then I realized it's the way the Pdf is created, so I tried different ways of creating a pdf, but no solution found.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900