Merging Documents

Discussions about PDF management.
Post Reply
indxlogic
Posts: 6
Joined: Tue Aug 21, 2012 4:42 pm

Merging Documents

Post by indxlogic » Fri Nov 03, 2017 3:54 pm

I am having issues with merging documents. Here is the code

Code: Select all

var oLicenceManager = new LicenseManager();
oLicenceManager.RegisterKEY("XXXXXXXXXXXXXXXXXXXX");

var PdfSource = new GdPicturePDF();
var PdfDest = new GdPicturePDF();

var selected = new List<string> {@"z:\Users\mmorvant\Desktop\PDF Explore\AB.pdf", @"z:\Users\mmorvant\Desktop\PDF Explore\C.pdf"};

PdfDest.NewPDF();
try
{

	selected.ForEach(f =>
	{
		GdPictureStatus status = PdfSource.LoadFromFile(f, false);
		if (status != GdPictureStatus.OK)
		{
			Console.WriteLine("Error openning the first PDF", "Error " + status.ToString());
			return;
		}

		for (int i = 0; i < PdfSource.GetPageCount(); i++)
		{
			PdfDest.ClonePage(PdfSource, i);
		}

		PdfSource.CloseDocument();
	});

	var fi = new FileInfo(selected[0]);
	var merged = Path.Combine(fi.DirectoryName, string.Format("Merged_PDF_{0:MMddyyyyhhmmss}.pdf", DateTime.Now));
	var sstatus = PdfDest.SaveToFile(merged);

	if (sstatus == GdPictureStatus.OK)
	{
		PdfSource.CloseDocument();

		//File.Delete(selected[0]);
		//File.Delete(selected[1]);
	}
}
catch (Exception exp)
{
	Console.WriteLine("There was an error merging your documents.\r\n\r\n" + exp.Message);
}
The AB document is a two page document with the letter A on the first page and B on the second. The C document is a single page document with the letter C on the first page. When the code is run I get a three page document [A A C]. I have attached the source files as well.

The AB document was generated in the same manner only with two single page documents A and B.

Any ideas?

EDIT: I just realized that I forgot to add the PdfDest.CloseDocument(); I know that should be there, I just missed it. It doesn't change the results.
Attachments
C.pdf
(85.31 KiB) Downloaded 305 times
AB.pdf
(162.01 KiB) Downloaded 271 times

Costinel
Posts: 36
Joined: Mon Jul 11, 2016 9:35 am

Re: Merging Documents

Post by Costinel » Mon Nov 06, 2017 10:56 am

Hi,

The 'for' loop should be:

Code: Select all

for (int i = 1; i <= PdfSource.GetPageCount(); i++)
because
PageNo
The page number of the required page from the source document, which you want to clone into the current document. It should be a value within the interval from 1 to GetPageCount() of the source document.
see: https://www.gdpicture.com/guides/gdpicture/Gd ... nt32).html

Best regards,
Costinel Mitrea

indxlogic
Posts: 6
Joined: Tue Aug 21, 2012 4:42 pm

Re: Merging Documents

Post by indxlogic » Mon Nov 06, 2017 1:44 pm

Thanks. That was an awfully silly mistake on my part. A secondary question would be; why didn't the call to clone page[0] fail if the array of pages is 1 based? It appears that it treated page[0] and page[1] as the same page. As a developer I would certainly prefer the call to page[0] fail.

Gabriela
Posts: 436
Joined: Wed Nov 22, 2017 9:52 am

Re: Merging Documents

Post by Gabriela » Wed Feb 07, 2018 8:33 pm

Hi,
The answer to your question regarding this: "It appears that it treated page[0] and page[1] as the same page." is because of the SelectPage() method, as you can read here - specifically PageNo parameter :
https://www.gdpicture.com/guides/gdpicture/web ... tPage.html
"...for values lower than 1 the first page is selected and for values higher than GetPageCount() the last page is selected..."
Regards,
Gabriela

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest