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.

Wed Oct 28, 2020 10:58 am

Hello!!

I have a problem and I would need help please.
The point is that if I try to divide a four-page pdf and keep the first one, the result I get is different if the document has been previously scanned.
If the document I have not scanned, everything is fine.
On the other hand, if the document is scanned, the result is a new document rotated and with two pages when in reality it is not rotated and I have only copied one page.
Can you help me please?

with the scanned document, the result is wrong:

PdfDocument source = new PdfDocument("Join and scanned document.pdf");
PdfDocument dest = new PdfDocument();
PdfPageBase page;
pageFrom = 1;
pageTo = 1;
for (int i = pageFrom; i <= pageTo; i++)
{
page = dest.Pages.Add(source.Pages[i].Size, new Spire.Pdf.Graphics.PdfMargins(0));
source.Pages[i].CreateTemplate().Draw(page, new System.Drawing.PointF(0, 0));
}
dest.SaveToFile(@"d:\ManualResult.pdf"); --> error: the result is a document rotate with two pages.
source.Split(@"d:\AutomaticResult_{0}.pdf"); --> correct: the result is a for document with one page every document.


with the NO scanned document, the result is correct:
PdfDocument source = new PdfDocument("Join but not scanned document.pdf");
PdfDocument dest = new PdfDocument();
PdfPageBase page;
pageFrom = 1;
pageTo = 1;
for (int i = pageFrom; i <= pageTo; i++)
{
page = dest.Pages.Add(source.Pages[i].Size, new Spire.Pdf.Graphics.PdfMargins(0));
source.Pages[i].CreateTemplate().Draw(page, new System.Drawing.PointF(0, 0));
}
dest.SaveToFile(@"d:\ManualResultNOSCANNED.pdf"); --> correct: the result is a document with one page.
source.Split(@"d:\AutomatictResultNOSCANNED_{0}.pdf"); --> correct: the result is a for document with one page every document.


I attach the documents and the results.

Thank you very much!

LauraNM
 
Posts: 92
Joined: Tue Feb 19, 2019 1:23 pm

Thu Oct 29, 2020 4:57 am

Hello,

When testing your code, we did notice the behavior you mentioned. After further investigate, we found that this issue is caused by your source document page being rotated.
Our Spire.PDF provides a directly method to copy pages from one PDF file to another, which can avoid this issue. Please use the following code instead. If there is any question, just feel free to contact us.
Code: Select all
            PdfDocument source = new PdfDocument("Join and scanned document.pdf");
            PdfDocument dest = new PdfDocument();
            PdfPageBase page;
            int pageFrom = 0;
            int pageTo = source.Pages.Count;
            int pageIndex = 0;
            for (int i = pageFrom; i < pageTo; i++)
            {
                dest.InsertPage(source, i, pageIndex++);
            }
            dest.SaveToFile(@"ManualResult.pdf");
            source.Split(@"AutomaticResult_{0}.pdf");


Sincerely,
Brian
E-iceblue support team
User avatar

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

Thu Oct 29, 2020 11:21 am

Hello!
Thank you very much for your help.
I have tried to use the function that you indicate to me.
My version of Spire.Office was 5.5.1. And that version doesn't contain that insert page function overhead.
So I have updated the version to 5.10.0.
This update has generated many incompatibility errors and does not include that overload of the Insert page function.

In which version is this function?

I am sending you the list of incompatibilities in case you can send me some information to solve this problem.

Greetings and thank you very much for your help!

LauraNM
 
Posts: 92
Joined: Tue Feb 19, 2019 1:23 pm

Fri Oct 30, 2020 6:27 am

Hello,

Thanks for your feedback.
Sorry the method “InsertPage(PdfDocument ldDoc, int pageIndex, int resultPageIndex)”is newly added in Spire.PDF v6.10.4 (released on 10/20/2020) and is not yet included in the latest Spire.Office v5.10.10 (released on 10/12/2020). For your case, you can also use the method“InsertPage(PdfDocument ldDoc, int pageIndex)”, I have verified that it also works fine.

As for the code incompatibility errors, you are installing Spire.Office in your .NET Core app via Nuget, right?
Since .NET Core itself has limitations in processing images and it is currently difficult to optimize, we removed the .NET Core dlls of the Spire.Office Nuget package. Thus, for .NET Core App, if you install Spire.Office via NuGet, the .NET Standard dlls will be used by default. This is why the incompatibility errors appear. To eliminate them, please use the following code instead.
Code: Select all
            BarcodeSettings settings = new BarcodeSettings();
            settings.Type = BarCodeType.QRCode;

            settings.Data = "123456";
            settings.Data2D = "123456";
            settings.SetTextFont("Verdana", 10, SkiaSharp.System.Drawing.FontStyle.Regular);
            settings.QRCodeDataMode = QRCodeDataMode.AlphaNumber;
            settings.X = 1.0f;
            settings.QRCodeECL = QRCodeECL.H;
            settings.ShowTextOnBottom = true;
            settings.ShowText = false;

            BarCodeGenerator generator = new BarCodeGenerator(settings);

            var skImage = generator.GenerateImage();

            // Translate SKImage to bitmap
            var bitmap = new System.Drawing.Bitmap(skImage.Width, skImage.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
            var data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, bitmap.PixelFormat);

            // copy
            using (var pixmap = new SkiaSharp.SKPixmap(new SkiaSharp.SKImageInfo(data.Width, data.Height), data.Scan0, data.Stride))
            {
                skImage.ReadPixels(pixmap, 0, 0);
            }

            bitmap.UnlockBits(data);
            bitmap.Save(@"test.bmp");


Moreover, if you still want to use the .NET Core dlls, you can download Spire.Office from our website, manually reference the .NET core dlls and then install the following two libraries in your .NET Core App. If there is any question, please feel free to write back.

.NETCoreApp 2.0
System.Drawing.Common (>= 4.5.0)
System.Text.Encoding.CodePages (>= 4.5.0)
.NETCoreApp 3.0
System.Drawing.Common (>= 4.7.0)
System.Text.Encoding.CodePages (>= 4.7.0)

Sincerely,
Brian
E-iceblue support team
User avatar

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

Fri Oct 30, 2020 7:13 am

Hello!

Regarding the insert function, I found this one that I think suits my needs and I have verified that it works:

PdfDocument dest = new PdfDocument();
PdfPageBase page;
//int pageIndex = 0;
//for (int i = pageFrom; i <= pageTo; i++)
//{
// dest.InsertPage(source, );
// //page = dest.Pages.Add(source.Pages[i].Size, new Spire.Pdf.Graphics.PdfMargins(0));
// //source.Pages[i].CreateTemplate().Draw(page, new System.Drawing.PointF(0, 0));

// pageIndex++;
//}

dest.InsertPageRange(source, pageFrom, pageTo);
dest.SaveToStream(result);

I will try the update to see if I get it.

Thank you very much for your help!

Regards,

LauraNM
 
Posts: 92
Joined: Tue Feb 19, 2019 1:23 pm

Fri Oct 30, 2020 7:35 am

Hello,

OK, thanks for your feedback.
If you have further questions, just feel free to contact us.

Sincerely,
Brian
E-iceblue support team
User avatar

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

Return to Spire.PDF