裁剪Pdf页面,同时将其转换为图像



在转换为图像之前,我正在尝试裁剪PDF/图像文件。因此,在这段代码中,我生成了一个pdf文件,我想在将其转换为图像之前/之后裁剪它。

这里的问题是我如何设置或裁剪我从pdf生成的图像?

下面是我使用的代码:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using IronPdf;
using System.Windows.Media.Imaging;
using Syncfusion.Windows.PdfViewer;
using Syncfusion.Pdf.Parsing;
using System.Windows;
using System.IO;
namespace WpfApp2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();         
}
private async void GeneratePdf()
{
var Renderer = new IronPdf.HtmlToPdf();
Renderer.PrintOptions.SetCustomPaperSizeInInches(12.5, 20);
Renderer.PrintOptions.PaperOrientation= PdfPrintOptions.PdfPaperOrientation.Portrait;
Renderer.PrintOptions.EnableJavaScript = true;
Renderer.PrintOptions.RenderDelay = 50; //ms
Renderer.PrintOptions.CssMediaType = PdfPrintOptions.PdfCssMediaType.Screen;
Renderer.PrintOptions.DPI = 300;
Renderer.PrintOptions.FitToPaperWidth = true;
Renderer.PrintOptions.JpegQuality = 100;
Renderer.PrintOptions.GrayScale = false;
Renderer.PrintOptions.FitToPaperWidth = true;
Renderer.PrintOptions.InputEncoding = Encoding.UTF8;
Renderer.PrintOptions.Zoom = 100;
Renderer.PrintOptions.CreatePdfFormsFromHtml = true;
Renderer.PrintOptions.MarginTop = 60;  //millimeters
Renderer.PrintOptions.MarginLeft = 20;  //millimeters
Renderer.PrintOptions.MarginRight = 30;  //millimeters
Renderer.PrintOptions.MarginBottom = 20;  //millimeters
Renderer.PrintOptions.FirstPageNumber = 1; //use 2 if a coverpage  will be 
Renderer.PrintOptions.InputEncoding = System.Text.Encoding.UTF8;
var time = DateTime.Now;
Renderer.PrintOptions.Header = new HtmlHeaderFooter()
{
HtmlFragment = "<img src='https://i.imgur.com/QtFjt8p.jpg'>",
DrawDividerLine=true,
};
var pdf = Renderer.RenderHtmlAsPdf("" +
"<p style='text-align: center;font-size: 25px;font-family: Calibri, sans-serif';text-decoration: underline;>" + textbox3.Text+"</p>"
);
var BackgroundStamp = new HtmlStamp() { Html = "<img src='https://i.imgur.com/U8MUT3q.png'/>", Width = 100, Height = 100, Opacity = 90, Left = 100, Top = 200, ZIndex = HtmlStamp.StampLayer.BehindExistingPDFContent };
pdf.StampHTML(BackgroundStamp);
pdf.SaveAs("Watermarked.pdf");
pdf.Dispose();
Thread.Sleep(6000);

PdfViewerControl pdfViewer = new PdfViewerControl();
//Load the input PDF file
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Watermarked.pdf");
pdfViewer.Load(loadedDocument);

//Export the particular PDF page as image at the page index of 0.
BitmapSource image = pdfViewer.ExportAsImage(0);
//Setup the output path
if (image != null)
{
//Initialize the new PngBitmapEncoder
BitmapEncoder encoder = new PngBitmapEncoder();
//Create the bitmap frame using the bitmap source and add it to the encoder.
encoder.Frames.Add(BitmapFrame.Create(image));
//Create the file stream for the output in the desired image format.
FileStream stream = new FileStream("Watermarked.png", FileMode.Create);
//Save the stream, so that the image will be generated in the output location.
encoder.Save(stream);
}
//Dispose the document.
loadedDocument.Dispose();
loadedDocument = null;



//  await Task.Run(() => croppdf());
}

public async void croppdf()
{

}
private void Button_Click(object sender, RoutedEventArgs e)
{
GeneratePdf();
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
}
}
}

如果有人能帮我解决这个问题,我试过了,任何解决方案,但实际上都不适合我。

我会使用CroppedBitmap

...
//Export the particular PDF page as image at the page index of 0.
BitmapSource image = pdfViewer.ExportAsImage(0);
//Setup the output path
if (image != null)
{
// init croppedBitmap
CroppedBitmap croppedBitmap = new CroppedBitmap(image,new Int32Rect(5, 5, (int)(image.Width-10), (int)(image.Height-10)));
//Initialize the new PngBitmapEncoder
BitmapEncoder encoder = new PngBitmapEncoder();
//Create the bitmap frame using the cropped bitmap source and add it to the encoder.
encoder.Frames.Add(BitmapFrame.Create(croppedBitmap));
//Create the file stream for the output in the desired image format.
FileStream stream = new FileStream("Watermarked.png", FileMode.Create);
//Save the stream, so that the image will be generated in the output location.
encoder.Save(stream);
}

最新更新