How do I convert a PDF to rasterized (i.e. bitmap) PDF?

193 views
Skip to first unread message

Support

unread,
Jun 13, 2014, 9:21:22 PM6/13/14
to pdfne...@googlegroups.com
Q:

How do I convert a PDF to rasterized (i.e. bitmap) PDF?

I was looking at PDF/A conversion (and PDFTron's PDF/A converter ... ),  however it seems that my submission site requires that all PDF are rasterized?


We are looking into the rasterization of a PDF file into another PDF file with the following expectations:

·         Processing time must still be be acceptable in reference to “PDF to PDF/A” in-memory and file operations, realizing that the pay-off of better security is achieved through rasterization.

·         Processing must occur in-memory as much as possible to eliminate performance issues and other risks in utilizing disk storage.

·         Code-wise the conversion must be efficient and straightforward without unnecessary manipulation of source and target page content.


Essentially our use case is to have PDFNet convert and existing PDF document into a new rasterized PDF document. In other words, we just want to convert each page of a PDF document into an image and get those images into a new PDF. Pretty much like what you have for PDF to PDF/A conversion.

------------

A:

Use the following snippet to convert generic PDF  to raster PDF:


using (PDFDoc doc = new PDFDoc(inpdf))

using (PDFDoc newdoc = new PDFDoc())

using (PDFDraw draw = new PDFDraw())

using (ElementBuilder builder = new ElementBuilder())

using (ElementWriter writer = new ElementWriter())

using (ObjSet objset = new ObjSet())

{

    draw.SetDPI(dpi);

    doc.InitSecurityHandler();

 

    Obj encoder_hint = objset.CreateArray();

    encoder_hint.PushBackName("Flate");

    encoder_hint.PushBackName("Level");

    encoder_hint.PushBackNumber(9); // Set max compression vs speed.

 

    for (PageIterator itr = doc.GetPageIterator(); itr.HasNext(); itr.Next())

    {

        System.Drawing.Bitmap bmp = draw.GetBitmap(itr.Current());

        Image image = Image.Create(doc, bmp, encoder_hint);

        bmp.Dispose();

        Page page = doc.PageCreate(itr.Current().GetCropBox());

        writer.Begin(page);

        writer.WritePlacedElement(builder.CreateImage(image, 0, 0, page.GetPageWidth(), page.GetPageHeight()));

        writer.End();

        newdoc.PagePushBack(page);

    }

    newdoc.Save(outpdf, SDFDoc.SaveOptions.e_remove_unused);

}

}

catch (PDFNetException e)

{

Console.WriteLine(e.Message);

}

 

The relevant utility function and a drop-in replacement for AddImage C# sample project is attached.

The function has a parameter DPI that you can control quality. If you prefer to use JPEG rather than flate you can comment-out encoder_hint.

 

Please note that there are still some distinct advantages in using PDF/A Converter (pdftron.PDF.PDFA.PDFComplaince):

 

  • It is faster

  • Output PDF is high quality ( you have vector graphics)

  • Output PDF size is usually much smaller (unless you rasterize at really low-rez)

  • Output PDF is searchable

  • You maintain metadata.

  • ….

AddImageTest.cs
Reply all
Reply to author
Forward
0 new messages