SharpPDF PDF runtime generator

Hi!
I am using SharpPDF to runtime PDF generation. I’m using it on Windows. I found two problems:

  • I need to put an scaled image on a page. I’m using addImage.

It doesn’t scale the image but it cuts the image starting from upper-left corner with the size I would like to scale it to.
How can I solve this? In MAC doesn’t happen that.

  • I have a problem adding images to a PDF file with WINDOWS. The PDF doesnt show the images well (they are grated) when I open it with ADOBE READER. But, if I open it with another pdf reader, the images are well. How can I solve this problem? On the contrary, if I use SharpPDF in MAC, this problem doesnt exist, I need to use it on Windows.

I have post both questions in the page of the lib inside SourceForge but I didn’t get any answer.
Could anybody help me with this please?

Did you find a solution to this? I have the same problem. If I open it with another viewer and save as pdf again, acrobat opens it fine.

if you are using the public unity conversion of sharp PDF then you need to make use of the new height field on Windows, in imageElement. This is read in getText when the PDF encoding takes place.

internal imageElement(Byte[ ] myImage, int newCoordX, int newCoordY, int imageHeight, int imageWidth, int newImageHeight, int newImageWidth) //added newImg H/W to constructor
{

try
{
//_content = new byte[myImage.Length];
_content = myImage;
_height = imageHeight;
_width = imageWidth;
_newHeight = newImageHeight; //new code
_newWidth = newImageWidth; //new code
_coordX = newCoordX;
_coordY = newCoordY;
}
catch (System.IO.FileNotFoundException ex)
{
throw new pdfImageNotFoundException(“Immagine non trovata!”, ex);
}
catch (System.IO.IOException ex)
{
throw new pdfImageIOException("Errore generale di IO sull’immagine ", ex);
} finally {
// if (tmpContent != null) {
// tmpContent.Dispose();
// tmpContent = null;
// }
// if (outStream != null) {
// outStream.Close();
// outStream = null;
// }
}
}

Great, Exactly what I was looking for!
But where do you get the 2 extra values that you pass as arguments?
thanks!

The imageHeight and imageWidth are the pixel height and width of the source image, newImageHeight and newImageWidth are the target height of the image. The standard for relative size is 72 pixels per inch, so a standard sheet of paper is 612 X 792 (8.5* 72 X 11 * 72). Say you wanted the image to take up exactly 1/4 of the page, you would set newImageHeight to 396 (11/272) and newImageWidth to 306 (8.5/272). Sharp PDF does not account for margins, so I recommend calculating a 54 pixel margin (3/4 inch) when you are creating the algorithm to position your export elements.

I used the same i.e imageHeight=newImageHeight , imageWidth=newImageWidth, because I wanted to have a high PPI (400) A4 page end result , so my image is 3072 by 3072. The result is the same as before: it still only opens in edge, while in chrome its completely blank and in acrobat reader it says “There was an error while reading this Stream”

Sorry I have been busy with work and wasn’t watching my forum alerts very diligently.

First its important that you understand that imageHeight != newImageHeight and imageWidth != newImageWidth, if you read through the code above, you will see that I am setting _newHeight = newImageHeight and _newWidth = newImageWidth. In the poorly named getText() export function inside imageElement.cs, _newHeight and _newWidth get passed as the end size if they are greater than 0 while _height and _width always get passed as the source image size. By setting _height = newImageHeight you are declaring newImageHeight to be the original size of your source image instead of the new size you want the image to be displayed at, so no resizing takes place, your image will just be clipped outside the size you declared. An A4 page’s maximum dimensions are 595w x 841h (cm → in * 72), if you are passing 3072 as your new height/width then of course it will not process properly. Instead set _height and _width to be your 3072 then set newHeight and newWidth to be 552 (3072/400*72 assuming consistent dimensions) which will give you exactly 400 dpi over a 7.68 in square area.

Hey, no problem I was also away!

I had also modified the following part (of a PDFPage definition) to start with a way bigger page, but I also changed it back now.

public pdfPage()
        {
            //taking 3072 pixel pics
            //_height = 4677;
            //_width = 3307;
            //A4 at 400 ppi

            //old values

            _height = 792;
            _width = 612;

            _elements = new ArrayList();
        }

Whether I pass the suggested values or 3072 to

_newHeight = newImageHeight; //new code
_newWidth = newImageWidth; //new code

Here’s the bigger picture, following the sample code from http://www.francescogallorini.com/2011/02/unity-sharp-pdf/

path = Path.Combine(Application.persistentDataPath, "data");

string attacName = "info.pdf";

StartCoroutine(CreatePDF());

public IEnumerator CreatePDF()
    {
        List<Texture2D> tempCopy = new List<Texture2D>(generatedPages);

        int counter = 0;
        myDoc = new pdfDocument("Info", "Name", false);

        foreach (var item in tempCopy)
        {
            pdfPage page = myDoc.addPage();
            Debug.Log(Path.Combine(path, counter.ToString() + ".png"));

            yield return StartCoroutine(page.newAddImage(Path.Combine(path, counter.ToString() + ".png"), 0, 0));
            counter++;
        }
        myDoc.createPDF(Path.Combine(path, attacName));

All pictures are saved before, in the persistent data path, under a “data” folder and named with increasing numbers:
“0.png” , " 1.png", etc…

Maybe it helps:
-It opens in edge (shows content normally)
-Blank page in Chrome
-Error reading stream on acrobat reader

-Intended for use with iOS BUT the behavior is the same when I run it on my desktop as well.

Could you post your pdfPage.newAddImage and pdfPage.AddImage functions? it doesn’t look like you are passing new height width values in your initial call or you are overwriting your height/width with the initial call.

Hello , I am not able to add the image in the pdf. I am using sharp pdf plugin with unity3d .Can u help me out with code or tips.

Hi

This AddImage is driving me crazy too.
I have the same requirements as Merksim : reasonably High DP(not necessarily 400Dpi though)
I followed Aciusia’s reco and modified the imageElement accordingly.
After what I used the following code to add images to the pdf pages

byte[]img;

//After generating a 2970*2100 img, I add it as an image to the current page
currentPage.addImage(img, 0, 0, 2100, 2970, mmToPoints(210), mmToPoints(297));

// I Modified the AddImage to be able to set the new Width and new Height.(Note: I use a utility to convert mm to Points(the physical unit. not the pixels)

    public void addImage(Byte[] myImage, int X, int Y, int imageHeight, int imageWidth, int newHeight, int newWidth)
        {
            try
            {
                imageElement objImage = new imageElement(myImage, X, Y, imageHeight, imageWidth,newHeight,newWidth);
                _elements.Add(objImage);
                objImage = null;
            }
            catch (pdfImageNotFoundException ex)
            {
                throw new pdfImageNotFoundException(ex.Message, ex);
            }
            catch (pdfImageIOException ex)
            {
                throw new pdfImageIOException(ex.Message, ex);
            }
            Debug.Log("addedIMage");
        }

The result pretty bad : Image resolution is pretty low… and of course it doesn’t read in Acrobat. It works in preview on my mac tough.

Strangely I get better result when I do that:

byte[]img;

currentPage.addImage(img, 0, 0, mmToPoints(210), mmToPoints(297));

    public void addImage(Byte[] myImage, int X, int Y, int imageHeight, int imageWidth)
        {
            try
            {
                imageElement objImage = new imageElement(myImage, X, Y, imageHeight, imageWidth,imageHeight,imageWidth);
                _elements.Add(objImage);
                objImage = null;
            }
            catch (pdfImageNotFoundException ex)
            {
                throw new pdfImageNotFoundException(ex.Message, ex);
            }
            catch (pdfImageIOException ex)
            {
                throw new pdfImageIOException(ex.Message, ex);
            }
            Debug.Log("addedIMage");
        }

The resolution is still pretty bad but I can open it in Acrobat…

@Everyone Any idea?