Question

I am trying to convert a text pdf to image pdf, and for that I found the following article:

ABCpdf convert text to image

So I took the code an produced the following code:

WebSupergoo.ABCpdf9.Doc firstDoc = new WebSupergoo.ABCpdf9.Doc();
WebSupergoo.ABCpdf9.Doc secondDoc = new WebSupergoo.ABCpdf9.Doc();

firstDoc.Read(@"C:\pdf1.pdf");

for (int i = 1; i <= firstDoc.PageCount; i++)
{
    secondDoc.Page = secondDoc.AddPage();
    firstDoc.PageNumber = i;
    secondDoc.MediaBox.String = firstDoc.MediaBox.String;

    using (Bitmap bm = firstDoc.Rendering.GetBitmap())
    {
        secondDoc.AddImageBitmap(bm, false);
    }
}

secondDoc.Save(@"c:\pdf2.pdf");

Now the code above works well except when I have pdf documents that have some page in portrait layout and other pages in landscape. What ends happening is the following:

let's say that I have a pdf document that has;

Page 1 - portrait
Page 2 - landscape
Page 3 - portrait
Page 4 - portrait

The result that this code is producing is:

Page 1 - portrait
Page 2 - portrait
Page 3 - landscape
Page 4 - portrait

Is there anything else that I would need to do other than setting the MediaBox to have the correct outcome?

Was it helpful?

Solution

Thanks for the helpful feedback in the comments, I was able to solve the problem by putting

secondDoc.Page = secondDoc.AddPage();

after

secondDoc.MediaBox.String = firstDoc.MediaBox.String;

The working code now looks like this:

WebSupergoo.ABCpdf9.Doc firstDoc = new WebSupergoo.ABCpdf9.Doc();
WebSupergoo.ABCpdf9.Doc secondDoc = new WebSupergoo.ABCpdf9.Doc();

firstDoc.Read(@"C:\pdf1.pdf");

for (int i = 1; i <= firstDoc.PageCount; i++)
{
    firstDoc.PageNumber = i;
    secondDoc.MediaBox.String = firstDoc.MediaBox.String;
    secondDoc.Page = secondDoc.AddPage();

    using (Bitmap bm = firstDoc.Rendering.GetBitmap())
    {
        secondDoc.AddImageBitmap(bm, false);
    }
}

secondDoc.Save(@"c:\pdf2.pdf");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top