0

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?

Community
  • 1
  • 1
malkassem
  • 1,937
  • 1
  • 20
  • 39
  • 1
    Have you tried putting `secondDoc.Page = secondDoc.AddPage();` after `secondDoc.MediaBox.String = firstDoc.MediaBox.String;`? – mkl Apr 02 '14 at 09:13
  • @mkl I tried... that did not work neither. – malkassem Apr 02 '14 at 12:21
  • You probably need to detect the rotation of each page in firstDoc and apply the same to secondDoc. See: http://stackoverflow.com/questions/15572165/detect-orientation-of-every-page-in-a-pdf-using-abcpdf – WhoIsRich Apr 06 '14 at 03:23
  • @mkl your solution was correct. I was not clear on what should have come first. Please add an answer, and I will accept it. Thank you – malkassem Apr 09 '14 at 15:19

1 Answers1

1

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");
malkassem
  • 1,937
  • 1
  • 20
  • 39