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 Nov 30, 2016 3:51 pm

Hello,

I generate a PDF with a 64string

PdfDocument doc = new PdfDocument();
byte[] bytes = Convert.FromBase64String(base64String);
doc.LoadFromBytes(bytes);

After loading the document, I set the margins.

Spire.Pdf.Graphics.PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
PdfMargins pdfMargin = new PdfMargins();
pdfMargin.Left = unitCvtr.ConvertUnits(1, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
pdfMargin.Right = unitCvtr.ConvertUnits(5, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
pdfMargin.Top = unitCvtr.ConvertUnits(2, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
pdfMargin.Bottom = unitCvtr.ConvertUnits(2, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
doc.PageSettings.Margins = pdfMargin;

doc.PrintDocument.Print();
When sending to printer, nothing happens and the margins are ignored.

Is there any other configuration? Is it possible to set the margin of an existing document?

douglasalonso
 
Posts: 10
Joined: Tue Oct 25, 2016 1:18 pm

Wed Nov 30, 2016 6:39 pm

Hello

Fiz um outro teste carregando um arquivo, também não surtiu efeito o ajuste das margens.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile("P0000000078S003.pdf");

PdfMargins pdfMargin = new PdfMargins();

PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
pdfMargin.Left = unitCvtr.ConvertUnits(1, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
pdfMargin.Right = unitCvtr.ConvertUnits(20, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
pdfMargin.Top = unitCvtr.ConvertUnits(1, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
pdfMargin.Bottom = unitCvtr.ConvertUnits(1, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
doc.PageSettings.Margins = pdfMargin;

doc.PrintDocument.Print();

Estou usando a versão free para os testes.

douglasalonso
 
Posts: 10
Joined: Tue Oct 25, 2016 1:18 pm

Thu Dec 01, 2016 5:46 am

Hi douglasalonso,

Thanks for your inquiry.
I have noticed the issue and posted it to our Dev team. Once there is any update, I will inform you immediately.
Sorry for the inconvenience.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Thu Dec 01, 2016 8:21 am

Hello,

To set the margin, you have to create a new pdf and clone the original content.
But pay attention that you have to change the pagesize of the new pdf, which means make the new pdf pagesize different from the original one. If not, it won't effect.
You can try the following code to set margins.
Code: Select all
PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(path);
            PdfDocument doc1 = new PdfDocument();

            PdfMargins pdfMargin = new PdfMargins();
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            pdfMargin.Left = unitCvtr.ConvertUnits(80f, PdfGraphicsUnit.Pixel, PdfGraphicsUnit.Point);
            pdfMargin.Right = unitCvtr.ConvertUnits(30f, PdfGraphicsUnit.Pixel, PdfGraphicsUnit.Point);
            pdfMargin.Top = unitCvtr.ConvertUnits(80f, PdfGraphicsUnit.Pixel, PdfGraphicsUnit.Point);
            pdfMargin.Bottom = unitCvtr.ConvertUnits(100f, PdfGraphicsUnit.Pixel, PdfGraphicsUnit.Point);

            //clone the original pdf file
            foreach (PdfPageBase page in doc.Pages)
            {
                SizeF size = page.Size;
                //change the pagesize of the new pdf file
                PdfPageBase p = doc1.Pages.Add(new SizeF(size.Width + 1, size.Height + 1), pdfMargin);
                page.CreateTemplate().Draw(p, 0, 0);
            }
            doc1.SaveToFile(targetpath);


If there is any question, welcome to get it back to us.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Fri Dec 02, 2016 7:07 am

Hi douglasalonso,

Did you try the code I provided? Did it work? Could you give us some feedback at your convenience?

Many Thanks,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Mon Dec 05, 2016 9:26 pm

Hello,

The margin setting worked, but it did not solve my problem because by increasing the left margin, the document exceeded the size of the page on the right side, generating another page.

Is there a fit to page option to resize the document as we have in print control?

I found in the forum other post about scale, I made a test the document was smaller but left aligned in the part that can not contain impression due to the company logo.

Is there any way to leave the document aligned right in the printout?

Thanks

douglasalonso
 
Posts: 10
Joined: Tue Oct 25, 2016 1:18 pm

Tue Dec 06, 2016 1:47 am

Hello I got what I needed, I had to create the page with bigger dimensions so I can adjust the left margin and then work with the scala and also adjust the starting point of the draw drawing.

Spire.Pdf.Graphics.PdfUnitConvertor unitCvtr = new PdfUnitConvertor ();
PdfMargins pdfMargin = new PdfMargins ();

PdfMargin.Left = unitCvtr.ConvertUnits (margins.Left, PdfGraphicsUnit.Millimeter, PdfGraphicsUnit.Point);
PdfMargin.Right = 10;
PdfMargin.Top = 90;
PdfMargin.Bottom = 5;

PdfDocument doc1 = new PdfDocument ();

Foreach (PdfPageBase page in doc.Pages)
{
      SizeF size = page.Size;
      PdfPageBase p = doc1.Pages.Add (new SizeF (size.Width + 90F, size.Height + 100F), pdfMargin);
      Page.CreateTemplate () .Draw (p, 25, 30);
}

Doc1.PageScaling = PdfPrintPageScaling.CustomSacle;
Doc1.CustomScaling = 90;
Doc1.PrintDocument.Print ();

I do not know if this was the best solution, I have not yet assessed the cost of this in large-scale printing.

Thanks

douglasalonso
 
Posts: 10
Joined: Tue Oct 25, 2016 1:18 pm

Tue Dec 06, 2016 6:56 am

Hi douglasalonso,

I'm afraid that magnifying the page and then scaling the page is the only way to solve your issue.
Sorry for the inconvenience caused and if there is still any problem, please feel free to contact us.

Many Thanks,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Return to Spire.PDF