2

I receive a pdf as a byte[]. When I save this binary as pdf, pageSize is too big. I want to change the pageSize in the code.

Currently I am trying it this way, based on what I found in other questions:

using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf; 

using (MemoryStream stream = new MemoryStream(pdfAsBinary))
{
    using (PdfReader reader = new PdfReader(pdfAsBinary))
    {
        using (Document doc = new Document(PageSize.A4))
        {
            PdfWriter writer = PdfWriter.GetInstance(doc, stream);
            PdfImportedPage page = writer.GetImportedPage(reader, 1);


            image = Image.GetInstance(page);
            using (var pdfStream = new FileStream(tempPath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
            {
                 PdfWriter pdfwriter = PdfWriter.GetInstance(doc, pdfStream);
                 doc.Open();
                 doc.SetPageSize(PageSize.A4);
                 image.ScalePercent(30f);
                 doc.Add(image);
                 doc.Close();
             }
         }
    }
 File.Copy(tempPath, pathToFile);
 }

Or I tried using this method:

private static byte[] resizeToA4(byte[] inputDoc)
{
    using (MemoryStream out = new MemoryStream())
    {
        using (PdfReader reader = new PdfReader(inputDoc))
        {
            using (Document doc = new Document(PageSize.A4))
            {
                PdfWriter writer = PdfWriter.GetInstance(doc, out);
            }
        }
    return outPDF.ToArray();
    }

None of the above is working and it feels like I'm overcomplicating things. How can I achieve my resizing of the pageSize to A4?

Magnus
  • 45,362
  • 8
  • 80
  • 118
xtra
  • 1,957
  • 4
  • 22
  • 40
  • what does "neither is working" mean? – Harry Jan 23 '18 at 14:03
  • I mean "none of the above" – xtra Jan 23 '18 at 14:05
  • What is `PdfReader`? If you are using a third-party lib it helps us if you tell us which one. And if you are getting an error, it helps if you include the exception stacktrace. – Dirk Vollmar Jan 23 '18 at 14:06
  • External libs are: using iTextSharp; using iTextSharp.text; using iTextSharp.text.pdf; – xtra Jan 23 '18 at 14:09
  • I get it in a huge size as a byte[] and then want to save it as an A4 pdf – xtra Jan 23 '18 at 14:16
  • what is the pdf size to start with? – Harry Jan 23 '18 at 14:25
  • originally size = 69 x 97.4 in – xtra Jan 23 '18 at 14:34
  • 1
    You may want to look at [this old answer](https://stackoverflow.com/a/21378162/1729265); it is for iText for Java but should not be too difficult to port to C#. Merely initialize the `PdfReader` from your `byte[]` instead of the file used in that answer. – mkl Jan 23 '18 at 16:02

1 Answers1

0

This method works for one-page-files.

    public static void ScaleToA4self(byte[] pdfAsBinary, string locationOfPdfOut)
    {
        PdfReader reader = new PdfReader(pdfAsBinary);
        Rectangle originalSize = reader.GetPageSize(1);
        float originalHeight = originalSize.Height;
        float originalWidth = originalSize.Width;

        Rectangle newSize = PageSize.A4;
        float newHeight = newSize.Height;
        float newWidth = newSize.Width;
        float scaleHeight = newHeight / originalHeight;
        float scaleWidth = newWidth / originalWidth;

        Document doc = new Document(newSize, 0, 0, 0, 0);
        PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(locationOfPdfOut, FileMode.Create));
        doc.Open();
        PdfContentByte cb = writer.DirectContent;
        PdfImportedPage page = writer.GetImportedPage(reader, 1);

        cb.AddTemplate(page, scaleWidth, 0, 0, scaleHeight, 0, 0);
        doc.Close();
    }
xtra
  • 1,957
  • 4
  • 22
  • 40
  • 1
    using a regular `PdfWriter` like that for such a task can be lossy: It drops interactivity (annotations, JavaScript,...) and adds a level of XObject indirection. A `PdfStamper` based solution does not have those disadvantages. – mkl Jan 24 '18 at 05:21