Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Mon Jun 07, 2021 3:28 pm

Hi,

I´m quite new and testing your components and I´m very pleased how simple it is to use them!!

I searched the forum, but i did not found a solution for this, if any body had already this problem, and you have a solution for it, please excuse the duplicate entry.

I have a single page PDF file, containing a large graphic (for example a construction plan) and I want to convert this PDF file to tif image file. The convert itself is no problem, I found that I can use "SaveAsImage" method - and then one image file is generated - BUT as a requirement its neccesary to split the single page PDF document into parts which have a size of a DIN-A4 page for example. Is it possible to split the PDF document to parts before saving them to image format?

Thanks in advance and best regards
Alexander Kommer

Alexande.Kommer
 
Posts: 3
Joined: Thu May 27, 2021 9:18 am

Tue Jun 08, 2021 7:56 am

Hello Alexander,

Thanks for your inquiry.
Please refer to the following code to meet your requirement.
Code: Select all
        static void Main(string[] args)
        {
            String file = "test.pdf";
            PdfDocument doc = SplitPDF(file);

            Image[] images = new Image[doc.Pages.Count];
            for (int i = 0; i < doc.Pages.Count; i++)
            {
                images[i] = doc.SaveAsImage(i);
            }
            doc.Close();

            JoinTiffImages(images, "result.tiff", EncoderValue.CompressionLZW);
        }

        static PdfDocument SplitPDF(string input)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(input);
           

            //Horizontal split
            PdfDocument newPdf = new PdfDocument();
            newPdf.PageSettings.Margins.All = 0;
            newPdf.PageSettings.Width = doc.Pages[0].Size.Width;
            newPdf.PageSettings.Height = PdfPageSize.A4.Height;
            PdfPageBase newPage;
            PdfTextLayout format = new PdfTextLayout();
            format.Break = PdfLayoutBreakType.FitPage;
            format.Layout = PdfLayoutType.Paginate;
            foreach( PdfPageBase page in doc.Pages)
            {
                newPage = newPdf.Pages.Add();
                page.CreateTemplate().Draw(newPage, new PointF(0, 0), format);
            }

            //Vertical split
            doc = newPdf;
            newPdf = new PdfDocument();
            newPdf.PageSettings.Margins.All = 0;
            newPdf.PageSettings.Width = PdfPageSize.A4.Width;
            newPdf.PageSettings.Height = PdfPageSize.A4.Height; 
            foreach (PdfPageBase page2 in doc.Pages)
            {
                newPage = newPdf.Pages.Add();
                page2.CreateTemplate().Draw(newPage, new PointF(0, 0), format);
            }

            return newPdf;
        }


        static ImageCodecInfo GetEncoderInfo(string mimeType)
        {
            ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
            for (int j = 0; j < encoders.Length; j++)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
        }
        public static void JoinTiffImages(Image[] images, String outFile, EncoderValue compressEncoder)
        {
            ImageCodecInfo info = GetEncoderInfo("image/tiff");
            if (images != null)
            {
                if (images.Length == 1)
                {
                    var iparams = new EncoderParameters(1);
                    var iparam = Encoder.Compression;
                    var iparamPara = new EncoderParameter(iparam, (long)(EncoderValue.CompressionNone));

                    iparams.Param[0] = iparamPara;
                    images[0].Save(outFile, info, iparams);
                }
                else if (images.Length > 1)
                {
                    // Save the first page (frame).
                    var encoderParam1 = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.MultiFrame);
                    var encoderParam2 = new EncoderParameter(Encoder.Compression,
                                                                (long)EncoderValue.CompressionNone);

                    var encoderParams = new EncoderParameters(2);
                    encoderParams.Param[0] = encoderParam1;
                    encoderParams.Param[1] = encoderParam2;

                    images[0].Save(outFile, info, encoderParams);

                    for (int i = 1; i < images.Length; i++)
                    {
                        if (images[i] == null)
                            break;

                        encoderParams.Param[0] = encoderParam1;
                        encoderParams.Param[1] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage);
                        images[0].SaveAdd(images[i], encoderParams);
                    }

                    encoderParams.Param[0] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.Flush);
                    images[0].SaveAdd(encoderParams);
                }
            }
        }


If this cannot help you, please provide your input file and the desired output for further investigation. Thanks in advance for your assistance.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Tue Jun 08, 2021 9:02 am

Hi Brian,

thank you for the response. My problem is, that my PDF file only has one page (which is at 5% scale, if you open the file). So its neccessary to split this large image in the PDF into parts with for example a size of DIN-A4

So for example if the image has a size of DIN-A2 - the image should be splittet into 4 seperate pages. The input size can vary, so the split into parts should be dynamic.

With your code, I´m generating a array with one element - and save this to one image...

Do you understand my problem?

Best regards,
Alexander Kommer

Alexande.Kommer
 
Posts: 3
Joined: Thu May 27, 2021 9:18 am

Tue Jun 08, 2021 10:14 am

Hello Alexander,

Thanks for your feedback.
I simulated a PDF file with one page, and its page size is A3 (290mm×420mm). Then I tested it with the code I provided, but the “images[]” array I got out contained 4 elements, not only one element (as shown below). To help us investigate further, please provide us with your test PDF. You could send it to us ([email protected]) via email.
screenshot.png

Besides, the code I provided will convert all the images to one tiff file. If you need to convert to multiple tiff files, please refer to the following code snippet.
Code: Select all
                images[i].Save("result"+i+".tiff",ImageFormat.Tiff);


Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Tue Jun 15, 2021 11:11 am

Hello Alexander,

How is your issue now? Could you please give us some feedback at your convenience?

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.PDF

cron