We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to remove margins

hi. i want to resize pdf in actual size. but can’t remove margins. my code is here:

   Private Shared Function resizeStrech(loadFullFileName As String, saveFullFileName As String, newWidth As Decimal, newHeight As Decimal) As Boolean
    If IO.File.Exists(loadFullFileName) = False Then
        Throw New Exception("file not found")
    End If

    Dim doc As New Document(loadFullFileName)

    If doc.Pages.Count > 1 Then
        Throw New Exception("file have more than one page")
    End If
    doc.Pages(1).PageInfo.Margin.Bottom = 0
    doc.Pages(1).PageInfo.Margin.Top = 0
    doc.Pages(1).PageInfo.Margin.Left = 0
    doc.Pages(1).PageInfo.Margin.Right = 0

    Dim editor As New PdfFileEditor
    Dim parameter As New PdfFileEditor.ContentsResizeParameters(
        Nothing, PdfFileEditor.ContentsResizeValue.Units(mil2pix(newWidth)),
        Nothing,
        Nothing, PdfFileEditor.ContentsResizeValue.Units(mil2pix(newHeight)),
        Nothing)

    ' 
    '

    editor.ResizeContents(doc, {1}, parameter)
    '
    doc.Save("temp.pdf")
    Dim info As New PdfFileInfo("temp.pdf")

    Dim pw = info.GetPageWidth(1)
    Dim ph = info.GetPageHeight(1)
    Dim left = (pw - mil2pix(newWidth)) / 2
    Dim bottom = (ph - mil2pix(newHeight)) / 2
    Dim PageEditor As New Aspose.Pdf.Facades.PdfPageEditor()

    PageEditor.BindPdf("temp.pdf")

    PageEditor.MovePosition((-1 * left), (-1 * bottom))
    PageEditor.Save("temp.pdf")
    Dim newDoc As New Document("temp.pdf")

    newDoc.Pages(1).SetPageSize(mil2pix(newWidth), mil2pix(newHeight))
    newDoc.Save(saveFullFileName)
    Return True
End Function

@mr.bank

Thanks for contacting support.

Could you please share your sample PDF document with us. We will test the scenario in our environment and address it accordingly.

hi. thank you for reply.
i changed my code. but my new code not work too.

Private Shared Function resizeStrech(loadFullFileName As String, saveFullFileName As String, newWidth As Decimal, newHeight As Decimal) As Boolean
    If IO.File.Exists(loadFullFileName) = False Then
        Throw New Exception("file not found")
    End If

    Dim doc As New Document(loadFullFileName)

    If doc.Pages.Count > 1 Then
        Throw New Exception("file have more than one page")
    End If
    Dim rectOld = doc.Pages(1).MediaBox
    Dim newWidthPix = mil2pix(newWidth)
    Dim newHeightPix = mil2pix(newHeight)
    Dim centX = ((rectOld.Width / 2) + rectOld.LLX)
    Dim centY = ((rectOld.Height / 2) + rectOld.LLY)

    Dim editor As New PdfFileEditor
    Dim parameter As New PdfFileEditor.ContentsResizeParameters(
        Nothing, PdfFileEditor.ContentsResizeValue.Units(newWidthPix),
        Nothing,
        Nothing, PdfFileEditor.ContentsResizeValue.Units(newHeightPix),
        Nothing)


    editor.ResizeContents(doc, {1}, parameter)


    Dim pg = Doc.Pages(1)
    Dim rect = pg.CalculateContentBBox
    rect.LLX = centX - newWidthPix / 2
    rect.LLY = centY - newHeightPix / 2
    rect.URX = centX + newWidthPix / 2
    rect.URY = centY + newHeightPix / 2
    pg.CropBox = rect
    pg.MediaBox = rect
    pg.TrimBox=rect


    Doc.Save(saveFullFileName)
    Return True
End Function

@mr.bank

As requested earlier, would you please share your sample PDF document with us. It would really help us testing your code snippet accordingly and share our feedback with you.

sorry i forgot that.
file uploaded to dropbox

@mr.bank

We have tried to trim white space around the page using example given in the API documentation and were able to remove margins from your PDF document. We have used Aspose.PDF for .NET 19.3 and for your kind reference, an output PDF is also linked here. Could you please try using shared example at your side with latest version of the API and in case you still face any issue, please feel free to let us know.