PDFsharp edit a pdf file - c#-4.0

Environment - PDFsharp Library, Visual Studio 2012 and C# as the language.
I am trying to:
read Test1.pdf (Width = 17 inches, Height – 11 inches) with 1 page
add some text to it
save it as another file (Test2.pdf)
I am able to do all the following. But when I open the file Test2.pdf the size of the page is getting reduced to Width = 11 inches, Height – 11 inches.
These PDF files that I am using are Product Specification Sheets that I have downloaded from the internet. I believe this is happening on only certain types of file and I am not sure how to differentiate these files.
Code given below:
//File dimentions - Width = 17 inches, Height - 11 inches (Tabloid Format)
PdfDocument pdfDocument = PdfReader.Open(#"D:\Test1.pdf", PdfDocumentOpenMode.Modify);
PdfPage page = pdfDocument.Pages[0];
XGraphics gfx = XGraphics.FromPdfPage(page);
XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic);
gfx.DrawString("Hello, World!", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
//When the file is saved dimentions change to - Width = 11 inches, Height - 11 inches
pdfDocument.Save(#"D:\Test2.pdf");
I have uploaded the file here Test1.pdf
==================================================================================
As suggested by the PDFsharp Team the code should be as follows:
PdfDocument PDFDoc = PdfReader.Open(#"D:\Test1.pdf", PdfDocumentOpenMode.Import);
PdfDocument PDFNewDoc = new PdfDocument();
for (int Pg = 0; Pg < PDFDoc.Pages.Count; Pg++)
{
PdfPage pp = PDFNewDoc.AddPage(PDFDoc.Pages[Pg]);
XGraphics gfx = XGraphics.FromPdfPage(pp);
XFont font = new XFont("Arial", 10, XFontStyle.Regular);
gfx.DrawString("Hello, World!", font, XBrushes.Black, new XRect(0, 0, pp.Width, pp.Height), XStringFormats.BottomCenter);
}
PDFNewDoc.Save(#"D:\Test2.pdf");

Instead of modifying the document, please create a new document and copy the pages from the old document to the new document.
Sample code can be found in this post on the PDFsharp forum:
http://forum.pdfsharp.net/viewtopic.php?p=2637#p2637

Related

SVG in PPT( generated by Pptxgenjs) shown incorrectly when the PPT is opened in libreOffice

Pptxgenjs is able to add SVG images and generate PPT correctly. When this PPT is opened using Microsoft office, SVG images are shown properly. However, when this PPT is opened using libreOffice, SVG images are shown as invalid cross symbols as mentioned below.
ENV: Windows 10
LibreOffice Version 7
Am I missing something here?
You can try using Aspose.Slides Cloud SDK for Node.js to add SVG images and other content to your presentations. You can evaluate this REST-based API making 150 free API calls per month for API learning and presentation processing. The following code example shows you how to add an SVG image to a presentation using Aspose.Slides Cloud:
const cloud = require("asposeslidescloud")
const model = require("asposeslidescloud/model")
const fs = require("fs")
const slidesApi = new cloud.SlidesApi("client_id", "client_key")
const fileName = "example.pptx"
const imagePath = "image.svg"
const slideIndex = 1
// Create a picture frame for the SVG image with a specified size and position.
var pictureFrame = new model.PictureFrame()
pictureFrame.pictureFillFormat = new model.PictureFill()
pictureFrame.pictureFillFormat.svgData = fs.readFileSync(imagePath, "utf8")
pictureFrame.X = 20
pictureFrame.Y = 20
pictureFrame.Width = 300
pictureFrame.Height = 200
// Add the picture frame on the slide.
slidesApi.createShape(fileName, slideIndex, pictureFrame).then((response) => {
console.log(JSON.stringify(response))
})
I work as a Support Developer at Aspose.

Adding sticky note to existing PDF. losing original PDF content [duplicate]

I want to add Annotations comment in existing PDF file using iTextSharp with C#.
Please give sample code to add Annotations in existing PDF file.
Here PS Script for my Annotation:
[/Contents (My Text contents) /Rect [100 600 150 550] /SrcPg 1 /Title (My Title text) /Color [0 0 1] /Subtype /Caret /ANN pdfmark
The iText(Sharp) example TimetableAnnotations1.java / TimetableAnnotations1.cs from chapter 7 of iText in Action — 2nd Edition shows how to add annotations to existing PDFs in general.
The central code is (in the C# example):
rect = GetPosition(screening);
annotation = PdfAnnotation.CreateText(
stamper.Writer, rect, movie.MovieTitle,
string.Format(INFO, movie.Year, movie.Duration),
false, "Help"
);
annotation.Color = WebColors.GetRGBColor(
"#" + movie.entry.category.color
);
stamper.AddAnnotation(annotation, page);
where stamper is a PdfStamper working on your PDF file; movie is a data structure the example retrieves title, text and color of the annotation from.
PdfAnnotation offers multiple other Create... methods to create other types of annotations.
rect = GetPosition(screening);
can someone plz explain why is this is used..is there any way to find the current cursor position (top,bottom,height,width)
as with the annotation,
Document doc = new Document(PageSize.A4, 50, 50, 50, 50);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(#"C:\Users\Asus\Desktop\Test.pdf", FileMode.OpenOrCreate));
doc.AddDocListener(writer);
doc.Open();
doc.Add(new Annotation("annotation", "The text displayed in the sticky note", 100f, 500f, 200f, 600f));
doc.Close();
this works fine to me..

How can I prevent my image from changing size when placing it on a spreadsheet (Aspose Cells)?

I have an image that is embedded in my solution and is used on the main form of a Winforms app, and also for pasting into a spreadsheet. The image size is 156X121.
I put it on the sheet like so:
var ms = new MemoryStream();
_logo.Save(ms, ImageFormat.Png);
ms.Position = 0;
pivotTableSheet.Pictures.Add(0, _grandTotalsColumnPivotTable, ms);
Yet when it is on the sheet, it stretches out and spills into neighboring cells, partially obscuring other data:
As you can see, the size is no longer 156X121. The height has been increased by 25%. Why? And how can I prevent that?
This code:
MessageBox.Show(string.Format("image height is {0}", _logo.Height));
MessageBox.Show(string.Format("image width is {0}", _logo.Width));
...showed me "126" as the height and "151" as the width, matching the image as it is in the project. So why is the original size changed? Is there a property I can set to leave the size alone and not stretch it? Or how can I prevent this gumbification of the image?
It's bizarre to me that the image is one size (126X151), its original size is purported to be 1.26" X 1.63", and its size after being scaled is 1.57" X 1.63".
Who or what is allowing this 25% increase in Height?
NOTE: If I select the "Reset" button in the image's "Size and Properties" dialog, it shrinks up as I want it to be, setting the Height "back" to 100% from 125%. Is there a way to do this "Reset" programmatically?
UPDATE
Based on the answer, I tried this:
var ms = new MemoryStream();
//_logo.Height = 121; <= cannot set; Height is a readonly property
_logo.Save(ms, ImageFormat.Png);
ms.Position = 0;
pivotTableSheet.Pictures.Add(0, _grandTotalsColumnPivotTable, ms);
Picture pic = pivotTableSheet.Pictures[0];
//Workbook.Worksheets[0].Pictures[0]; <= does not compile
pic.HeightScale = 100;
pic.WidthScale = 100;
(Workbook.Worksheets[0] does not compile for me).
It makes no difference; the image is still stretching vertically.
UPDATE 2
I realized I needed "Workbook" to be "workBook" due to this:
private static Workbook workBook;
...and so I tried this:
Picture pic = workBook.Worksheets[1].Pictures[0]; // Worksheet 0 is the data sheet that feeds the PivotTable and subsequently gets hidden, so need to use 1
pic.Height = 121;
pic.WidthScale = 100;
...but it still gumbifies the image vertically. So does replacing "pic.Height = 121" with "pic.HeightScale = 100;"
So this is the code currently, which adds the image, but in a vertically gumbified manner:
var ms = new MemoryStream();
//_logo.Height = 121; readonly
_logo.Save(ms, ImageFormat.Png);
ms.Position = 0;
pivotTableSheet.Pictures.Add(0, _grandTotalsColumnPivotTable, ms);
Picture pic = workBook.Worksheets[1].Pictures[0]; // Worksheet 0 is the data sheet that feeds the PivotTable
//pic.Height = 121;
pic.HeightScale = 100;
pic.WidthScale = 100;
Please use this code to reset it to original height.
Picture pic = wb.Worksheets[0].Pictures[0];
pic.HeightScale = 100;
pic.WidthScale = 100;
Note: I am working as Developer Evangelist at Aspose

Large image not displaying in Excel using Apache POI

I am trying to paste a 'JPEG' image file to Excel 2013 32-bit version. But after writing the workbook when I am opening it I am getting an error "The image cannot currently be displayed". The image file is very large.
I am using Windows 7 64 bit with Microsoft Excel 32 bit verison.
here is the portion of code to paste the image following the apache POI documentation. -
XSSFDrawing drawing = sheet.createDrawingPatriarch();
InputStream graphImage = new FileInputStream(fileName);
//Byte array to store the Image in Byte format
byte[] bytes = IOUtils.toByteArray(graphImage);
//Image Id
int imageId = frameWorkbook.addPicture(bytes,Workbook.PICTURE_TYPE_JPEG);
graphImage.close();
//Instance of ClientAnchor
ClientAnchor anchor = new XSSFClientAnchor();
anchor.setAnchorType(ClientAnchor.DONT_MOVE_AND_RESIZE);
//Setting start position of the Image
anchor.setCol1(5);
anchor.setRow1(5);
//Instance of XSSFpicture to paint the image
XSSFPicture picOfGraph = drawing.createPicture(anchor,imageId);
picOfGraph.resize();
This code works fine with small size image but if the image size gets larger then I am getting the error????Any solution??

trying to extract formatted images from indesign into a separate folder

I'm looking for a way to extract images from an ID file in a 'formatted' / cropped form.
i.e: a. I have placed numerous, hi-res (tiff, psd) images into an InDesign CS5 file
b. The image boxes that they have been placed into, are smaller than the actual image (pretty intense cropping occurred) c. I am trying to collect these images in their new stage (cropped to the image box) and export them as jpg at 72dpi.
Are there any plug-ins out there that would automatically collect "formatted" images from ID for me? Or is there some other way?
If you're familiar with Indesign Scripting, this can very easily be done via a script. I use Javascript but this can be done with VBSript or AppleScript as well. Here is a basic example of a script that will open a document, and export a rectangle (your image box) as a JPG. Basically, you can just loop through the pictures in your document and export each one to a location/filename you choose (see the myFile variable below). There are several "jpegExportPreferences" you can pick from to determine how your output JPG will be (i.e. DPI).
test();
function test(){
var myDoc = app.open('c:/user/desktop/testDocument.indd');
var myGroups = myDoc.groups;
//for each group...
for (var i = 0;i < myGroups.length; i++){
// for each rectangle in the group...
for(var r = 0; r< myGroups[i].rectangles.length; r++){
var myRect = myGroups[i].rectangles[r];
app.jpegExportPreferences.exportResolution = 300;
app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.MAXIMUM;
//give it a unique name
var myFile = new File('c:/users/desktop/newJPG' + myRect.id + '.jpg');
myRect.exportFile(ExportFormat.JPG, myFile);
}
}
}
For a list of the other optional JPG Export preferences, see this link:
http://indesignscriptingreference.com/cs2/javascript-cs2/jpegexportpreference.htm
Hope this helps!

Resources